-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocka_test.go
142 lines (110 loc) · 3.71 KB
/
mocka_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 mocka
import (
"errors"
"log"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("mocka", func() {
Describe("Function", func() {
var (
callCount int
fn func(str string, num int) (int, error)
failTestReporter *mockTestReporter
)
BeforeEach(func() {
failTestReporter = &mockTestReporter{}
callCount = 0
fn = func(str string, num int) (int, error) {
callCount++
return len(str) + num, nil
}
})
It("reports an error if passed a nil as the function pointer", func() {
stub := Function(failTestReporter, nil)
Expect(stub).To(BeNil())
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected the second argument to be a pointer to a function, but received a nil",
}))
})
It("reports an error if a non-pointer value is passed as the function pointer", func() {
stub := Function(failTestReporter, 42)
Expect(stub).To(BeNil())
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected the second argument to be a pointer to a function, but received a int",
}))
})
It("reports an error if a non-function value is passed as the function pointer", func() {
num := 42
stub := Function(failTestReporter, &num)
Expect(stub).To(BeNil())
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected the second argument to be a pointer to a function, but received a pointer to a int",
}))
})
It("reports an error supplied out parameters are not of the same type", func() {
stub := Function(failTestReporter, &fn, "42", nil)
Expect(stub).To(BeNil())
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected return values of type (int, error), but received (string, <nil>)",
}))
})
It("reports an error if cloneValue returns an error", func() {
_cloneValue = func(interface{}, interface{}) error {
return errors.New("Ope")
}
defer func() {
_cloneValue = cloneValue
}()
stub := Function(failTestReporter, &fn, 42, nil)
Expect(stub).To(BeNil())
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: could not clone function pointer to new memory address: Ope",
}))
})
It("returns a stub with a reference to the original function", func() {
stub := Function(GinkgoT(), &fn, 42, nil)
Expect(stub).ToNot(BeNil())
Expect(stub.originalFunc).ToNot(BeNil())
_, _ = stub.originalFunc.(func(str string, num int) (int, error))("", 0)
Expect(callCount).To(Equal(1))
})
It("returns a stub with properties initialized with zero values", func() {
stub := Function(GinkgoT(), &fn, 42, nil)
Expect(stub).ToNot(BeNil())
Expect(stub.calls).To(BeNil())
Expect(stub.customArgs).To(BeNil())
})
It("returns a stub with outParameters as supplied", func() {
stub := Function(GinkgoT(), &fn, 42, nil)
Expect(stub).ToNot(BeNil())
Expect(stub.outParameters).To(Equal([]interface{}{42, nil}))
})
})
Describe("CreateSandbox", func() {
It("returns a sandbox with stub assigned as nil", func() {
s := CreateSandbox(GinkgoT())
Expect(s).ToNot(BeNil())
Expect(s.stubs).To(BeNil())
})
})
Describe("ensureTestReporter", func() {
It("calls exit if testReporter is nil", func() {
var message string
exitFn := func(args ...interface{}) {
Expect(args).To(HaveLen(1))
msg, ok := args[0].(string)
Expect(ok).To(BeTrue())
message = msg
}
actual := ensureTestReporter(nil, exitFn)
Expect(actual).To(BeNil())
Expect(message).To(Equal("mocka: test reporter required to fail tests"))
})
It("returns the provided testReporter", func() {
expected := GinkgoT()
actual := ensureTestReporter(expected, log.Fatal)
Expect(actual).To(Equal(expected))
})
})
})