-
Notifications
You must be signed in to change notification settings - Fork 41
/
operation_test.go
167 lines (155 loc) · 3.2 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package jsondiff
import "testing"
func TestOperation_MarshalJSON(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,
},
`{"value":null,"op":"replace","path":"/foo/bar"}`,
},
{
Operation{
Type: OperationReplace,
Path: "/foo/bar",
Value: typeNilIface(),
},
`{"value":null,"op":"replace","path":"/foo/bar"}`,
},
{
Operation{
Type: OperationReplace,
Path: "/foo/bar",
Value: "foo",
},
`{"value":"foo","op":"replace","path":"/foo/bar"}`,
},
{
// assigned interface
Operation{
Type: OperationAdd,
Path: "",
Value: nil,
},
`{"value":null,"op":"add","path":""}`,
},
{
// unassigned interface Value
Operation{
Type: OperationAdd,
Path: "",
},
`{"value":null,"op":"add","path":""}`,
},
{
Operation{
Type: OperationAdd,
Path: "",
Value: typeNilIface(),
},
`{"value":null,"op":"add","path":""}`,
},
{
// 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 TestPatch_String(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 = `{"value":42,"op":"replace","path":"/foo/baz"}
{"op":"remove","path":"/xxx"}
<invalid operation>`
if s != expected {
t.Errorf("stringified operation mismatch, got %q, want %q", s, expected)
}
}
func TestPatch_String_nil(t *testing.T) {
p := new(Patch)
s := p.String()
if s != "" {
t.Errorf("stringified operation mismatch, got %q, want empty string", s)
}
}
func TestPatch_jsonLength(t *testing.T) {
t.Run("empty", func(t *testing.T) {
p := new(Patch)
l := p.jsonLength()
if l != 0 {
t.Error("expected zero length for empty patch")
}
})
t.Run("nil", func(t *testing.T) {
var p *Patch
l := p.jsonLength()
if l != 0 {
t.Error("expected zero length for nil patch")
}
})
}
func Test_issue26(_ *testing.T) {
p := new(Patch)
p.insert(6, OperationAdd, emptyPointer, "/a", nil, 42, 0)
}
func typeNilIface() interface{} {
var i *int
var p interface{}
p = i
return p
}