| 1 |
/* |
| 2 |
Copyright 2016 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 wholepkg |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
|
| 23 |
fuzz "github.com/google/gofuzz" |
| 24 |
) |
| 25 |
|
| 26 |
func TestDeepCopyPrimitives(t *testing.T) { |
| 27 |
x := Struct_Primitives{} |
| 28 |
y := Struct_Primitives{} |
| 29 |
|
| 30 |
if !reflect.DeepEqual(&x, &y) { |
| 31 |
t.Errorf("objects should be equal to start, but are not") |
| 32 |
} |
| 33 |
|
| 34 |
fuzzer := fuzz.New() |
| 35 |
fuzzer.Fuzz(&x) |
| 36 |
fuzzer.Fuzz(&y) |
| 37 |
|
| 38 |
if reflect.DeepEqual(&x, &y) { |
| 39 |
t.Errorf("objects should not be equal, but are") |
| 40 |
} |
| 41 |
|
| 42 |
x.DeepCopyInto(&y) |
| 43 |
if !reflect.DeepEqual(&x, &y) { |
| 44 |
t.Errorf("objects should be equal, but are not") |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestDeepCopyInterfaceFields(t *testing.T) { |
| 49 |
x := Struct_Interfaces{} |
| 50 |
y := Struct_Interfaces{} |
| 51 |
|
| 52 |
if !reflect.DeepEqual(&x, &y) { |
| 53 |
t.Errorf("objects should be equal to start, but are not") |
| 54 |
} |
| 55 |
|
| 56 |
fuzzer := fuzz.New() |
| 57 |
|
| 58 |
obj := Struct_ExplicitObject{} |
| 59 |
fuzzer.Fuzz(&obj) |
| 60 |
x.ObjectField = &obj |
| 61 |
|
| 62 |
sel := Struct_ExplicitSelectorExplicitObject{} |
| 63 |
fuzzer.Fuzz(&sel) |
| 64 |
x.SelectorField = &sel |
| 65 |
|
| 66 |
if reflect.DeepEqual(&x, &y) { |
| 67 |
t.Errorf("objects should not be equal, but are") |
| 68 |
} |
| 69 |
|
| 70 |
x.DeepCopyInto(&y) |
| 71 |
if !reflect.DeepEqual(&x, &y) { |
| 72 |
t.Errorf("objects should be equal, but are not") |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestNilCopy(t *testing.T) { |
| 77 |
var x *Struct_B |
| 78 |
y := x.DeepCopy() |
| 79 |
if y != nil { |
| 80 |
t.Errorf("Expected nil as deepcopy of nil, got %+v", y) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func assertMethod(t *testing.T, typ reflect.Type, name string) { |
| 85 |
if _, found := typ.MethodByName(name); !found { |
| 86 |
t.Errorf("Struct_ExplicitObject must have %v method", name) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func assertNotMethod(t *testing.T, typ reflect.Type, name string) { |
| 91 |
if _, found := typ.MethodByName(name); found { |
| 92 |
t.Errorf("%v must not have %v method", typ, name) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func TestInterfaceTypes(t *testing.T) { |
| 97 |
explicitObject := reflect.TypeOf(&Struct_ExplicitObject{}) |
| 98 |
assertMethod(t, explicitObject, "DeepCopyObject") |
| 99 |
|
| 100 |
typeMeta := reflect.TypeOf(&Struct_TypeMeta{}) |
| 101 |
assertNotMethod(t, typeMeta, "DeepCopy") |
| 102 |
|
| 103 |
objectAndList := reflect.TypeOf(&Struct_ObjectAndList{}) |
| 104 |
assertMethod(t, objectAndList, "DeepCopyObject") |
| 105 |
assertMethod(t, objectAndList, "DeepCopyList") |
| 106 |
|
| 107 |
objectAndObject := reflect.TypeOf(&Struct_ObjectAndObject{}) |
| 108 |
assertMethod(t, objectAndObject, "DeepCopyObject") |
| 109 |
|
| 110 |
explicitSelectorExplicitObject := reflect.TypeOf(&Struct_ExplicitSelectorExplicitObject{}) |
| 111 |
assertMethod(t, explicitSelectorExplicitObject, "DeepCopySelector") |
| 112 |
assertMethod(t, explicitSelectorExplicitObject, "DeepCopyObject") |
| 113 |
} |
| 114 |
|
| 115 |
func TestInterfaceDeepCopy(t *testing.T) { |
| 116 |
x := Struct_ExplicitObject{} |
| 117 |
|
| 118 |
fuzzer := fuzz.New() |
| 119 |
fuzzer.Fuzz(&x) |
| 120 |
|
| 121 |
y_obj := x.DeepCopyObject() |
| 122 |
y, ok := y_obj.(*Struct_ExplicitObject) |
| 123 |
if !ok { |
| 124 |
t.Fatalf("epxected Struct_ExplicitObject from Struct_ExplicitObject.DeepCopyObject, got: %t", y_obj) |
| 125 |
} |
| 126 |
if !reflect.DeepEqual(y, &x) { |
| 127 |
t.Error("objects should be equal, but are not") |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func TestInterfaceNonPointerDeepCopy(t *testing.T) { |
| 132 |
x := Struct_NonPointerExplicitObject{} |
| 133 |
|
| 134 |
fuzzer := fuzz.New() |
| 135 |
fuzzer.Fuzz(&x) |
| 136 |
|
| 137 |
y_obj := x.DeepCopyObject() |
| 138 |
y, ok := y_obj.(Struct_NonPointerExplicitObject) |
| 139 |
if !ok { |
| 140 |
t.Fatalf("epxected Struct_NonPointerExplicitObject from Struct_NonPointerExplicitObject.DeepCopyObject, got: %t", y_obj) |
| 141 |
} |
| 142 |
if !reflect.DeepEqual(y, x) { |
| 143 |
t.Error("objects should be equal, but are not") |
| 144 |
} |
| 145 |
} |