This repository has been archived by the owner on Jun 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testnode_test.go
226 lines (180 loc) · 6.63 KB
/
testnode_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"testing"
)
func TestTestNode(t *testing.T) {
t.Run(".AppendOutput()", func(t *testing.T) {
t.Run("strips leading and trailing whitespace", func(t *testing.T) {
n := newTestNode()
n.AppendOutput(" \t some output")
n.AppendOutput("other output ")
if actual, expected := len(n.Output), 2; actual != expected {
t.Fatalf("Expected %d lines of output but got %d", expected, actual)
}
if actual, expected := n.Output[0], "some output"; actual != expected {
t.Errorf("Expected first line of output to be '%s' but was '%s'", expected, actual)
}
if actual, expected := n.Output[1], "other output"; actual != expected {
t.Errorf("Expected second line of output to be '%s' but was '%s'", expected, actual)
}
})
t.Run("ignores lines generated by 'go test' command", func(t *testing.T) {
n := newTestNode()
n.AppendOutput(" FAIL")
n.AppendOutput(" PASS")
n.AppendOutput(" FAIL\tsome/package/test\t0.103s")
n.AppendOutput(" === RUN SomeTest")
n.AppendOutput(" --- FAIL: SomeTest (0.00s)")
n.AppendOutput(" --- PASS: SomeTest (0.00s)")
n.AppendOutput(" exit status 1")
n.AppendOutput(" coverage: 80.2% of statements")
n.AppendOutput(" ? \tsome/package/test\t[no test files]")
if actual, expected := len(n.Output), 0; actual != expected {
t.Errorf("Expected all lines of output to be filtered out but got %#v", n.Output)
}
})
})
t.Run(".Get()", func(t *testing.T) {
t.Run("creates children for each path step", func(t *testing.T) {
n := newTestNode()
child := n.Get("apple/banana/pear")
apple := n.ChildrenByName["apple"]
if apple == nil {
t.Fatalf("Expected a child to be created for path step 'apple' but was nil")
}
if apple.State != Unknown {
t.Errorf("Expected child 'apple' to be marked as unknown but wasn't")
}
banana := apple.ChildrenByName["banana"]
if banana == nil {
t.Fatalf("Expected a child to be created for path step 'banana' but was nil")
}
if banana.State != Unknown {
t.Errorf("Expected child 'banana' to be marked as unknown but wasn't")
}
pear := banana.ChildrenByName["pear"]
if pear == nil {
t.Fatalf("Expected a child to be created for path step 'pear' but was nil")
}
if pear.State != Unknown {
t.Errorf("Expected child 'pear' to be marked as unknown but wasn't")
}
if pear != child {
t.Errorf("Expected final child to be returned but was different")
}
})
t.Run("returns an existing child addressed by the path", func(t *testing.T) {
n := newTestNode()
child1 := n.Get("apple/banana")
child2 := n.Get("apple/banana")
if child1 != child2 {
t.Fatalf("Expected same child to be returned for same path but was different")
}
})
})
t.Run(".MarkFailed()", func(t *testing.T) {
t.Run("creates children for each path step", func(t *testing.T) {
n := newTestNode()
n.MarkFailed("apple/banana/pear")
apple := n.ChildrenByName["apple"]
if apple == nil {
t.Fatalf("Expected a child to be created for path step 'apple' but was nil")
}
if apple.State != Failed {
t.Errorf("Expected child 'apple' to be marked as failed but wasn't")
}
banana := apple.ChildrenByName["banana"]
if banana == nil {
t.Fatalf("Expected a child to be created for path step 'banana' but was nil")
}
if banana.State != Failed {
t.Errorf("Expected child 'banana' to be marked as failed but wasn't")
}
pear := banana.ChildrenByName["pear"]
if pear == nil {
t.Fatalf("Expected a child to be created for path step 'pear' but was nil")
}
if pear.State != Failed {
t.Errorf("Expected child 'pear' to be marked as failed but wasn't")
}
})
t.Run("marks existing children along the path as failed", func(t *testing.T) {
n := newTestNode()
child := n.Get("apple/banana")
if child.State != Unknown {
t.Errorf("Expected child to start off in unknown state but wasn't")
}
n.MarkFailed("apple/banana")
if child.State != Failed {
t.Errorf("Expected child to be marked as failed but wasn't")
}
})
})
t.Run(".MarkPassed()", func(t *testing.T) {
t.Run("creates children for each path step, marking the last as passed", func(t *testing.T) {
n := newTestNode()
n.MarkPassed("apple/banana/pear")
apple := n.ChildrenByName["apple"]
if apple == nil {
t.Fatalf("Expected a child to be created for path step 'apple' but was nil")
}
if apple.State != Unknown {
t.Errorf("Expected child 'apple' to be in unknown state but wasn't")
}
banana := apple.ChildrenByName["banana"]
if banana == nil {
t.Fatalf("Expected a child to be created for path step 'banana' but was nil")
}
if banana.State != Unknown {
t.Errorf("Expected child 'banana' to be in unknown state but wasn't")
}
pear := banana.ChildrenByName["pear"]
if pear == nil {
t.Fatalf("Expected a child to be created for path step 'pear' but was nil")
}
if pear.State != Passed {
t.Errorf("Expected child 'pear' to be marked as passed but wasn't")
}
})
t.Run("leaves existing children along the path in their existing state", func(t *testing.T) {
n := newTestNode()
child := n.Get("apple/banana")
if child.State != Unknown {
t.Errorf("Expected child to start off in unknown state but wasn't")
}
n.MarkPassed("apple/banana")
if child.State != Passed {
t.Errorf("Expected child to be marked as passed but wasn't")
}
})
})
t.Run("pathStep()", func(t *testing.T) {
t.Run("separates a node name into the next path step and the remainder", func(t *testing.T) {
next, rest := pathStep("apple/banana/pear")
if expected := "apple"; next != expected {
t.Errorf("Expected next path step to be '%s' but got '%s'", expected, next)
}
if expected := "banana/pear"; rest != expected {
t.Errorf("Expected rest of path to be '%s' but got '%s'", expected, rest)
}
})
t.Run("returns empty string as the remainder if there are no more remaining steps", func(t *testing.T) {
next, rest := pathStep("apple")
if expected := "apple"; next != expected {
t.Errorf("Expected next path step to be '%s' but got '%s'", expected, next)
}
if expected := ""; rest != expected {
t.Errorf("Expected rest of path to be '%s' but got '%s'", expected, rest)
}
})
t.Run("returns empty string as the next step if there are no more steps", func(t *testing.T) {
next, rest := pathStep("")
if expected := ""; next != expected {
t.Errorf("Expected next path step to be '%s' but got '%s'", expected, next)
}
if expected := ""; rest != expected {
t.Errorf("Expected rest of path to be '%s' but got '%s'", expected, rest)
}
})
})
}