-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-deride.py
More file actions
338 lines (241 loc) · 9.08 KB
/
test-deride.py
File metadata and controls
338 lines (241 loc) · 9.08 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import unittest
from pyderide.deride import Deride, ObjectKey
class Logger:
@staticmethod
def log(msg):
Logger.messages.append(msg)
@staticmethod
def has_message(msg):
return msg in Logger.messages
@staticmethod
def clear():
Logger.messages = []
Logger.messages = []
class Person(object):
def __init__(self, name):
self.name = name
def greet(self, other):
return 'hello ' + other.name
def pay(self, other, amount):
other.credit_with(amount)
def credit_with(self, amount):
pass
class TestObjectKey(unittest.TestCase):
def test_returns_key(self):
key1 = ObjectKey.value(1, 2, 3, a=4)
key2 = ObjectKey.value(2, 3, 4, a=5)
self.assertNotEquals(key1, key2)
class TestDeride(unittest.TestCase):
def setUp(self):
self.deride = Deride()
Logger.clear()
def test_called_times(self):
andy = self.deride.wrap(Person('Andy'))
bob = self.deride.wrap(Person('Bob'))
self.assertEquals('hello Bob', andy.greet(bob))
andy.expect.greet.called.times(1)
def test_called_times_fails(self):
andy = self.deride.wrap(Person('Andy'))
bob = self.deride.wrap(Person('Bob'))
self.assertEquals('hello Bob', andy.greet(bob))
with self.assertRaises(AssertionError):
andy.expect.greet.called.times(2)
def test_called_once(self):
andy = self.deride.wrap(Person('Andy'))
bob = self.deride.wrap(Person('Bob'))
self.assertEquals('hello Bob', andy.greet(bob))
andy.expect.greet.called.once()
def test_called_once_fails(self):
andy = self.deride.wrap(Person('Andy'))
bob = self.deride.wrap(Person('Bob'))
self.assertEquals('hello Bob', andy.greet(bob))
self.assertEquals('hello Bob', andy.greet(bob))
with self.assertRaises(AssertionError):
andy.expect.greet.called.once()
def test_called_twice(self):
andy = self.deride.wrap(Person('Andy'))
bob = self.deride.wrap(Person('Bob'))
self.assertEquals('hello Bob', andy.greet(bob))
self.assertEquals('hello Bob', andy.greet(bob))
andy.expect.greet.called.twice()
def test_called_twice_fails(self):
andy = self.deride.wrap(Person('Andy'))
bob = self.deride.wrap(Person('Bob'))
self.assertEquals('hello Bob', andy.greet(bob))
with self.assertRaises(AssertionError):
andy.expect.greet.called.twice()
def test_called_range(self):
bob = self.deride.wrap(Person('Bob'))
alice = Person('Alice')
bob.greet(alice)
bob.greet(alice)
bob.greet(alice)
bob.expect.greet.called.lt(4)
bob.expect.greet.called.lte(3)
bob.expect.greet.called.gt(2)
bob.expect.greet.called.gte(3)
def test_called_range_fails(self):
bob = self.deride.wrap(Person('Bob'))
alice = Person('Alice')
bob.greet(alice)
bob.greet(alice)
bob.greet(alice)
with self.assertRaises(AssertionError):
bob.expect.greet.called.lt(3)
with self.assertRaises(AssertionError):
bob.expect.greet.called.lte(2)
with self.assertRaises(AssertionError):
bob.expect.greet.called.gt(3)
with self.assertRaises(AssertionError):
bob.expect.greet.called.gte(4)
def test_called_never(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
bob.expect.greet.called.never()
def test_called_never_fails(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
bob.greet(Person('alice'))
with self.assertRaises(AssertionError):
bob.expect.greet.called.never()
def test_reset_call_counts(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
bob.greet(alice)
bob.expect.greet.called.once()
bob.expect.reset()
bob.expect.greet.called.never()
def test_with_arg(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
bob.greet(alice)
bob.greet(bob)
bob.expect.greet.called.with_arg(bob)
def test_with_arg_fails(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
bob.greet(alice)
bob.greet(bob)
with self.assertRaises(AssertionError):
bob.expect.greet.called.with_arg(Person('jeremy'))
def test_with_args(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
bob.pay(alice, 25.00)
bob.expect.pay.called.with_args(25.00, alice)
def test_with_args_fails(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
bob.pay(alice, 25.00)
with self.assertRaises(AssertionError):
bob.expect.pay.called.with_args(35.00, alice)
def test_with_args_strict(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
bob.pay(alice, 25.00)
bob.expect.pay.called.with_args_strict(alice, 25.00)
def test_with_args_strict_fails(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
bob.pay(alice, 25.00)
with self.assertRaises(AssertionError):
bob.expect.pay.called.with_args_strict(25.00, alice)
def test_to_do_this(self):
bob = Person('bob')
bob = self.deride.wrap(bob)
alice = Person('alice')
def shout(other):
return 'yo ' + other.name
bob.setup.greet.to_do_this(shout)
result = bob.greet(alice)
self.assertEquals(result, 'yo alice')
def test_to_return(self):
bob = Person('bob')
alice = Person('alice')
bob = self.deride.wrap(bob)
bob.setup.greet.to_return('foobar')
result = bob.greet(alice)
self.assertEquals(result, 'foobar')
def test_to_raise(self):
bob = Person('bob')
alice = Person('alice')
bob = self.deride.wrap(bob)
bob.setup.greet.to_raise(Exception('something went wrong'))
with self.assertRaises(Exception):
bob.greet(alice)
def test_to_intercept_with(self):
bob = Person('bob')
alice = Person('alice')
bob = self.deride.wrap(bob)
def intercept(*args, **kwargs):
del args, kwargs
Logger.log('something')
bob.setup.greet.to_intercept_with(intercept)
result = bob.greet(alice)
self.assertEquals(result, 'hello alice')
self.assertTrue(Logger.has_message('something'))
def test_specific_to_return(self):
bob = Person('bob')
alice = Person('alice')
carol = Person('carol')
bob = self.deride.wrap(bob)
bob.setup.greet.when(carol).to_return('yo yo yo')
self.assertEquals(bob.greet(alice), 'hello alice')
self.assertEquals(bob.greet(carol), 'yo yo yo')
def test_specific_to_do_this(self):
bob = Person('bob')
alice = Person('alice')
bob = self.deride.wrap(bob)
def one_yo(other):
return 'yo {name}'.format(name=other.name)
def two_yo(other):
return 'yo yo {name}'.format(name=other.name)
bob.setup.greet.when(alice).to_do_this(two_yo)
bob.setup.greet.to_do_this(one_yo)
self.assertEquals(bob.greet(alice), 'yo yo alice')
self.assertEquals(bob.greet(bob), 'yo bob')
def test_specific_to_raise(self):
bob = Person('bob')
alice = Person('alice')
bob = self.deride.wrap(bob)
bob.setup.greet.when(alice).to_raise(Exception('something went wrong'))
with self.assertRaises(Exception):
bob.greet(alice)
self.assertEquals(bob.greet(bob), 'hello bob')
def test_specific_to_intercept_with(self):
bob = Person('bob')
alice = Person('alice')
bob = self.deride.wrap(bob)
def intercept_for_bob(*args, **kwargs):
del args, kwargs
Logger.log('bob')
def intercept_for_alice(*args, **kwargs):
del args, kwargs
Logger.log('alice')
bob.setup.greet.to_intercept_with(intercept_for_bob)
bob.setup.greet.when(alice).to_intercept_with(intercept_for_alice)
self.assertEquals(bob.greet(alice), 'hello alice')
self.assertEquals(bob.greet(bob), 'hello bob')
self.assertTrue(Logger.has_message('alice'))
self.assertTrue(Logger.has_message('bob'))
self.assertEqual(len(Logger.messages), 2)
def test_invocations_access(self):
bob = Person('bob')
alice = Person('alice')
jack = Person('jack')
bob = self.deride.wrap(bob)
bob.greet(jack)
bob.greet(alice)
bob.greet(bob)
bob.expect.greet.invocation(0).with_arg(jack)
bob.expect.greet.invocation(1).with_arg(alice)
bob.expect.greet.invocation(2).with_arg(bob)
if __name__ == '__main__':
unittest.main()