-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactory_test.go
169 lines (152 loc) · 3.83 KB
/
factory_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
157
158
159
160
161
162
163
164
165
166
167
168
169
package map2struct
import (
"fmt"
"reflect"
"testing"
"time"
)
type stringer interface {
String() string
}
type texter interface {
Text() string
}
type foo struct {
Text string
}
func (foo foo) String() string {
return "foo:" + foo.Text
}
type bar struct {
Duration time.Duration
}
func (bar bar) String() string {
return fmt.Sprintf("bar:%s", bar.Duration)
}
func initializeInstance(i interface{}) error {
return nil
}
func initializeInstanceFail(i interface{}) error {
return fmt.Errorf("error")
}
func TestRegisterFactory(t *testing.T) {
defer func() { factories = make(map[string]Factory) }()
var s string
factory := NewGeneralInterfaceFactory(reflect.TypeOf(s), "type", nil)
RegisterFactory(factory)
if f := factories[getTypeName(reflect.TypeOf(s))]; f != factory {
t.Error("register factory fail:", f)
return
}
}
func TestGeneralInterfaceFactory(t *testing.T) {
defer func() { factories = make(map[string]Factory) }()
factory := NewGeneralInterfaceFactory(reflect.TypeOf((*stringer)(nil)).Elem(),
"type", initializeInstance)
factory.RegisterType("Foo", reflect.TypeOf((*foo)(nil)).Elem())
factory.RegisterType("Bar", reflect.TypeOf((*bar)(nil)).Elem())
factory.RegisterInstance("Instance", &foo{Text: "instance"})
RegisterFactory(factory)
src := map[string]interface{}{
"Stringer1": map[string]interface{}{
"type": "Foo",
"Text": "hello",
},
"Stringer2": map[string]interface{}{
"type": "Bar",
"Duration": "1s",
},
"Instance": map[string]interface{}{
"type": "Instance",
},
}
type TestStruct struct {
Stringer1 stringer
Stringer2 stringer
Instance stringer
}
type TestStruct2 struct {
Texter texter
}
var output TestStruct
if err := Unmarshal(&output, src); err != nil {
t.Error("unmarshal map fail:", err.Error())
return
}
if output.Stringer1.String() != "foo:hello" || output.Stringer2.String() != "bar:1s" || output.Instance.String() != "foo:instance" {
t.Error("unexpected output:", output)
return
}
src = map[string]interface{}{
"type": "Foo",
"Text": "world",
}
var s stringer
var x texter
if err := Unmarshal(&s, src); err != nil {
t.Error("unmarshal map fail:", err.Error())
return
}
if s.String() != "foo:world" {
t.Error("unexpected output:", s)
return
}
// test unknown type
src = map[string]interface{}{
"type": "unknown",
}
if err := Unmarshal(&s, src); err == nil {
t.Error("unexpected unmarshal success:", s)
return
}
// test missing type key
src = map[string]interface{}{
"Text": "hello",
}
if err := Unmarshal(&s, src); err == nil {
t.Error("unexpected unmarshal success:", s)
return
}
// test unknown dest type
if err := Unmarshal(&x, src); err == nil {
t.Error("unexpected unmarshal success:", t)
return
}
// test ptr
src = map[string]interface{}{
"type": "Foo",
"Text": "world",
}
factory = NewGeneralInterfaceFactory(reflect.TypeOf((*stringer)(nil)).Elem(),
"type", initializeInstance)
factory.RegisterType("Foo", reflect.TypeOf((**foo)(nil)).Elem())
factory.RegisterType("Bar", reflect.TypeOf((**bar)(nil)).Elem())
RegisterFactory(factory)
if err := Unmarshal(&s, src); err != nil {
t.Error("unmarshal map fail:", err.Error())
return
}
if s.String() != "foo:world" {
t.Error("unexpected output:", s)
return
}
// test initializer fail
factory = NewGeneralInterfaceFactory(reflect.TypeOf((*stringer)(nil)).Elem(),
"type", initializeInstanceFail)
factory.RegisterType("Foo", reflect.TypeOf((**foo)(nil)).Elem())
factory.RegisterType("Bar", reflect.TypeOf((**bar)(nil)).Elem())
RegisterFactory(factory)
if err := Unmarshal(&s, src); err == nil {
t.Error("unexpected unmarshal success:", s)
return
}
// test interval fail
src = map[string]interface{}{
"type": "Bar",
"Duration": "s",
}
if err := Unmarshal(&s, src); err == nil {
t.Error("unexpected unmarshal success:", s)
return
}
}