-
Notifications
You must be signed in to change notification settings - Fork 41
/
apply_test.go
93 lines (88 loc) · 1.39 KB
/
apply_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
package jsondiff
import (
"testing"
)
func Test_toDotPath(t *testing.T) {
for _, tc := range []struct {
ptr string
json string
path string
}{
{
"",
`{}`,
"@this",
},
{
"/a/b/c",
`{"a":{"b":{"c":1}}}`,
"a.b.c",
},
{
"/a/1/c",
`{"a":[null,{"c":1}]}`,
"a.1.c",
},
{
"/a/123/b",
`{"a":{"123":{"b":1"}}}`,
"a.:123.b",
},
{
"/1",
`["a","b","c"]`,
"1",
},
{
"/0",
`{"0":"a"}`,
":0",
},
{
"/a/-",
`{"a":[1,2,3]}`,
"a.-1",
},
} {
s, err := toDotPath(tc.ptr, []byte(tc.json))
if err != nil {
t.Error(err)
}
if s != tc.path {
t.Errorf("got %q, want %q", s, tc.path)
}
}
}
func Test_isArrayIndex(t *testing.T) {
for _, tc := range []struct {
path string
isIndex bool
}{
{"a.b.c", false},
{"", false},
{"a.b.:124", false},
{"a.-1", false},
{"0.1.a", false},
{"0.1.2.:3", false},
{"a\\.b", false},
{"0.1\\.2", false},
{"0", true},
{"a.b.1", true},
{"0.1.2", true},
} {
b := isArrayIndex(tc.path)
if tc.isIndex && !b {
t.Errorf("expected path %q to be an array index", tc.path)
}
if !tc.isIndex && b {
t.Errorf("expected path %q to not be an array index", tc.path)
}
}
}
func TestPatch_apply_invalidJSON(t *testing.T) {
badJSON := []byte(`{"example":2:]}}`)
var p Patch
if _, err := p.apply(badJSON, true); err == nil {
t.Errorf("expected non-nil error")
}
}