-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
02_advanced_test.go
65 lines (49 loc) · 1.19 KB
/
02_advanced_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
package example_test
import (
"testing"
"time"
"github.com/ysmood/got"
"github.com/ysmood/got/lib/example"
)
func TestChainMethods(t *testing.T) {
g := setup(t)
g.Desc("1 must equal 1").Must().Eq(example.Sum("1", "2"), "3")
}
func TestUtils(t *testing.T) {
g := setup(t)
// Run "go doc got.Utils" to list available helpers
s := g.Serve()
s.Mux.HandleFunc("/", example.ServeSum)
val := g.Req("", s.URL("?a=1&b=2")).Bytes().String()
g.Eq(val, "3")
}
func TestTableDriven(t *testing.T) {
testCases := []struct{ desc, a, b, expected string }{{
"first",
"1", "2", "3",
}, {
"second",
"2", "3", "5",
}}
for _, c := range testCases {
t.Run(c.desc, func(t *testing.T) {
g := setup(t)
g.Eq(example.Sum(c.a, c.b), c.expected)
})
}
}
func TestSnapshot(t *testing.T) {
g := setup(t)
g.Snapshot("snapshot the map value", map[int]string{1: "1", 2: "2"})
}
func TestWaitGroup(t *testing.T) {
g := got.T(t)
check := func() {
time.Sleep(time.Millisecond * 30)
g.Eq(1, 1)
}
// This check won't be executed because the test will end before the goroutine starts.
go check()
// This check will be executed because the test will wait for the goroutine to finish.
g.Go(check)
}