1
|
|
|
from __future__ import print_function |
2
|
|
|
|
3
|
|
|
from pytest import raises |
4
|
|
|
from test_pkg1.test_pkg2 import test_mod |
5
|
|
|
|
6
|
|
|
from aspectlib import PY2 |
7
|
|
|
from aspectlib.test import OrderedDict |
8
|
|
|
from aspectlib.test import Story |
9
|
|
|
from aspectlib.test import StoryResultWrapper |
10
|
|
|
from aspectlib.test import _Binds |
11
|
|
|
from aspectlib.test import _format_calls |
12
|
|
|
from aspectlib.test import _Raises |
13
|
|
|
from aspectlib.test import _Returns |
14
|
|
|
from aspectlib.test import mock |
15
|
|
|
from aspectlib.test import record |
16
|
|
|
from aspectlib.utils import PY26 |
17
|
|
|
from aspectlib.utils import repr_ex |
18
|
|
|
|
19
|
|
|
format_calls = lambda calls: ''.join(_format_calls(calls)) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def module_fun(a, b=2): |
23
|
|
|
pass |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def module_fun2(a, b=2): |
27
|
|
|
pass |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
exc = RuntimeError() |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def rfun(): |
34
|
|
|
raise exc |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def nfun(a, b=2): |
38
|
|
|
return a, b |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def test_record(): |
42
|
|
|
fun = record(nfun) |
43
|
|
|
|
44
|
|
|
assert fun(2, 3) == (2, 3) |
45
|
|
|
assert fun(3, b=4) == (3, 4) |
46
|
|
|
assert fun.calls == [ |
47
|
|
|
(None, (2, 3), {}), |
48
|
|
|
(None, (3, ), {'b': 4}), |
49
|
|
|
] |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def test_record_result(): |
53
|
|
|
fun = record(results=True)(nfun) |
54
|
|
|
|
55
|
|
|
assert fun(2, 3) == (2, 3) |
56
|
|
|
assert fun(3, b=4) == (3, 4) |
57
|
|
|
assert fun.calls == [ |
58
|
|
|
(None, (2, 3), {}, (2, 3), None), |
59
|
|
|
(None, (3, ), {'b': 4}, (3, 4), None), |
60
|
|
|
] |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def test_record_exception(): |
64
|
|
|
fun = record(results=True)(rfun) |
65
|
|
|
|
66
|
|
|
raises(RuntimeError, fun) |
67
|
|
|
assert fun.calls == [ |
68
|
|
|
(None, (), {}, None, exc), |
69
|
|
|
] |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def test_record_result_callback(): |
73
|
|
|
calls = [] |
74
|
|
|
|
75
|
|
|
fun = record(results=True, callback=lambda *args: calls.append(args))(nfun) |
76
|
|
|
|
77
|
|
|
assert fun(2, 3) == (2, 3) |
78
|
|
|
assert fun(3, b=4) == (3, 4) |
79
|
|
|
assert calls == [ |
80
|
|
|
(None, 'test_aspectlib_test.nfun', (2, 3), {}, (2, 3), None), |
81
|
|
|
(None, 'test_aspectlib_test.nfun', (3, ), {'b': 4}, (3, 4), None), |
82
|
|
|
] |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def test_record_exception_callback(): |
86
|
|
|
calls = [] |
87
|
|
|
|
88
|
|
|
fun = record(results=True, callback=lambda *args: calls.append(args))(rfun) |
89
|
|
|
|
90
|
|
|
raises(RuntimeError, fun) |
91
|
|
|
assert calls == [ |
92
|
|
|
(None, 'test_aspectlib_test.rfun', (), {}, None, exc), |
93
|
|
|
] |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
def test_record_callback(): |
97
|
|
|
calls = [] |
98
|
|
|
|
99
|
|
|
fun = record(callback=lambda *args: calls.append(args))(nfun) |
100
|
|
|
|
101
|
|
|
assert fun(2, 3) == (2, 3) |
102
|
|
|
assert fun(3, b=4) == (3, 4) |
103
|
|
|
assert calls == [ |
104
|
|
|
(None, 'test_aspectlib_test.nfun', (2, 3), {}), |
105
|
|
|
(None, 'test_aspectlib_test.nfun', (3, ), {'b': 4}), |
106
|
|
|
] |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def test_record_with_no_call(): |
110
|
|
|
called = [] |
111
|
|
|
|
112
|
|
|
@record(iscalled=False) |
113
|
|
|
def fun(): |
114
|
|
|
called.append(True) |
115
|
|
|
|
116
|
|
|
assert fun() is None |
117
|
|
|
assert fun.calls == [ |
118
|
|
|
(None, (), {}), |
119
|
|
|
] |
120
|
|
|
assert called == [] |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
def test_record_with_call(): |
124
|
|
|
called = [] |
125
|
|
|
|
126
|
|
|
@record |
127
|
|
|
def fun(): |
128
|
|
|
called.append(True) |
129
|
|
|
|
130
|
|
|
fun() |
131
|
|
|
assert fun.calls == [ |
132
|
|
|
(None, (), {}), |
133
|
|
|
] |
134
|
|
|
assert called == [True] |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def test_record_as_context(): |
138
|
|
|
with record(module_fun) as history: |
139
|
|
|
module_fun(2, 3) |
140
|
|
|
module_fun(3, b=4) |
141
|
|
|
|
142
|
|
|
assert history.calls == [ |
143
|
|
|
(None, (2, 3), {}), |
144
|
|
|
(None, (3, ), {'b': 4}), |
145
|
|
|
] |
146
|
|
|
del history.calls[:] |
147
|
|
|
|
148
|
|
|
module_fun(2, 3) |
149
|
|
|
module_fun(3, b=4) |
150
|
|
|
assert history.calls == [] |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
def test_bad_mock(): |
154
|
|
|
raises(TypeError, mock) |
155
|
|
|
raises(TypeError, mock, call=False) |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
def test_simple_mock(): |
159
|
|
|
assert "foobar" == mock("foobar")(module_fun)(1) |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def test_mock_no_calls(): |
163
|
|
|
with record(module_fun) as history: |
164
|
|
|
assert "foobar" == mock("foobar")(module_fun)(2) |
165
|
|
|
assert history.calls == [] |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
def test_mock_with_calls(): |
169
|
|
|
with record(module_fun) as history: |
170
|
|
|
assert "foobar" == mock("foobar", call=True)(module_fun)(3) |
171
|
|
|
assert history.calls == [(None, (3,), {})] |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
def test_double_recording(): |
175
|
|
|
with record(module_fun) as history: |
176
|
|
|
with record(module_fun2) as history2: |
177
|
|
|
module_fun(2, 3) |
178
|
|
|
module_fun2(2, 3) |
179
|
|
|
|
180
|
|
|
assert history.calls == [ |
181
|
|
|
(None, (2, 3), {}), |
182
|
|
|
] |
183
|
|
|
del history.calls[:] |
184
|
|
|
assert history2.calls == [ |
185
|
|
|
(None, (2, 3), {}), |
186
|
|
|
] |
187
|
|
|
del history2.calls[:] |
188
|
|
|
|
189
|
|
|
module_fun(2, 3) |
190
|
|
|
assert history.calls == [] |
191
|
|
|
assert history2.calls == [] |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
def test_record_not_iscalled_and_results(): |
195
|
|
|
raises(AssertionError, record, module_fun, iscalled=False, results=True) |
196
|
|
|
record(module_fun, iscalled=False, results=False) |
197
|
|
|
record(module_fun, iscalled=True, results=True) |
198
|
|
|
record(module_fun, iscalled=True, results=False) |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
def test_story_empty_play_noproxy(): |
202
|
|
|
with Story(test_mod).replay(recurse_lock=True, proxy=False, strict=False) as replay: |
203
|
|
|
raises(AssertionError, test_mod.target) |
204
|
|
|
|
205
|
|
|
assert replay._actual == {} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
def test_story_empty_play_proxy(): |
209
|
|
|
assert test_mod.target() is None |
210
|
|
|
raises(TypeError, test_mod.target, 123) |
211
|
|
|
|
212
|
|
|
with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay: |
213
|
|
|
assert test_mod.target() is None |
214
|
|
|
raises(TypeError, test_mod.target, 123) |
215
|
|
|
|
216
|
|
|
assert format_calls(replay._actual) == format_calls(OrderedDict([ |
217
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.target', '', ''), _Returns("None")), |
218
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.target', '123', ''), _Raises(repr_ex(TypeError( |
219
|
|
|
'target() takes no arguments (1 given)' if PY2 else |
220
|
|
|
'target() takes 0 positional arguments but 1 was given', |
221
|
|
|
)))) |
222
|
|
|
])) |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
def test_story_empty_play_noproxy_class(): |
226
|
|
|
with Story(test_mod).replay(recurse_lock=True, proxy=False, strict=False) as replay: |
227
|
|
|
raises(AssertionError, test_mod.Stuff, 1, 2) |
228
|
|
|
|
229
|
|
|
assert replay._actual == {} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
def test_story_empty_play_error_on_init(): |
233
|
|
|
with Story(test_mod).replay(strict=False) as replay: |
234
|
|
|
raises(ValueError, test_mod.Stuff, "error") |
235
|
|
|
print(replay._actual) |
236
|
|
|
assert replay._actual == OrderedDict([ |
237
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "'error'", ''), _Raises('ValueError()')) |
238
|
|
|
]) |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
def test_story_half_play_noproxy_class(): |
242
|
|
|
with Story(test_mod) as story: |
243
|
|
|
obj = test_mod.Stuff(1, 2) |
244
|
|
|
|
245
|
|
|
with story.replay(recurse_lock=True, proxy=False, strict=False): |
246
|
|
|
obj = test_mod.Stuff(1, 2) |
247
|
|
|
raises(AssertionError, obj.mix, 3, 4) |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
def test_xxx(): |
251
|
|
|
with Story(test_mod) as story: |
252
|
|
|
obj = test_mod.Stuff(1, 2) |
253
|
|
|
test_mod.target(1) == 2 |
254
|
|
|
test_mod.target(2) == 3 |
255
|
|
|
test_mod.target(3) ** ValueError |
256
|
|
|
other = test_mod.Stuff(2, 2) |
257
|
|
|
obj.other('a') == other |
258
|
|
|
obj.meth('a') == 'x' |
259
|
|
|
obj = test_mod.Stuff(2, 3) |
260
|
|
|
obj.meth() ** ValueError('crappo') |
261
|
|
|
obj.meth('c') == 'x' |
262
|
|
|
|
263
|
|
|
with story.replay(recurse_lock=True, strict=False) as replay: |
264
|
|
|
obj = test_mod.Stuff(1, 2) |
265
|
|
|
obj.meth('a') |
266
|
|
|
test_mod.target(1) |
267
|
|
|
obj.meth() |
268
|
|
|
test_mod.func(5) |
269
|
|
|
|
270
|
|
|
obj = test_mod.Stuff(4, 4) |
271
|
|
|
obj.meth() |
272
|
|
|
|
273
|
|
|
for k, v in story._calls.items(): |
274
|
|
|
print(k, "=>", v) |
275
|
|
|
print("############## UNEXPECTED ##############") |
276
|
|
|
for k, v in replay._actual.items(): |
277
|
|
|
print(k, "=>", v) |
278
|
|
|
|
279
|
|
|
# TODO |
280
|
|
|
|
281
|
|
|
def test_story_text_helpers(): |
282
|
|
|
with Story(test_mod) as story: |
283
|
|
|
obj = test_mod.Stuff(1, 2) |
284
|
|
|
obj.meth('a') == 'x' |
285
|
|
|
obj.meth('b') == 'y' |
286
|
|
|
obj = test_mod.Stuff(2, 3) |
287
|
|
|
obj.meth('c') == 'z' |
288
|
|
|
test_mod.target(1) == 2 |
289
|
|
|
test_mod.target(2) == 3 |
290
|
|
|
|
291
|
|
|
with story.replay(recurse_lock=True, strict=False) as replay: |
292
|
|
|
obj = test_mod.Stuff(1, 2) |
293
|
|
|
obj.meth('a') |
294
|
|
|
obj.meth() |
295
|
|
|
obj = test_mod.Stuff(4, 4) |
296
|
|
|
obj.meth() |
297
|
|
|
test_mod.func(5) |
298
|
|
|
test_mod.target(1) |
299
|
|
|
|
300
|
|
|
print (replay.missing) |
301
|
|
|
assert replay.missing == """stuff_1.meth('b') == 'y' # returns |
302
|
|
|
stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(2, 3) |
303
|
|
|
stuff_2.meth('c') == 'z' # returns |
304
|
|
|
test_pkg1.test_pkg2.test_mod.target(2) == 3 # returns |
305
|
|
|
""" |
306
|
|
|
print (replay.unexpected) |
307
|
|
|
assert replay.unexpected == """stuff_1.meth() == None # returns |
308
|
|
|
stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(4, 4) |
309
|
|
|
stuff_2.meth() == None # returns |
310
|
|
|
test_pkg1.test_pkg2.test_mod.func(5) == None # returns |
311
|
|
|
""" |
312
|
|
|
print (replay.diff) |
313
|
|
|
if PY26: |
314
|
|
|
assert replay.diff == """--- expected """ """ |
315
|
|
|
+++ actual """ """ |
316
|
|
|
@@ -1,7 +1,7 @@ |
317
|
|
|
stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2) |
318
|
|
|
stuff_1.meth('a') == 'x' # returns |
319
|
|
|
-stuff_1.meth('b') == 'y' # returns |
320
|
|
|
-stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(2, 3) |
321
|
|
|
-stuff_2.meth('c') == 'z' # returns |
322
|
|
|
+stuff_1.meth() == None # returns |
323
|
|
|
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(4, 4) |
324
|
|
|
+stuff_2.meth() == None # returns |
325
|
|
|
+test_pkg1.test_pkg2.test_mod.func(5) == None # returns |
326
|
|
|
test_pkg1.test_pkg2.test_mod.target(1) == 2 # returns |
327
|
|
|
-test_pkg1.test_pkg2.test_mod.target(2) == 3 # returns |
328
|
|
|
""" |
329
|
|
|
else: |
330
|
|
|
assert replay.diff == """--- expected |
331
|
|
|
+++ actual |
332
|
|
|
@@ -1,7 +1,7 @@ |
333
|
|
|
stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2) |
334
|
|
|
stuff_1.meth('a') == 'x' # returns |
335
|
|
|
-stuff_1.meth('b') == 'y' # returns |
336
|
|
|
-stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(2, 3) |
337
|
|
|
-stuff_2.meth('c') == 'z' # returns |
338
|
|
|
+stuff_1.meth() == None # returns |
339
|
|
|
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(4, 4) |
340
|
|
|
+stuff_2.meth() == None # returns |
341
|
|
|
+test_pkg1.test_pkg2.test_mod.func(5) == None # returns |
342
|
|
|
test_pkg1.test_pkg2.test_mod.target(1) == 2 # returns |
343
|
|
|
-test_pkg1.test_pkg2.test_mod.target(2) == 3 # returns |
344
|
|
|
""" |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
def test_story_empty_play_proxy_class_missing_report(): |
348
|
|
|
with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay: |
349
|
|
|
obj = test_mod.Stuff(1, 2) |
350
|
|
|
obj.mix(3, 4) |
351
|
|
|
obj.mix('a', 'b') |
352
|
|
|
raises(ValueError, obj.raises, 123) |
353
|
|
|
obj = test_mod.Stuff(0, 1) |
354
|
|
|
obj.mix('a', 'b') |
355
|
|
|
obj.mix(3, 4) |
356
|
|
|
test_mod.target() |
357
|
|
|
raises(ValueError, test_mod.raises, 'badarg') |
358
|
|
|
raises(ValueError, obj.raises, 123) |
359
|
|
|
test_mod.ThatLONGStuf(1).mix(2) |
360
|
|
|
test_mod.ThatLONGStuf(3).mix(4) |
361
|
|
|
obj = test_mod.ThatLONGStuf(2) |
362
|
|
|
obj.mix() |
363
|
|
|
obj.meth() |
364
|
|
|
obj.mix(10) |
365
|
|
|
|
366
|
|
|
print(repr(replay.diff)) |
367
|
|
|
|
368
|
|
|
if PY26: |
369
|
|
|
assert replay.diff == """--- expected """ """ |
370
|
|
|
+++ actual """ """ |
371
|
|
|
@@ -1,0 +1,18 @@ |
372
|
|
|
+stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2) |
373
|
|
|
+stuff_1.mix(3, 4) == (1, 2, 3, 4) # returns |
374
|
|
|
+stuff_1.mix('a', 'b') == (1, 2, 'a', 'b') # returns |
375
|
|
|
+stuff_1.raises(123) ** ValueError((123,),) # raises |
376
|
|
|
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(0, 1) |
377
|
|
|
+stuff_2.mix('a', 'b') == (0, 1, 'a', 'b') # returns |
378
|
|
|
+stuff_2.mix(3, 4) == (0, 1, 3, 4) # returns |
379
|
|
|
+test_pkg1.test_pkg2.test_mod.target() == None # returns |
380
|
|
|
+test_pkg1.test_pkg2.test_mod.raises('badarg') ** ValueError(('badarg',),) # raises |
381
|
|
|
+stuff_2.raises(123) ** ValueError((123,),) # raises |
382
|
|
|
+that_long_stuf_1 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(1) |
383
|
|
|
+that_long_stuf_1.mix(2) == (1, 2) # returns |
384
|
|
|
+that_long_stuf_2 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(3) |
385
|
|
|
+that_long_stuf_2.mix(4) == (3, 4) # returns |
386
|
|
|
+that_long_stuf_3 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(2) |
387
|
|
|
+that_long_stuf_3.mix() == (2,) # returns |
388
|
|
|
+that_long_stuf_3.meth() == None # returns |
389
|
|
|
+that_long_stuf_3.mix(10) == (2, 10) # returns |
390
|
|
|
""" |
391
|
|
|
else: |
392
|
|
|
assert replay.diff == """--- expected |
393
|
|
|
+++ actual |
394
|
|
|
@@ -0,0 +1,18 @@ |
395
|
|
|
+stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2) |
396
|
|
|
+stuff_1.mix(3, 4) == (1, 2, 3, 4) # returns |
397
|
|
|
+stuff_1.mix('a', 'b') == (1, 2, 'a', 'b') # returns |
398
|
|
|
+stuff_1.raises(123) ** ValueError((123,),) # raises |
399
|
|
|
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(0, 1) |
400
|
|
|
+stuff_2.mix('a', 'b') == (0, 1, 'a', 'b') # returns |
401
|
|
|
+stuff_2.mix(3, 4) == (0, 1, 3, 4) # returns |
402
|
|
|
+test_pkg1.test_pkg2.test_mod.target() == None # returns |
403
|
|
|
+test_pkg1.test_pkg2.test_mod.raises('badarg') ** ValueError(('badarg',),) # raises |
404
|
|
|
+stuff_2.raises(123) ** ValueError((123,),) # raises |
405
|
|
|
+that_long_stuf_1 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(1) |
406
|
|
|
+that_long_stuf_1.mix(2) == (1, 2) # returns |
407
|
|
|
+that_long_stuf_2 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(3) |
408
|
|
|
+that_long_stuf_2.mix(4) == (3, 4) # returns |
409
|
|
|
+that_long_stuf_3 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(2) |
410
|
|
|
+that_long_stuf_3.mix() == (2,) # returns |
411
|
|
|
+that_long_stuf_3.meth() == None # returns |
412
|
|
|
+that_long_stuf_3.mix(10) == (2, 10) # returns |
413
|
|
|
""" |
414
|
|
|
|
415
|
|
|
|
416
|
|
View Code Duplication |
def test_story_empty_play_proxy_class(): |
|
|
|
|
417
|
|
|
assert test_mod.Stuff(1, 2).mix(3, 4) == (1, 2, 3, 4) |
418
|
|
|
|
419
|
|
|
with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay: |
420
|
|
|
obj = test_mod.Stuff(1, 2) |
421
|
|
|
assert obj.mix(3, 4) == (1, 2, 3, 4) |
422
|
|
|
assert obj.mix('a', 'b') == (1, 2, 'a', 'b') |
423
|
|
|
|
424
|
|
|
raises(TypeError, obj.meth, 123) |
425
|
|
|
|
426
|
|
|
obj = test_mod.Stuff(0, 1) |
427
|
|
|
assert obj.mix('a', 'b') == (0, 1, 'a', 'b') |
428
|
|
|
assert obj.mix(3, 4) == (0, 1, 3, 4) |
429
|
|
|
|
430
|
|
|
raises(TypeError, obj.meth, 123) |
431
|
|
|
|
432
|
|
|
assert format_calls(replay._actual) == format_calls(OrderedDict([ |
433
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "1, 2", ''), _Binds('stuff_1')), |
434
|
|
|
(('stuff_1', 'mix', "3, 4", ''), _Returns("(1, 2, 3, 4)")), |
435
|
|
|
(('stuff_1', 'mix', "'a', 'b'", ''), _Returns("(1, 2, 'a', 'b')")), |
436
|
|
|
(('stuff_1', 'meth', "123", ''), _Raises(repr_ex(TypeError( |
437
|
|
|
'meth() takes exactly 1 argument (2 given)' if PY2 else |
438
|
|
|
'meth() takes 1 positional argument but 2 were given' |
439
|
|
|
)))), |
440
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "0, 1", ''), _Binds('stuff_2')), |
441
|
|
|
(('stuff_2', 'mix', "'a', 'b'", ''), _Returns("(0, 1, 'a', 'b')")), |
442
|
|
|
(('stuff_2', 'mix', "3, 4", ''), _Returns("(0, 1, 3, 4)")), |
443
|
|
|
(('stuff_2', 'meth', "123", ''), _Raises(repr_ex(TypeError( |
444
|
|
|
'meth() takes exactly 1 argument (2 given)' if PY2 else |
445
|
|
|
'meth() takes 1 positional argument but 2 were given' |
446
|
|
|
)))) |
447
|
|
|
])) |
448
|
|
|
|
449
|
|
|
|
450
|
|
View Code Duplication |
def test_story_half_play_proxy_class(): |
|
|
|
|
451
|
|
|
assert test_mod.Stuff(1, 2).mix(3, 4) == (1, 2, 3, 4) |
452
|
|
|
|
453
|
|
|
with Story(test_mod) as story: |
454
|
|
|
obj = test_mod.Stuff(1, 2) |
455
|
|
|
obj.mix(3, 4) == (1, 2, 3, 4) |
456
|
|
|
|
457
|
|
|
with story.replay(recurse_lock=True, proxy=True, strict=False) as replay: |
458
|
|
|
obj = test_mod.Stuff(1, 2) |
459
|
|
|
assert obj.mix(3, 4) == (1, 2, 3, 4) |
460
|
|
|
assert obj.meth() is None |
461
|
|
|
|
462
|
|
|
raises(TypeError, obj.meth, 123) |
463
|
|
|
|
464
|
|
|
obj = test_mod.Stuff(0, 1) |
465
|
|
|
assert obj.mix('a', 'b') == (0, 1, 'a', 'b') |
466
|
|
|
assert obj.mix(3, 4) == (0, 1, 3, 4) |
467
|
|
|
|
468
|
|
|
raises(TypeError, obj.meth, 123) |
469
|
|
|
assert replay.unexpected == format_calls(OrderedDict([ |
470
|
|
|
(('stuff_1', 'meth', '', ''), _Returns('None')), |
471
|
|
|
(('stuff_1', 'meth', '123', ''), _Raises(repr_ex(TypeError( |
472
|
|
|
'meth() takes exactly 1 argument (2 given)' if PY2 else |
473
|
|
|
'meth() takes 1 positional argument but 2 were given' |
474
|
|
|
)))), |
475
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.Stuff', '0, 1', ''), _Binds("stuff_2")), |
476
|
|
|
(('stuff_2', 'mix', "'a', 'b'", ''), _Returns("(0, 1, 'a', 'b')")), |
477
|
|
|
(('stuff_2', 'mix', '3, 4', ''), _Returns('(0, 1, 3, 4)')), |
478
|
|
|
(('stuff_2', 'meth', '123', ''), _Raises(repr_ex(TypeError( |
479
|
|
|
'meth() takes exactly 1 argument (2 given)' if PY2 else |
480
|
|
|
'meth() takes 1 positional argument but 2 were given' |
481
|
|
|
)))) |
482
|
|
|
])) |
483
|
|
|
|
484
|
|
|
|
485
|
|
View Code Duplication |
def test_story_full_play_noproxy(): |
|
|
|
|
486
|
|
|
with Story(test_mod) as story: |
487
|
|
|
test_mod.target(123) == 'foobar' |
488
|
|
|
test_mod.target(1234) ** ValueError |
489
|
|
|
|
490
|
|
|
with story.replay(recurse_lock=True, proxy=False, strict=False, dump=False) as replay: |
491
|
|
|
raises(AssertionError, test_mod.target) |
492
|
|
|
assert test_mod.target(123) == 'foobar' |
493
|
|
|
raises(ValueError, test_mod.target, 1234) |
494
|
|
|
|
495
|
|
|
assert replay.unexpected == "" |
496
|
|
|
|
497
|
|
|
|
498
|
|
View Code Duplication |
def test_story_full_play_noproxy_dump(): |
|
|
|
|
499
|
|
|
with Story(test_mod) as story: |
500
|
|
|
test_mod.target(123) == 'foobar' |
501
|
|
|
test_mod.target(1234) ** ValueError |
502
|
|
|
|
503
|
|
|
with story.replay(recurse_lock=True, proxy=False, strict=False, dump=True) as replay: |
504
|
|
|
raises(AssertionError, test_mod.target) |
505
|
|
|
assert test_mod.target(123) == 'foobar' |
506
|
|
|
raises(ValueError, test_mod.target, 1234) |
507
|
|
|
|
508
|
|
|
assert replay.unexpected == "" |
509
|
|
|
|
510
|
|
|
|
511
|
|
View Code Duplication |
def test_story_full_play_proxy(): |
|
|
|
|
512
|
|
|
with Story(test_mod) as story: |
513
|
|
|
test_mod.target(123) == 'foobar' |
514
|
|
|
test_mod.target(1234) ** ValueError |
515
|
|
|
|
516
|
|
|
with story.replay(recurse_lock=True, proxy=True, strict=False) as replay: |
517
|
|
|
assert test_mod.target() is None |
518
|
|
|
assert test_mod.target(123) == 'foobar' |
519
|
|
|
raises(ValueError, test_mod.target, 1234) |
520
|
|
|
raises(TypeError, test_mod.target, 'asdf') |
521
|
|
|
|
522
|
|
|
assert replay.unexpected == format_calls(OrderedDict([ |
523
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.target', '', ''), _Returns("None")), |
524
|
|
|
((None, 'test_pkg1.test_pkg2.test_mod.target', "'asdf'", ''), _Raises(repr_ex(TypeError( |
525
|
|
|
'target() takes no arguments (1 given)' |
526
|
|
|
if PY2 |
527
|
|
|
else 'target() takes 0 positional arguments but 1 was given',) |
528
|
|
|
))) |
529
|
|
|
])) |
530
|
|
|
|
531
|
|
|
|
532
|
|
|
def test_story_result_wrapper(): |
533
|
|
|
x = StoryResultWrapper(lambda *a: None) |
534
|
|
|
raises(AttributeError, setattr, x, 'stuff', 1) |
535
|
|
|
raises(AttributeError, getattr, x, 'stuff') |
536
|
|
|
raises(TypeError, lambda: x >> 2) |
537
|
|
|
raises(TypeError, lambda: x << 1) |
538
|
|
|
raises(TypeError, lambda: x > 1) |
539
|
|
|
x == 1 |
540
|
|
|
x ** Exception() |
541
|
|
|
|
542
|
|
|
|
543
|
|
|
def test_story_result_wrapper_bad_exception(): |
544
|
|
|
x = StoryResultWrapper(lambda *a: None) |
545
|
|
|
raises(RuntimeError, lambda: x ** 1) |
546
|
|
|
x ** Exception |
547
|
|
|
x ** Exception('boom!') |
548
|
|
|
|
549
|
|
|
|
550
|
|
|
def test_story_create(): |
551
|
|
|
with Story(test_mod) as story: |
552
|
|
|
test_mod.target('a', 'b', 'c') == 'abc' |
553
|
|
|
test_mod.target() ** Exception |
554
|
|
|
test_mod.target(1, 2, 3) == 'foobar' |
555
|
|
|
obj = test_mod.Stuff('stuff') |
556
|
|
|
assert isinstance(obj, test_mod.Stuff) |
557
|
|
|
obj.meth('other', 1, 2) == 123 |
558
|
|
|
obj.mix('other') == 'mixymix' |
559
|
|
|
#from pprint import pprint as print |
560
|
|
|
#print (dict(story._calls)) |
561
|
|
|
assert dict(story._calls) == { |
562
|
|
|
(None, 'test_pkg1.test_pkg2.test_mod.Stuff', "'stuff'", ''): _Binds('stuff_1'), |
563
|
|
|
('stuff_1', 'meth', "'other', 1, 2", ''): _Returns("123"), |
564
|
|
|
('stuff_1', 'mix', "'other'", ''): _Returns("'mixymix'"), |
565
|
|
|
(None, 'test_pkg1.test_pkg2.test_mod.target', '', ''): _Raises("Exception"), |
566
|
|
|
(None, 'test_pkg1.test_pkg2.test_mod.target', "1, 2, 3", ''): _Returns("'foobar'"), |
567
|
|
|
(None, 'test_pkg1.test_pkg2.test_mod.target', "'a', 'b', 'c'", ''): _Returns("'abc'"), |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
def xtest_story_empty_play_proxy_class_dependencies(): |
571
|
|
|
with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay: |
572
|
|
|
obj = test_mod.Stuff(1, 2) |
573
|
|
|
other = obj.other('x') |
574
|
|
|
raises(ValueError, other.raises, 'badarg') |
575
|
|
|
other.mix(3, 4) |
576
|
|
|
obj = test_mod.Stuff(0, 1) |
577
|
|
|
obj.mix(3, 4) |
578
|
|
|
other = obj.other(2) |
579
|
|
|
other.mix(3, 4) |
580
|
|
|
|
581
|
|
|
print(repr(replay.diff)) |
582
|
|
|
|
583
|
|
|
assert replay.diff == "" |
584
|
|
|
|