-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpatch_test.go
More file actions
150 lines (136 loc) · 3.83 KB
/
Copy pathpatch_test.go
File metadata and controls
150 lines (136 loc) · 3.83 KB
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
package jsonpatch
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// TestMakePatch_nop ensures that MakePatch returns an empty patch when the
// input equals the output.
func TestMakePatch_nop(t *testing.T) {
for i, test := range []struct {
src string
}{
{ // simple map
`{"a":1,"b":2}`,
},
{ // nested map
`{"a":{"b":"c", "d":"e"}}`,
},
{ // array
`{"a":[0, 1, 2, 3]}`,
},
} {
index := fmt.Sprintf("test %d", i)
docA := getMapDoc(test.src)
docB := getMapDoc(test.src)
patch, err := MakePatch(docA, docB)
assert.Nil(t, err, index)
assert.Equal(t, len(patch.Operations), 0, index)
}
}
// TestMakePatch ensures that patches cleanly apply to the original documents
// and produce a document equivalent to the target.
func TestMakePatch(t *testing.T) {
for i, test := range []struct {
src, dst string
}{
{ // simple map operations.
`{"a":1,"b":2}`,
`{"a":2,"b":1}`,
},
{ // nested map operations
`{"this":{"is":"my", "document":"sir"}}`,
`{"this":{"document":"my", "is":"sir", "now":{"go":"away!"}}}`,
},
{ // array operations
`{"a":[0, 1, 2, 3]}`,
`{"a":[1, 2, 4, "hi"]}`,
},
/* BUG: this test panics
{ // nested array operations
`{"a":[0, [1, 2, 3], 5, 2]}`,
`{"a":[1, [1, 0, 2, 3, 5], "x", 2]}`,
},
*/
} {
index := fmt.Sprintf("test %d", i)
docA := getMapDoc(test.src)
docB := getMapDoc(test.dst)
patch, err := MakePatch(docA, docB)
assert.Nil(t, err, index)
err = patch.Apply(&docA)
assert.Nil(t, err, index)
assert.Equal(t, docA, docB, index)
}
}
func TestPatchMarshalAndUnmarshal(t *testing.T) {
for i, test := range []struct {
src string
dest Patch
}{
{ // empty
`[]`,
Patch{Operations: []PatchOperation{}},
},
{ // value is ommitted
`[{"from":"/foo","op":"move","path":"/foo2"}]`,
Patch{Operations: []PatchOperation{PatchOperation{Op: Move, From: "/foo", Path: "/foo2"}}},
},
{ // from is ommitted
`[{"op":"replace","path":"/foo","value":"foo"}]`,
Patch{Operations: []PatchOperation{PatchOperation{Op: Replace, Path: "/foo", Value: "foo"}}},
},
{ // value and from are ommitted
`[{"op":"remove","path":"/foo"}]`,
Patch{Operations: []PatchOperation{PatchOperation{Op: Remove, Path: "/foo"}}},
},
} {
index := fmt.Sprintf("test %d", i)
var p Patch
err := json.Unmarshal([]byte(test.src), &p)
assert.Nil(t, err, index)
assert.Equal(t, p, test.dest)
jp, err := json.Marshal(p)
assert.Nil(t, err, index)
assert.Equal(t, string(jp), test.src)
}
}
func TestApplyPatchFromString(t *testing.T) {
doc := getMapDoc(`{"foo": "bar"}`)
patchOp, err := FromString(`[{"op": "add", "path": "/baz", "value": "qux"}]`)
assert.Nil(t, err)
patchOp.Apply(&doc)
val, found := doc["baz"]
assert.True(t, found)
assert.Equal(t, "qux", val.(string))
}
func TestLcs(t *testing.T) {
pairA, pairB := longestCommonSubseq(slice(1, 2, 3, 4), slice(0, 1, 2, 3, 5))
assert.Equal(t, intPair{0, 3}, *pairA)
assert.Equal(t, intPair{1, 4}, *pairB)
pairA, pairB = longestCommonSubseq(slice(1, 3, 5), slice(0, 1, 2, 3, 4, 5, 6))
assert.Equal(t, intPair{2, 3}, *pairA)
assert.Equal(t, intPair{5, 6}, *pairB)
}
func TestSplitByCommonSeq(t *testing.T) {
node := splitByCommonSeq(slice(0, 1, 2, 3), slice(1, 2, 4, 5), &intPair{0, -1}, &intPair{0, -1})
assert.Nil(t, node.left)
assert.Nil(t, node.right)
// Left subtree
assert.NotNil(t, node.leftPtr)
assert.Equal(t, intPair{0, 1}, *node.leftPtr.left)
assert.Nil(t, node.leftPtr.leftPtr)
assert.Nil(t, node.leftPtr.rightPtr)
// Right subtree
assert.NotNil(t, node.rightPtr)
assert.Equal(t, intPair{3, 4}, *node.rightPtr.left)
assert.Equal(t, intPair{2, 4}, *node.rightPtr.right)
assert.Nil(t, node.rightPtr.rightPtr)
assert.Nil(t, node.rightPtr.leftPtr)
}
func slice(args ...interface{}) []interface{} {
s := []interface{}{}
s = append(s, args...)
return s
}