@@ -37,7 +37,7 @@ def equals(
3737 print ("Test passed" )
3838
3939 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 } " )
4141
4242def not_equals (
4343 a : Any ,
@@ -58,7 +58,7 @@ def not_equals(
5858 print ("Test passed" )
5959
6060 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 } " )
6262
6363def expect (
6464 value : Any ,
@@ -84,8 +84,8 @@ def expect(
8484def raises (
8585 function : Callable [..., Any ],
8686 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 ,
8989) -> None :
9090 """
9191 Assertion of a function raising an exception.
@@ -100,11 +100,13 @@ def raises(
100100 exceptions = (exceptions ,)
101101
102102 def handle ():
103+ expected_exceptions = list (map (lambda e : e .__name__ , exceptions ))
104+
103105 raise AssertionError (
104106 message_on_fail
105107 + ": expected given function to raise "
106108 + ("" if len (exceptions ) == 1 else "any of the following: " )
107- + ", " .join (map ( lambda e : e . __name__ , exceptions ) )
109+ + ", " .join (expected_exceptions )
108110 )
109111
110112 try :
@@ -113,8 +115,6 @@ def handle():
113115 except exceptions :
114116 if verbose :
115117 print ("Test passed" )
116-
117- return None
118118
119119 except :
120120 # handle cases when the exception is not of the expected type
@@ -144,11 +144,13 @@ def does_not_raise(
144144 exceptions = (exceptions ,)
145145
146146 def handle ():
147+ avoided_exceptions = list (map (lambda e : e .__name__ , exceptions ))
148+
147149 raise AssertionError (
148150 message_on_fail
149151 + ": expected given function to not raise "
150152 + ("" if len (exceptions ) == 1 else "any of the following: " )
151- + ", " .join (map ( lambda e : e . __name__ , exceptions ) )
153+ + ", " .join (avoided_exceptions )
152154 )
153155
154156 try :
@@ -158,7 +160,7 @@ def handle():
158160 handle ()
159161
160162 except :
161- # handle cases when the exception is not of the expected type
163+ # handle other types of exceptions
162164 if verbose :
163165 print ("Test passed" )
164166
@@ -187,7 +189,7 @@ def approximately_equals(
187189 print ("Test passed" )
188190
189191 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 } " )
191193
192194def not_approximately_equals (
193195 a : int | float ,
@@ -210,7 +212,7 @@ def not_approximately_equals(
210212 print ("Test passed" )
211213
212214 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 } " )
214216
215217def contains (
216218 it : Iterable [Any ],
@@ -231,7 +233,7 @@ def contains(
231233 print ("Test passed" )
232234
233235 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 } " )
235237
236238def does_not_contain (
237239 it : Iterable [Any ],
@@ -252,7 +254,7 @@ def does_not_contain(
252254 print ("Test passed" )
253255
254256 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 } " )
256258
257259def is_instance (
258260 value : Any ,
@@ -275,7 +277,7 @@ def is_instance(
275277 print ("Test passed" )
276278
277279 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 } " )
279281
280282def not_is_instance (
281283 value : Any ,
@@ -298,7 +300,7 @@ def not_is_instance(
298300 print ("Test passed" )
299301
300302 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 } " )
302304
303305def greater (
304306 value : int | float ,
@@ -319,7 +321,7 @@ def greater(
319321 print ("Test passed" )
320322
321323 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 } " )
323325
324326def less (
325327 value : int | float ,
@@ -340,4 +342,4 @@ def less(
340342 print ("Test passed" )
341343
342344 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