-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonDiff_test.go
175 lines (157 loc) · 3.44 KB
/
jsonDiff_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
package main
import (
"testing"
)
func slicesEqual(s1, s2 [][]interface{}) bool {
if len(s1) != len(s2) {
return false
}
for i := range s1 {
s1_1 := s1[i]
s2_1 := s2[i]
if len(s1_1) != len(s2_1) {
return false
}
for ii, v := range s1_1 {
if v != s2_1[ii] {
return false
}
}
}
return true
}
type getIgnoreTestpair struct {
args []string
expected [][]interface{}
}
var getIgnoreTests = []getIgnoreTestpair{
{
[]string{`{}`, `{}`},
[][]interface{}{},
},
{
[]string{`{}`, `{"k1": "v1"}`},
[][]interface{}{{"k1"}},
},
{
[]string{`{"k1" : "v1"}`, `{"k1" : "v1"}`},
[][]interface{}{},
},
{
[]string{`{"k1" : "v1", "k2": "v2"}`, `{"k1" : "v1", "k2": "diff"}`},
[][]interface{}{{"k2"}},
},
{
[]string{`{"k1" : "v1"}`, `{}`},
[][]interface{}{{"k1"}},
},
{
[]string{`{}`, `{"k1" : "v1"}`},
[][]interface{}{{"k1"}},
},
{
[]string{`{"k1": 1}`, `{"k1" : "1"}`},
[][]interface{}{{"k1"}},
},
{
[]string{`{"k1": 1}`, `{"k2" : 1}`},
[][]interface{}{{"k1"}, {"k2"}},
},
{
[]string{`{"k1": {"k1": "v1"}}`, `{"k1": {"k1": "v1"}}`},
[][]interface{}{},
},
{
[]string{`{"k1": {"k1": "v1"}}`, `{"k1": {"k1": "diff"}}`},
[][]interface{}{{"k1", "k1"}},
},
{
[]string{`{"k1": {"k1": "v1"}}`, `{"k1": {"k2": "v2"}}`},
[][]interface{}{{"k1", "k1"}, {"k1", "k2"}},
},
{
[]string{`{"k1": {"k1": "v1"}}`, `{"k1": {"k1": "v1", "k2": "v2"}}`},
[][]interface{}{{"k1", "k2"}},
},
{
[]string{`{"k1": [1,3]}`, `{"k1": [1,2]}`},
[][]interface{}{{"k1", 1}},
},
{
[]string{`{"k1": [2,2]}`, `{"k1": [1,2]}`},
[][]interface{}{{"k1", 0}},
},
{
[]string{`{"k1": [1,2]}`, `{"k1": [1]}`},
[][]interface{}{{"k1", 1}},
},
{
[]string{`{"k1": [2,2]}`, `{"k1": [1]}`},
[][]interface{}{{"k1", 0}, {"k1", 1}},
},
{
[]string{`{"k1": [{"k1": "v1"},1]}`, `{"k1": [{"k1": "v1"},2]}`},
[][]interface{}{{"k1", 1}},
},
}
func TestGetIgnores(t *testing.T) {
for _, pair := range getIgnoreTests {
result, err := getIgnores(pair.args[0], pair.args[1])
if err != nil {
t.Error("Got unexpected error ", err)
}
if !slicesEqual(result, pair.expected) {
t.Error("Failed with args: ", pair.args[0], " and ", pair.args[1], ". Expected ", pair.expected, " but got ", result)
}
}
}
type getJSONPathTestpair struct {
arg []interface{}
expected string
}
var getJSONPathTests = []getJSONPathTestpair{
{
[]interface{}{},
"",
},
{
[]interface{}{"a", "b", "c"},
"[\"a\"][\"b\"][\"c\"]",
},
{
[]interface{}{"a", 0, "c"},
"[\"a\"][0][\"c\"]",
},
}
func TestGetJSONPath(t *testing.T) {
for _, pair := range getJSONPathTests {
result := getJSONPath(pair.arg)
if result != pair.expected {
t.Error("Failed with arg: ", pair.arg)
}
}
}
func TestGetMessage(t *testing.T) {
for _, pair := range getIgnoreTests {
result := jsonDiffMessage(pair.args[0], pair.args[1])
expected := getMessage(pair.expected)
if result != expected {
t.Error("Failed with args: ", pair.args[0], " and ", pair.args[1], ". Expected ", expected, " but got ", result)
}
}
}
func TestJSONDiffMessage(t *testing.T) {
// Test non-JSON
if jsonDiffMessage("not json", "not json") != "" {
t.Error("Expected empty message")
}
if jsonDiffMessage(`{"k1": "v1"}`, "not json") != "" {
t.Error("Expected empty message")
}
if jsonDiffMessage("not json", `{"k1": "v1"}`) != "" {
t.Error("Expected empty message")
}
if jsonDiffMessage(`{"k1": "v1"}`, `{"k1": "v1"}`) != "" {
t.Error("Expected empty message")
}
}