-
Notifications
You must be signed in to change notification settings - Fork 13
/
gostub_test.go
142 lines (111 loc) · 2.4 KB
/
gostub_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
package gostub
import "testing"
// Variables used in stubbing.
var v1, v2, v3, v4 int
// resetVars is used to reset the variables used in stubbing tests to their default values.
func resetVars() {
v1 = 100
v2 = 200
v3 = 300
v4 = 400
}
func TestStub(t *testing.T) {
resetVars()
stubs := Stub(&v1, 1)
expectVal(t, v1, 1)
stubs.Reset()
expectVal(t, v1, 100)
}
func TestNewT(t *testing.T) {
resetVars()
t.Run("inner", func(t *testing.T) {
// No need for reset.
NewT(t).Stub(&v1, 1)
})
expectVal(t, 100, v1)
}
func TestValue(t *testing.T) {
resetVars()
t.Run("test", func(t *testing.T) {
reset1 := Value(t, &v1, 1)
reset2 := Value(t, &v2, 2)
expectVal(t, v1, 1)
expectVal(t, v2, 2)
reset1()
expectVal(t, v1, 100)
expectVal(t, v2, 2)
reset2()
expectVal(t, v1, 100)
expectVal(t, v2, 200)
Value(t, &v1, 0)
Value(t, &v2, 0)
Value(t, &v3, 0)
})
t.Run("verify Cleanup", func(t *testing.T) {
expectVal(t, v1, 100)
expectVal(t, v2, 200)
expectVal(t, v3, 300)
})
}
func TestRestub(t *testing.T) {
resetVars()
stubs := Stub(&v1, 1)
expectVal(t, v1, 1)
stubs.Stub(&v1, 2)
expectVal(t, v1, 2)
stubs.Reset()
expectVal(t, v1, 100)
}
func TestResetSingle(t *testing.T) {
resetVars()
stubs := Stub(&v1, 1).Stub(&v2, 2)
expectVal(t, v1, 1)
expectVal(t, v2, 2)
stubs.ResetSingle(&v1)
expectVal(t, v1, 100)
expectVal(t, v2, 2)
stubs.Reset()
expectVal(t, v1, 100)
expectVal(t, v2, 200)
}
func TestResetSingleNotStubbed(t *testing.T) {
resetVars()
stubs := Stub(&v1, 1)
expectVal(t, v1, 1)
defer expectPanic(t, "ResetSingle unstubbed variable", "not been stubbed")
stubs.ResetSingle(&v2)
}
func TestResetTwice(t *testing.T) {
resetVars()
stubs := Stub(&v1, 1)
expectVal(t, v1, 1)
stubs.Reset()
expectVal(t, v1, 100)
stubs.Stub(&v1, 2)
expectVal(t, v1, 2)
stubs.Reset()
expectVal(t, v1, 100)
}
func TestMultipleStubs(t *testing.T) {
resetVars()
stubs := Stub(&v1, 1).Stub(&v2, 2).Stub(&v3, 3)
expectVal(t, v1, 1)
expectVal(t, v2, 2)
expectVal(t, v3, 3)
expectVal(t, v4, 400)
stubs.Stub(&v4, 4)
expectVal(t, v4, 4)
stubs.Reset()
expectVal(t, v1, 100)
expectVal(t, v2, 200)
expectVal(t, v3, 300)
expectVal(t, v4, 400)
}
func TestVarNotPtr(t *testing.T) {
defer expectPanic(t, "Stub non-pointer", "expected to be a pointer")
Stub(v1, 1)
}
func TestTypeMismatch(t *testing.T) {
defer expectPanic(t, "Stub wrong type", "not assignable")
Stub(&v1, "test")
}