Debian Codegraph @

internal/testutil/assert/assert_test.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Tests for assertion functions.
5 // Verifies the behavior of internal test assertion helpers.
6
7 package assert
8
9 import (
10 "errors"
11 "fmt"
12 "io"
13 "regexp"
14 "strings"
15 "testing"
16 )
17
18 func TestAssertEqual_Success(t *testing.T) {
19 Equal(t, 2, 2)
20 Equal(t, "ok", "ok")
21 }
22
23 func TestAssertDeepEqual_Success(t *testing.T) {
24 DeepEqual(t, []int{1, 2, 3}, []int{1, 2, 3})
25 DeepEqual(t, map[string]int{"a": 1}, map[string]int{"a": 1})
26 }
27
28 func TestErrorMatches_Success(t *testing.T) {
29 err := fmt.Errorf("http 404: not found")
30 ErrorMatches(t, `http \d+: not found`, err)
31 }
32
33 func TestAssertNoError_Success(t *testing.T) {
34 var err error
35 NoError(t, err)
36 }
37
38 func TestIsNil_NotNil_Success(t *testing.T) {
39 var p *int
40 IsNil(t, p)
41
42 var s []int
43 IsNil(t, s)
44
45 var w io.Writer
46 IsNil(t, w)
47
48 s2 := make([]int, 0)
49 NotNil(t, s2)
50
51 x := 0
52 NotNil(t, &x)
53 }
54
55 func TestPanicMatches_Success(t *testing.T) {
56 PanicMatches(t, `boom \d+`, func() { panic("boom 123") })
57 PanicMatches(t, `fail xyz`, func() { panic(fmt.Errorf("fail xyz")) })
58 }
59
60 func TestAssertTrueAndFalse_Success(t *testing.T) {
61 True(t, true)
62 False(t, false)
63 }
64
65 func Test_isNil_Basics(t *testing.T) {
66 if !isNil(nil) {
67 t.Fatalf("nil should be nil")
68 }
69 var p *int
70 if !isNil(p) {
71 t.Fatalf("nil pointer should be nil")
72 }
73 if isNil(0) {
74 t.Fatalf("non-nil value reported as nil")
75 }
76 s := make([]int, 0)
77 if isNil(s) {
78 t.Fatalf("non-nil slice reported as nil")
79 }
80 }
81
82 /************** failure-path checks **************/
83
84 type fakeTB struct {
85 failed bool
86 msg string
87 }
88
89 func (f *fakeTB) Helper() {}
90
91 func (f *fakeTB) Fatalf(format string, args ...any) {
92 f.failed = true
93 f.msg = fmt.Sprintf(format, args...)
94 }
95
96 // assertFailureMessageMatches checks if fakeTB recorded a failure and that
97 // its message matches the regexp.
98 func assertFailureMessageMatches(t *testing.T, f *fakeTB, pattern string) {
99 t.Helper()
100 if !f.failed {
101 t.Fatalf("expected failure")
102 }
103 re := regexp.MustCompile(pattern)
104 if !re.MatchString(f.msg) {
105 t.Fatalf("message does not match:\ngot: `%s`\nregexp: `%s`", f.msg, pattern)
106 }
107 }
108
109 func assertFailureMessageContains(t *testing.T, f *fakeTB, substr string) {
110 t.Helper()
111 if !f.failed {
112 t.Fatalf("expected failure")
113 }
114 if !strings.Contains(f.msg, substr) {
115 t.Fatalf("message doesn't contain:\ngot: `%s`\nwant: `%s`", f.msg, substr)
116 }
117 }
118
119 func TestAssertEqual_Fails(t *testing.T) {
120 mock := &fakeTB{}
121 Equal(mock, 2, 1)
122 assertFailureMessageMatches(t, mock, `^got 1; want 2$`)
123 }
124
125 func TestAssertDeepEqual_Fails(t *testing.T) {
126 // slice mismatch
127 mock := &fakeTB{}
128 DeepEqual(mock, []int{2}, []int{1})
129 assertFailureMessageMatches(t, mock, `^got \[1\]; want \[2\]$`)
130
131 // map mismatch
132 mock2 := &fakeTB{}
133 DeepEqual(mock2, map[string]int{"a": 2}, map[string]int{"a": 1})
134 assertFailureMessageMatches(t, mock2, `^got map\[a:1\]; want map\[a:2\]$`)
135 }
136
137 func TestErrorMatches_Fails(t *testing.T) {
138 // nil error
139 mockTB1 := &fakeTB{}
140 ErrorMatches(mockTB1, `x`, nil)
141 assertFailureMessageMatches(t, mockTB1, `^got nil; want error matching "x"$`)
142
143 // invalid regexp (message may include parser details; check prefix)
144 mockTB2 := &fakeTB{}
145 ErrorMatches(mockTB2, `(`, fmt.Errorf("x"))
146 assertFailureMessageMatches(t, mockTB2, `^invalid regexp "`)
147
148 // no match
149 mockTB3 := &fakeTB{}
150 ErrorMatches(mockTB3, `def`, fmt.Errorf("abc"))
151 assertFailureMessageMatches(t, mockTB3, `^error "abc" does not match "def"$`)
152 }
153
154 func TestAssertNoError_Fails(t *testing.T) {
155 m := &fakeTB{}
156 NoError(m, fmt.Errorf("problem"))
157 assertFailureMessageMatches(t, m, `^unexpected error: problem$`)
158 }
159
160 func TestErrorIs_Success(t *testing.T) {
161 base := fmt.Errorf("base error")
162 wrapped := fmt.Errorf("wrap: %w", base)
163 // direct match
164 ErrorIs(t, base, base)
165 ErrorIs(t, wrapped, wrapped)
166 ErrorIs(t, wrapped, base)
167 // both nil
168 ErrorIs(t, nil, nil)
169 }
170
171 func TestErrorIs_Fails(t *testing.T) {
172 // mismatch
173 mock1 := &fakeTB{}
174 base := fmt.Errorf("base")
175 other := fmt.Errorf("other")
176 ErrorIs(mock1, base, other)
177 assertFailureMessageMatches(t, mock1, `got &errors.errorString{s:"base"}; want &errors.errorString{s:"other"}`)
178
179 // expected non-nil, actual nil
180 mock2 := &fakeTB{}
181 ErrorIs(mock2, nil, base)
182 assertFailureMessageMatches(t, mock2, `got <nil>; want &errors.errorString{s:"base"}`)
183
184 // expected nil, actual non-nil
185 mock3 := &fakeTB{}
186 ErrorIs(mock3, other, nil)
187 assertFailureMessageMatches(t, mock3, `got &errors.errorString{s:"other"}; want <nil>`)
188 }
189
190 // customErr is a custom error type for testing.
191 type customErr struct {
192 msg string
193 }
194
195 func (e *customErr) Error() string {
196 return e.msg
197 }
198
199 func TestErrorAs_Success(t *testing.T) {
200 // this simulates an error returned from a function as error
201 var err error = &customErr{"foo"}
202
203 var target *customErr
204 ErrorAs(t, err, &target)
205 Equal(t, "foo", target.Error())
206 }
207
208 func TestErrorAs_Fails(t *testing.T) {
209 tb := &fakeTB{}
210
211 err := errors.New("foo")
212
213 var target *customErr
214 ErrorAs(tb, err, &target)
215 assertFailureMessageContains(t, tb, `got &errors.errorString{s:"foo"}; want *assert.customErr`)
216
217 tb = &fakeTB{}
218 ErrorAs(tb, nil, &target)
219 assertFailureMessageContains(t, tb, `got <nil>; want *assert.customErr`)
220
221 tb = &fakeTB{}
222 ErrorAs(tb, err, nil) // this is invalid, a panic is expected
223 assertFailureMessageContains(t, tb, `panic`)
224
225 tb = &fakeTB{}
226 ErrorAs(tb, err, 42) // this is invalid, a panic is expected
227 assertFailureMessageContains(t, tb, `panic`)
228
229 var a int
230 tb = &fakeTB{}
231 ErrorAs(tb, err, &a) // this is invalid, a panic is expected
232 assertFailureMessageContains(t, tb, `panic`)
233 }
234
235 func TestIsNil_Fails(t *testing.T) {
236 // non-nil slice
237 mockTB1 := &fakeTB{}
238 s := make([]int, 0)
239 IsNil(mockTB1, s)
240 assertFailureMessageMatches(t, mockTB1, `^got non-nil \(type `)
241
242 // non-nil pointer
243 mockTB2 := &fakeTB{}
244 x := 1
245 IsNil(mockTB2, &x)
246 assertFailureMessageMatches(t, mockTB2, `^got non-nil \(type `)
247 }
248
249 func TestNotNil_Fails(t *testing.T) {
250 // nil interface
251 mockTB1 := &fakeTB{}
252 var w io.Writer
253 NotNil(mockTB1, w)
254 assertFailureMessageMatches(t, mockTB1, `^got nil; want non-nil$`)
255
256 // nil pointer
257 mockTB2 := &fakeTB{}
258 var p *int
259 NotNil(mockTB2, p)
260 assertFailureMessageMatches(t, mockTB2, `^got nil; want non-nil$`)
261 }
262
263 func TestPanicMatches_Fails(t *testing.T) {
264 // no panic
265 mockTB1 := &fakeTB{}
266 PanicMatches(mockTB1, `x`, func() {})
267 assertFailureMessageMatches(t, mockTB1, `^function did not panic; want panic matching "x"$`)
268
269 // invalid regexp
270 mockTB2 := &fakeTB{}
271 PanicMatches(mockTB2, `(`, func() { panic("oops") })
272 assertFailureMessageMatches(t, mockTB2, `^invalid regexp "`)
273
274 // pattern mismatch
275 mockTB3 := &fakeTB{}
276 PanicMatches(mockTB3, `bar`, func() { panic("foo") })
277 assertFailureMessageMatches(t, mockTB3, `^panic "foo" does not match "bar"$`)
278 }
279
280 func TestAssertTrueAndFalse_Fails(t *testing.T) {
281 mock1 := &fakeTB{}
282 True(mock1, false)
283 assertFailureMessageMatches(t, mock1, `^got false; want true$`)
284
285 mock2 := &fakeTB{}
286 False(mock2, true)
287 assertFailureMessageMatches(t, mock2, `^got true; want false$`)
288 }
289
290 func TestFormatSuffix_NoArgs(t *testing.T) {
291 if got := formatSuffix(""); got != "" {
292 t.Fatalf("expected empty suffix; got %q", got)
293 }
294 }
295
296 func TestFormatSuffix_FormatString(t *testing.T) {
297 got := formatSuffix("failed %s", "case")
298 want := " - failed case"
299 if got != want {
300 t.Fatalf("got %q; want %q", got, want)
301 }
302 }
303
304 func TestFormatSuffix_JustString(t *testing.T) {
305 got := formatSuffix("hello")
306 want := " - hello"
307 if got != want {
308 t.Fatalf("got %q; want %q", got, want)
309 }
310 }
311
312 func TestFormatSuffix_AsUsedByAssertions(t *testing.T) {
313 mockTB1 := &fakeTB{}
314 var w io.Writer // nil interface
315
316 // with format string
317 NotNilf(mockTB1, w, "extra %s options %d foo %+v", "str", 42, map[int]bool{3: true})
318 assertFailureMessageMatches(t, mockTB1, `^got nil; want non-nil - extra str options 42 foo map\[3:true\]$`)
319
320 // with just a string arg
321 NotNilf(mockTB1, w, "ba-dum-tss")
322 assertFailureMessageMatches(t, mockTB1, `^got nil; want non-nil - ba-dum-tss$`)
323
324 // with no message args
325 NotNil(mockTB1, w)
326 assertFailureMessageMatches(t, mockTB1, `^got nil; want non-nil$`)
327 }