forked from yoo/jasmine
/
jasmine-spec.go
244 lines (224 loc) · 4.9 KB
/
jasmine-spec.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package jasmine
import "time"
// +testing
func caller(f func()) {
f()
}
func RunJasmineTests() {
Describe("testig basic jasmine functions", func() {
Describe("nesed siute", func() {
XIt("schould not be called", func() {
Expect(1).ToBe(2)
})
})
XDescribe("this suite should not be called", func() {
It("should not be called", func() {
Expect(1).ToBe(2)
})
})
})
Describe("testing basic expectations", func() {
It("test spec ToBe", func() {
Expect(true).ToBe(true)
Expect(true).Not.ToBe(false)
})
It("test spec ToEqual", func() {
Expect(1).ToEqual(1)
Expect(1).Not.ToEqual(2)
})
It("test spec ToMatch", func() {
Expect("foo").ToMatch("foo")
Expect("foo").Not.ToMatch("bar")
})
It("test spec ToBeDefined", func() {
Expect(1).ToBeDefined()
})
It("test spec ToBeNull", func() {
Expect(nil).ToBeNull()
})
It("test spec ToBeTruthy", func() {
Expect(true).ToBeTruthy()
Expect(false).Not.ToBeTruthy()
})
It("test spec ToBeFalsy", func() {
Expect(false).ToBeFalsy()
Expect(true).Not.ToBeFalsy()
})
It("test spec ToContain", func() {
array := []int{1, 2, 3}
exp := Expect(array)
exp.ToContain(2)
exp.Not.ToContain(5)
})
It("test spec ToBeLessThan", func() {
Expect(1).ToBeLessThan(4)
})
It("test spec ToBeGreaterThan", func() {
Expect(4).ToBeGreaterThan(1)
})
})
Describe("testing setup and teardown methods", func() {
var a = 0
BeforeEach(func() {
a = 1
})
AfterEach(func() {
a = 2
})
It("test setup and teardown", func() {
Expect(a).ToBe(1)
})
})
Describe("testing spy like functinallity with ItAsync", func() {
var ch chan bool
BeforeEach(func() {
ch = make(chan bool)
})
ItAsync("test toHaveBeenCalled", func(done func()) {
caller(func() {
go func() {
ch <- true
}()
})
go func() {
b := <-ch
Expect(b).ToBeTruthy()
done()
}()
})
})
Describe("testing sync functions with blocked channels", func() {
SetDefaultTimeoutInterval(10)
BeforeEach(func() {
var c = make(chan bool)
go func() {
time.AfterFunc(time.Millisecond*3, func() {
c <- true
})
}()
<-c
})
It("testing It, BeforeEach, AfterEach", func() {
var c = make(chan bool)
go func() {
time.AfterFunc(time.Millisecond*3, func() {
c <- true
})
}()
<-c
})
AfterEach(func() {
var c = make(chan bool)
go func() {
time.AfterFunc(time.Millisecond*3, func() {
c <- true
})
}()
<-c
})
})
Describe("testing async functions", func() {
SetDefaultTimeoutInterval(10)
BeforeEachAsync(func(done func()) {
var c = make(chan bool)
go func() {
time.AfterFunc(time.Millisecond*3, func() {
c <- true
})
}()
<-c
done()
})
ItAsync("ItAsync with BeforeEachAsync and AfterEachAsync", func(done func()) {
var c = make(chan bool)
go func() {
time.AfterFunc(time.Millisecond*3, func() {
c <- true
})
}()
<-c
done()
})
AfterEachAsync(func(done func()) {
var c = make(chan bool)
go func() {
time.AfterFunc(time.Millisecond*3, func() {
c <- true
})
}()
<-c
done()
})
})
Describe("Spy", func() {
var spy1 *Spy
BeforeEach(func() {
spy1 = CreateSpy("Test", func(arg1 int) int {
return arg1 + 2
})
})
It("And return value", func() {
spy1.And.ReturnValue("val1")
Expect(spy1.Invoke()).ToBe("val1")
})
It("And call fake", func() {
spy1.And.CallFake(func(arg1 int, arg2 int) int {
return arg1 + arg2
})
Expect(spy1.Invoke(1, 2)).ToBe(3)
})
It("And callThrough", func() {
spy1.And.CallThrough()
Expect(spy1.Invoke(2)).ToBe(4)
})
It("And stub", func() {
spy1.And.Stub()
Expect(spy1.Invoke(1, 2, 3)).ToBeUndefined()
})
It("Calls any()", func() {
Expect(spy1.Calls.Any()).ToBeFalsy()
spy1.Invoke()
Expect(spy1.Calls.Any()).ToBeTruthy()
})
It("Calls Count()", func() {
spy1.Invoke()
spy1.Invoke()
Expect(spy1.Calls.Count()).ToBe(2)
})
It("Calls ArgsFor()", func() {
spy1.Invoke(1, 2)
spy1.Invoke(3, 4)
Expect(spy1.Calls.ArgsFor(0)[0]).ToBe(1)
Expect(spy1.Calls.ArgsFor(1)).ToEqual([]interface{}{3, 4})
})
It("Calls AllArgs()", func() {
spy1.Invoke(5, "6")
spy1.Invoke(7)
Expect(spy1.Calls.AllArgs()).ToEqual([]interface{}{[]interface{}{5, "6"}, []interface{}{7}})
})
It("Calls All()", func() {
spy1.Invoke(1)
Expect(spy1.Calls.All()[0].args).ToEqual([]interface{}{1})
})
It("Calls MostRecent", func() {
spy1.Invoke(1)
spy1.Invoke(2)
spy1.Invoke(3)
Expect(spy1.Calls.MostRecent().args).ToEqual([]interface{}{3})
})
It("Calls First", func() {
spy1.Invoke(4)
spy1.Invoke(2)
spy1.Invoke(3)
Expect(spy1.Calls.First().args).ToEqual([]interface{}{4})
})
It("Calls First", func() {
spy1.Invoke(4)
spy1.Invoke(4)
Expect(spy1.Calls.Count()).ToBe(2)
spy1.Calls.Reset()
spy1.Invoke(2)
Expect(spy1.Calls.Count()).ToBe(1)
})
})
}