-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_test.go
151 lines (131 loc) · 3.87 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gotest
import (
"fmt"
"testing"
)
func person(name string, age int) struct {
name string
age int
} {
return struct {
name string
age int
}{
name,
age,
}
}
func Test_ShouldFailSimpleTest(t *testing.T) {
t.Parallel()
shouldFail := func(expect *Expectation) {
t.Run(fmt.Sprintf("%v should fail", expect), func(tt *testing.T) {
tt.Parallel()
spy := getSpy(expect)
if !spy.wasCalled("fail") {
tt.Fatalf("Didn't fail when expected")
}
spy.reset()
})
}
I := createExpectationBuilder(createContextSpy())
shouldFail(I.Expect(1 + 1).ToBe(3))
shouldFail(I.Expect(true).ToBe(false))
shouldFail(I.Expect("cat").ToBe("dog"))
shouldFail(I.Expect(nil).ToBe("not nil"))
shouldFail(I.Expect(person("Frank", 43)).ToBe(person("Bob", 42)))
if I.context.(*fakeTestContext).failWasCalled {
t.Fatalf("Only subtests are expected to fail.")
}
}
func Test_ShouldPassSimpleTest(t *testing.T) {
t.Parallel()
shouldPass := func(expect *Expectation) {
t.Run(expect.String(), func(tt *testing.T) {
tt.Parallel()
spy := getSpy(expect)
if spy.wasCalled("fail") {
t.Fatalf("Test was expected to pass.")
}
})
}
I := createExpectationBuilder(createContextSpy())
shouldPass(I.Expect(1 + 1).ToBe(2))
shouldPass(I.Expect(true).ToBe(true))
shouldPass(I.Expect("cat").ToBe("cat"))
shouldPass(I.Expect(nil).ToBe(nil))
shouldPass(I.Expect(person("Bob", 42)).ToBe(person("Bob", 42)))
if I.context.(*fakeTestContext).failWasCalled {
t.Fatalf("All tests expected to pass.")
}
}
func Test_ShouldRunInParallel(t *testing.T) {
t.Parallel()
spy := createContextSpy()
createExpectationBuilder(spy.context())
if !spy.wasCalled("parallel") {
t.Errorf("Test did not run in parallel.")
}
}
func Test_ShouldCreateSubtestForExpectation(t *testing.T) {
t.Parallel()
spy := createContextSpy()
builder := createExpectationBuilder(spy.context())
expectation := builder.Expect(true).ToBe(true)
if expectation.context == spy.context() {
t.Fatalf("Subtest context is the same as the test context.")
}
}
func Test_ShouldCreateSubtestsInParallel(t *testing.T) {
t.Parallel()
builder := createExpectationBuilder(createContextSpy())
expectation := builder.Expect(true).ToBe(true)
if !getSpy(expectation).wasCalled("parallel") {
t.Fatalf("Subtest was not run in parallel.")
}
}
func Test_ShouldDistinguishBetweenSubtests(t *testing.T) {
t.Parallel()
builder := createExpectationBuilder(createContextSpy())
first := builder.Expect(1 + 1).ToBe(3)
subSpyOne := getSpy(first)
if !subSpyOne.wasCalled("fail") {
t.Fatalf("First test didn't fail")
}
subSpyOne.reset()
second := builder.Expect(2 + 2).ToBe(5)
subSpyTwo := getSpy(second)
if !subSpyTwo.wasCalled("fail") {
t.Fatalf("Second test didn't fail")
}
if subSpyOne.wasCalled("fail") {
t.Fatalf("First test failed when it shouldn't have")
}
}
func Test_ShouldNameExpectations(t *testing.T) {
t.Parallel()
I := createExpectationBuilder(createContextSpy())
name := "I expect 2 to be 2"
expectation := I.Expect(1 + 1).ToBe(2)
if expectation.String() != name {
t.Fatalf("Expectation was not named properly: expected \"%v\" to be \"%v\"", expectation, name)
}
}
func Test_ShouldSupportCustomNamesForExpectations(t *testing.T) {
t.Parallel()
I := createExpectationBuilder(createContextSpy())
name := "I expect 1 + 1 to be 2"
expectation := I.Expect(1 + 1).As("1 + 1").ToBe(2)
if expectation.String() != name {
t.Fatalf("Expectation was not named properly: expected \"%v\" to be \"%v\"", expectation, name)
}
}
func Test_ShouldUseExpectationNameWhenCreatingSubtest(t *testing.T) {
t.Parallel()
I := createExpectationBuilder(createContextSpy())
name := "I expect 1 + 1 to be 2"
expectation := I.Expect(1 + 1).As("1 + 1").ToBe(2)
nameInContext := getSpy(expectation).name
if nameInContext != name {
t.Fatalf("Subtest was not invoked with expected name: expected \"%v\" to be \"%v\"", nameInContext, name)
}
}