forked from barkimedes/go-deepcopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepcopy_test.go
181 lines (163 loc) · 3.28 KB
/
deepcopy_test.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package deepcopy
import (
"fmt"
. "reflect"
"testing"
)
func ExampleAnything() {
tests := []interface{}{
`"Now cut that out!"`,
39,
true,
false,
2.14,
[]string{
"Phil Harris",
"Rochester van Jones",
"Mary Livingstone",
"Dennis Day",
},
[2]string{
"Jell-O",
"Grape-Nuts",
},
}
for _, expected := range tests {
actual := MustAnything(expected)
fmt.Println(actual)
}
// Output:
// "Now cut that out!"
// 39
// true
// false
// 2.14
// [Phil Harris Rochester van Jones Mary Livingstone Dennis Day]
// [Jell-O Grape-Nuts]
}
type Foo struct {
Foo *Foo
Bar int
}
func ExampleMap() {
x := map[string]*Foo{
"foo": &Foo{Bar: 1},
"bar": &Foo{Bar: 2},
}
y := MustAnything(x).(map[string]*Foo)
for _, k := range []string{"foo", "bar"} { // to ensure consistent order
fmt.Printf("x[\"%v\"] = y[\"%v\"]: %v\n", k, k, x[k] == y[k])
fmt.Printf("x[\"%v\"].Foo = y[\"%v\"].Foo: %v\n", k, k, x[k].Foo == y[k].Foo)
fmt.Printf("x[\"%v\"].Bar = y[\"%v\"].Bar: %v\n", k, k, x[k].Bar == y[k].Bar)
}
// Output:
// x["foo"] = y["foo"]: false
// x["foo"].Foo = y["foo"].Foo: false
// x["foo"].Bar = y["foo"].Bar: true
// x["bar"] = y["bar"]: false
// x["bar"].Foo = y["bar"].Foo: false
// x["bar"].Bar = y["bar"].Bar: true
}
func TestInterface(t *testing.T) {
x := []interface{}{nil}
y := MustAnything(x).([]interface{})
if !DeepEqual(x, y) || len(y) != 1 {
t.Errorf("expect %v == %v; y had length %v (expected 1)", x, y, len(y))
}
var a interface{}
b := MustAnything(a)
if a != b {
t.Errorf("expected %v == %v", a, b)
}
}
func ExampleAvoidInfiniteLoops() {
x := &Foo{
Bar: 4,
}
x.Foo = x
y := MustAnything(x).(*Foo)
fmt.Printf("x == y: %v\n", x == y)
fmt.Printf("x == x.Foo: %v\n", x == x.Foo)
fmt.Printf("y == y.Foo: %v\n", y == y.Foo)
// Output:
// x == y: false
// x == x.Foo: true
// y == y.Foo: true
}
func TestUnsupportedKind(t *testing.T) {
x := func() {}
tests := []interface{}{
x,
map[bool]interface{}{true: x},
[]interface{}{x},
}
for _, test := range tests {
y, err := Anything(test)
if y != nil {
t.Errorf("expected %v to be nil", y)
}
if err == nil {
t.Errorf("expected err to not be nil")
}
}
}
func TestUnsupportedKindPanicsOnMust(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected a panic; didn't get one")
}
}()
x := func() {}
MustAnything(x)
}
func TestMismatchedTypesFail(t *testing.T) {
tests := []struct {
input interface{}
kind Kind
}{
{
map[int]int{1: 2, 2: 4, 3: 8},
Map,
},
{
[]int{2, 8},
Slice,
},
}
for _, test := range tests {
for kind, copier := range copiers {
if kind == test.kind {
continue
}
actual, err := copier(test.input, nil)
if actual != nil {
t.Errorf("%v attempted value %v as %v; should be nil value, got %v", test.kind, test.input, kind, actual)
}
if err == nil {
t.Errorf("%v attempted value %v as %v; should have gotten an error", test.kind, test.input, kind)
}
}
}
}
func TestTwoNils(t *testing.T) {
type Foo struct {
A int
}
type Bar struct {
B int
}
type FooBar struct {
Foo *Foo
Bar *Bar
Foo2 *Foo
Bar2 *Bar
}
src := &FooBar{
Foo2: &Foo{1},
Bar2: &Bar{2},
}
dst := MustAnything(src)
if !DeepEqual(src, dst) {
t.Errorf("expect %v == %v; ", src, dst)
}
}