Debian Codegraph @

report_test.go

raw
1 // Licensed under the MIT license, see LICENSE file for details.
2
3 package qt_test
4
5 import (
6 "runtime"
7 "strings"
8 "testing"
9
10 "github.com/go-quicktest/qt"
11 )
12
13 // The tests in this file rely on their own source code lines.
14
15 func TestReportOutput(t *testing.T) {
16 tt := &testingT{}
17 qt.Assert(tt, qt.Equals(42, 47))
18 want := `
19 error:
20 values are not equal
21 got:
22 int(42)
23 want:
24 int(47)
25 stack:
26 $file:17
27 qt.Assert(tt, qt.Equals(42, 47))
28 `
29 assertReport(t, tt, want)
30 }
31
32 func f1(t testing.TB) {
33 f2(t)
34 }
35
36 func f2(t testing.TB) {
37 qt.Assert(t, qt.IsNil([]int{})) // Real assertion here!
38 }
39
40 func TestIndirectReportOutput(t *testing.T) {
41 tt := &testingT{}
42 f1(tt)
43 want := `
44 error:
45 got non-nil value
46 got:
47 []int{}
48 stack:
49 $file:37
50 qt.Assert(t, qt.IsNil([]int{}))
51 $file:33
52 f2(t)
53 $file:42
54 f1(tt)
55 `
56 assertReport(t, tt, want)
57 }
58
59 func TestMultilineReportOutput(t *testing.T) {
60 tt := &testingT{}
61 qt.Assert(tt,
62 qt.Equals(
63 "this string", // Comment 1.
64 "another string",
65 ),
66 qt.Commentf("a comment"), // Comment 2.
67 ) // Comment 3.
68 want := `
69 error:
70 values are not equal
71 comment:
72 a comment
73 got:
74 "this string"
75 want:
76 "another string"
77 stack:
78 $file:61
79 qt.Assert(tt,
80 qt.Equals(
81 "this string", // Comment 1.
82 "another string",
83 ),
84 qt.Commentf("a comment"), // Comment 2.
85 )
86 `
87 assertReport(t, tt, want)
88 }
89
90 func TestCmpReportOutput(t *testing.T) {
91 tt := &testingT{}
92 gotExamples := []*reportExample{{
93 AnInt: 42,
94 }, {
95 AnInt: 47,
96 }, {
97 AnInt: 2,
98 }, {
99 AnInt: 1,
100 }, {}}
101 wantExamples := []*reportExample{{
102 AnInt: 42,
103 }, {
104 AnInt: 47,
105 }, {
106 AnInt: 1,
107 }, {
108 AnInt: 2,
109 }}
110 qt.Assert(tt, qt.DeepEquals(gotExamples, wantExamples))
111 want := `
112 error:
113 values are not deep equal
114 diff (-want +got):
115 []*qt_test.reportExample{
116 &{AnInt: 42},
117 &{AnInt: 47},
118 + &{AnInt: 2},
119 &{AnInt: 1},
120 - &{AnInt: 2},
121 + &{},
122 }
123 got:
124 []*qt_test.reportExample{
125 &qt_test.reportExample{AnInt:42},
126 &qt_test.reportExample{AnInt:47},
127 &qt_test.reportExample{AnInt:2},
128 &qt_test.reportExample{AnInt:1},
129 &qt_test.reportExample{},
130 }
131 want:
132 []*qt_test.reportExample{
133 &qt_test.reportExample{AnInt:42},
134 &qt_test.reportExample{AnInt:47},
135 &qt_test.reportExample{AnInt:1},
136 &qt_test.reportExample{AnInt:2},
137 }
138 stack:
139 $file:110
140 qt.Assert(tt, qt.DeepEquals(gotExamples, wantExamples))
141 `
142 assertReport(t, tt, want)
143 }
144
145 func TestTopLevelAssertReportOutput(t *testing.T) {
146 tt := &testingT{}
147 qt.Assert(tt, qt.Equals(42, 47))
148 want := `
149 error:
150 values are not equal
151 got:
152 int(42)
153 want:
154 int(47)
155 stack:
156 $file:147
157 qt.Assert(tt, qt.Equals(42, 47))
158 `
159 assertReport(t, tt, want)
160 }
161
162 func assertReport(t *testing.T, tt *testingT, want string) {
163 t.Helper()
164 got := strings.Replace(tt.fatalString(), "\t", " ", -1)
165 // go-cmp can include non-breaking spaces in its output.
166 got = strings.Replace(got, "\u00a0", " ", -1)
167 // Adjust for file names in different systems.
168 _, file, _, ok := runtime.Caller(0)
169 assertBool(t, ok, true)
170 want = strings.Replace(want, "$file", file, -1)
171 if got != want {
172 t.Fatalf(`failure:
173 %q
174 %q
175 ------------------------------ got ------------------------------
176 %s------------------------------ want -----------------------------
177 %s-----------------------------------------------------------------`,
178 got, want, got, want)
179 }
180 }
181
182 type reportExample struct {
183 AnInt int
184 }