-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgithub_test.go
68 lines (57 loc) · 1.63 KB
/
github_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
package patchwerk
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
evanphx "github.com/evanphx/json-patch"
"github.com/stretchr/testify/assert"
)
type jsonTest2 struct {
Comment string `json:"comment"`
Original interface{} `json:"doc"`
Target interface{} `json:"expected"`
Disabled bool `json:"disabled"`
Error *string `json:"error"`
}
// evanphx/json-patch fails when replacing elements on root level, let's wrap everything into an object
type RootWrap struct {
Root interface{} `json:"root"`
}
// Tests copied from github.com/json-patch/json-patch-tests
func TestBase(t *testing.T) {
file, err := ioutil.ReadFile("github.json")
assert.NoError(t, err)
var jsonTests []jsonTest2
err = json.Unmarshal([]byte(file), &jsonTests)
assert.NoError(t, err)
for i, tc := range jsonTests {
if tc.Disabled || tc.Error != nil || tc.Original == nil || tc.Target == nil {
continue
}
testName := fmt.Sprintf(`Test #%d %s`, i, 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 := Diff(b1, b2)
assert.NoError(t, err)
pb, err := json.Marshal(patch)
assert.NoError(t, err)
t.Log("original", string(b1))
t.Log("target", string(b2))
for i, p := range patch {
b, _ := json.Marshal(p)
t.Log("diff", i, string(b))
}
ep, err := evanphx.DecodePatch(pb)
assert.NoError(t, err)
modified, err := ep.Apply(b1)
assert.NoError(t, err)
assert.Equal(t, b2, modified)
})
}
}