-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsondiff_test.go
81 lines (78 loc) · 2.04 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
package jsondiff
import (
"testing"
)
func TestCompareJSON(t *testing.T) {
tests := []struct {
name string
a []byte
b []byte
opts *CompareJSONOptions
expectedRes string
expectedErr error
}{
{
name: "empty json",
a: []byte(`{}`),
b: []byte(`{}`),
opts: nil,
expectedRes: `{}`,
expectedErr: nil,
},
{
name: "json with added key",
a: []byte(`{"foo": "bar"}`),
b: []byte(`{"foo": "bar", "baz": "qux"}`),
opts: nil,
expectedRes: `{"added":{"baz":"qux"}}`,
expectedErr: nil,
},
{
name: "json with removed key",
a: []byte(`{"foo": "bar", "baz": "qux"}`),
b: []byte(`{"foo": "bar"}`),
opts: nil,
expectedRes: `{"removed":{"baz":"qux"}}`,
expectedErr: nil,
},
{
name: "json with changed key",
a: []byte(`{"foo": "bar", "baz": "qux"}`),
b: []byte(`{"foo": "bar", "baz": "quux"}`),
opts: nil,
expectedRes: `{"changed":{"baz":{"new":"quux","old":"qux"}}}`,
expectedErr: nil,
},
{
name: "json with nested objects",
a: []byte(`{"foo": {"bar": "baz"}}`),
b: []byte(`{"foo": {"bar": "qux"}}`),
opts: nil,
expectedRes: `{"changed":{"foo":{"bar":{"new":"qux","old":"baz"}}}}`,
expectedErr: nil,
},
{
name: "json with custom option keys",
a: []byte(`{"foo": "bar", "baz": "qux"}`),
b: []byte(`{"foo": "bar", "baz": "quux"}`),
opts: &CompareJSONOptions{
AddedKey: "new",
RemovedKey: "del",
ChangedKey: "diff",
},
expectedRes: `{"diff":{"baz":{"new":"quux","old":"qux"}}}`,
expectedErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := CompareJSON(tt.a, tt.b, tt.opts)
if err != tt.expectedErr {
t.Errorf("Expected error: %v, but got: %v", tt.expectedErr, err)
}
if res != tt.expectedRes {
t.Errorf("Expected result: %v, but got: %v", tt.expectedRes, res)
}
})
}
}