-
Notifications
You must be signed in to change notification settings - Fork 0
/
toast_test.go
71 lines (57 loc) · 1.33 KB
/
toast_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
package toast
import (
"fmt"
"io"
"testing"
"time"
)
var (
i = 0
)
func initTest(t Toaster) {
if i != 0 {
t.Error("Init failed")
}
t.Log("Doing some init job")
i++
}
func cleanupTest(t Toaster) {
if i != 2 {
t.Error("Cleanup test failed")
}
t.Log("Doing some cleanup job")
}
func testWrap(t Toaster) {
if i != 1 {
t.Error("Wrapped test failed")
}
t.Log("This is a wrapped test")
i++
}
func TestToast(t *testing.T) {
tt := FromT(t)
// making tests
tt.FailNow = false
tt.mock = true
tt.ExpectErr(fmt.Errorf("random error"), nil)
// should not print anything
tt.ExpectErr(fmt.Errorf("encountered error %w", io.ErrClosedPipe), io.ErrClosedPipe)
// should print error message
tt.ExpectErr(fmt.Errorf("encountered error %w", io.ErrClosedPipe), io.ErrUnexpectedEOF)
tt.Wrap(initTest, testWrap, cleanupTest)
// ok
tt.Assert(mkfmt(1, 2, 3) == "%v %v %v")
// fail
tt.Assert("one" == "two")
tt.ShouldPanic(func() { AssertOrPanic(2 == 3) })
tt.ShouldPanic(func() { AssertOrPanic(true) })
// should not print message
tt.CheckErr(nil)
// should print message
tt.CheckErr(fmt.Errorf("This is a random error"))
tt.TimeIt("sleeping", func() { time.Sleep(50 * time.Millisecond) })
}
func TestToastNoMock(t *testing.T) {
tt := FromT(t)
tt.ExpectErr(fmt.Errorf("encountered error %w", io.ErrClosedPipe), io.ErrClosedPipe)
}