Skip to content

Commit ea1d56f

Browse files
committed
test path element less
1 parent 35695b7 commit ea1d56f

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

fieldpath/element_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package fieldpath
1818

1919
import (
2020
"testing"
21+
22+
"sigs.k8s.io/structured-merge-diff/value"
2123
)
2224

2325
func TestPathElementSet(t *testing.T) {
@@ -59,3 +61,111 @@ func TestPathElementSet(t *testing.T) {
5961
i++
6062
})
6163
}
64+
65+
func strptr(s string) *string { return &s }
66+
func intptr(i int) *int { return &i }
67+
func valptr(i int) *value.Value { v := value.IntValue(i); return &v }
68+
69+
func TestPathElementLess(t *testing.T) {
70+
table := []struct {
71+
name string
72+
// we expect a < b and !(b < a) unless eq is true, in which
73+
// case we expect less to return false in both orders.
74+
a, b PathElement
75+
eq bool
76+
}{
77+
{
78+
name: "FieldName-0",
79+
a: PathElement{},
80+
b: PathElement{},
81+
eq: true,
82+
}, {
83+
name: "FieldName-1",
84+
a: PathElement{FieldName: strptr("anteater")},
85+
b: PathElement{FieldName: strptr("zebra")},
86+
}, {
87+
name: "FieldName-2",
88+
a: PathElement{FieldName: strptr("bee")},
89+
b: PathElement{FieldName: strptr("bee")},
90+
eq: true,
91+
}, {
92+
name: "FieldName-3",
93+
a: PathElement{FieldName: strptr("capybara")},
94+
b: PathElement{Key: []value.Field{{Name: "dog", Value: value.IntValue(3)}}},
95+
}, {
96+
name: "FieldName-4",
97+
a: PathElement{FieldName: strptr("elephant")},
98+
b: PathElement{Value: valptr(4)},
99+
}, {
100+
name: "FieldName-5",
101+
a: PathElement{FieldName: strptr("falcon")},
102+
b: PathElement{Index: intptr(5)},
103+
}, {
104+
name: "Key-1",
105+
a: PathElement{Key: []value.Field{{Name: "goat", Value: value.IntValue(1)}}},
106+
b: PathElement{Key: []value.Field{{Name: "goat", Value: value.IntValue(1)}}},
107+
eq: true,
108+
}, {
109+
name: "Key-2",
110+
a: PathElement{Key: []value.Field{{Name: "horse", Value: value.IntValue(1)}}},
111+
b: PathElement{Key: []value.Field{{Name: "horse", Value: value.IntValue(2)}}},
112+
}, {
113+
name: "Key-3",
114+
a: PathElement{Key: []value.Field{{Name: "ibex", Value: value.IntValue(1)}}},
115+
b: PathElement{Key: []value.Field{{Name: "jay", Value: value.IntValue(1)}}},
116+
}, {
117+
name: "Key-4",
118+
a: PathElement{Key: []value.Field{{Name: "kite", Value: value.IntValue(1)}}},
119+
b: PathElement{Key: []value.Field{{Name: "kite", Value: value.IntValue(1)}, {Name: "kite-2", Value: value.IntValue(1)}}},
120+
}, {
121+
name: "Key-5",
122+
a: PathElement{Key: []value.Field{{Name: "kite", Value: value.IntValue(1)}}},
123+
b: PathElement{Value: valptr(1)},
124+
}, {
125+
name: "Key-6",
126+
a: PathElement{Key: []value.Field{{Name: "kite", Value: value.IntValue(1)}}},
127+
b: PathElement{Index: intptr(5)},
128+
}, {
129+
name: "Value-1",
130+
a: PathElement{Value: valptr(1)},
131+
b: PathElement{Value: valptr(2)},
132+
}, {
133+
name: "Value-2",
134+
a: PathElement{Value: valptr(1)},
135+
b: PathElement{Value: valptr(1)},
136+
eq: true,
137+
}, {
138+
name: "Value-3",
139+
a: PathElement{Value: valptr(1)},
140+
b: PathElement{Index: intptr(1)},
141+
}, {
142+
name: "Index-1",
143+
a: PathElement{Index: intptr(1)},
144+
b: PathElement{Index: intptr(2)},
145+
}, {
146+
name: "Index-2",
147+
a: PathElement{Index: intptr(1)},
148+
b: PathElement{Index: intptr(1)},
149+
eq: true,
150+
},
151+
}
152+
153+
for i := range table {
154+
i := i
155+
t.Run(table[i].name, func(t *testing.T) {
156+
tt := table[i]
157+
if tt.eq {
158+
if tt.a.Less(tt.b) {
159+
t.Errorf("oops, a < b: %#v, %#v", tt.a, tt.b)
160+
}
161+
} else {
162+
if !tt.a.Less(tt.b) {
163+
t.Errorf("oops, a >= b: %#v, %#v", tt.a, tt.b)
164+
}
165+
}
166+
if tt.b.Less(tt.b) {
167+
t.Errorf("oops, b < a: %#v, %#v", tt.b, tt.a)
168+
}
169+
})
170+
}
171+
}

0 commit comments

Comments
 (0)