-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroundtrip_test.go
159 lines (148 loc) · 2.74 KB
/
roundtrip_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
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
151
152
153
154
155
156
157
158
159
package mendoza_test
import (
"encoding/json"
"fmt"
"github.com/sanity-io/mendoza"
"github.com/stretchr/testify/require"
"testing"
)
var Documents = []struct {
Left string
Right string
}{
{
`{}`,
`{}`,
},
{
`1`,
`{}`,
},
{
`{"a": "b"}`,
`{"a": "b"}`,
},
{
`{"a": "a"}`,
`{"a": "b"}`,
},
{
`{"a": "a", "b": "b"}`,
`{"a": "b"}`,
},
{
`{"a": "a", "b": "b", "c": "c"}`,
`{"a": "a", "b": "b", "c": "c", "d": "d"}`,
},
{
`{"a": "a", "b": "b", "c": "c"}`,
`{"d": "d"}`,
},
{
`{"a": "a", "b": {"a": "a"}}`,
`{"a": "a", "b": {"a": "b", "b": "a"}}`,
},
{
`{"a": ["a", "b", "c"]}`,
`{"a": ["a", "b", "c"]}`,
},
{
`{"a": ["a", "b", "c"]}`,
`{"a": ["a", "b"]}`,
},
{
`{"a": [1, 2]}`,
`{"a": [2, 3]}`,
},
{
`{"a": "abcdef"}`,
`{"a": "abcdefg"}`,
},
{
`{"a": "abcdef"}`,
`{"a": "abcgihdef"}`,
},
{
`{"a": "abcdefghijk"}`,
`{"a": "abcdehijk"}`,
},
{
`{"a": "abcdefghijk"}`,
`{"a": "bcdeghijk"}`,
},
{
`"abc"`,
`"abcdef"`,
},
{
`"abc"`,
`"abc"`,
},
{
`"a:{},:{},"`,
`"a:{},"`,
},
{
`[[]]`,
`[]`,
},
{
`{"":""}`,
`{"":"","0000":""}`,
},
{
`{"H":{"":{}}}`,
`{"H":0}`,
},
{
`"݆݆݅Ʌ"`,
`"І݆Ʌ"`,
},
}
func decodePatch(data []byte, patch *mendoza.Patch) error {
var value []interface{}
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
err = patch.DecodeJSON(value)
if err != nil {
return err
}
return nil
}
func TestRoundtrip(t *testing.T) {
for idx, pair := range Documents {
t.Run(fmt.Sprintf("N%d", idx), func(t *testing.T) {
var left, right interface{}
err := json.Unmarshal([]byte(pair.Left), &left)
require.NoError(t, err)
err = json.Unmarshal([]byte(pair.Right), &right)
require.NoError(t, err)
patch1, patch2, err := mendoza.CreateDoublePatch(left, right)
require.NoError(t, err)
result1 := mendoza.ApplyPatch(left, patch1)
require.EqualValues(t, right, result1)
result2 := mendoza.ApplyPatch(right, patch2)
require.EqualValues(t, left, result2)
// Now try to encode and decode the patch
json1, err := json.Marshal(patch1)
require.NoError(t, err)
var parsedPatch1, decodedPatch1 mendoza.Patch
err = json.Unmarshal(json1, &parsedPatch1)
require.NoError(t, err)
err = decodePatch(json1, &decodedPatch1)
require.EqualValues(t, patch1, parsedPatch1)
require.EqualValues(t, parsedPatch1, decodedPatch1)
json2, err := json.Marshal(patch2)
require.NoError(t, err)
var parsedPatch2, decodedPatch2 mendoza.Patch
err = json.Unmarshal(json2, &parsedPatch2)
require.NoError(t, err)
err = decodePatch(json2, &decodedPatch2)
require.NoError(t, err)
require.EqualValues(t, patch2, parsedPatch2)
require.EqualValues(t, parsedPatch2, decodedPatch2)
})
}
}