...
1 package assert_test
2
3 import (
4 "testing"
5
6 "oss.terrastruct.com/util-go/assert"
7 )
8
9 func TestStringJSON(t *testing.T) {
10 t.Parallel()
11
12 gen := func() (m1, m2 map[string]interface{}) {
13 m1 = map[string]interface{}{
14 "one": 1,
15 "two": 2,
16 "three": 3,
17 "four": 4,
18 "five": map[string]interface{}{
19 "yes": "yes",
20 "no": "yes",
21 "five": map[string]interface{}{
22 "yes": "no",
23 "no": "yes",
24 },
25 },
26 }
27
28 m2 = map[string]interface{}{
29 "one": 1,
30 "two": 2,
31 "three": 3,
32 "four": 4,
33 "five": map[string]interface{}{
34 "yes": "yes",
35 "no": "yes",
36 "five": map[string]interface{}{
37 "yes": "no",
38 "no": "yes",
39 },
40 },
41 }
42
43 return m1, m2
44 }
45
46 t.Run("equal", func(t *testing.T) {
47 t.Parallel()
48
49 m1, m2 := gen()
50 assert.JSON(t, m1, m2)
51 })
52
53 t.Run("diff", func(t *testing.T) {
54 t.Parallel()
55
56 m1, m2 := gen()
57 m2["five"].(map[string]interface{})["five"].(map[string]interface{})["no"] = "ys"
58
59 fataledWithDiff := false
60 ftb := &fakeTB{
61 TB: t,
62 fatalf: func(f string, v ...interface{}) {
63 t.Helper()
64 if len(v) == 1 {
65 t.Logf(f, v...)
66 fataledWithDiff = true
67 return
68 }
69
70 t.Fatalf(f, v...)
71 }}
72
73 defer func() {
74 if t.Failed() || !fataledWithDiff {
75 t.Error("expected assert.StringJSON to fatal with correct diff")
76 }
77 }()
78 assert.JSON(ftb, m1, m2)
79 })
80 }
81
82 type fakeTB struct {
83 fatalf func(string, ...interface{})
84 testing.TB
85 }
86
87 func (ftb *fakeTB) Fatalf(f string, v ...interface{}) {
88 ftb.TB.Helper()
89 ftb.fatalf(f, v...)
90 }
91
View as plain text