forked from nauyey/factory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blueprint_test.go
98 lines (79 loc) · 1.6 KB
/
blueprint_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
package factory
import "testing"
import "reflect"
func TestSetInstanceFieldValue(t *testing.T) {
type test2 struct {
A string
b int
}
type test struct {
A string
b int
C *string
D test2
E *test2
}
// test field
tt := &test{}
v := reflect.ValueOf(tt)
setInstanceFieldValue(v, "A", "aaa")
if tt.A != "aaa" {
t.Errorf("setInstanceFieldValue failed")
}
// test ptr field
tt = &test{}
v = reflect.ValueOf(tt)
c := "ccc"
setInstanceFieldValue(v, "C", &c)
if *tt.C != "ccc" {
t.Errorf("setInstanceFieldValue failed")
}
// test struct field
tt = &test{}
v = reflect.ValueOf(tt)
d := test2{
A: "test2-AAA",
b: 1,
}
setInstanceFieldValue(v, "D", d)
if tt.D.A != "test2-AAA" {
t.Errorf("setInstanceFieldValue failed")
}
if tt.D.b != 1 {
t.Errorf("setInstanceFieldValue failed")
}
// test ptr struct field
tt = &test{}
v = reflect.ValueOf(tt)
e := &test2{
A: "ptr test2-AAA",
b: 2,
}
setInstanceFieldValue(v, "E", e)
if tt.E.A != "ptr test2-AAA" {
t.Errorf("setInstanceFieldValue failed")
}
if tt.E.b != 2 {
t.Errorf("setInstanceFieldValue failed")
}
// test sub field of struct field
tt = &test{}
v = reflect.ValueOf(tt)
setInstanceFieldValue(v, "D.A", "D.A-AAA")
if tt.D.A != "D.A-AAA" {
t.Errorf("setInstanceFieldValue failed")
}
if tt.D.b != 0 {
t.Errorf("setInstanceFieldValue failed")
}
// test ptr struct field
tt = &test{}
v = reflect.ValueOf(tt)
setInstanceFieldValue(v, "E.A", "ptr E.A-AAA")
if tt.E.A != "ptr E.A-AAA" {
t.Errorf("setInstanceFieldValue failed")
}
if tt.E.b != 0 {
t.Errorf("setInstanceFieldValue failed")
}
}