-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff_test.go
83 lines (78 loc) · 1.73 KB
/
diff_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
package jsondiff
import (
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/itchyny/gojq"
)
func TestDiff(t *testing.T) {
testCases := []struct {
name string
opts []Option
wantDiffPath string
wantErr bool
}{
{
"nothing",
[]Option{},
"./testdata/nothing.diff",
false,
},
{
"both only and ignore",
[]Option{Ignore(parseQuery(t, ".b, .c")), Only(parseQuery(t, ".d"))},
"",
true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
lhs := map[string]interface{}{"a": 1, "b": 2, "c": 3, "d": 4}
rhs := map[string]interface{}{"a": 1, "b": 1, "c": 2, "d": 3}
got, err := DiffFromObjects(lhs, rhs, tc.opts...)
if (err != nil) != tc.wantErr {
t.Fatalf("wantErr=%v got=%v (%#v)", tc.wantErr, err, err)
}
if tc.wantErr {
return
}
want, err := os.ReadFile(tc.wantDiffPath)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(string(want), got); diff != "" {
t.Errorf("-want,+got:\n%s", diff)
}
})
}
}
func Test_withUpdate(t *testing.T) {
queries := []struct {
query string
want string
}{
{".age", ".age = null"},
{".age, .name", ".age = null | .name = null"},
{".age, .name, .meta", ".age = null | .name = null | .meta = null"},
{".meta[]", ".meta[] = null"},
{".meta[0:-1]", ".meta[0:-1] = null"},
}
for _, c := range queries {
t.Run(c.query, func(t *testing.T) {
want := parseQuery(t, c.want)
got := WithUpdate(parseQuery(t, c.query))
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("-want, +got:\n%s", diff)
}
})
}
}
func parseQuery(t *testing.T, q string) *gojq.Query {
t.Helper()
parsed, err := gojq.Parse(q)
if err != nil {
t.Fatal(err)
return nil
}
return parsed
}