-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpatch_test.go
More file actions
409 lines (342 loc) · 8.93 KB
/
patch_test.go
File metadata and controls
409 lines (342 loc) · 8.93 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package deep
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
"github.com/brunoga/deep/v4/cond"
)
func TestPatch_String_Basic(t *testing.T) {
a, b := "foo", "bar"
patch := MustDiff(a, b)
if !strings.Contains(patch.String(), "foo -> bar") {
t.Errorf("String() missing transition: %s", patch.String())
}
}
func TestPatch_String_Complex(t *testing.T) {
type Child struct {
Name string
}
type Data struct {
Tags []string
Meta map[string]any
Kids []Child
Status *string
}
active := "active"
inactive := "inactive"
a := Data{
Tags: []string{"tag1", "tag2"},
Meta: map[string]any{
"key1": "val1",
"key2": 123,
},
Kids: []Child{
{Name: "Kid1"},
},
Status: &active,
}
b := Data{
Tags: []string{"tag1", "tag2", "tag3"},
Meta: map[string]any{
"key1": "val1-mod",
"key3": true,
},
Kids: []Child{
{Name: "Kid1"},
{Name: "Kid2"},
},
Status: &inactive,
}
patch := MustDiff(a, b)
if patch == nil {
t.Fatal("Expected non-nil patch")
}
summary := patch.String()
if !strings.Contains(summary, "+ [1]: {Kid2}") {
t.Errorf("String() missing added kid: %s", summary)
}
}
func TestPatch_ApplyResolved(t *testing.T) {
type Config struct {
Value int
}
c1 := Config{Value: 10}
c2 := Config{Value: 20}
patch := MustDiff(c1, c2)
target := Config{Value: 10}
// Resolver that rejects everything
err := patch.ApplyResolved(&target, ConflictResolverFunc(func(path string, op OpKind, key, prevKey any, current, proposed reflect.Value) (reflect.Value, bool) {
return reflect.Value{}, false
}))
if err != nil {
t.Fatalf("ApplyResolved failed: %v", err)
}
if target.Value != 10 {
t.Errorf("Value should not have changed, got %d", target.Value)
}
// Resolver that accepts everything
err = patch.ApplyResolved(&target, ConflictResolverFunc(func(path string, op OpKind, key, prevKey any, current, proposed reflect.Value) (reflect.Value, bool) {
return proposed, true
}))
if err != nil {
t.Fatalf("ApplyResolved failed: %v", err)
}
if target.Value != 20 {
t.Errorf("Value should have changed to 20, got %d", target.Value)
}
}
type ConflictResolverFunc func(path string, op OpKind, key, prevKey any, current, proposed reflect.Value) (reflect.Value, bool)
func (f ConflictResolverFunc) Resolve(path string, op OpKind, key, prevKey any, current, proposed reflect.Value) (reflect.Value, bool) {
return f(path, op, key, prevKey, current, proposed)
}
func TestPatch_ConditionsExhaustive(t *testing.T) {
type InnerC struct{ V int }
type DataC struct {
A int
P *InnerC
I any
M map[string]InnerC
S []InnerC
Arr [1]InnerC
}
builder := NewPatchBuilder[DataC]()
c := cond.Eq[DataC]("A", 1)
builder.If(c).Unless(c).Test(DataC{A: 1})
builder.Field("P").If(c).Unless(c)
builder.Field("I").If(c).Unless(c)
builder.Field("M").If(c).Unless(c)
builder.Field("S").If(c).Unless(c)
builder.Field("Arr").If(c).Unless(c)
patch, _ := builder.Build()
if patch == nil {
t.Fatal("Build failed")
}
}
func TestPatch_MoreApplyChecked(t *testing.T) {
// ptrPatch
t.Run("ptrPatch", func(t *testing.T) {
val1 := 1
p1 := &val1
val2 := 2
p2 := &val2
patch := MustDiff(p1, p2)
if err := patch.ApplyChecked(&p1); err != nil {
t.Errorf("ptrPatch ApplyChecked failed: %v", err)
}
})
// interfacePatch
t.Run("interfacePatch", func(t *testing.T) {
var i1 any = 1
var i2 any = 2
patch := MustDiff(i1, i2)
if err := patch.ApplyChecked(&i1); err != nil {
t.Errorf("interfacePatch ApplyChecked failed: %v", err)
}
})
}
func TestPatch_ToJSONPatch_Exhaustive(t *testing.T) {
type Inner struct{ V int }
type Data struct {
P *Inner
I any
A []Inner
M map[string]Inner
}
builder := NewPatchBuilder[Data]()
builder.Field("P").Elem().Field("V").Set(1, 2)
builder.Field("I").Elem().Set(1, 2)
builder.Field("A").Index(0).Field("V").Set(1, 2)
builder.Field("M").MapKey("k").Field("V").Set(1, 2)
patch, _ := builder.Build()
patch.ToJSONPatch()
}
func TestPatch_LogExhaustive(t *testing.T) {
lp := &logPatch{message: "test"}
lp.apply(reflect.Value{}, reflect.ValueOf(1), "/path")
if err := lp.applyChecked(reflect.ValueOf(1), reflect.ValueOf(1), false, "/path"); err != nil {
t.Errorf("logPatch applyChecked failed: %v", err)
}
if lp.reverse() != lp {
t.Error("logPatch reverse should return itself")
}
if lp.format(0) == "" {
t.Error("logPatch format returned empty string")
}
ops := lp.toJSONPatch("/path")
if len(ops) != 1 || ops[0]["op"] != "log" {
t.Errorf("Unexpected toJSONPatch output: %+v", ops)
}
}
func TestPatch_Walk_Basic(t *testing.T) {
a := 10
b := 20
patch := MustDiff(a, b)
var ops []string
err := patch.Walk(func(path string, op OpKind, old, new any) error {
ops = append(ops, fmt.Sprintf("%s:%s:%v:%v", path, op, old, new))
return nil
})
if err != nil {
t.Fatalf("Walk failed: %v", err)
}
expected := []string{"/:replace:10:20"}
if fmt.Sprintf("%v", ops) != fmt.Sprintf("%v", expected) {
t.Errorf("Expected ops %v, got %v", expected, ops)
}
}
func TestPatch_Walk_Struct(t *testing.T) {
type S struct {
A int
B string
}
a := S{A: 1, B: "one"}
b := S{A: 2, B: "two"}
patch := MustDiff(a, b)
ops := make(map[string]string)
err := patch.Walk(func(path string, op OpKind, old, new any) error {
ops[path] = fmt.Sprintf("%s:%v:%v", op, old, new)
return nil
})
if err != nil {
t.Fatalf("Walk failed: %v", err)
}
if len(ops) != 2 {
t.Errorf("Expected 2 ops, got %d", len(ops))
}
if ops["/A"] != "replace:1:2" {
t.Errorf("Unexpected op for A: %s", ops["/A"])
}
if ops["/B"] != "replace:one:two" {
t.Errorf("Unexpected op for B: %s", ops["/B"])
}
}
func TestPatch_Walk_Slice(t *testing.T) {
a := []int{1, 2, 3}
b := []int{1, 4, 3, 5}
patch := MustDiff(a, b)
var ops []string
err := patch.Walk(func(path string, op OpKind, old, new any) error {
ops = append(ops, fmt.Sprintf("%s:%s:%v:%v", path, op, old, new))
return nil
})
if err != nil {
t.Fatalf("Walk failed: %v", err)
}
found4 := false
found5 := false
for _, op := range ops {
if strings.Contains(op, ":2:4") || (strings.Contains(op, ":remove:2:<nil>") || strings.Contains(op, ":add:<nil>:4")) {
if strings.Contains(op, "4") {
found4 = true
}
}
if strings.Contains(op, ":add:<nil>:5") {
found5 = true
}
}
if !found4 || !found5 {
t.Errorf("Missing expected ops in %v", ops)
}
}
func TestPatch_Walk_Map(t *testing.T) {
a := map[string]int{"one": 1, "two": 2}
b := map[string]int{"one": 1, "two": 20, "three": 3}
patch := MustDiff(a, b)
ops := make(map[string]string)
err := patch.Walk(func(path string, op OpKind, old, new any) error {
ops[path] = fmt.Sprintf("%s:%v:%v", op, old, new)
return nil
})
if err != nil {
t.Fatalf("Walk failed: %v", err)
}
if ops["/two"] != "replace:2:20" {
t.Errorf("Unexpected op for two: %s", ops["/two"])
}
if ops["/three"] != "add:<nil>:3" {
t.Errorf("Unexpected op for three: %s", ops["/three"])
}
}
func TestPatch_Walk_KeyedSlice(t *testing.T) {
type KeyedTask struct {
ID string `deep:"key"`
Status string
}
a := []KeyedTask{
{ID: "t1", Status: "todo"},
{ID: "t2", Status: "todo"},
}
b := []KeyedTask{
{ID: "t2", Status: "done"},
{ID: "t1", Status: "todo"},
}
patch := MustDiff(a, b)
ops := make(map[string]string)
err := patch.Walk(func(path string, op OpKind, old, new any) error {
ops[path] = fmt.Sprintf("%s:%v:%v", op, old, new)
return nil
})
if err != nil {
t.Fatalf("Walk failed: %v", err)
}
if len(ops) == 0 {
t.Errorf("Expected some ops, got none")
}
}
func TestPatch_Walk_ErrorStop(t *testing.T) {
a := map[string]int{"one": 1, "two": 2}
b := map[string]int{"one": 10, "two": 20}
patch := MustDiff(a, b)
count := 0
err := patch.Walk(func(path string, op OpKind, old, new any) error {
count++
return fmt.Errorf("stop")
})
if err == nil || err.Error() != "stop" {
t.Errorf("Expected 'stop' error, got %v", err)
}
if count != 1 {
t.Errorf("Expected walk to stop after 1 call, got %d", count)
}
}
type customTestStruct struct {
V int
}
func TestCustomDiffPatch_ToJSONPatch(t *testing.T) {
builder := NewPatchBuilder[customTestStruct]()
builder.Field("V").Set(1, 2)
patch, _ := builder.Build()
// Manually wrap it in customDiffPatch
custom := &customDiffPatch{
patch: patch,
}
jsonBytes := custom.toJSONPatch("") // Use empty prefix for root
var ops []map[string]any
data, _ := json.Marshal(jsonBytes)
json.Unmarshal(data, &ops)
if len(ops) != 1 {
t.Fatalf("expected 1 op, got %d", len(ops))
}
if ops[0]["path"] != "/V" {
t.Errorf("expected path /V, got %s", ops[0]["path"])
}
}
func TestPatch_Summary(t *testing.T) {
type Config struct {
Name string
Value int
Options []string
}
c1 := Config{Name: "v1", Value: 10, Options: []string{"a", "b"}}
c2 := Config{Name: "v2", Value: 20, Options: []string{"a", "c"}}
patch := MustDiff(c1, c2)
if patch == nil {
t.Fatal("Expected patch")
}
summary := patch.Summary()
if summary == "" || summary == "No changes." {
t.Errorf("Unexpected summary: %q", summary)
}
}