/
example_group.go
72 lines (63 loc) · 1.84 KB
/
example_group.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
package gospel
import (
"fmt"
"testing"
)
// Describe or Context creates this object.
type ExampleGroup struct {
*testing.T
Parent *ExampleGroup
Description string
Result string
BeforeFilters []func()
AfterFilters []func()
}
// In non-verbose mode, we print test results for each Describe.
func (group *ExampleGroup) PrintResult() {
fmt.Fprint(Output, group.Result)
}
// Returns its ancestor ExampleGroups in ascending order according to distance.
func (group *ExampleGroup) Ancestors() []*ExampleGroup {
ancestors := []*ExampleGroup{}
for parent := group.Parent; parent != nil; parent = parent.Parent {
ancestors = append(ancestors, parent)
}
return ancestors
}
// Returns its ancestor ExampleGroups in descending order according to distance.
func (group *ExampleGroup) ReverseAncestors() []*ExampleGroup {
reversed := []*ExampleGroup{}
ancestors := group.Ancestors()
for i := len(ancestors) - 1; i >= 0; i-- {
reversed = append(reversed, ancestors[i])
}
return reversed
}
// Gets root ExampleGroup defined by Describe func.
func (group *ExampleGroup) Root() *ExampleGroup {
if group.Parent == nil {
return group
}
ancestors := group.Ancestors()
return ancestors[len(ancestors) - 1]
}
// Runs all BeforeFilters defined to its ancestors and itself.
func (group *ExampleGroup) RunBeforeFilters() {
for _, ancestor := range group.ReverseAncestorsAndSelf() {
for _, beforeFilter := range ancestor.BeforeFilters {
beforeFilter()
}
}
}
// Runs all AfterFilters defined to its ancestors and itself.
func (group *ExampleGroup) RunAfterFilters() {
for _, ancestor := range group.ReverseAncestorsAndSelf() {
for _, afterFilter := range ancestor.AfterFilters {
afterFilter()
}
}
}
// Merges its ancestors and itself.
func (group *ExampleGroup) ReverseAncestorsAndSelf() []*ExampleGroup {
return append(group.ReverseAncestors(), group)
}