-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperation_remove_test.go
More file actions
77 lines (72 loc) · 1.62 KB
/
operation_remove_test.go
File metadata and controls
77 lines (72 loc) · 1.62 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
package jsonpatch
import (
"encoding/json"
"reflect"
"testing"
)
type applyRemoveTestCase[T any] struct {
name string
patches []*PatchRequest[T]
input *T
newEmptyInputFunc func() *T
expectError bool
expect *T
}
func TestPatchRequest_applyRemove(t *testing.T) {
testCases := []applyRemoveTestCase[MyStruct]{
{
name: "remove_string",
patches: []*PatchRequest[MyStruct]{
{
Operation: "remove",
Path: "$.field_string",
},
},
newEmptyInputFunc: NewMyStruct,
input: &MyStruct{
FieldString: "foo",
},
expectError: false,
expect: &MyStruct{
FieldString: "",
},
},
{
name: "remove_string_ptr",
patches: []*PatchRequest[MyStruct]{
{
Operation: "remove",
Path: "$.field_string_ptr",
},
},
newEmptyInputFunc: NewMyStruct,
input: &MyStruct{
FieldStringPtr: getPtr("foo"),
},
expectError: false,
expect: &MyStruct{
FieldStringPtr: nil,
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
var err error
var patched *MyStruct
patched = tt.input
for _, pr := range tt.patches {
patched, err = pr.remove(patched, tt.newEmptyInputFunc())
}
if (err != nil) != tt.expectError {
t.Errorf("applyRemove() error = %v, expectError %v", err, tt.expectError)
return
}
if !reflect.DeepEqual(patched, tt.expect) {
bPatched, _ := json.Marshal(patched)
bExpected, _ := json.Marshal(tt.expect)
t.Errorf("applyRemove() got = %s", string(bPatched))
t.Errorf("applyRemove() expect = %s", string(bExpected))
}
})
}
}