-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_test.go
66 lines (60 loc) · 1.21 KB
/
struct_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
package valkyrie
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
type structCopy struct {
Name string `json:"name"`
Address string `json:"address"`
}
func TestCloneStruct(t *testing.T) {
s := &structCopy{
Name: "name",
Address: "address",
}
a := &structCopy{}
err := CloneStruct(s, a)
assert.NoError(t, err)
assert.Equal(t, s, a)
assert.Equal(t, s.Name, a.Name)
assert.Equal(t, "name", a.Name)
}
func TestCloneStructNil(t *testing.T) {
var (
a *structCopy = nil
sliceCycle = []interface{}{nil}
)
cc := &structCopy{
Name: "name",
Address: "address",
}
er := &json.UnmarshalTypeError{
Value: "array",
Type: reflect.TypeOf(structCopy{}),
Offset: 1,
Struct: "",
Field: "",
}
tt := []struct {
src interface{}
dest interface{}
want error
}{
{cc, a, NilPointer},
{cc, structCopy{}, NotPointer},
{a, cc, NilPointer},
{structCopy{}, cc, NotPointer},
{&sliceCycle, cc, er},
}
for _, c := range tt {
t.Run(fmt.Sprintf("src: %v - dest: %v", c.src, c.dest), func(t *testing.T) {
err := CloneStruct(c.src, c.dest)
t.Log(err)
assert.Error(t, err)
assert.EqualError(t, c.want, err.Error())
})
}
}