Debian Codegraph @

td/td_all_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 TestAll(t *testing.T) {
18 checkOK(t, 6, td.All(6, 6, 6))
19 checkOK(t, nil, td.All(nil, nil, nil))
20
21 checkError(t, 6, td.All(6, 5, 6),
22 expectedError{
23 Message: mustBe("compared (part 2 of 3)"),
24 Path: mustBe("DATA"),
25 Got: mustBe("6"),
26 Expected: mustBe("5"),
27 })
28
29 checkError(t, 6, td.All(6, nil, 6),
30 expectedError{
31 Message: mustBe("compared (part 2 of 3)"),
32 Path: mustBe("DATA"),
33 Got: mustBe("6"),
34 Expected: mustBe("nil"),
35 })
36
37 checkError(t, nil, td.All(nil, 5, nil),
38 expectedError{
39 Message: mustBe("compared (part 2 of 3)"),
40 Path: mustBe("DATA"),
41 Got: mustBe("nil"),
42 Expected: mustBe("5"),
43 })
44
45 checkError(t,
46 6,
47 td.All(
48 6,
49 td.All(td.Between(3, 8), td.Between(4, 5)),
50 6),
51 expectedError{
52 Message: mustBe("compared (part 2 of 3)"),
53 Path: mustBe("DATA"),
54 Got: mustBe("6"),
55 Expected: mustBe("All(3 ≤ got ≤ 8,\n 4 ≤ got ≤ 5)"),
56 Origin: &expectedError{
57 Message: mustBe("compared (part 2 of 2)"),
58 Path: mustBe("DATA<All#2/3>"),
59 Got: mustBe("6"),
60 Expected: mustBe("4 ≤ got ≤ 5"),
61 Origin: &expectedError{
62 Message: mustBe("values differ"),
63 Path: mustBe("DATA<All#2/3><All#2/2>"),
64 Got: mustBe("6"),
65 Expected: mustBe("4 ≤ got ≤ 5"),
66 },
67 },
68 })
69
70 //
71 // String
72 test.EqualStr(t, td.All(6).String(), "All(6)")
73 test.EqualStr(t, td.All(6, 7).String(), "All(6,\n 7)")
74 }
75
76 func TestAllTypeBehind(t *testing.T) {
77 equalTypes(t, td.All(6, nil), nil)
78 equalTypes(t, td.All(6, "toto"), nil)
79
80 equalTypes(t, td.All(6, td.Zero(), 7, 8), 26)
81
82 // Always the same non-interface type (even if we encounter several
83 // interface types)
84 equalTypes(t,
85 td.All(
86 td.Empty(),
87 5,
88 td.Isa((*error)(nil)), // interface type (in fact pointer to ...)
89 td.All(6, 7),
90 td.Isa((*fmt.Stringer)(nil)), // interface type
91 8),
92 42)
93
94 // Only one interface type
95 equalTypes(t,
96 td.All(
97 td.Isa((*error)(nil)),
98 td.Isa((*error)(nil)),
99 td.Isa((*error)(nil)),
100 ),
101 (*error)(nil))
102
103 // Several interface types, cannot be sure
104 equalTypes(t,
105 td.All(
106 td.Isa((*error)(nil)),
107 td.Isa((*fmt.Stringer)(nil)),
108 ),
109 nil)
110 }