forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffer_test.go
104 lines (94 loc) · 2.83 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
package jsondiff
import (
"encoding/json"
"io/ioutil"
"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 []Operation `json:"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/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 := ioutil.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) {
beforeBytes, err := json.Marshal(tc.Before)
if err != nil {
t.Error(err)
}
d := Differ{
targetBytes: beforeBytes,
}
d.applyOpts(opts...)
d.Compare(tc.Before, tc.After)
if d.patch != nil {
t.Logf("\n%s", d.patch)
}
if len(d.patch) != len(tc.Patch) {
t.Errorf("got %d patches, want %d", len(d.patch), len(tc.Patch))
return
}
for i, op := range d.patch {
want := tc.Patch[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.String(), want.Path.String(); 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.String(), want.From.String(); 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)
}
}
}
})
}
}