| 1 |
/* |
| 2 |
Copyright 2020 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package marker |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/google/go-cmp/cmp" |
| 23 |
|
| 24 |
"k8s.io/gengo/examples/defaulter-gen/output_tests/marker/external" |
| 25 |
externalexternal "k8s.io/gengo/examples/defaulter-gen/output_tests/marker/external/external" |
| 26 |
"k8s.io/gengo/examples/defaulter-gen/output_tests/marker/external2" |
| 27 |
"k8s.io/gengo/examples/defaulter-gen/output_tests/marker/external3" |
| 28 |
) |
| 29 |
|
| 30 |
func getPointerFromString(s string) *string { |
| 31 |
return &s |
| 32 |
} |
| 33 |
|
| 34 |
var ( |
| 35 |
defaultInt32 int32 = 32 |
| 36 |
defaultInt64 int64 = 64 |
| 37 |
) |
| 38 |
|
| 39 |
func Test_Marker(t *testing.T) { |
| 40 |
testcases := []struct { |
| 41 |
name string |
| 42 |
in Defaulted |
| 43 |
out Defaulted |
| 44 |
}{ |
| 45 |
{ |
| 46 |
name: "default", |
| 47 |
in: Defaulted{}, |
| 48 |
out: Defaulted{ |
| 49 |
StringDefault: "bar", |
| 50 |
StringEmptyDefault: "", |
| 51 |
StringEmpty: "", |
| 52 |
StringPointer: getPointerFromString("default"), |
| 53 |
Int64: &defaultInt64, |
| 54 |
Int32: &defaultInt32, |
| 55 |
IntDefault: 1, |
| 56 |
IntEmptyDefault: 0, |
| 57 |
IntEmpty: 0, |
| 58 |
FloatDefault: 0.5, |
| 59 |
FloatEmptyDefault: 0.0, |
| 60 |
FloatEmpty: 0.0, |
| 61 |
List: []Item{ |
| 62 |
getPointerFromString("foo"), |
| 63 |
getPointerFromString("bar"), |
| 64 |
}, |
| 65 |
Sub: &SubStruct{ |
| 66 |
S: "foo", |
| 67 |
I: 5, |
| 68 |
}, |
| 69 |
OtherSub: SubStruct{ |
| 70 |
S: "", |
| 71 |
I: 1, |
| 72 |
}, |
| 73 |
StructList: []SubStruct{ |
| 74 |
{ |
| 75 |
S: "foo1", |
| 76 |
I: 1, |
| 77 |
}, |
| 78 |
{ |
| 79 |
S: "foo2", |
| 80 |
I: 1, |
| 81 |
}, |
| 82 |
}, |
| 83 |
PtrStructList: []*SubStruct{ |
| 84 |
{ |
| 85 |
S: "foo1", |
| 86 |
I: 1, |
| 87 |
}, |
| 88 |
{ |
| 89 |
S: "foo2", |
| 90 |
I: 1, |
| 91 |
}, |
| 92 |
}, |
| 93 |
StringList: []string{ |
| 94 |
"foo", |
| 95 |
}, |
| 96 |
Map: map[string]Item{ |
| 97 |
"foo": getPointerFromString("bar"), |
| 98 |
}, |
| 99 |
StructMap: map[string]SubStruct{ |
| 100 |
"foo": SubStruct{ |
| 101 |
S: "string", |
| 102 |
I: 1, |
| 103 |
}, |
| 104 |
}, |
| 105 |
PtrStructMap: map[string]*SubStruct{ |
| 106 |
"foo": &SubStruct{ |
| 107 |
S: "string", |
| 108 |
I: 1, |
| 109 |
}, |
| 110 |
}, |
| 111 |
AliasPtr: getPointerFromString("banana"), |
| 112 |
}, |
| 113 |
}, |
| 114 |
{ |
| 115 |
name: "values-omitempty", |
| 116 |
in: Defaulted{ |
| 117 |
StringDefault: "changed", |
| 118 |
IntDefault: 5, |
| 119 |
}, |
| 120 |
out: Defaulted{ |
| 121 |
StringDefault: "changed", |
| 122 |
StringEmptyDefault: "", |
| 123 |
StringEmpty: "", |
| 124 |
StringPointer: getPointerFromString("default"), |
| 125 |
Int64: &defaultInt64, |
| 126 |
Int32: &defaultInt32, |
| 127 |
IntDefault: 5, |
| 128 |
IntEmptyDefault: 0, |
| 129 |
IntEmpty: 0, |
| 130 |
FloatDefault: 0.5, |
| 131 |
FloatEmptyDefault: 0.0, |
| 132 |
FloatEmpty: 0.0, |
| 133 |
List: []Item{ |
| 134 |
getPointerFromString("foo"), |
| 135 |
getPointerFromString("bar"), |
| 136 |
}, |
| 137 |
Sub: &SubStruct{ |
| 138 |
S: "foo", |
| 139 |
I: 5, |
| 140 |
}, |
| 141 |
StructList: []SubStruct{ |
| 142 |
{ |
| 143 |
S: "foo1", |
| 144 |
I: 1, |
| 145 |
}, |
| 146 |
{ |
| 147 |
S: "foo2", |
| 148 |
I: 1, |
| 149 |
}, |
| 150 |
}, |
| 151 |
PtrStructList: []*SubStruct{ |
| 152 |
{ |
| 153 |
S: "foo1", |
| 154 |
I: 1, |
| 155 |
}, |
| 156 |
{ |
| 157 |
S: "foo2", |
| 158 |
I: 1, |
| 159 |
}, |
| 160 |
}, |
| 161 |
StringList: []string{ |
| 162 |
"foo", |
| 163 |
}, |
| 164 |
OtherSub: SubStruct{ |
| 165 |
S: "", |
| 166 |
I: 1, |
| 167 |
}, |
| 168 |
Map: map[string]Item{ |
| 169 |
"foo": getPointerFromString("bar"), |
| 170 |
}, |
| 171 |
StructMap: map[string]SubStruct{ |
| 172 |
"foo": SubStruct{ |
| 173 |
S: "string", |
| 174 |
I: 1, |
| 175 |
}, |
| 176 |
}, |
| 177 |
PtrStructMap: map[string]*SubStruct{ |
| 178 |
"foo": &SubStruct{ |
| 179 |
S: "string", |
| 180 |
I: 1, |
| 181 |
}, |
| 182 |
}, |
| 183 |
AliasPtr: getPointerFromString("banana"), |
| 184 |
}, |
| 185 |
}, |
| 186 |
{ |
| 187 |
name: "lists", |
| 188 |
in: Defaulted{ |
| 189 |
List: []Item{ |
| 190 |
nil, |
| 191 |
getPointerFromString("bar"), |
| 192 |
}, |
| 193 |
}, |
| 194 |
out: Defaulted{ |
| 195 |
StringDefault: "bar", |
| 196 |
StringEmptyDefault: "", |
| 197 |
StringEmpty: "", |
| 198 |
StringPointer: getPointerFromString("default"), |
| 199 |
Int64: &defaultInt64, |
| 200 |
Int32: &defaultInt32, |
| 201 |
IntDefault: 1, |
| 202 |
IntEmptyDefault: 0, |
| 203 |
IntEmpty: 0, |
| 204 |
FloatDefault: 0.5, |
| 205 |
FloatEmptyDefault: 0.0, |
| 206 |
FloatEmpty: 0.0, |
| 207 |
List: []Item{ |
| 208 |
getPointerFromString("apple"), |
| 209 |
getPointerFromString("bar"), |
| 210 |
}, |
| 211 |
Sub: &SubStruct{ |
| 212 |
S: "foo", |
| 213 |
I: 5, |
| 214 |
}, |
| 215 |
StructList: []SubStruct{ |
| 216 |
{ |
| 217 |
S: "foo1", |
| 218 |
I: 1, |
| 219 |
}, |
| 220 |
{ |
| 221 |
S: "foo2", |
| 222 |
I: 1, |
| 223 |
}, |
| 224 |
}, |
| 225 |
PtrStructList: []*SubStruct{ |
| 226 |
{ |
| 227 |
S: "foo1", |
| 228 |
I: 1, |
| 229 |
}, |
| 230 |
{ |
| 231 |
S: "foo2", |
| 232 |
I: 1, |
| 233 |
}, |
| 234 |
}, |
| 235 |
StringList: []string{ |
| 236 |
"foo", |
| 237 |
}, |
| 238 |
OtherSub: SubStruct{ |
| 239 |
S: "", |
| 240 |
I: 1, |
| 241 |
}, |
| 242 |
Map: map[string]Item{ |
| 243 |
"foo": getPointerFromString("bar"), |
| 244 |
}, |
| 245 |
StructMap: map[string]SubStruct{ |
| 246 |
"foo": SubStruct{ |
| 247 |
S: "string", |
| 248 |
I: 1, |
| 249 |
}, |
| 250 |
}, |
| 251 |
PtrStructMap: map[string]*SubStruct{ |
| 252 |
"foo": &SubStruct{ |
| 253 |
S: "string", |
| 254 |
I: 1, |
| 255 |
}, |
| 256 |
}, |
| 257 |
AliasPtr: getPointerFromString("banana"), |
| 258 |
}, |
| 259 |
}, |
| 260 |
{ |
| 261 |
name: "stringmap", |
| 262 |
in: Defaulted{ |
| 263 |
Map: map[string]Item{ |
| 264 |
"foo": nil, |
| 265 |
"bar": getPointerFromString("banana"), |
| 266 |
}, |
| 267 |
}, |
| 268 |
out: Defaulted{ |
| 269 |
StringDefault: "bar", |
| 270 |
StringEmptyDefault: "", |
| 271 |
StringEmpty: "", |
| 272 |
StringPointer: getPointerFromString("default"), |
| 273 |
Int64: &defaultInt64, |
| 274 |
Int32: &defaultInt32, |
| 275 |
IntDefault: 1, |
| 276 |
IntEmptyDefault: 0, |
| 277 |
IntEmpty: 0, |
| 278 |
FloatDefault: 0.5, |
| 279 |
FloatEmptyDefault: 0.0, |
| 280 |
FloatEmpty: 0.0, |
| 281 |
List: []Item{ |
| 282 |
getPointerFromString("foo"), |
| 283 |
getPointerFromString("bar"), |
| 284 |
}, |
| 285 |
Sub: &SubStruct{ |
| 286 |
S: "foo", |
| 287 |
I: 5, |
| 288 |
}, |
| 289 |
StructList: []SubStruct{ |
| 290 |
{ |
| 291 |
S: "foo1", |
| 292 |
I: 1, |
| 293 |
}, |
| 294 |
{ |
| 295 |
S: "foo2", |
| 296 |
I: 1, |
| 297 |
}, |
| 298 |
}, |
| 299 |
PtrStructList: []*SubStruct{ |
| 300 |
{ |
| 301 |
S: "foo1", |
| 302 |
I: 1, |
| 303 |
}, |
| 304 |
{ |
| 305 |
S: "foo2", |
| 306 |
I: 1, |
| 307 |
}, |
| 308 |
}, |
| 309 |
StringList: []string{ |
| 310 |
"foo", |
| 311 |
}, |
| 312 |
OtherSub: SubStruct{ |
| 313 |
S: "", |
| 314 |
I: 1, |
| 315 |
}, |
| 316 |
Map: map[string]Item{ |
| 317 |
"foo": getPointerFromString("apple"), |
| 318 |
"bar": getPointerFromString("banana"), |
| 319 |
}, |
| 320 |
StructMap: map[string]SubStruct{ |
| 321 |
"foo": SubStruct{ |
| 322 |
S: "string", |
| 323 |
I: 1, |
| 324 |
}, |
| 325 |
}, |
| 326 |
PtrStructMap: map[string]*SubStruct{ |
| 327 |
"foo": &SubStruct{ |
| 328 |
S: "string", |
| 329 |
I: 1, |
| 330 |
}, |
| 331 |
}, |
| 332 |
AliasPtr: getPointerFromString("banana"), |
| 333 |
}, |
| 334 |
}, |
| 335 |
} |
| 336 |
|
| 337 |
for _, tc := range testcases { |
| 338 |
t.Run(tc.name, func(t *testing.T) { |
| 339 |
SetObjectDefaults_Defaulted(&tc.in) |
| 340 |
if diff := cmp.Diff(tc.out, tc.in); len(diff) > 0 { |
| 341 |
t.Errorf("Error: Expected and actual output are different \n %s\n", diff) |
| 342 |
} |
| 343 |
}) |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
func Test_DefaultingFunction(t *testing.T) { |
| 348 |
in := DefaultedWithFunction{} |
| 349 |
SetObjectDefaults_DefaultedWithFunction(&in) |
| 350 |
out := DefaultedWithFunction{ |
| 351 |
S1: "default_function", |
| 352 |
S2: "default_marker", |
| 353 |
} |
| 354 |
if diff := cmp.Diff(out, in); len(diff) > 0 { |
| 355 |
t.Errorf("Error: Expected and actual output are different \n %s\n", diff) |
| 356 |
} |
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
func Test_DefaultingReference(t *testing.T) { |
| 361 |
dv := DefaultedValueItem(SomeValue) |
| 362 |
SomeDefault := SomeDefault |
| 363 |
SomeValue := SomeValue |
| 364 |
|
| 365 |
ptrVar9 := string(SomeValue) |
| 366 |
ptrVar8 := &ptrVar9 |
| 367 |
ptrVar7 := (*B1)(&ptrVar8) |
| 368 |
ptrVar6 := (*B2)(&ptrVar7) |
| 369 |
ptrVar5 := &ptrVar6 |
| 370 |
ptrVar4 := &ptrVar5 |
| 371 |
ptrVar3 := &ptrVar4 |
| 372 |
ptrVar2 := (*B3)(&ptrVar3) |
| 373 |
ptrVar1 := &ptrVar2 |
| 374 |
|
| 375 |
var external2Str external2.String = external2.String(SomeValue) |
| 376 |
|
| 377 |
testcases := []struct { |
| 378 |
name string |
| 379 |
in DefaultedWithReference |
| 380 |
out DefaultedWithReference |
| 381 |
}{ |
| 382 |
{ |
| 383 |
name: "default", |
| 384 |
in: DefaultedWithReference{}, |
| 385 |
out: DefaultedWithReference{ |
| 386 |
AliasPointerInside: Item(&SomeDefault), |
| 387 |
AliasOverride: Item(&SomeDefault), |
| 388 |
AliasConvertDefaultPointer: &dv, |
| 389 |
AliasPointerDefault: &dv, |
| 390 |
PointerAliasDefault: Item(getPointerFromString("apple")), |
| 391 |
AliasNonPointer: SomeValue, |
| 392 |
AliasPointer: &SomeValue, |
| 393 |
SymbolReference: SomeDefault, |
| 394 |
SameNamePackageSymbolReference1: external.AConstant, |
| 395 |
SameNamePackageSymbolReference2: externalexternal.AnotherConstant, |
| 396 |
PointerConversion: (*B4)(&ptrVar1), |
| 397 |
PointerConversionValue: (B4)(ptrVar1), |
| 398 |
FullyQualifiedLocalSymbol: string(SomeValue), |
| 399 |
ImportFromAliasCast: external3.StringPointer(&external2Str), |
| 400 |
}, |
| 401 |
}, |
| 402 |
} |
| 403 |
|
| 404 |
for _, tc := range testcases { |
| 405 |
t.Run(tc.name, func(t *testing.T) { |
| 406 |
SetObjectDefaults_DefaultedWithReference(&tc.in) |
| 407 |
if diff := cmp.Diff(tc.out, tc.in); len(diff) > 0 { |
| 408 |
t.Errorf("Error: Expected and actual output are different \n %s\n", diff) |
| 409 |
} |
| 410 |
}) |
| 411 |
} |
| 412 |
} |