forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpatch_recursive_set_test.go
More file actions
120 lines (106 loc) · 3.98 KB
/
Copy pathjsonpatch_recursive_set_test.go
File metadata and controls
120 lines (106 loc) · 3.98 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
package jsonpatch
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestMatchesValue_RecursiveSetEquality covers the core invariant of the
// default list-as-set semantics: two lists must compare equal whenever
// their elements form the same multiset, *including* when individual
// elements contain nested collections whose order happens to differ.
//
// Prior to the recursive fix, matchesValue serialised each element via
// json.Marshal and compared the multiset of resulting byte strings.
// json.Marshal sorts map keys (good) but preserves array order (bad for
// us), so a nested list-ordering difference propagated out as a false
// inequality on the outer element comparison.
func TestMatchesValue_NestedArrayOrderingInsideElement(t *testing.T) {
a := []any{
map[string]any{
"Name": "grafana",
"Ports": []any{float64(3000), float64(4318)},
"Env": []any{map[string]any{"Name": "X"}, map[string]any{"Name": "Y"}},
},
map[string]any{"Name": "mimir", "Ports": []any{float64(9009)}},
}
b := []any{
map[string]any{"Name": "mimir", "Ports": []any{float64(9009)}},
map[string]any{
"Name": "grafana",
"Ports": []any{float64(4318), float64(3000)},
"Env": []any{map[string]any{"Name": "Y"}, map[string]any{"Name": "X"}},
},
}
assert.True(t, matchesValue(a, b, true),
"lists should compare equal: same multiset of elements, nested collections only differ by order")
}
// TestMatchesValue_NestedArrayContentDifferenceStillDetected guards the
// negative direction: if a nested list in an element has a real content
// difference (not just reordering), matchesValue must return false.
func TestMatchesValue_NestedArrayContentDifferenceStillDetected(t *testing.T) {
a := []any{
map[string]any{"Name": "grafana", "Ports": []any{float64(3000), float64(4318)}},
}
b := []any{
map[string]any{"Name": "grafana", "Ports": []any{float64(3000), float64(9999)}},
}
assert.False(t, matchesValue(a, b, true),
"different nested content must still be detected as inequality")
}
// TestMatchesValue_NestedMapValueDifferenceStillDetected guards the
// negative direction for nested maps as well.
func TestMatchesValue_NestedMapValueDifferenceStillDetected(t *testing.T) {
a := []any{
map[string]any{"Name": "grafana", "Meta": map[string]any{"Role": "admin"}},
}
b := []any{
map[string]any{"Name": "grafana", "Meta": map[string]any{"Role": "viewer"}},
}
assert.False(t, matchesValue(a, b, true),
"different nested map content must still be detected as inequality")
}
// TestMatchesValue_DifferentLengthsAreUnequal keeps the length short-circuit
// working.
func TestMatchesValue_DifferentLengthsAreUnequal(t *testing.T) {
a := []any{map[string]any{"Name": "grafana"}}
b := []any{map[string]any{"Name": "grafana"}, map[string]any{"Name": "mimir"}}
assert.False(t, matchesValue(a, b, true),
"lists of different lengths are not equal")
}
// TestMatchesValue_DuplicatesAreMultisetSensitive ensures duplicate
// elements are counted as a multiset, not a set — [A, A] != [A, B].
func TestMatchesValue_DuplicatesAreMultisetSensitive(t *testing.T) {
a := []any{
map[string]any{"Name": "grafana"},
map[string]any{"Name": "grafana"},
}
b := []any{
map[string]any{"Name": "grafana"},
map[string]any{"Name": "mimir"},
}
assert.False(t, matchesValue(a, b, true),
"multiset semantics: duplicated elements should not match against distinct elements")
}
// TestMatchesValue_DeepNestingIsSymmetric ensures matching is symmetric
// — matchesValue(a, b) == matchesValue(b, a) — even across arbitrary depth.
func TestMatchesValue_DeepNestingIsSymmetric(t *testing.T) {
a := []any{
map[string]any{
"Outer": []any{
map[string]any{
"Inner": []any{float64(1), float64(2), float64(3)},
},
},
},
}
b := []any{
map[string]any{
"Outer": []any{
map[string]any{
"Inner": []any{float64(3), float64(2), float64(1)},
},
},
},
}
assert.True(t, matchesValue(a, b, true), "a vs b")
assert.True(t, matchesValue(b, a, true), "b vs a — symmetric")
}