-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassert_test.go
90 lines (75 loc) · 1.5 KB
/
assert_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
package assert_test
import (
"testing"
"oss.terrastruct.com/util-go/assert"
)
func TestStringJSON(t *testing.T) {
t.Parallel()
gen := func() (m1, m2 map[string]interface{}) {
m1 = map[string]interface{}{
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": map[string]interface{}{
"yes": "yes",
"no": "yes",
"five": map[string]interface{}{
"yes": "no",
"no": "yes",
},
},
}
m2 = map[string]interface{}{
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": map[string]interface{}{
"yes": "yes",
"no": "yes",
"five": map[string]interface{}{
"yes": "no",
"no": "yes",
},
},
}
return m1, m2
}
t.Run("equal", func(t *testing.T) {
t.Parallel()
m1, m2 := gen()
assert.JSON(t, m1, m2)
})
t.Run("diff", func(t *testing.T) {
t.Parallel()
m1, m2 := gen()
m2["five"].(map[string]interface{})["five"].(map[string]interface{})["no"] = "ys"
fataledWithDiff := false
ftb := &fakeTB{
TB: t,
fatalf: func(f string, v ...interface{}) {
t.Helper()
if len(v) == 1 {
t.Logf(f, v...)
fataledWithDiff = true
return
}
t.Fatalf(f, v...)
}}
defer func() {
if t.Failed() || !fataledWithDiff {
t.Error("expected assert.StringJSON to fatal with correct diff")
}
}()
assert.JSON(ftb, m1, m2)
})
}
type fakeTB struct {
fatalf func(string, ...interface{})
testing.TB
}
func (ftb *fakeTB) Fatalf(f string, v ...interface{}) {
ftb.TB.Helper()
ftb.fatalf(f, v...)
}