-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcase_test.go
100 lines (89 loc) · 2.03 KB
/
testcase_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
package testcase_test
import (
"testing"
"time"
. "github.com/go-tk/testcase"
"github.com/stretchr/testify/assert"
)
func TestTestCase_WithTag(t *testing.T) {
type C struct{}
var name string
New(func(t *testing.T, c *C) {
name = t.Name()
}).WithTag("my_tag").Run(t)
assert.Equal(t, "TestTestCase_WithTag/testcase_test.go:17@my_tag", name)
New(func(t *testing.T, c *C) {
name = t.Name()
}).WithTag("").Run(t)
assert.Equal(t, "TestTestCase_WithTag/testcase_test.go:22", name)
}
func TestTestCase_Callback(t *testing.T) {
{
type C struct {
n string
}
var s string
New(func(t *testing.T, c *C) {
c.n += "1"
Callback(t, "cb_1")
c.n += "2"
Callback(t, "cb_2")
c.n += "3"
Callback(t, "cb_3")
}).WithCallback("cb_1", func(t *testing.T, c *C) {
s += "a"
s += c.n
}).WithCallback("cb_2", func(t *testing.T, c *C) {
s += "b"
s += c.n
}).WithCallback("cb_3", func(t *testing.T, c *C) {
s += "c"
s += c.n
}).Run(t)
assert.Equal(t, "a1b12c123", s)
}
assert.PanicsWithValue(t, "can't find context", func() {
Callback(t, "foo")
})
New(func(t *testing.T, c *struct{}) {
assert.PanicsWithValue(t, "can't find callback by id: foo", func() {
Callback(t, "foo")
})
}).Run(t)
}
func TestTestCase_OptionalCallback(t *testing.T) {
{
type C struct {
n string
}
var s string
New(func(t *testing.T, c *C) {
c.n += "1"
OptionalCallback(t, "cb_1")
c.n += "2"
OptionalCallback(t, "cb_2")
c.n += "3"
OptionalCallback(t, "cb_3")
}).WithCallback("cb_1", func(t *testing.T, c *C) {
s += "a"
s += c.n
}).WithCallback("cb_3", func(t *testing.T, c *C) {
s += "c"
s += c.n
}).Run(t)
assert.Equal(t, "a1c123", s)
}
assert.PanicsWithValue(t, "can't find context", func() {
OptionalCallback(t, "foo")
})
}
func TestTestCase_RunParallel(t *testing.T) {
defer func(t0 time.Time) {
assert.Less(t, time.Since(t0).Seconds(), 1.0)
}(time.Now())
for i := 0; i < 2; i++ {
New(func(t *testing.T, c *struct{}) {
time.Sleep(time.Second * 4 / 5)
}).RunParallel(t)
}
}