-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathclone_test.go
84 lines (60 loc) · 1.07 KB
/
clone_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
package helpers
import (
"testing"
"github.com/stretchr/testify/assert"
)
type child struct {
Name string
}
type cloneable struct {
A string
B int
C float32
D child
}
func TestClone(t *testing.T) {
cloneMe := cloneable{
A: "abcd",
B: 123,
C: 1.5,
D: child{
Name: "aname",
},
}
cloned := Clone(cloneMe)
assert.Equal(t, cloneMe, cloned)
cloneMePtr := &cloneMe
clonedPtr := Clone(cloneMePtr)
assert.NotEmpty(t, clonedPtr)
assert.Equal(t, *cloneMePtr, *clonedPtr)
clonedPtr = nil
clonedNil := Clone(clonedPtr)
assert.Empty(t, clonedNil)
}
func TestClonePointer(t *testing.T) {
type child struct {
S string
}
type cloned struct {
Ch *child
}
c := cloned{
Ch: &child{
S: "hello",
},
}
clonedObj := Clone(c)
c.Ch.S = "modified"
assert.NotEqualValues(t, c.Ch.S, clonedObj.Ch.S)
}
func TestCloneMap(t *testing.T) {
cloneMe := map[string]int{
"one": 1,
"two": 2,
}
cloned := Clone(cloneMe)
assert.EqualValues(t, cloneMe, cloned)
cloned["two"]++
assert.Equal(t, cloneMe["two"], 2)
assert.Equal(t, cloned["two"], 3)
}