-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
expectation_sequence.go
70 lines (55 loc) · 1.36 KB
/
expectation_sequence.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
package queryhistory
// ExpectationSequence represents a temporal ordering of expectations.
type ExpectationSequence interface {
Count() int
// Head returns the head of the sequence. A sequence may only have one
// head.
Head() SequencedExpectation
// Visit every expectation in the sequence, in any order.
Visit(ExpectationSequenceVisitor)
}
type ExpectationSequenceVisitor func(SequencedExpectation) VisitControl
type VisitControl int
type expectationSequence struct {
head SequencedExpectation
}
const (
VisitContinue VisitControl = iota
VisitTerminate
)
func (es *expectationSequence) Count() int {
count := 0
es.Visit(func(_ SequencedExpectation) VisitControl {
count++
return VisitContinue
})
return count
}
func (es *expectationSequence) Head() SequencedExpectation {
return es.head
}
func (es *expectationSequence) Visit(visitor ExpectationSequenceVisitor) {
next := make([]SequencedExpectation, 0)
visited := make(map[SequencedExpectation]bool)
if es.head != nil {
next = append(next, es.head)
}
for len(next) > 0 {
se := next[0]
next = next[1:]
if visited[se] {
continue
}
control := visitor(se)
switch control {
case VisitTerminate:
return
default:
visited[se] = true
}
if se.ImmediatelyBefore() != nil {
next = append(next, se.ImmediatelyBefore())
}
next = append(next, se.EventuallyBefore().Slice()...)
}
}