-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
wire_test.go
133 lines (110 loc) · 2.89 KB
/
wire_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
package wire_test
import (
"testing"
"github.com/Fs02/wire"
"github.com/stretchr/testify/assert"
)
type Valuer interface {
Value() string
}
type Setter interface {
Set(string)
}
type ComponentA struct {
Value1 string
Value2 int
value3 interface{} // unexported
}
func (c ComponentA) Value() string {
return c.Value1
}
func (c *ComponentA) Set(v string) {
c.Value1 = v
}
type ComponentB struct {
Value1 []int
Value2 ComponentA `wire:"hello"`
Value3 string
Value4 bool `wire:""`
}
type ComponentC struct {
Value1 *ComponentA `wire:""`
Value2 *ComponentB `wire:""`
Value3 bool
Value4 []int `wire:""`
Value5 Valuer `wire:"component_d,ComponentD"`
}
type ComponentD struct {
Value1 string `wire:",ComponentA"`
Value3 *int `wire:"-"`
}
func (c ComponentD) Value() string {
return c.Value1
}
type ComponentE struct {
Value1 Valuer `wire:""`
Value2 Setter `wire:""`
}
func TestWire(t *testing.T) {
componentA := ComponentA{Value1: "Hi!", Value2: 10}
componentB := ComponentB{Value1: []int{1}, Value3: "Hello!"}
componentC := ComponentC{Value3: false}
componentD := ComponentD{}
componentE := ComponentE{}
app := wire.New()
app.Connect("LGTM!")
app.Connect(true)
app.Connect([]int{1, 2, 3})
app.Connect(&componentA)
app.Connect(&componentB)
app.Connect(&componentC)
app.Connect(&componentD, "component_d")
app.Connect(&componentE)
app.Connect(&ComponentA{Value1: "Hello!", Value2: 20}, "hello")
app.Apply()
var resolvedString string
var resolvedBool bool
var resolvedSint []int
var resolvedA ComponentA
var resolvedB ComponentB
var resolvedC ComponentC
var resolvedD *ComponentD
var resolvedE *ComponentE
app.Resolve(&resolvedString)
app.Resolve(&resolvedBool)
app.Resolve(&resolvedSint)
app.Resolve(&resolvedA)
app.Resolve(&resolvedB)
app.Resolve(&resolvedC)
app.Resolve(&resolvedD, "component_d")
app.Resolve(&resolvedE)
assert.Equal(t, ComponentA{Value1: "Hi!", Value2: 10}, componentA)
assert.Equal(t, ComponentB{
Value1: []int{1},
Value2: ComponentA{Value1: "Hello!", Value2: 20},
Value3: "Hello!",
Value4: true,
}, componentB)
assert.Equal(t, ComponentC{
Value1: &ComponentA{Value1: "Hi!", Value2: 10},
Value2: &ComponentB{
Value1: []int{1},
Value2: ComponentA{Value1: "Hello!", Value2: 20},
Value3: "Hello!",
Value4: true,
},
Value3: false,
Value4: []int{1, 2, 3},
Value5: ComponentD{Value1: "LGTM!"},
}, componentC)
assert.Equal(t, ComponentD{Value1: "LGTM!"}, componentD)
assert.Equal(t, ComponentE{Value1: ComponentA{Value1: "Hi!", Value2: 10}, Value2: &ComponentA{Value1: "Hi!", Value2: 10}}, componentE)
assert.Equal(t, "LGTM!", resolvedString)
assert.Equal(t, true, resolvedBool)
assert.Equal(t, []int{1, 2, 3}, resolvedSint)
assert.Equal(t, componentA, resolvedA)
assert.Equal(t, componentB, resolvedB)
assert.Equal(t, componentC, resolvedC)
assert.Equal(t, componentD, *resolvedD)
assert.Equal(t, componentE, *resolvedE)
}