Debian Codegraph @

internal/testutil/assert/assert.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package assert provides assertion functions for tests.
5 // This is an internal package that was created to provide a simple way to
6 // write tests.
7 //
8 // The idea was to do not add a dependency on a testing framework.
9 package assert
10
11 import (
12 "errors"
13 "fmt"
14 "reflect"
15 "regexp"
16 )
17
18 // miniTB defines the minimal interface required for test assertions.
19 type miniTB interface {
20 Helper()
21 Fatalf(string, ...any)
22 }
23
24 // formatSuffix builds an optional suffix from a printf-style format and args.
25 // If msgFormat is empty, an empty string is returned.
26 func formatSuffix(msgFormat string, args ...any) string {
27 if msgFormat == "" {
28 return ""
29 }
30 return " - " + fmt.Sprintf(msgFormat, args...)
31 }
32
33 // Equal asserts that two values are equal.
34 //
35 // It can be used with comparable types: numbers, strings, pointers to the same object, etc.
36 //
37 // For any other types, use [DeepEqual].
38 func Equal(tb miniTB, want, got any) {
39 tb.Helper()
40 Equalf(tb, want, got, "")
41 }
42
43 // Equalf asserts that two values are equal, and reports a message if they are not.
44 func Equalf(tb miniTB, want, got any, msgFormat string, args ...any) {
45 tb.Helper()
46 if got != want {
47 suffix := formatSuffix(msgFormat, args...)
48 tb.Fatalf("got %v; want %v%s", got, want, suffix)
49 }
50 }
51
52 // DeepEqual asserts that two values are deeply equal.
53 //
54 // It can be used with slices, maps, structs with slices...
55 //
56 // Please consider [Equal] for other types.
57 func DeepEqual(tb miniTB, want, got any) {
58 tb.Helper()
59 DeepEqualf(tb, want, got, "")
60 }
61
62 // DeepEqualf asserts that two values are deeply equal, and reports a message if they are not.
63 func DeepEqualf(tb miniTB, want, got any, msgFormat string, args ...any) {
64 tb.Helper()
65 if !reflect.DeepEqual(got, want) {
66 suffix := formatSuffix(msgFormat, args...)
67 tb.Fatalf("got %+v; want %+v%s", got, want, suffix)
68 }
69 }
70
71 // ErrorMatches asserts that an error matches a regular expression.
72 func ErrorMatches(tb miniTB, pattern string, err error) {
73 tb.Helper()
74 ErrorMatchesf(tb, pattern, err, "")
75 }
76
77 // ErrorMatchesf asserts that an error matches a regular expression, and reports a message if it does not.
78 func ErrorMatchesf(tb miniTB, pattern string, err error, msgFormat string, args ...any) {
79 tb.Helper()
80 if err == nil {
81 suffix := formatSuffix(msgFormat, args...)
82 tb.Fatalf("got nil; want error matching %q%s", pattern, suffix)
83 return
84 }
85 re, reErr := regexp.Compile(pattern)
86 if reErr != nil {
87 suffix := formatSuffix(msgFormat, args...)
88 tb.Fatalf("invalid regexp %q: %v%s", pattern, reErr, suffix)
89 return
90 }
91 if !re.MatchString(err.Error()) {
92 suffix := formatSuffix(msgFormat, args...)
93 tb.Fatalf("error %q does not match %q%s", err.Error(), pattern, suffix)
94 }
95 }
96
97 // ErrorIs asserts that two errors are equal by using [errors.Is].
98 func ErrorIs(tb miniTB, got, want error) {
99 tb.Helper()
100 if !errors.Is(got, want) {
101 tb.Fatalf("got %#v; want %#v", got, want)
102 }
103 }
104
105 // errorAsNoPanic calls [errors.As], but catch possible panic and returns it as an error
106 func errorAsNoPanic(tb miniTB, err error, target any) (ok bool, panic error) {
107 defer func() {
108 if r := recover(); r != nil {
109 ok = false
110 panic = fmt.Errorf("panic: %v", r)
111 return
112 }
113 }()
114
115 return errors.As(err, target), nil
116 }
117
118 // ErrorAs asserts that an error can be assigned to a target variable by using [errors.As].
119 func ErrorAs(tb miniTB, err error, target any) {
120 tb.Helper()
121
122 ok, panicErr := errorAsNoPanic(tb, err, target)
123 if panicErr != nil {
124 tb.Fatalf("%s", panicErr)
125 return
126 }
127 if ok {
128 return
129 }
130
131 reflectedType := reflect.TypeOf(target)
132 if reflectedType.Kind() != reflect.Pointer {
133 // this is not supposed to happen with the current implementation of [errors.As]
134 tb.Fatalf("a pointer was expected: got: %s; want: ptr", reflectedType.Kind())
135 return
136 }
137
138 tb.Fatalf("got %#v; want %s", err, reflectedType.Elem())
139 }
140
141 // NoError asserts that an error is nil.
142 func NoError(tb miniTB, err error) {
143 tb.Helper()
144 NoErrorf(tb, err, "")
145 }
146
147 // NoErrorf asserts that an error is nil, and reports a message if it is not.
148 func NoErrorf(tb miniTB, err error, msgFormat string, args ...any) {
149 tb.Helper()
150 if err != nil {
151 suffix := formatSuffix(msgFormat, args...)
152 tb.Fatalf("unexpected error: %v%s", err, suffix)
153 }
154 }
155
156 // IsNil asserts that a value is nil.
157 func IsNil(tb miniTB, v any) {
158 tb.Helper()
159 IsNilf(tb, v, "")
160 }
161
162 // IsNilf asserts that a value is nil, and reports a message if it is not.
163 func IsNilf(tb miniTB, v any, msgFormat string, args ...any) {
164 tb.Helper()
165 if !isNil(v) {
166 suffix := formatSuffix(msgFormat, args...)
167 tb.Fatalf("got non-nil (type %T): %#v%s", v, v, suffix)
168 }
169 }
170
171 // NotNil asserts that a value is not nil.
172 func NotNil(tb miniTB, v any) {
173 tb.Helper()
174 NotNilf(tb, v, "")
175 }
176
177 // NotNilf asserts that a value is not nil, and reports a message if it is.
178 func NotNilf(tb miniTB, v any, msgFormat string, args ...any) {
179 tb.Helper()
180 if isNil(v) {
181 suffix := formatSuffix(msgFormat, args...)
182 tb.Fatalf("got nil; want non-nil%s", suffix)
183 }
184 }
185
186 // True asserts that a value is true.
187 func True(tb miniTB, got bool) {
188 tb.Helper()
189 Truef(tb, got, "")
190 }
191
192 // Truef asserts that a value is true, and reports a message if it is not.
193 func Truef(tb miniTB, got bool, msgFormat string, args ...any) {
194 tb.Helper()
195 if !got {
196 suffix := formatSuffix(msgFormat, args...)
197 tb.Fatalf("got false; want true%s", suffix)
198 }
199 }
200
201 // False asserts that a value is false.
202 func False(tb miniTB, got bool) {
203 tb.Helper()
204 Falsef(tb, got, "")
205 }
206
207 // Falsef asserts that a value is false, and reports a message if it is not.
208 func Falsef(tb miniTB, got bool, msgFormat string, args ...any) {
209 tb.Helper()
210 if got {
211 suffix := formatSuffix(msgFormat, args...)
212 tb.Fatalf("got true; want false%s", suffix)
213 }
214 }
215
216 // PanicMatches asserts that a function panics with a message matching the given pattern.
217 func PanicMatches(tb miniTB, pattern string, f func()) {
218 tb.Helper()
219 PanicMatchesf(tb, pattern, f, "")
220 }
221
222 // PanicMatchesf asserts that a function panics with a message matching the given pattern,
223 // and reports a message if it does not.
224 func PanicMatchesf(tb miniTB, pattern string, f func(), msgFormat string, args ...any) {
225 tb.Helper()
226 var pan any
227 func() {
228 defer func() { pan = recover() }()
229 f()
230 }()
231 if pan == nil {
232 suffix := formatSuffix(msgFormat, args...)
233 tb.Fatalf("function did not panic; want panic matching %q%s", pattern, suffix)
234 return
235 }
236 var pmsg string
237 switch x := pan.(type) {
238 case error:
239 pmsg = x.Error()
240 case string:
241 pmsg = x
242 default:
243 pmsg = fmt.Sprint(x)
244 }
245 re, reErr := regexp.Compile(pattern)
246 if reErr != nil {
247 suffix := formatSuffix(msgFormat, args...)
248 tb.Fatalf("invalid regexp %q: %v%s", pattern, reErr, suffix)
249 return
250 }
251 if !re.MatchString(pmsg) {
252 suffix := formatSuffix(msgFormat, args...)
253 tb.Fatalf("panic %q does not match %q%s", pmsg, pattern, suffix)
254 }
255 }
256
257 // isNil checks if a value is nil, handling both direct nil and typed nil values.
258 func isNil(v any) bool {
259 if v == nil {
260 return true
261 }
262 rv := reflect.ValueOf(v)
263 switch rv.Kind() {
264 case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.Interface, reflect.UnsafePointer:
265 return rv.IsNil()
266 default:
267 return false
268 }
269 }