forked from urnest/urnest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxn.py.test
245 lines (222 loc) · 5.87 KB
/
xn.py.test
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
#!/usr/bin/env python3
# coding: utf-8
# Copyright (c) 2018 Trevor Taylor
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that all
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
from sys import path
from os.path import dirname
if path[0]==dirname(__file__): path.pop(0)
from xju.xn import in_context,readable_repr,AllFailed,in_function_context,Xn,capitalise,first_para_of, first_line_of
from xju.assert_ import Assert
from typing import cast
import io
import sys
from xju.assert_ import Assert
def test1(prog=sys.argv[0]):
def a():
raise Exception('fred')
def b():
a()
pass
def c():
b()
pass
try:
c()
except:
e=in_context('jock')
Assert(readable_repr(e))=='''\
Failed to jock because
fred.\
'''
Assert(str(e)).matches('''\
failed to jock because
[^:]*:40: failed to c[(][)] because
[^:]*:37: failed to b[(][)] because
[^:]*:34: failed to a[(][)] because
[^:]*:32: fred'''.format(**vars()))
pass
pass
class E(Exception):
def __str__(self):
return 'deadline reached'
pass
def test2():
try:
try:
raise E()
except:
raise in_context('await message') from None
pass
except E as e:
Assert(readable_repr(e))=='''\
Failed to await message because
deadline reached.'''
pass
pass
def test3():
try:
try:
try:
raise E()
except:
raise in_context('await message') from None
pass
except E as e:
try:
raise Exception('open failed')
except:
raise AllFailed([e,in_context('read default file')]) from None
pass
pass
except:
Assert(readable_repr(in_context('get content')))=='''\
Failed to get content because
- failed to await message because
deadline reached; and
- failed to read default file because
open failed.'''
pass
pass
from inspect import currentframe, getframeinfo
class _Scope():
def __init__(self,s):
self.s=s
cf=currentframe()
assert cf is not None
fb=cf.f_back
assert fb is not None
frameinfo = getframeinfo(fb)
self.fl=(frameinfo.filename, frameinfo.lineno)
pass
def __enter__(self):
pass
def __exit__(self,t,e,b):
if e:
raise in_context(self.s,(t,e,b),self.fl) from None
pass
def test4(prog=sys.argv[0]):
def a():
raise Exception('fred')
def b():
with _Scope('b'):
True
a()
pass
pass
def c():
b()
pass
try:
c()
except:
e=in_context('jock')
else:
assert False, 'c should have raised?'
pass
Assert(str(e)).matches('''\
failed to jock because
[^:]*:132: failed to c[(][)] because
[^:]*:129: failed to b[(][)] because
[^:]*:123: failed to with _Scope[(]'b'[)]: because
[^:]*:116: failed to raise in_context[(]self.s,[(]t,e,b[)],self.fl[)] from None because
[^:]*:123: failed to b because
[^:]*:125: failed to a[(][)] because
[^:]*:121: fred''')
Assert(readable_repr(e))=='''\
Failed to jock because
failed to b because
fred.\
'''
pass
def test6():
class X:
def jump(self, height:int):
'''
jump {height}m in the air
- from a standing start
'''
try:
raise Exception('I have lead feet')
except Exception as e:
raise in_function_context(X.jump,vars()) from None
pass
pass
try:
X().jump(10)
except Exception as e:
Assert(readable_repr(e))=='Failed to jump 10m in the air because\nI have lead feet.'
else:
assert False, 'should not be here'
pass
pass
def test8():
Assert(capitalise('Fred'))=='Fred'
pass
def test9():
Assert(str(AllFailed([Exception('fred'),Exception('jock')])))=="fred, and\njock"
pass
def test10():
# sometimes exception attributes cannot be copied due to AttributeError
class EA(Exception):
def __dir__(self):
return ['x']+list(Exception.__dir__(self))
pass
try:
try:
raise EA()
except EA:
raise in_context('fred')
pass
pass
except EA as y:
pass
pass
def test11():
Assert(first_para_of("""
the blind
mice
ran
"""))=="the blind mice ran"
Assert(first_para_of("""
the blind
mice
ran
"""))=="the blind"
Assert(first_para_of(""""""))==""
Assert(first_para_of(""" fred """))=="fred"
def test12():
try:
try:
raise TimeoutError
except Exception as e:
raise in_context('fred') from None
pass
except Exception as e:
Assert(readable_repr(e))=="Failed to fred because\nTimeoutError."
Assert(str(e)).matches('''\
failed to fred because
[^:]*:222: TimeoutError''')
pass
pass
def test13():
Assert(first_line_of('fred\njock'))=='fred'
Assert(first_line_of(''))==''
Assert(first_line_of('fred'))=='fred'
tests=[var for name,var in sorted(vars().items())
if name.startswith('test') and callable(var)]
for t in tests: t()
n=len(tests)
print('PASS - {n} steps'.format(**vars()))
pass