@@ -25,15 +25,16 @@ import (
25
25
"k8s.io/utils/ptr"
26
26
)
27
27
28
- type Struct struct {
28
+ type StructComparable struct {
29
29
S string
30
30
I int
31
31
B bool
32
32
}
33
33
34
- func TestImmutable (t * testing.T ) {
35
- structA := Struct {"abc" , 123 , true }
36
- structB := Struct {"xyz" , 456 , false }
34
+ func TestImmutableByCompare (t * testing.T ) {
35
+ structA := StructComparable {"abc" , 123 , true }
36
+ structA2 := structA
37
+ structB := StructComparable {"xyz" , 456 , false }
37
38
38
39
for _ , tc := range []struct {
39
40
name string
@@ -42,62 +43,194 @@ func TestImmutable(t *testing.T) {
42
43
}{{
43
44
name : "nil both values" ,
44
45
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
45
- return Immutable [int ](context .Background (), op , fld , nil , nil )
46
+ return ImmutableByCompare [int ](context .Background (), op , fld , nil , nil )
46
47
},
47
48
}, {
48
49
name : "nil value" ,
49
50
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
50
- return Immutable (context .Background (), op , fld , nil , ptr .To (123 ))
51
+ return ImmutableByCompare (context .Background (), op , fld , nil , ptr .To (123 ))
51
52
},
52
53
fail : true ,
53
54
}, {
54
55
name : "nil oldValue" ,
55
56
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
56
- return Immutable (context .Background (), op , fld , ptr .To (123 ), nil )
57
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (123 ), nil )
57
58
},
58
59
fail : true ,
59
60
}, {
60
61
name : "int" ,
61
62
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
62
- return Immutable (context .Background (), op , fld , ptr .To (123 ), ptr .To (123 ))
63
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (123 ), ptr .To (123 ))
63
64
},
64
65
}, {
65
66
name : "int fail" ,
66
67
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
67
- return Immutable (context .Background (), op , fld , ptr .To (123 ), ptr .To (456 ))
68
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (123 ), ptr .To (456 ))
68
69
},
69
70
fail : true ,
70
71
}, {
71
72
name : "string" ,
72
73
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
73
- return Immutable (context .Background (), op , fld , ptr .To ("abc" ), ptr .To ("abc" ))
74
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To ("abc" ), ptr .To ("abc" ))
74
75
},
75
76
}, {
76
77
name : "string fail" ,
77
78
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
78
- return Immutable (context .Background (), op , fld , ptr .To ("abc" ), ptr .To ("xyz" ))
79
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To ("abc" ), ptr .To ("xyz" ))
79
80
},
80
81
fail : true ,
81
82
}, {
82
83
name : "bool" ,
83
84
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
84
- return Immutable (context .Background (), op , fld , ptr .To (true ), ptr .To (true ))
85
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (true ), ptr .To (true ))
85
86
},
86
87
}, {
87
88
name : "bool fail" ,
88
89
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
89
- return Immutable (context .Background (), op , fld , ptr .To (true ), ptr .To (false ))
90
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (true ), ptr .To (false ))
90
91
},
91
92
fail : true ,
92
93
}, {
93
- name : "struct" ,
94
+ name : "same struct" ,
94
95
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
95
- return Immutable (context .Background (), op , fld , ptr .To (structA ), ptr .To (structA ))
96
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (structA ), ptr .To (structA ))
97
+ },
98
+ }, {
99
+ name : "equal struct" ,
100
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
101
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (structA ), ptr .To (structA2 ))
102
+ },
103
+ }, {
104
+ name : "struct fail" ,
105
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
106
+ return ImmutableByCompare (context .Background (), op , fld , ptr .To (structA ), ptr .To (structB ))
107
+ },
108
+ fail : true ,
109
+ }} {
110
+ t .Run (tc .name , func (t * testing.T ) {
111
+ errs := tc .fn (operation.Operation {Type : operation .Create }, field .NewPath ("" ))
112
+ if len (errs ) != 0 { // Create should always succeed
113
+ t .Errorf ("case %q (create): expected success: %v" , tc .name , errs )
114
+ }
115
+ errs = tc .fn (operation.Operation {Type : operation .Update }, field .NewPath ("" ))
116
+ if tc .fail && len (errs ) == 0 {
117
+ t .Errorf ("case %q (update): expected failure" , tc .name )
118
+ } else if ! tc .fail && len (errs ) != 0 {
119
+ t .Errorf ("case %q (update): expected success: %v" , tc .name , errs )
120
+ }
121
+ })
122
+ }
123
+ }
124
+
125
+ type StructNonComparable struct {
126
+ S string
127
+ SP * string
128
+ I int
129
+ IP * int
130
+ B bool
131
+ BP * bool
132
+ SS []string
133
+ MSS map [string ]string
134
+ }
135
+
136
+ func TestImmutableByReflect (t * testing.T ) {
137
+ structA := StructNonComparable {
138
+ S : "abc" ,
139
+ SP : ptr .To ("abc" ),
140
+ I : 123 ,
141
+ IP : ptr .To (123 ),
142
+ B : true ,
143
+ BP : ptr .To (true ),
144
+ SS : []string {"a" , "b" , "c" },
145
+ MSS : map [string ]string {"a" : "b" , "c" : "d" },
146
+ }
147
+
148
+ structA2 := structA
149
+ structA2 .SP = ptr .To ("abc" )
150
+ structA2 .IP = ptr .To (123 )
151
+ structA2 .BP = ptr .To (true )
152
+ structA2 .SS = []string {"a" , "b" , "c" }
153
+ structA2 .MSS = map [string ]string {"a" : "b" , "c" : "d" }
154
+
155
+ structB := StructNonComparable {
156
+ S : "xyz" ,
157
+ SP : ptr .To ("xyz" ),
158
+ I : 456 ,
159
+ IP : ptr .To (456 ),
160
+ B : false ,
161
+ BP : ptr .To (false ),
162
+ SS : []string {"x" , "y" , "z" },
163
+ MSS : map [string ]string {"x" : "X" , "y" : "Y" },
164
+ }
165
+
166
+ for _ , tc := range []struct {
167
+ name string
168
+ fn func (operation.Operation , * field.Path ) field.ErrorList
169
+ fail bool
170
+ }{{
171
+ name : "nil both values" ,
172
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
173
+ return ImmutableByReflect [* int ](context .Background (), op , fld , nil , nil )
174
+ },
175
+ }, {
176
+ name : "nil value" ,
177
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
178
+ return ImmutableByReflect (context .Background (), op , fld , nil , ptr .To (123 ))
179
+ },
180
+ fail : true ,
181
+ }, {
182
+ name : "nil oldValue" ,
183
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
184
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (123 ), nil )
185
+ },
186
+ fail : true ,
187
+ }, {
188
+ name : "int" ,
189
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
190
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (123 ), ptr .To (123 ))
191
+ },
192
+ }, {
193
+ name : "int fail" ,
194
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
195
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (123 ), ptr .To (456 ))
196
+ },
197
+ fail : true ,
198
+ }, {
199
+ name : "string" ,
200
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
201
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To ("abc" ), ptr .To ("abc" ))
202
+ },
203
+ }, {
204
+ name : "string fail" ,
205
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
206
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To ("abc" ), ptr .To ("xyz" ))
207
+ },
208
+ fail : true ,
209
+ }, {
210
+ name : "bool" ,
211
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
212
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (true ), ptr .To (true ))
213
+ },
214
+ }, {
215
+ name : "bool fail" ,
216
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
217
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (true ), ptr .To (false ))
218
+ },
219
+ fail : true ,
220
+ }, {
221
+ name : "same struct" ,
222
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
223
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (structA ), ptr .To (structA ))
224
+ },
225
+ }, {
226
+ name : "equal struct" ,
227
+ fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
228
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (structA ), ptr .To (structA2 ))
96
229
},
97
230
}, {
98
231
name : "struct fail" ,
99
232
fn : func (op operation.Operation , fld * field.Path ) field.ErrorList {
100
- return Immutable (context .Background (), op , fld , ptr .To (structA ), ptr .To (structB ))
233
+ return ImmutableByReflect (context .Background (), op , fld , ptr .To (structA ), ptr .To (structB ))
101
234
},
102
235
fail : true ,
103
236
}} {
0 commit comments