-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_test.go
156 lines (128 loc) · 2.85 KB
/
object_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
package sod
import (
"reflect"
"testing"
"time"
"github.com/0xrawsec/toast"
)
type subSubStruct struct {
S string
I interface{}
}
type subStruct struct {
PtrMap map[string]*subSubStruct
I interface{}
}
type structPtr struct {
Item
Time time.Time
Slice []string
EmptySlice []int
Map map[int]string
EmptyMap map[string]int
//PtrMap map[string]*testStruct
Sub *subStruct
Ptr *int
I1 interface{}
I2 interface{}
I3 interface{}
}
func TestCloneObject(t *testing.T) {
tt := toast.FromT(t)
ts := &testStruct{
M: time.Now(),
}
ts.A = 42
ts.B = 43
new := CloneObject(ts).(*testStruct)
tt.Assert(ts.A == new.A)
tt.Assert(ts.B == new.B)
tt.Assert(ts.M.Equal(new.M))
new.M = time.Now()
tt.Assert(!ts.M.Equal(new.M))
tt.Assert(ts.M.Before(new.M))
// modifying one struct
ts.A = 41
ts.B = 44
tt.Assert(ts.A != new.A)
tt.Assert(ts.B != new.B)
}
func TestCloneObjectWithPtr(t *testing.T) {
tt := toast.FromT(t)
i := 42
s1 := &structPtr{
Ptr: &i,
Slice: []string{"foo", "bar"},
Map: map[int]string{
0: "foo",
1: "bar",
},
}
s2 := CloneObject(s1).(*structPtr)
tt.Assert(*s1.Ptr == 42)
tt.Assert(*s2.Ptr == 42)
tt.Assert(s2.I1 == nil)
tt.Assert(s2.I2 == nil)
tt.Assert(s2.I3 == nil)
tt.Assert(s2.Sub == nil)
tt.Assert(s2.EmptyMap == nil)
tt.Assert(s2.EmptySlice == nil)
*s1.Ptr = 43
(*s1).Slice[0] = "foofoo"
(*s1).Map[0] = "foofoo"
tt.Assert(*s1.Ptr == 43)
t.Log(*s2.Ptr)
tt.Assert(*s2.Ptr == 42)
tt.Assert(s1.UUID() == s2.UUID())
// testing slice copy
tt.Assert(len((*s1).Slice) == len((*s2).Slice))
tt.Assert(!reflect.DeepEqual((*s1).Slice, (*s2).Slice))
tt.Assert((*s1).Slice[0] == "foofoo")
tt.Assert((*s2).Slice[0] == "foo")
// testing map copy
tt.Assert(len((*s1).Map) == len((*s2).Map))
tt.Assert((*s1).Map[0] == "foofoo")
tt.Assert((*s2).Map[0] == "foo")
}
func TestCloneBug(t *testing.T) {
tt := toast.FromT(t)
i := 42
s1 := &structPtr{
Ptr: &i,
Slice: []string{"foo", "bar"},
Map: map[int]string{
0: "foo",
1: "bar",
},
I1: map[string]string{
"foo": "bar",
},
I2: []string{
"foofoo", "babar",
},
}
s2 := CloneObject(s1).(*structPtr)
tt.Assert(*s1.Ptr == 42)
tt.Assert(*s2.Ptr == 42)
*s1.Ptr = 43
(*s1).Slice[0] = "foofoo"
(*s1).Map[0] = "foofoo"
tt.Assert(*s1.Ptr == 43)
tt.Assert(*s2.Ptr == 42)
tt.Assert(s1.UUID() == s2.UUID())
// testing slice copy
tt.Assert(len((*s1).Slice) == len((*s2).Slice))
tt.Assert(!reflect.DeepEqual((*s1).Slice, (*s2).Slice))
tt.Assert((*s1).Slice[0] == "foofoo")
tt.Assert((*s2).Slice[0] == "foo")
// testing map copy
tt.Assert(len((*s1).Map) == len((*s2).Map))
tt.Assert((*s1).Map[0] == "foofoo")
tt.Assert((*s2).Map[0] == "foo")
im := s2.I1.(map[string]string)
tt.Assert(im["foo"] == "bar")
is := s2.I2.([]string)
tt.Assert(is[0] == "foofoo")
tt.Assert(is[1] == "babar")
tt.Assert(s2.I3 == nil)
}