Debian Codegraph @

report_test.go

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