-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers_test.go
159 lines (140 loc) · 4.08 KB
/
layers_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
package cake
import "testing"
type Service interface {
Fruits() []string
Veggies() []string
}
type LayerA struct{ Service }
func (l *LayerA) Fruits() []string {
return []string{"Apple"}
}
func (l *LayerA) Veggies() []string {
return []string{"Artichoke"}
}
type LayerB struct{ Service }
func (l *LayerB) Fruits() []string {
return append(l.Service.Fruits(), "Banana")
}
func (l *LayerB) Veggies() []string {
return append(l.Service.Veggies(), "Basil")
}
type LayerC struct{ Service }
func (l *LayerC) Veggies() []string {
return append(l.Service.Veggies(), "Cilantro")
}
type LayerD struct{ Service }
func (l *LayerD) Fruits() []string {
return append(l.Service.Fruits(), "Durian")
}
func (l *LayerD) Veggies() []string {
return append(l.Service.Veggies(), "Dill")
}
type LayerE struct{ Service } // E for Empty!
func Test_Layers(t *testing.T) {
testTable := map[string]struct {
baseLayer Service
layers []Service
expectedFruits []string
expectedVeggies []string
}{
"Works as a wrapper even with no additional layers": {
baseLayer: &LayerA{},
layers: []Service{},
expectedFruits: []string{"Apple"},
expectedVeggies: []string{"Artichoke"},
},
"Works as a wrapper one or more additional layers": {
baseLayer: &LayerA{},
layers: []Service{
&LayerB{},
&LayerC{},
&LayerD{},
},
expectedFruits: []string{"Apple", "Durian", "Banana"},
expectedVeggies: []string{"Artichoke", "Dill", "Cilantro", "Basil"},
},
"Properly sets the entry layer when one or more of the first layers are nil": {
baseLayer: &LayerA{},
layers: []Service{
If(false, &LayerC{}),
If(false, &LayerC{}),
&LayerB{},
&LayerC{},
&LayerE{},
If(false, &LayerC{}),
&LayerD{},
},
expectedFruits: []string{"Apple", "Durian", "Banana"},
expectedVeggies: []string{"Artichoke", "Dill", "Cilantro", "Basil"},
},
"Falls through layers with nil method implementations": {
baseLayer: &LayerA{},
layers: []Service{
&LayerB{},
&LayerC{},
&LayerE{},
&LayerD{},
},
expectedFruits: []string{"Apple", "Durian", "Banana"},
expectedVeggies: []string{"Artichoke", "Dill", "Cilantro", "Basil"},
},
"Falls through one or many consecutive nil layers": {
baseLayer: &LayerA{},
layers: []Service{
&LayerB{},
&LayerC{},
If(false, &LayerC{}),
If(false, &LayerC{}),
&LayerE{},
If(false, &LayerC{}),
&LayerD{},
},
expectedFruits: []string{"Apple", "Durian", "Banana"},
expectedVeggies: []string{"Artichoke", "Dill", "Cilantro", "Basil"},
},
"If returns a nil layer when the given condition is false": {
baseLayer: &LayerA{},
layers: []Service{
&LayerB{},
If(false, &LayerC{}),
&LayerD{},
},
expectedFruits: []string{"Apple", "Durian", "Banana"},
expectedVeggies: []string{"Artichoke", "Dill", "Basil"},
},
"IfCallback does not call its callback when the given condition is false": {
baseLayer: &LayerA{},
layers: []Service{
&LayerB{},
IfCallback(false, func() Service { panic("i should not execute") }),
&LayerD{},
},
expectedFruits: []string{"Apple", "Durian", "Banana"},
expectedVeggies: []string{"Artichoke", "Dill", "Basil"},
},
}
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
svc, err := Layered[Service](testCase.baseLayer, testCase.layers...)
if err != nil {
t.Fatalf("failed to layer cake: %+v", err)
}
if len(svc.Fruits()) != len(testCase.expectedFruits) {
t.Fatalf("expectedFruits %d words, got %d", len(testCase.expectedFruits), len(svc.Fruits()))
}
for i, fruit := range svc.Fruits() {
if fruit != testCase.expectedFruits[i] {
t.Fatalf("expectedFruits %s, got %s", testCase.expectedFruits[i], fruit)
}
}
if len(svc.Veggies()) != len(testCase.expectedVeggies) {
t.Fatalf("expectedVeggies %d words, got %d", len(testCase.expectedVeggies), len(svc.Veggies()))
}
for i, veg := range svc.Veggies() {
if veg != testCase.expectedVeggies[i] {
t.Fatalf("expectedVeggies %s, got %s", testCase.expectedVeggies[i], veg)
}
}
})
}
}