forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
differ_test.go
157 lines (141 loc) · 4.02 KB
/
differ_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
package jsondiff
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
var testNameReplacer = strings.NewReplacer(",", "", "(", "", ")", "")
type testcase struct {
Name string `json:"name"`
Before interface{} `json:"before"`
After interface{} `json:"after"`
Patch Patch `json:"patch"`
IncompletePatch Patch `json:"incomplete_patch"`
Ignores []string `json:"ignores"`
}
type patchGetter func(tc *testcase) Patch
func TestArrayCases(t *testing.T) { runCasesFromFile(t, "testdata/tests/array.json") }
func TestObjectCases(t *testing.T) { runCasesFromFile(t, "testdata/tests/object.json") }
func TestRootCases(t *testing.T) { runCasesFromFile(t, "testdata/tests/root.json") }
func TestOptions(t *testing.T) {
makeopts := func(opts ...Option) []Option { return opts }
for _, tt := range []struct {
testfile string
options []Option
}{
{"testdata/tests/options/invertible.json", makeopts(Invertible())},
{"testdata/tests/options/factorization.json", makeopts(Factorize())},
{"testdata/tests/options/rationalization.json", makeopts(Rationalize())},
{"testdata/tests/options/equivalence.json", makeopts(Equivalent())},
{"testdata/tests/options/ignore.json", makeopts()},
{"testdata/tests/options/omitempty.json", makeopts(OmitEmpty())},
{"testdata/tests/options/all.json", makeopts(Factorize(), Rationalize(), Invertible(), Equivalent())},
} {
var (
ext = filepath.Ext(tt.testfile)
base = filepath.Base(tt.testfile)
name = strings.TrimSuffix(base, ext)
)
t.Run(name, func(t *testing.T) {
runCasesFromFile(t, tt.testfile, tt.options...)
})
}
}
func runCasesFromFile(t *testing.T, filename string, opts ...Option) {
b, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
var cases []testcase
if err := json.Unmarshal(b, &cases); err != nil {
t.Fatal(err)
}
runTestCases(t, cases, opts...)
}
func runTestCases(t *testing.T, cases []testcase, opts ...Option) {
for _, tc := range cases {
name := testNameReplacer.Replace(tc.Name)
t.Run(name, func(t *testing.T) {
runTestCase(t, tc, func(tc *testcase) Patch {
return tc.Patch
}, opts...)
})
if len(tc.Ignores) != 0 {
name = fmt.Sprintf("%s_with_ignore", name)
xopts := append(opts, Ignores(tc.Ignores...))
t.Run(name, func(t *testing.T) {
runTestCase(t, tc, func(tc *testcase) Patch {
return tc.IncompletePatch
}, xopts...)
})
}
}
}
func runTestCase(t *testing.T, tc testcase, pc patchGetter, opts ...Option) {
beforeBytes, err := json.Marshal(tc.Before)
if err != nil {
t.Error(err)
}
d := &Differ{
targetBytes: beforeBytes,
}
d = d.WithOpts(opts...)
d.Compare(tc.Before, tc.After)
patch := d.Patch()
wantPatch := pc(&tc)
if patch != nil {
t.Logf("\n%s", patch)
}
if len(patch) != len(wantPatch) {
t.Errorf("got %d patches, want %d", len(patch), len(wantPatch))
return
}
for i, op := range patch {
want := wantPatch[i]
if g, w := op.Type, want.Type; g != w {
t.Errorf("op #%d mismatch: op: got %q, want %q", i, g, w)
}
if g, w := op.Path, want.Path; g != w {
t.Errorf("op #%d mismatch: path: got %q, want %q", i, g, w)
}
switch want.Type {
case OperationCopy, OperationMove:
if g, w := op.From, want.From; g != w {
t.Errorf("op #%d mismatch: from: got %q, want %q", i, g, w)
}
case OperationAdd, OperationReplace:
if !reflect.DeepEqual(op.Value, want.Value) {
t.Errorf("op #%d mismatch: value: unequal", i)
}
}
}
}
func TestDiffer_Reset(t *testing.T) {
d := &Differ{
ptr: pointer{
buf: make([]byte, 15, 15),
end: 15,
},
hashmap: map[uint64]jsonNode{
1: {},
},
patch: make([]Operation, 42, 42),
}
d.Reset()
if l := len(d.patch); l != 0 {
t.Errorf("expected empty patch collection, got length %d", l)
}
if l := len(d.hashmap); l != 0 {
t.Errorf("expected cleared hashmap, got length %d", l)
}
if d.ptr.end != 0 {
t.Errorf("expected reset ptr")
}
if l := len(d.ptr.buf); l != 0 {
t.Errorf("expected empty ptr buf, got length %d", l)
}
}