-
Notifications
You must be signed in to change notification settings - Fork 5
/
arrays_test.go
62 lines (49 loc) · 1.36 KB
/
arrays_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
package patchwerk
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
evanphx "github.com/evanphx/json-patch"
"github.com/stretchr/testify/assert"
)
type jsonTest struct {
Comment string `json:"comment"`
Original []interface{} `json:"original"`
Target []interface{} `json:"target"`
Patch []interface{} `json:"patch"`
Disabled bool `json:"disabled"`
}
func TestArrays(t *testing.T) {
file, err := ioutil.ReadFile("arrays.json")
assert.NoError(t, err)
var jsonTests []jsonTest
err = json.Unmarshal([]byte(file), &jsonTests)
assert.NoError(t, err)
for i, tc := range jsonTests {
if tc.Disabled {
continue
}
testName := fmt.Sprintf(`Test #%d %s`, i+1, tc.Comment)
// tc.Original = RootWrap{tc.Original}
// tc.Target = RootWrap{tc.Target}
t.Run(testName, func(t *testing.T) {
b1, err := json.Marshal(tc.Original)
assert.NoError(t, err)
b2, err := json.Marshal(tc.Target)
assert.NoError(t, err)
patch, err := diffArrays(tc.Original, tc.Target, "")
assert.NoError(t, err)
pb1, err := json.Marshal(tc.Patch)
assert.NoError(t, err)
pb2, err := json.Marshal(patch)
assert.NoError(t, err)
assert.JSONEq(t, string(pb1), string(pb2))
ep, err := evanphx.DecodePatch(pb2)
assert.NoError(t, err)
modified, err := ep.Apply(b1)
assert.NoError(t, err)
assert.Equal(t, b2, modified)
})
}
}