forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_test.go
148 lines (138 loc) · 2.77 KB
/
operation_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
package jsondiff
import "testing"
func TestOperationMarshalJSON(t *testing.T) {
for _, tc := range []struct {
Op Operation
Out string // marshaled output
}{
// Replace and add operations should ALWAYS be marshaled
// with a value, even if it is null (override omitempty).
{
Operation{
Type: OperationReplace,
Path: "/foo/bar",
Value: nil,
},
`{"op":"replace","path":"/foo/bar","value":null}`,
},
{
Operation{
Type: OperationReplace,
Path: "/foo/bar",
Value: typeNilIface(),
},
`{"op":"replace","path":"/foo/bar","value":null}`,
},
{
Operation{
Type: OperationReplace,
Path: "/foo/bar",
Value: "foo",
},
`{"op":"replace","path":"/foo/bar","value":"foo"}`,
},
{
// assigned interface
Operation{
Type: OperationAdd,
Path: "",
Value: nil,
},
`{"op":"add","path":"","value":null}`,
},
{
// unassigned interface Value
Operation{
Type: OperationAdd,
Path: "",
},
`{"op":"add","path":"","value":null}`,
},
{
Operation{
Type: OperationAdd,
Path: "",
Value: typeNilIface(),
},
`{"op":"add","path":"","value":null}`,
},
{
// Remove operation should NEVER be marshaled with
// a value.
Operation{
Type: OperationRemove,
Path: "/foo/bar",
Value: 0,
},
`{"op":"remove","path":"/foo/bar"}`,
},
{
// Copy operation should NEVER be marshaled with
// a value.
Operation{
Type: OperationCopy,
From: "/bar",
Path: "/baz",
Value: 0,
},
`{"op":"copy","from":"/bar","path":"/baz"}`,
},
{
// Move operation should NEVER be marshaled with
// a value.
Operation{
Type: OperationMove,
From: "/bar",
Path: "/baz",
Value: 0,
},
`{"op":"move","from":"/bar","path":"/baz"}`,
},
} {
b, err := tc.Op.MarshalJSON()
if err != nil {
t.Errorf("failed to marshal operation: %s", err)
}
if tc.Out != string(b) {
t.Errorf("marshaled patch mismatched, got %q, want %q", string(b), tc.Out)
}
}
}
func TestPatchString(t *testing.T) {
patch := Patch{
{
Type: OperationReplace,
Path: "/foo/baz",
Value: 42,
},
{
Type: OperationRemove,
Path: "/xxx",
},
{
Type: OperationAdd,
Path: "/zzz",
Value: make(chan<- string), // UnsupportedTypeError
},
}
s := patch.String()
const expected = `{"op":"replace","path":"/foo/baz","value":42}
{"op":"remove","path":"/xxx"}
<invalid operation>`
if s != expected {
t.Errorf("stringified operation mismatch, got %q, want %q", s, expected)
}
}
func TestNilPatchString(t *testing.T) {
p := new(Patch)
s := p.String()
if s != "" {
t.Errorf("stringified operation mismatch, got %q, want empty string", s)
}
}
func typeNilIface() interface{} {
var i *int
var p interface{}
p = i
return p
}