Debian Codegraph @

td_compat_test.go

raw
1 // Copyright (c) 2020, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package testdeep_test
8
9 import (
10 "errors"
11 "math"
12 "testing"
13 "time"
14
15 td "github.com/maxatome/go-testdeep"
16 )
17
18 // These tests are only here to ensure that all obsolete but aliased
19 // functions are still callable from outside.
20 //
21 // See https://pkg.go.dev/github.com/maxatome/go-testdeep/td for real
22 // tests and examples.
23 func TestCompat(tt *testing.T) {
24 type MyStruct struct {
25 Num int64 `json:"num"`
26 Str string `json:"str"`
27 }
28
29 tt.Run("Cmp", func(t *testing.T) {
30 td.Cmp(t, 1, 1)
31 td.CmpDeeply(t, 1, 1)
32
33 td.CmpTrue(t, true)
34 td.CmpFalse(t, false)
35 td.CmpError(t, errors.New("Error"))
36 td.CmpNoError(t, nil)
37 td.CmpPanic(t, func() { panic("boom!") }, "boom!")
38 td.CmpNotPanic(t, func() {})
39
40 td.CmpTrue(t, td.EqDeeply(1, 1))
41 td.CmpNoError(t, td.EqDeeplyError(1, 1))
42 })
43
44 td.AddAnchorableStructType(func(nextAnchor int) MyStruct {
45 return MyStruct{Num: 999999999 - int64(nextAnchor)}
46 })
47
48 tt.Run("td.T", func(tt *testing.T) {
49 t := td.NewT(tt)
50 t.Cmp(1, 1)
51
52 assert := td.Assert(tt)
53 assert.Cmp(1, 1)
54
55 require := td.Require(tt)
56 require.Cmp(1, 1)
57
58 assert, require = td.AssertRequire(tt)
59 assert.Cmp(1, 1)
60 require.Cmp(1, 1)
61 })
62
63 tt.Run("All", func(t *testing.T) {
64 td.Cmp(t, 1, td.All(1))
65 td.CmpAll(t, 1, []any{1})
66 })
67
68 tt.Run("Any", func(t *testing.T) {
69 td.Cmp(t, 1, td.Any(3, 2, 1))
70 td.CmpAny(t, 1, []any{3, 2, 1})
71 })
72
73 tt.Run("Array", func(t *testing.T) {
74 td.Cmp(t, [2]int{1, 2}, td.Array([2]int{}, td.ArrayEntries{0: 1, 1: 2}))
75 td.CmpArray(t, [2]int{1, 2}, [2]int{}, td.ArrayEntries{0: 1, 1: 2})
76 })
77
78 tt.Run("ArrayEach", func(t *testing.T) {
79 got := []int{1, 1}
80 td.Cmp(t, got, td.ArrayEach(1))
81 td.CmpArrayEach(t, got, 1)
82 })
83
84 tt.Run("Bag", func(t *testing.T) {
85 got := []int{1, 2}
86 td.Cmp(t, got, td.Bag(1, 2))
87 td.CmpBag(t, got, []any{1, 2})
88 })
89
90 tt.Run("Between", func(t *testing.T) {
91 for _, bounds := range []td.BoundsKind{
92 td.BoundsInIn, td.BoundsInOut, td.BoundsOutIn, td.BoundsOutOut,
93 } {
94 td.Cmp(t, 5, td.Between(0, 10, bounds))
95 td.CmpBetween(t, 5, 0, 10, bounds)
96 }
97 })
98
99 tt.Run("Cap", func(t *testing.T) {
100 got := make([]int, 2, 3)
101 td.Cmp(t, got, td.Cap(3))
102 td.CmpCap(t, got, 3)
103 })
104
105 tt.Run("Catch", func(t *testing.T) {
106 var num int
107 td.Cmp(t, 12, td.Catch(&num, 12))
108 td.Cmp(t, num, 12)
109 })
110
111 tt.Run("Code", func(t *testing.T) {
112 fn := func(n int) bool { return n == 5 }
113 td.Cmp(t, 5, td.Code(fn))
114 td.CmpCode(t, 5, fn)
115 })
116
117 tt.Run("Contains", func(t *testing.T) {
118 td.Cmp(t, "foobar", td.Contains("ob"))
119 td.CmpContains(t, "foobar", "ob")
120 })
121
122 tt.Run("ContainsKey", func(t *testing.T) {
123 got := map[string]bool{"a": false}
124 td.Cmp(t, got, td.ContainsKey("a"))
125 td.CmpContainsKey(t, got, "a")
126 })
127
128 tt.Run("Empty", func(t *testing.T) {
129 td.Cmp(t, "", td.Empty())
130 td.CmpEmpty(t, "")
131 })
132
133 tt.Run("Gt", func(t *testing.T) {
134 td.Cmp(t, 5, td.Gt(3))
135 td.CmpGt(t, 5, 3)
136 })
137
138 tt.Run("Gte", func(t *testing.T) {
139 td.Cmp(t, 5, td.Gte(3))
140 td.CmpGte(t, 5, 3)
141 })
142
143 tt.Run("HasPrefix", func(t *testing.T) {
144 td.Cmp(t, "foobar", td.HasPrefix("foo"))
145 td.CmpHasPrefix(t, "foobar", "foo")
146 })
147
148 tt.Run("HasSuffix", func(t *testing.T) {
149 td.Cmp(t, "foobar", td.HasSuffix("bar"))
150 td.CmpHasSuffix(t, "foobar", "bar")
151 })
152
153 td.Cmp(tt, 42, td.Ignore())
154
155 tt.Run("Isa", func(t *testing.T) {
156 td.Cmp(t, 2, td.Isa(0))
157 td.CmpIsa(t, 2, 0)
158 })
159
160 tt.Run("JSON", func(t *testing.T) {
161 td.Cmp(t, []int{1, 2}, td.JSON(`[1,$val]`, td.Tag("val", 2)))
162 td.CmpJSON(t, []int{1, 2}, `[1,$val]`, []any{td.Tag("val", 2)})
163 })
164
165 tt.Run("Keys", func(t *testing.T) {
166 got := map[string]bool{"a": false}
167 td.Cmp(t, got, td.Keys([]string{"a"}))
168 td.CmpKeys(t, got, []string{"a"})
169 })
170
171 tt.Run("Lax", func(t *testing.T) {
172 td.Cmp(t, int64(42), td.Lax(42))
173 td.CmpLax(t, int64(42), 42)
174 })
175
176 tt.Run("Len", func(t *testing.T) {
177 got := make([]int, 2, 3)
178 td.Cmp(t, got, td.Len(2))
179 td.CmpLen(t, got, 2)
180 })
181
182 tt.Run("Lt", func(t *testing.T) {
183 td.Cmp(t, 5, td.Lt(10))
184 td.CmpLt(t, 5, 10)
185 })
186
187 tt.Run("Lte", func(t *testing.T) {
188 td.Cmp(t, 5, td.Lte(10))
189 td.CmpLte(t, 5, 10)
190 })
191
192 tt.Run("Map", func(t *testing.T) {
193 got := map[string]bool{"a": false, "b": true}
194 td.Cmp(t, got, td.Map(map[string]bool{"a": false}, td.MapEntries{"b": true}))
195 td.CmpMap(t, got, map[string]bool{"a": false}, td.MapEntries{"b": true})
196 })
197
198 tt.Run("MapEach", func(t *testing.T) {
199 got := map[string]int{"a": 1}
200 td.Cmp(t, got, td.MapEach(1))
201 td.CmpMapEach(t, got, 1)
202 })
203
204 tt.Run("N", func(t *testing.T) {
205 td.Cmp(t, 12, td.N(10, 2))
206 td.CmpN(t, 12, 10, 2)
207 })
208
209 tt.Run("NaN", func(t *testing.T) {
210 td.Cmp(t, math.NaN(), td.NaN())
211 td.CmpNaN(t, math.NaN())
212 })
213
214 tt.Run("Nil", func(t *testing.T) {
215 td.Cmp(t, nil, td.Nil())
216 td.CmpNil(t, nil)
217 })
218
219 tt.Run("None", func(t *testing.T) {
220 td.Cmp(t, 28, td.None(3, 4, 5))
221 td.CmpNone(t, 28, []any{3, 4, 5})
222 })
223
224 tt.Run("Not", func(t *testing.T) {
225 td.Cmp(t, 28, td.Not(3))
226 td.CmpNot(t, 28, 3)
227 })
228
229 tt.Run("NotAny", func(t *testing.T) {
230 got := []int{5}
231 td.Cmp(t, got, td.NotAny(1, 2, 3))
232 td.CmpNotAny(t, got, []any{1, 2, 3})
233 })
234
235 tt.Run("NotEmpty", func(t *testing.T) {
236 td.Cmp(t, "OOO", td.NotEmpty())
237 td.CmpNotEmpty(t, "OOO")
238 })
239
240 tt.Run("NotNaN", func(t *testing.T) {
241 td.Cmp(t, 12., td.NotNaN())
242 td.CmpNotNaN(t, 12.)
243 })
244
245 tt.Run("NotNil", func(t *testing.T) {
246 td.Cmp(t, 4, td.NotNil())
247 td.CmpNotNil(t, 4)
248 })
249
250 tt.Run("NotZero", func(t *testing.T) {
251 td.Cmp(t, 3, td.NotZero())
252 td.CmpNotZero(t, 3)
253 })
254
255 tt.Run("Ptr", func(t *testing.T) {
256 num := 12
257 td.Cmp(t, &num, td.Ptr(12))
258 td.CmpPtr(t, &num, 12)
259 })
260
261 tt.Run("PPtr", func(t *testing.T) {
262 num := 12
263 pnum := &num
264 td.Cmp(t, &pnum, td.PPtr(12))
265 td.CmpPPtr(t, &pnum, 12)
266 })
267
268 tt.Run("Re", func(t *testing.T) {
269 td.Cmp(t, "foobar", td.Re(`o+`))
270 td.CmpRe(t, "foobar", `o+`, nil)
271 })
272
273 tt.Run("ReAll", func(t *testing.T) {
274 td.Cmp(t, "foo bar", td.ReAll(`([a-z]+)(?: |\z)`, td.Bag("bar", "foo")))
275 td.CmpReAll(t, "foo bar", `([a-z]+)(?: |\z)`, td.Bag("bar", "foo"))
276 })
277
278 tt.Run("Set", func(t *testing.T) {
279 got := []int{1, 1, 2}
280 td.Cmp(t, got, td.Set(2, 1))
281 td.CmpSet(t, got, []any{2, 1})
282 })
283
284 tt.Run("Shallow", func(t *testing.T) {
285 got := []int{1, 2, 3}
286 expected := got[:1]
287 td.Cmp(t, got, td.Shallow(expected))
288 td.CmpShallow(t, got, expected)
289 })
290
291 tt.Run("Slice", func(t *testing.T) {
292 got := []int{1, 2}
293 td.Cmp(t, got, td.Slice([]int{}, td.ArrayEntries{0: 1, 1: 2}))
294 td.CmpSlice(t, got, []int{}, td.ArrayEntries{0: 1, 1: 2})
295 })
296
297 tt.Run("Smuggle", func(t *testing.T) {
298 fn := func(v int) int { return v * 2 }
299 td.Cmp(t, 5, td.Smuggle(fn, 10))
300 td.CmpSmuggle(t, 5, fn, 10)
301 })
302
303 tt.Run("String", func(t *testing.T) {
304 td.Cmp(t, "foo", td.String("foo"))
305 td.CmpString(t, "foo", "foo")
306 })
307
308 tt.Run("SStruct", func(t *testing.T) {
309 got := MyStruct{
310 Num: 42,
311 Str: "foo",
312 }
313 td.Cmp(t, got, td.SStruct(MyStruct{Num: 42}, td.StructFields{"Str": "foo"}))
314 td.CmpSStruct(t, got, MyStruct{Num: 42}, td.StructFields{"Str": "foo"})
315 })
316
317 tt.Run("Struct", func(t *testing.T) {
318 got := MyStruct{
319 Num: 42,
320 Str: "foo",
321 }
322 td.Cmp(t, got, td.Struct(MyStruct{Num: 42}, td.StructFields{"Str": "foo"}))
323 td.CmpStruct(t, got, MyStruct{Num: 42}, td.StructFields{"Str": "foo"})
324 })
325
326 tt.Run("SubBagOf", func(t *testing.T) {
327 got := []int{1}
328 td.Cmp(t, got, td.SubBagOf(1, 1, 2))
329 td.CmpSubBagOf(t, got, []any{1, 1, 2})
330 })
331
332 tt.Run("SubJSONOf", func(t *testing.T) {
333 got := MyStruct{
334 Num: 42,
335 Str: "foo",
336 }
337 td.Cmp(t, got,
338 td.SubJSONOf(`{"num":42,"str":$str,"zip":45600}`, td.Tag("str", "foo")))
339 td.CmpSubJSONOf(t, got,
340 `{"num":42,"str":$str,"zip":45600}`, []any{td.Tag("str", "foo")})
341 })
342
343 tt.Run("SubMapOf", func(t *testing.T) {
344 got := map[string]int{"a": 1, "b": 2}
345 td.Cmp(t, got,
346 td.SubMapOf(map[string]int{"a": 1, "c": 3}, td.MapEntries{"b": 2}))
347 td.CmpSubMapOf(t, got, map[string]int{"a": 1, "c": 3}, td.MapEntries{"b": 2})
348 })
349
350 tt.Run("SubSetOf", func(t *testing.T) {
351 got := []int{1, 1}
352 td.Cmp(t, got, td.SubSetOf(1, 2))
353 td.CmpSubSetOf(t, got, []any{1, 2})
354 })
355
356 tt.Run("SuperBagOf", func(t *testing.T) {
357 got := []int{1, 1, 2}
358 td.Cmp(t, got, td.SuperBagOf(1))
359 td.CmpSuperBagOf(t, got, []any{1})
360 })
361
362 tt.Run("SuperJSONOf", func(t *testing.T) {
363 got := MyStruct{
364 Num: 42,
365 Str: "foo",
366 }
367 td.Cmp(t, got, td.SuperJSONOf(`{"str":$str}`, td.Tag("str", "foo")))
368 td.CmpSuperJSONOf(t, got, `{"str":$str}`, []any{td.Tag("str", "foo")})
369 })
370
371 tt.Run("SuperMapOf", func(t *testing.T) {
372 got := map[string]int{"a": 1, "b": 2, "c": 3}
373 td.Cmp(t, got, td.SuperMapOf(map[string]int{"a": 1}, td.MapEntries{"b": 2}))
374 td.CmpSuperMapOf(t, got, map[string]int{"a": 1}, td.MapEntries{"b": 2})
375 })
376
377 tt.Run("SuperSetOf", func(t *testing.T) {
378 got := []int{1, 1, 2}
379 td.Cmp(t, got, td.SuperSetOf(1))
380 td.CmpSuperSetOf(t, got, []any{1})
381 })
382
383 tt.Run("TruncTime", func(t *testing.T) {
384 got, err := time.Parse(time.RFC3339Nano, "2020-02-22T12:34:56.789Z")
385 if err != nil {
386 t.Fatal(err)
387 }
388 expected, err := time.Parse(time.RFC3339, "2020-02-22T12:34:56Z")
389 if err != nil {
390 t.Fatal(err)
391 }
392 td.Cmp(t, got, td.TruncTime(expected, time.Second))
393 td.CmpTruncTime(t, got, expected, time.Second)
394 })
395
396 tt.Run("Values", func(t *testing.T) {
397 got := map[string]string{"a": "b"}
398 td.Cmp(t, got, td.Values([]string{"b"}))
399 td.CmpValues(t, got, []string{"b"})
400 })
401
402 tt.Run("Zero", func(t *testing.T) {
403 td.Cmp(t, 0, td.Zero())
404 td.CmpZero(t, 0)
405 })
406 }