forked from smartystreets/goconvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
100 lines (92 loc) · 2.15 KB
/
scope.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
package convey
import (
"fmt"
"strings"
"github.com/smartystreets/goconvey/convey/reporting"
)
type scope struct {
name string
title string
action *action
children map[string]*scope
birthOrder []*scope
child int
resets map[string]*action
panicked bool
reporter reporting.Reporter
report *reporting.ScopeReport
}
func (parent *scope) adopt(child *scope) {
if parent.hasChild(child) {
return
}
parent.birthOrder = append(parent.birthOrder, child)
parent.children[child.name] = child
}
func (parent *scope) hasChild(child *scope) bool {
for _, ordered := range parent.birthOrder {
if ordered.name == child.name && ordered.title == child.title {
return true
}
}
return false
}
func (self *scope) registerReset(action *action) {
self.resets[action.name] = action
}
func (self *scope) visited() bool {
return self.panicked || self.child >= len(self.birthOrder)
}
func (parent *scope) visit() {
defer parent.exit()
parent.enter()
parent.action.Invoke()
parent.visitChildren()
}
func (parent *scope) enter() {
parent.reporter.Enter(parent.report)
}
func (parent *scope) visitChildren() {
if len(parent.birthOrder) == 0 {
parent.cleanup()
} else {
parent.visitChild()
}
}
func (parent *scope) visitChild() {
child := parent.birthOrder[parent.child]
child.visit()
if child.visited() {
parent.cleanup()
parent.child++
}
}
func (parent *scope) cleanup() {
for _, reset := range parent.resets {
reset.Invoke()
}
}
func (parent *scope) exit() {
if problem := recover(); problem != nil {
if strings.HasPrefix(fmt.Sprintf("%v", problem), extraGoTest) {
panic(problem)
}
if problem != failureHalt {
parent.panicked = true
parent.reporter.Report(reporting.NewErrorReport(problem))
}
}
parent.reporter.Exit()
}
func newScope(entry *registration, reporter reporting.Reporter) *scope {
self := new(scope)
self.reporter = reporter
self.name = entry.action.name
self.title = entry.Situation
self.action = entry.action
self.children = make(map[string]*scope)
self.birthOrder = []*scope{}
self.resets = make(map[string]*action)
self.report = reporting.NewScopeReport(self.title, self.name)
return self
}