-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
tblrun_test.go
116 lines (107 loc) · 2.54 KB
/
tblrun_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
package gocrawl
import (
"fmt"
"strings"
"testing"
)
var (
// Global variable for the in-context of the test case generic assert function.
assertTrue func(bool, string, ...interface{}) bool
)
func TestRunner(t *testing.T) {
var singleTC *testCase
// Check if a single case should run
for _, tc := range cases {
if strings.HasPrefix(tc.name, "*") {
if singleTC == nil {
singleTC = tc
} else {
t.Fatal("multiple test cases for isolated run (prefixed with '*')")
}
}
}
run := 0
ign := 0
if singleTC != nil {
singleTC.name = singleTC.name[1:]
run++
t.Logf("running %s in isolated run...", singleTC.name)
runTestCase(t, singleTC)
} else {
for _, tc := range cases {
if strings.HasPrefix(tc.name, "!") {
ign++
t.Logf("ignoring %s", tc.name[1:])
} else {
run++
t.Logf("running %s...", tc.name)
runTestCase(t, tc)
}
}
}
t.Logf("%d test(s) executed, %d test(s) ignored", run, ign)
}
func runTestCase(t *testing.T, tc *testCase) {
var spy *spyExtender
// Setup the global assertTrue variable to a closure on this T and test case.
assertCnt := 0
assertTrue = func(cond bool, msg string, args ...interface{}) bool {
assertCnt++
if !cond {
t.Errorf("FAIL %s - %s.", tc.name, fmt.Sprintf(msg, args...))
return false
}
return true
}
if tc.external != nil {
// External implementation, do not use this generic runner
tc.external(t, tc, !strings.HasSuffix(tc.name, ">"))
} else {
// Generic runner
if tc.http {
ext := new(DefaultExtender)
spy = newSpy(ext, true)
} else {
ff := newFileFetcher()
spy = newSpy(ff, true)
}
if strings.HasSuffix(tc.name, ">") {
// Debug mode, print log to screen instead of buffer, and log all
spy.useLogBuffer = false
tc.opts.LogFlags = LogAll
}
tc.opts.Extender = spy
c := NewCrawlerWithOptions(tc.opts)
if tc.funcs != nil {
for emk, f := range tc.funcs {
spy.setExtensionMethod(emk, f)
}
}
if tc.panics {
defer assertPanic(tc.name, t)
assertCnt++
}
if err := c.Run(tc.seeds); err != nil && err != ErrMaxVisits {
t.Errorf("FAIL %s - %s.", tc.name, err)
}
for emk, cnt := range tc.asserts {
assertCallCount(spy, tc.name, emk, cnt, t)
assertCnt++
}
for _, s := range tc.logAsserts {
if strings.HasPrefix(s, "!") {
assertIsNotInLog(tc.name, spy.b, s[1:], t)
} else {
assertIsInLog(tc.name, spy.b, s, t)
}
assertCnt++
}
if tc.customAssert != nil {
tc.customAssert(spy, t)
assertCnt++
}
if assertCnt == 0 {
t.Errorf("FAIL %s - no asserts.", tc.name)
}
}
}