Debian Codegraph @

debian/go/src/github.com/prashantv/gostub/gostub_test.go

raw
1 package gostub
2
3 import "testing"
4
5 // Variables used in stubbing.
6 var v1, v2, v3, v4 int
7
8 // resetVars is used to reset the variables used in stubbing tests to their default values.
9 func resetVars() {
10 v1 = 100
11 v2 = 200
12 v3 = 300
13 v4 = 400
14 }
15
16 func TestStub(t *testing.T) {
17 resetVars()
18
19 stubs := Stub(&v1, 1)
20
21 if v1 != 1 {
22 t.Errorf("expected")
23 }
24 expectVal(t, v1, 1)
25 stubs.Reset()
26 expectVal(t, v1, 100)
27 }
28
29 func TestRestub(t *testing.T) {
30 resetVars()
31
32 stubs := Stub(&v1, 1)
33 expectVal(t, v1, 1)
34 stubs.Stub(&v1, 2)
35 expectVal(t, v1, 2)
36 stubs.Reset()
37 expectVal(t, v1, 100)
38 }
39
40 func TestResetSingle(t *testing.T) {
41 resetVars()
42
43 stubs := Stub(&v1, 1).Stub(&v2, 2)
44 expectVal(t, v1, 1)
45 expectVal(t, v2, 2)
46
47 stubs.ResetSingle(&v1)
48 expectVal(t, v1, 100)
49 expectVal(t, v2, 2)
50
51 stubs.Reset()
52 expectVal(t, v1, 100)
53 expectVal(t, v2, 200)
54 }
55
56 func TestResetSingleNotStubbed(t *testing.T) {
57 resetVars()
58
59 stubs := Stub(&v1, 1)
60 expectVal(t, v1, 1)
61
62 defer expectPanic(t, "ResetSingle unstubbed variable", "not been stubbed")
63 stubs.ResetSingle(&v2)
64 }
65
66 func TestResetTwice(t *testing.T) {
67 resetVars()
68
69 stubs := Stub(&v1, 1)
70 expectVal(t, v1, 1)
71
72 stubs.Reset()
73 expectVal(t, v1, 100)
74
75 stubs.Stub(&v1, 2)
76 expectVal(t, v1, 2)
77
78 stubs.Reset()
79 expectVal(t, v1, 100)
80 }
81
82 func TestMultipleStubs(t *testing.T) {
83 resetVars()
84
85 stubs := Stub(&v1, 1).Stub(&v2, 2).Stub(&v3, 3)
86 expectVal(t, v1, 1)
87 expectVal(t, v2, 2)
88 expectVal(t, v3, 3)
89 expectVal(t, v4, 400)
90
91 stubs.Stub(&v4, 4)
92 expectVal(t, v4, 4)
93
94 stubs.Reset()
95 expectVal(t, v1, 100)
96 expectVal(t, v2, 200)
97 expectVal(t, v3, 300)
98 expectVal(t, v4, 400)
99 }
100
101 func TestVarNotPtr(t *testing.T) {
102 defer expectPanic(t, "Stub non-pointer", "expected to be a pointer")
103 Stub(v1, 1)
104 }
105
106 func TestTypeMismatch(t *testing.T) {
107 defer expectPanic(t, "Stub wrong type", "not assignable")
108 Stub(&v1, "test")
109 }