forked from naoina/kocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.go
168 lines (154 loc) · 3.73 KB
/
unit_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
package kocha
import (
"reflect"
"testing"
)
type testUnit struct {
name string
active bool
callCount int
}
func (u *testUnit) ActiveIf() bool {
u.callCount++
return u.active
}
type testUnit2 struct{}
func (u *testUnit2) ActiveIf() bool {
return true
}
func Test_Invoke(t *testing.T) {
// test that it invokes newFunc when ActiveIf returns true.
testInvokeWrapper(func() {
unit := &testUnit{"test1", true, 0}
called := false
Invoke(unit, func() {
called = true
}, func() {
t.Errorf("defaultFunc has been called")
})
actual := called
expected := true
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
})
// test that it invokes defaultFunc when ActiveIf returns false.
testInvokeWrapper(func() {
unit := &testUnit{"test2", false, 0}
called := false
Invoke(unit, func() {
t.Errorf("newFunc has been called")
}, func() {
called = true
})
actual := called
expected := true
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
})
// test that it invokes defaultFunc when any errors occurred in newFunc.
testInvokeWrapper(func() {
unit := &testUnit{"test3", true, 0}
called := false
Invoke(unit, func() {
panic("expected error")
}, func() {
called = true
})
actual := called
expected := true
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
})
// test that it will be panic when panic occurred in defaultFunc.
testInvokeWrapper(func() {
defer func() {
if err := recover(); err == nil {
t.Errorf("panic doesn't occurred")
} else if err != "expected error in defaultFunc" {
t.Errorf("panic doesn't occurred in defaultFunc: %v", err)
}
}()
unit := &testUnit{"test4", false, 0}
Invoke(unit, func() {
t.Errorf("newFunc has been called")
}, func() {
panic("expected error in defaultFunc")
})
})
// test that it panic when panic occurred in both newFunc and defaultFunc.
testInvokeWrapper(func() {
defer func() {
if err := recover(); err == nil {
t.Errorf("panic doesn't occurred")
} else if err != "expected error in defaultFunc" {
t.Errorf("panic doesn't occurred in defaultFunc: %v", err)
}
}()
unit := &testUnit{"test5", true, 0}
called := false
Invoke(unit, func() {
called = true
panic("expected error")
}, func() {
panic("expected error in defaultFunc")
})
actual := called
expected := true
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
})
testInvokeWrapper(func() {
unit := &testUnit{"test6", true, 0}
Invoke(unit, func() {
panic("expected error")
}, func() {
// do nothing.
})
var actual interface{} = unit.callCount
var expected interface{} = 1
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
// again.
Invoke(unit, func() {
t.Errorf("newFunc has been called")
}, func() {
// do nothing.
})
actual = unit.callCount
expected = 1
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
// same unit type.
unit = &testUnit{"test7", true, 0}
called := false
Invoke(unit, func() {
t.Errorf("newFunc has been called")
}, func() {
called = true
})
actual = called
expected = true
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
// different unit type.
unit2 := &testUnit2{}
called = false
Invoke(unit2, func() {
called = true
}, func() {
t.Errorf("defaultFunc has been called")
})
actual = called
expected = true
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
})
}