Debian Codegraph @

td/td_any_test.go

raw
1 // Copyright (c) 2018, 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 td_test
8
9 import (
10 "fmt"
11 "testing"
12
13 "github.com/maxatome/go-testdeep/internal/test"
14 "github.com/maxatome/go-testdeep/td"
15 )
16
17 func TestAny(t *testing.T) {
18 checkOK(t, 6, td.Any(nil, 5, 6, 7))
19 checkOK(t, nil, td.Any(5, 6, 7, nil))
20
21 checkError(t, 6, td.Any(5),
22 expectedError{
23 Message: mustBe("comparing with Any"),
24 Path: mustBe("DATA"),
25 Got: mustBe("6"),
26 Expected: mustBe("Any(5)"),
27 })
28
29 checkError(t, 6, td.Any(nil),
30 expectedError{
31 Message: mustBe("comparing with Any"),
32 Path: mustBe("DATA"),
33 Got: mustBe("6"),
34 Expected: mustBe("Any(nil)"),
35 })
36
37 checkError(t, nil, td.Any(6),
38 expectedError{
39 Message: mustBe("comparing with Any"),
40 Path: mustBe("DATA"),
41 Got: mustBe("nil"),
42 Expected: mustBe("Any(6)"),
43 })
44
45 checkError(t, 123, td.Any(td.JSON("{")),
46 expectedError{
47 Message: mustBe("bad usage of JSON operator"),
48 Path: mustBe("DATA"),
49 Summary: mustContain("JSON unmarshal error"),
50 Under: mustContain("under operator JSON at "),
51 },
52 "erroneous operator expected")
53
54 // Lax
55 checkOK(t, float64(123), td.Lax(td.Any(122, 123, 124)))
56
57 //
58 // String
59 test.EqualStr(t, td.Any(6).String(), "Any(6)")
60 test.EqualStr(t, td.Any(6, 7).String(), "Any(6,\n 7)")
61 }
62
63 func TestAnyTypeBehind(t *testing.T) {
64 equalTypes(t, td.Any(6, nil), nil)
65 equalTypes(t, td.Any(6, "toto"), nil)
66
67 equalTypes(t, td.Any(6, td.Zero(), 7, 8), 26)
68
69 // Always the same non-interface type (even if we encounter several
70 // interface types)
71 equalTypes(t,
72 td.Any(
73 td.Empty(),
74 5,
75 td.Isa((*error)(nil)), // interface type (in fact pointer to ...)
76 td.Any(6, 7),
77 td.Isa((*fmt.Stringer)(nil)), // interface type
78 8),
79 42)
80
81 // Only one interface type
82 equalTypes(t,
83 td.Any(
84 td.Isa((*error)(nil)),
85 td.Isa((*error)(nil)),
86 td.Isa((*error)(nil)),
87 ),
88 (*error)(nil))
89
90 // Several interface types, cannot be sure
91 equalTypes(t,
92 td.Any(
93 td.Isa((*error)(nil)),
94 td.Isa((*fmt.Stringer)(nil)),
95 ),
96 nil)
97
98 equalTypes(t,
99 td.Any(
100 td.Code(func(x any) bool { return true }),
101 td.Code(func(y int) bool { return true }),
102 ),
103 12)
104 }