@@ -37,7 +37,7 @@ def equals(
37
37
print ("Test passed" )
38
38
39
39
elif a != b :
40
- raise AssertionError (message_on_fail + f" : expected { a } to equal { b } " )
40
+ raise AssertionError (f" { message_on_fail } : expected { a } to equal { b } " )
41
41
42
42
def not_equals (
43
43
a : Any ,
@@ -58,7 +58,7 @@ def not_equals(
58
58
print ("Test passed" )
59
59
60
60
elif a == b :
61
- raise AssertionError (message_on_fail + f" : expected { a } to not equal { b } " )
61
+ raise AssertionError (f" { message_on_fail } : expected { a } to not equal { b } " )
62
62
63
63
def expect (
64
64
value : Any ,
@@ -84,8 +84,8 @@ def expect(
84
84
def raises (
85
85
function : Callable [..., Any ],
86
86
exceptions : Tuple [Type [BaseException ], ...] | Type [BaseException ] = (Exception ,),
87
- message_on_fail = "Test failed" ,
88
- verbose = False ,
87
+ message_on_fail : str = "Test failed" ,
88
+ verbose : bool = False ,
89
89
) -> None :
90
90
"""
91
91
Assertion of a function raising an exception.
@@ -100,11 +100,13 @@ def raises(
100
100
exceptions = (exceptions ,)
101
101
102
102
def handle ():
103
+ expected_exceptions = list (map (lambda e : e .__name__ , exceptions ))
104
+
103
105
raise AssertionError (
104
106
message_on_fail
105
107
+ ": expected given function to raise "
106
108
+ ("" if len (exceptions ) == 1 else "any of the following: " )
107
- + ", " .join (map ( lambda e : e . __name__ , exceptions ) )
109
+ + ", " .join (expected_exceptions )
108
110
)
109
111
110
112
try :
@@ -113,8 +115,6 @@ def handle():
113
115
except exceptions :
114
116
if verbose :
115
117
print ("Test passed" )
116
-
117
- return None
118
118
119
119
except :
120
120
# handle cases when the exception is not of the expected type
@@ -144,11 +144,13 @@ def does_not_raise(
144
144
exceptions = (exceptions ,)
145
145
146
146
def handle ():
147
+ avoided_exceptions = list (map (lambda e : e .__name__ , exceptions ))
148
+
147
149
raise AssertionError (
148
150
message_on_fail
149
151
+ ": expected given function to not raise "
150
152
+ ("" if len (exceptions ) == 1 else "any of the following: " )
151
- + ", " .join (map ( lambda e : e . __name__ , exceptions ) )
153
+ + ", " .join (avoided_exceptions )
152
154
)
153
155
154
156
try :
@@ -158,7 +160,7 @@ def handle():
158
160
handle ()
159
161
160
162
except :
161
- # handle cases when the exception is not of the expected type
163
+ # handle other types of exceptions
162
164
if verbose :
163
165
print ("Test passed" )
164
166
@@ -187,7 +189,7 @@ def approximately_equals(
187
189
print ("Test passed" )
188
190
189
191
elif abs (a - b ) > margin :
190
- raise AssertionError (message_on_fail + f" : expected { a } to be within { margin } of { b } " )
192
+ raise AssertionError (f" { message_on_fail } : expected { a } to be within { margin } of { b } " )
191
193
192
194
def not_approximately_equals (
193
195
a : int | float ,
@@ -210,7 +212,7 @@ def not_approximately_equals(
210
212
print ("Test passed" )
211
213
212
214
elif abs (a - b ) <= margin :
213
- raise AssertionError (message_on_fail + f" : expected { a } to not be within { margin } of { b } " )
215
+ raise AssertionError (f" { message_on_fail } : expected { a } to not be within { margin } of { b } " )
214
216
215
217
def contains (
216
218
it : Iterable [Any ],
@@ -231,7 +233,7 @@ def contains(
231
233
print ("Test passed" )
232
234
233
235
elif value not in it :
234
- raise AssertionError (message_on_fail + f" : expected { value } to be in { it } " )
236
+ raise AssertionError (f" { message_on_fail } : expected { value } to be in { it } " )
235
237
236
238
def does_not_contain (
237
239
it : Iterable [Any ],
@@ -252,7 +254,7 @@ def does_not_contain(
252
254
print ("Test passed" )
253
255
254
256
elif value in it :
255
- raise AssertionError (message_on_fail + f" : expected { value } to not be in { it } " )
257
+ raise AssertionError (f" { message_on_fail } : expected { value } to not be in { it } " )
256
258
257
259
def is_instance (
258
260
value : Any ,
@@ -275,7 +277,7 @@ def is_instance(
275
277
print ("Test passed" )
276
278
277
279
elif not isinstance (value , types ):
278
- raise AssertionError (message_on_fail + f" : expected { value } to be an instance of { types } " )
280
+ raise AssertionError (f" { message_on_fail } : expected { value } to be an instance of { types } " )
279
281
280
282
def not_is_instance (
281
283
value : Any ,
@@ -298,7 +300,7 @@ def not_is_instance(
298
300
print ("Test passed" )
299
301
300
302
elif isinstance (value , types ):
301
- raise AssertionError (message_on_fail + f" : expected { value } to not be an instance of { types } " )
303
+ raise AssertionError (f" { message_on_fail } : expected { value } to not be an instance of { types } " )
302
304
303
305
def greater (
304
306
value : int | float ,
@@ -319,7 +321,7 @@ def greater(
319
321
print ("Test passed" )
320
322
321
323
elif value <= comparison :
322
- raise AssertionError (message_on_fail + f" : expected { value } to be greater than { comparison } " )
324
+ raise AssertionError (f" { message_on_fail } : expected { value } to be greater than { comparison } " )
323
325
324
326
def less (
325
327
value : int | float ,
@@ -340,4 +342,4 @@ def less(
340
342
print ("Test passed" )
341
343
342
344
elif value >= comparison :
343
- raise AssertionError (message_on_fail + f" : expected { value } to be less than { comparison } " )
345
+ raise AssertionError (f" { message_on_fail } : expected { value } to be less than { comparison } " )
0 commit comments