-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloner_test.go
73 lines (55 loc) · 1.45 KB
/
cloner_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
package reflecthelper
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type str struct {
Field string
field string
}
func TestClone(t *testing.T) {
t.Run("Simple test", func(t *testing.T) {
s := &str{Field: "astr", field: "small field"}
valClone := Clone(reflect.ValueOf(s))
b := valClone.Interface().(*str)
s.Field = "changed field"
assert.NotEqual(t, s.field, b.field)
fmt.Println(s, b)
})
t.Run("Clone nil value", func(t *testing.T) {
var s *str
valClone := Clone(reflect.ValueOf(s))
b := valClone.Interface().(*str)
s = &str{Field: "astr", field: "small field"}
assert.NotEqual(t, s, b)
fmt.Println(s, b)
})
t.Run("Clone nil literal", func(t *testing.T) {
valClone := Clone(reflect.ValueOf(nil))
assert.Equal(t, reflect.Invalid, valClone.Kind())
})
}
func TestInitNew(t *testing.T) {
t.Run("Simple test", func(t *testing.T) {
s := &str{field: "hello"}
valInit := InitNew(reflect.ValueOf(s))
b := valInit.Interface().(*str)
assert.Nil(t, b)
})
t.Run("Init nil value", func(t *testing.T) {
valInit := InitNew(reflect.ValueOf(nil))
assert.Equal(t, reflect.Invalid, valInit.Kind())
})
}
func TestCloneInterface(t *testing.T) {
t.Run("Simple test", func(t *testing.T) {
s := &str{Field: "astr", field: "small field"}
valClone := CloneInterface(reflect.ValueOf(s))
b := valClone.(*str)
s.Field = "changed field"
assert.NotEqual(t, s.field, b.field)
fmt.Println(s, b)
})
}