44
55import unittest
66
7- from iptest import IronPythonTestCase , is_cli , is_mono , is_netcoreapp , is_posix , big , run_test , skipUnlessIronPython
7+ from iptest import IronPythonTestCase , is_cli , is_mono , is_netcoreapp , is_posix , big , clr_int_types , run_test , skipUnlessIronPython
88from types import FunctionType , MethodType
99
1010global init
@@ -560,19 +560,29 @@ def foo(x):
560560 return x
561561
562562 self .assertEqual (foo ('abc' ), 'abc' )
563- self .assertRaises (AssertionError , foo , 2 )
564- self .assertRaises (AssertionError , foo , big (2 ))
565- self .assertRaises (AssertionError , foo , 2.0 )
566- self .assertRaises (AssertionError , foo , True )
563+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected str, got int)" , foo , 2 )
564+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected str, got int)" , foo , big (2 ))
565+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected str, got float)" , foo , 2.0 )
566+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected str, got bool)" , foo , True )
567+
568+ @clr .accepts (int )
569+ def foo (x ):
570+ return x
571+
572+ self .assertEqual (foo (1 ), 1 )
573+ self .assertEqual (foo (big (1 )), 1 )
574+ self .assertEqual (foo (True ), True )
575+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected int, got str)" , foo , 'abc' )
576+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected int, got float)" , foo , 2.0 )
567577
568578 @clr .accepts (str , bool )
569579 def foo (x , y ):
570580 return x , y
571581
572582 self .assertEqual (foo ('abc' , True ), ('abc' , True ))
573- self .assertRaises (AssertionError , foo , ( 'abc' ,2 ) )
574- self .assertRaises (AssertionError , foo , ( 'abc' ,big (2 ) ))
575- self .assertRaises (AssertionError , foo , ( 'abc' ,2.0 ) )
583+ self .assertRaisesMessage (AssertionError , "argument 1 has bad value (expected bool, got int)" , foo , 'abc' , 2 )
584+ self .assertRaisesMessage (AssertionError , "argument 1 has bad value (expected bool, got int)" , foo , 'abc' , big (2 ))
585+ self .assertRaisesMessage (AssertionError , "argument 1 has bad value (expected bool, got float)" , foo , 'abc' , 2.0 )
576586
577587
578588 class bar :
@@ -583,21 +593,25 @@ def foo(self, x):
583593
584594 a = bar ()
585595 self .assertEqual (a .foo ('xyz' ), 'xyz' )
586- self .assertRaises (AssertionError , a .foo , 2 )
587- self .assertRaises (AssertionError , a .foo , big (2 ))
588- self .assertRaises (AssertionError , a .foo , 2.0 )
589- self .assertRaises (AssertionError , a .foo , True )
596+ self .assertRaisesMessage (AssertionError , "argument 1 has bad value (expected str, got int)" , a .foo , 2 )
597+ self .assertRaisesMessage (AssertionError , "argument 1 has bad value (expected str, got int)" , a .foo , big (2 ))
598+ self .assertRaisesMessage (AssertionError , "argument 1 has bad value (expected str, got float)" , a .foo , 2.0 )
599+ self .assertRaisesMessage (AssertionError , "argument 1 has bad value (expected str, got bool)" , a .foo , True )
590600
591601 @clr .returns (str )
592602 def foo (x ):
593603 return x
594604
595-
596605 self .assertEqual (foo ('abc' ), 'abc' )
597- self .assertRaises (AssertionError , foo , 2 )
598- self .assertRaises (AssertionError , foo , big (2 ))
599- self .assertRaises (AssertionError , foo , 2.0 )
600- self .assertRaises (AssertionError , foo , True )
606+ self .assertRaisesMessage (AssertionError , "bad return value returned (expected str, got int)" , foo , 2 )
607+ self .assertRaisesMessage (AssertionError , "bad return value returned (expected str, got int)" , foo , big (2 ))
608+ self .assertRaisesMessage (AssertionError , "bad return value returned (expected str, got float)" , foo , 2.0 )
609+ self .assertRaisesMessage (AssertionError , "bad return value returned (expected str, got bool)" , foo , True )
610+
611+ with self .assertRaisesMessage (TypeError , "expected type, got int" ):
612+ @clr .accepts (0 )
613+ def foo (x ): pass
614+
601615
602616 @clr .accepts (bool )
603617 @clr .returns (str )
@@ -607,15 +621,52 @@ def foo(x):
607621
608622 self .assertEqual (foo (True ), 'True' )
609623
610- self .assertRaises (AssertionError , foo , 2 )
611- self .assertRaises (AssertionError , foo , big (2 ))
612- self .assertRaises (AssertionError , foo , False )
624+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected bool, got int)" , foo , 2 )
625+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected bool, got int)" , foo , big (2 ))
626+ self .assertRaisesMessage (AssertionError , "bad return value returned (expected str, got int)" , foo , False )
627+
613628
614629 @clr .returns (None )
615630 def foo (): pass
616631
617632 self .assertEqual (foo (), None )
618633
634+ @clr .returns (type (None ))
635+ def foo (): pass
636+
637+ self .assertEqual (foo (), None )
638+
639+ @clr .returns (None )
640+ def foo ():
641+ return 1
642+
643+ self .assertRaisesMessage (AssertionError , "bad return value returned (expected NoneType, got int)" , foo )
644+
645+ with self .assertRaisesMessage (TypeError , "expected type, got int" ):
646+ @clr .returns (0 )
647+ def foo (): pass
648+
649+ import System
650+ for t in clr_int_types :
651+ @clr .accepts (t )
652+ def foo (x ):
653+ return x
654+
655+ self .assertRaisesMessage (AssertionError , f"""argument 0 has bad value (expected { str (t ).split ("'" )[1 ]} , got int)""" , foo , big (0 ))
656+ if t != System .Int32 :
657+ self .assertRaisesMessage (AssertionError , f"""argument 0 has bad value (expected { str (t ).split ("'" )[1 ]} , got int)""" , foo , 0 )
658+
659+ @clr .accepts (System .IConvertible )
660+ @clr .returns (System .IConvertible )
661+ def foo (x ):
662+ return x
663+
664+ self .assertEqual (foo (1 ), 1 )
665+ self .assertEqual (foo (True ), True )
666+ self .assertEqual (foo ('abc' ), 'abc' )
667+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected IConvertible, got int)" , foo , big (1 ))
668+ self .assertRaisesMessage (AssertionError , "argument 0 has bad value (expected IConvertible, got Guid)" , foo , System .Guid .Empty )
669+
619670 def test_error_message (self ):
620671 try :
621672 repr ()
0 commit comments