-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_test.go
103 lines (82 loc) · 1.89 KB
/
expect_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
package expect
import (
"reflect"
"testing"
"github.com/halimath/expect/internal/testhelper"
)
func TestExpectFunc(t *testing.T) {
f := ExpectFunc(func(t TB) {
t.Log("got")
})
tm := &testhelper.TB{}
f.Expect(tm)
if len(tm.Logs) != 1 {
t.Fatalf("expected 1 log message but got %v", tm.Logs)
}
if tm.Logs[0] != "got" {
t.Errorf("expected 1st log message to be 'got' but got %v", tm.Logs[0])
}
}
func TestThat_noFailure(t *testing.T) {
var e1Called, e2Called int
e1 := ExpectFunc(func(TB) { e1Called++ })
e2 := ExpectFunc(func(TB) { e2Called++ })
That(t, e1, e2)
}
func TestPrefixTB(t *testing.T) {
var tb testhelper.TB
prefixed := prefixedTB{TB: &tb, prefix: "prefix: "}
prefixed.Log("Log")
prefixed.Logf("Logf")
prefixed.Error("Error")
prefixed.Errorf("Errorf")
prefixed.Fatal("Fatal")
prefixed.Fatalf("Fatalf")
prefixed.Skip("Skip")
prefixed.Skipf("Skipf")
want := testhelper.TB{
ErrFlag: true,
FatalFlag: true,
SkippedFlag: true,
Logs: []string{
"prefix: Log",
"prefix: Logf",
"prefix: Error",
"prefix: Errorf",
"prefix: Fatal",
"prefix: Fatalf",
"prefix: Skip",
"prefix: Skipf",
},
}
if diff := reflect.DeepEqual(tb, want); !diff {
t.Errorf("TB interaction not equal. Wanted %v but got %v", want, tb)
}
}
func TestWithMessage(t *testing.T) {
var tb testhelper.TB
expect := WithMessage(&tb, "pref%s", "ix")
expect.That(Fail)
want := testhelper.TB{
ErrFlag: true,
Logs: []string{
"prefix: test failed",
},
}
if diff := reflect.DeepEqual(tb, want); !diff {
t.Errorf("TB interaction not equal. Wanted %v but got %v", want, tb)
}
}
func TestFailNow(t *testing.T) {
var tb testhelper.TB
That(&tb, FailNow(Fail))
want := testhelper.TB{
FatalFlag: true,
Logs: []string{
"test failed",
},
}
if diff := reflect.DeepEqual(tb, want); !diff {
t.Errorf("TB interaction not equal. Wanted %v but got %v", want, tb)
}
}