-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
is_test.go
291 lines (275 loc) · 5.79 KB
/
is_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package is
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"
)
type mockT struct {
failed bool
}
func (m *mockT) FailNow() {
m.failed = true
}
func (m *mockT) Fail() {
m.failed = true
}
var tests = []struct {
N string
F func(is *I)
Fail string
}{
// Equal
{
N: "Equal(1, 1)",
F: func(is *I) {
is.Equal(1, 1) // 1 doesn't equal 1
},
Fail: ``,
},
{
N: "Equal(1, 2)",
F: func(is *I) {
is.Equal(1, 2) // 1 doesn't equal 2
},
Fail: `1 != 2 // 1 doesn't equal 2`,
},
{
N: "Equal(1, nil)",
F: func(is *I) {
is.Equal(1, nil) // 1 doesn't equal nil
},
Fail: `int(1) != <nil> // 1 doesn't equal nil`,
},
{
N: "Equal(nil, 2)",
F: func(is *I) {
is.Equal(nil, 2) // nil doesn't equal 2
},
Fail: `<nil> != int(2) // nil doesn't equal 2`,
},
{
N: "Equal(false, false)",
F: func(is *I) {
is.Equal(false, false) // false doesn't equal false
},
Fail: ``,
},
{
N: "Equal(int32(1), int64(1))",
F: func(is *I) {
is.Equal(int32(1), int64(1)) // nope
},
Fail: `int32(1) != int64(1) // nope`,
},
{
N: "Equal(map1, map2)",
F: func(is *I) {
m1 := map[string]interface{}{"value": 1}
m2 := map[string]interface{}{"value": 2}
is.Equal(m1, m2) // maps
},
Fail: `map[value:1] != map[value:2] // maps`,
},
{
N: "Equal(true, map)",
F: func(is *I) {
m := map[string]interface{}{"value": 2}
is.Equal(true, m) // maps
},
Fail: `bool(true) != map[string]interface {}(map[value:2]) // maps`,
},
{
N: "Equal(slice1, slice2)",
F: func(is *I) {
s1 := []string{"one", "two", "three"}
s2 := []string{"one", "two", "three", "four"}
is.Equal(s1, s2) // slices
},
Fail: `[one two three] != [one two three four] // slices`,
},
{
N: "Equal(nil, chan)",
F: func(is *I) {
var a chan string
b := make(chan string)
is.Equal(a, b) // channels
},
Fail: ` // channels`,
},
{
N: "Equal(nil, slice)",
F: func(is *I) {
var s1 []string
s2 := []string{"one", "two", "three", "four"}
is.Equal(s1, s2) // nil slice
},
Fail: `<nil> != []string([one two three four]) // nil slice`,
},
{
N: "Equal(nil, nil)",
F: func(is *I) {
var s1 []string
var s2 []string
is.Equal(s1, s2) // nil slices
},
Fail: ``,
},
{
N: "Equal(nil, map)",
F: func(is *I) {
var m1 map[string]string
m2 := map[string]string{}
is.Equal(m1, m2) // nil map
},
Fail: `<nil> != map[string]string(map[]) // nil map`,
},
{
N: "Equal(nil, nil)",
F: func(is *I) {
var m1 map[string]string
var m2 map[string]string
is.Equal(m1, m2) // nil maps
},
Fail: ``,
},
{
N: `Equal("20% VAT", "0.2 VAT")`,
F: func(is *I) {
s1 := "20% VAT"
s2 := "0.2 VAT"
is.Equal(s1, s2) // strings
},
Fail: `20% VAT != 0.2 VAT // strings`,
},
// Fail
{
N: "Fail()",
F: func(is *I) {
is.Fail() // something went wrong
},
Fail: "failed // something went wrong",
},
// NoErr
{
N: "NoErr(nil)",
F: func(is *I) {
var err error
is.NoErr(err) // method shouldn't return error
},
Fail: "",
},
{
N: "NoErr(error)",
F: func(is *I) {
err := errors.New("nope")
is.NoErr(err) // method shouldn't return error
},
Fail: "err: nope // method shouldn't return error",
},
// True
{
N: "True(1 == 2)",
F: func(is *I) {
is.True(1 == 2)
},
Fail: "not true: 1 == 2",
},
}
func TestFailures(t *testing.T) {
colorful, notColorful := true, false
testFailures(t, colorful)
testFailures(t, notColorful)
}
func testFailures(t *testing.T, colorful bool) {
for _, test := range tests {
tt := &mockT{}
is := New(tt)
var buf bytes.Buffer
is.out = &buf
is.colorful = colorful
test.F(is)
if len(test.Fail) == 0 && tt.failed {
t.Errorf("shouldn't fail: %s", test.N)
continue
}
if len(test.Fail) > 0 && !tt.failed {
t.Errorf("didn't fail: %s", test.N)
}
if colorful {
// if colorful, we won't check the messages
// this test is run twice, one without colorful
// statements.
// see TestFailures
fmt.Print(buf.String())
continue
}
output := buf.String()
output = strings.TrimSpace(output)
if !strings.HasSuffix(output, test.Fail) {
t.Errorf("expected `%s` to end with `%s`", output, test.Fail)
}
}
}
func TestRelaxed(t *testing.T) {
tt := &mockT{}
is := NewRelaxed(tt)
var buf bytes.Buffer
is.out = &buf
is.colorful = false
is.NoErr(errors.New("oops"))
is.True(1 == 2)
actual := buf.String()
if !strings.Contains(actual, `oops`) {
t.Errorf("missing: oops")
}
if !strings.Contains(actual, `1 == 2`) {
t.Errorf("missing: 1 == 2")
}
if !tt.failed {
t.Errorf("didn't fail")
}
}
func TestLoadComment(t *testing.T) {
comment, ok := loadComment("./testdata/example_test.go", 14)
if !ok {
t.Errorf("loadComment: not ok")
}
if comment != `this comment will be extracted` {
t.Errorf("loadComment: bad comment %s", comment)
}
}
func TestLoadArguments(t *testing.T) {
arguments, ok := loadArguments("./testdata/example_test.go", 23)
if !ok {
t.Errorf("loadArguments: not ok")
}
if arguments != `a == getB()` {
t.Errorf("loadArguments: bad arguments %s", arguments)
}
arguments, ok = loadArguments("./testdata/example_test.go", 32)
if !ok {
t.Errorf("loadArguments: not ok")
}
if arguments != `a == getB()` {
t.Errorf("loadArguments: bad arguments %s", arguments)
}
arguments, _ = loadArguments("./testdata/example_test.go", 28)
if len(arguments) > 0 {
t.Errorf("should be no arguments: %s", arguments)
}
}
// TestArgumentsEscape ensures strings are correctly escaped before printing.
// https://github.com/matryer/is/issues/27
func TestFormatStringEscape(t *testing.T) {
tt := &mockT{}
is := NewRelaxed(tt)
var buf bytes.Buffer
is.out = &buf
is.Equal("20% VAT", "0.2 VAT") // % symbol should be correctly printed
actual := buf.String()
if strings.Contains(actual, `%!`) {
t.Errorf("string was not escaped correctly: %s", actual)
}
}