@@ -9,7 +9,7 @@ def _test_iteration(self, tree):
99 for field , value in tree :
1010 self .assertIsInstance (field , str )
1111 self .assertIsInstance (value , jast .JAST | List )
12- self .assertTrue (hasattr (tree , field ))
12+ self .assertTrue (hasattr (tree , field ), f" { field } not in { tree . __class__ } " )
1313 self .assertEqual (getattr (tree , field ), value )
1414
1515 def test_identifier (self ):
@@ -863,3 +863,187 @@ def test_BinOp_error(self):
863863 left = jast .Constant (jast .IntLiteral (42 )),
864864 right = jast .Constant (jast .IntLiteral (0 )),
865865 )
866+
867+ def test_InstanceOf (self ):
868+ instance_of = jast .InstanceOf (
869+ value = jast .identifier ("foo" ),
870+ type = jast .Int (),
871+ )
872+ self .assertIsInstance (instance_of , jast .InstanceOf )
873+ self .assertIsInstance (instance_of , jast .JAST )
874+ self .assertIsInstance (instance_of .value , jast .identifier )
875+ self .assertEqual ("foo" , instance_of .value )
876+ self .assertIsInstance (instance_of .type , jast .Int )
877+ self ._test_iteration (instance_of )
878+
879+ def test_InstanceOf_error (self ):
880+ self .assertRaises (
881+ ValueError ,
882+ jast .InstanceOf ,
883+ type = jast .Int (),
884+ )
885+ self .assertRaises (
886+ ValueError ,
887+ jast .InstanceOf ,
888+ value = jast .identifier ("foo" ),
889+ )
890+
891+ def test_UnaryOp (self ):
892+ unary_op = jast .UnaryOp (
893+ op = jast .USub (),
894+ operand = jast .Constant (jast .IntLiteral (42 )),
895+ )
896+ self .assertIsInstance (unary_op , jast .UnaryOp )
897+ self .assertIsInstance (unary_op , jast .JAST )
898+ self .assertIsInstance (unary_op .op , jast .USub )
899+ self .assertIsInstance (unary_op .operand , jast .Constant )
900+ self .assertIsInstance (unary_op .operand .value , jast .IntLiteral )
901+ self .assertEqual (42 , unary_op .operand .value )
902+ self ._test_iteration (unary_op )
903+
904+ def test_UnaryOp_error (self ):
905+ self .assertRaises (
906+ ValueError ,
907+ jast .UnaryOp ,
908+ operand = jast .Constant (jast .IntLiteral (42 )),
909+ )
910+ self .assertRaises (
911+ ValueError ,
912+ jast .UnaryOp ,
913+ op = jast .USub (),
914+ )
915+
916+ def test_PostOp (self ):
917+ post_op = jast .PostOp (
918+ operand = jast .identifier ("foo" ),
919+ op = jast .PostInc (),
920+ )
921+ self .assertIsInstance (post_op , jast .PostOp )
922+ self .assertIsInstance (post_op , jast .JAST )
923+ self .assertIsInstance (post_op .operand , jast .identifier )
924+ self .assertEqual ("foo" , post_op .operand )
925+ self .assertIsInstance (post_op .op , jast .PostInc )
926+ self ._test_iteration (post_op )
927+
928+ def test_PostOp_error (self ):
929+ self .assertRaises (
930+ ValueError ,
931+ jast .PostOp ,
932+ op = jast .PostInc (),
933+ )
934+ self .assertRaises (
935+ ValueError ,
936+ jast .PostOp ,
937+ operand = jast .identifier ("foo" ),
938+ )
939+
940+ def test_Cast (self ):
941+ cast = jast .Cast (
942+ annotations = [jast .Annotation (jast .qname ([jast .identifier ("foo" )]))],
943+ type = jast .Int (),
944+ value = jast .Constant (jast .IntLiteral (42 )),
945+ )
946+ self .assertIsInstance (cast , jast .Cast )
947+ self .assertIsInstance (cast , jast .JAST )
948+ self .assertEqual (1 , len (cast .annotations ))
949+ self .assertIsInstance (cast .annotations [0 ], jast .Annotation )
950+ self .assertIsInstance (cast .type , jast .Int )
951+ self .assertIsInstance (cast .value , jast .Constant )
952+ self .assertIsInstance (cast .value .value , jast .IntLiteral )
953+ self .assertEqual (42 , cast .value .value )
954+ self ._test_iteration (cast )
955+
956+ def test_Cast_error (self ):
957+ self .assertRaises (
958+ ValueError ,
959+ jast .Cast ,
960+ value = jast .Constant (jast .IntLiteral (42 )),
961+ )
962+ self .assertRaises (
963+ ValueError ,
964+ jast .Cast ,
965+ type = jast .Int (),
966+ )
967+
968+ def test_NewObject (self ):
969+ new_object = jast .NewObject (
970+ type_args = jast .typeargs ([jast .Int (), jast .Boolean ()]),
971+ type = jast .Coit (id = jast .identifier ("A" )),
972+ args = [jast .Constant (jast .IntLiteral (42 ))],
973+ body = [jast .EmptyDecl ()],
974+ )
975+ self .assertIsInstance (new_object , jast .NewObject )
976+ self .assertIsInstance (new_object , jast .JAST )
977+ self .assertIsInstance (new_object .type_args , jast .typeargs )
978+ self .assertEqual (2 , len (new_object .type_args .types ))
979+ self .assertIsInstance (new_object .type_args .types [0 ], jast .Int )
980+ self .assertIsInstance (new_object .type_args .types [1 ], jast .Boolean )
981+ self .assertIsInstance (new_object .type , jast .Coit )
982+ self .assertEqual ("A" , new_object .type .id )
983+ self .assertEqual (1 , len (new_object .args ))
984+ self .assertIsInstance (new_object .args [0 ], jast .Constant )
985+ self .assertIsInstance (new_object .args [0 ].value , jast .IntLiteral )
986+ self .assertEqual (42 , new_object .args [0 ].value )
987+ self .assertEqual (1 , len (new_object .body ))
988+ self .assertIsInstance (new_object .body [0 ], jast .EmptyDecl )
989+ self ._test_iteration (new_object )
990+
991+ def test_NewObject_error (self ):
992+ self .assertRaises (
993+ ValueError ,
994+ jast .NewObject ,
995+ type_args = jast .typeargs ([jast .Int (), jast .Boolean ()]),
996+ args = [jast .Constant (jast .IntLiteral (42 ))],
997+ body = [jast .EmptyDecl ()],
998+ )
999+
1000+ def test_NewArray (self ):
1001+ new_array = jast .NewArray (
1002+ type = jast .Int (),
1003+ expr_dims = [jast .Constant (jast .IntLiteral (1 ))],
1004+ dims = [jast .dim (), jast .dim ()],
1005+ )
1006+ self .assertIsInstance (new_array , jast .NewArray )
1007+ self .assertIsInstance (new_array , jast .JAST )
1008+ self .assertIsInstance (new_array .type , jast .Int )
1009+ self .assertEqual (1 , len (new_array .expr_dims ))
1010+ expr_dim = new_array .expr_dims [0 ]
1011+ self .assertIsInstance (expr_dim , jast .Constant )
1012+ self .assertIsInstance (expr_dim .value , jast .IntLiteral )
1013+ self .assertEqual (1 , expr_dim .value )
1014+ self .assertEqual (2 , len (new_array .dims ))
1015+ self .assertIsInstance (new_array .dims [0 ], jast .dim )
1016+ self .assertIsInstance (new_array .dims [1 ], jast .dim )
1017+ self ._test_iteration (new_array )
1018+
1019+ def test_NewArray_initializer (self ):
1020+ new_array = jast .NewArray (
1021+ type = jast .Int (),
1022+ dims = [jast .dim ()],
1023+ initializer = jast .arrayinit (values = [jast .Constant (jast .IntLiteral (1 ))]),
1024+ )
1025+ self .assertIsInstance (new_array , jast .NewArray )
1026+ self .assertIsInstance (new_array , jast .JAST )
1027+ self .assertIsInstance (new_array .type , jast .Int )
1028+ self .assertEqual (1 , len (new_array .dims ))
1029+ self .assertIsInstance (new_array .dims [0 ], jast .dim )
1030+ self .assertIsInstance (new_array .initializer , jast .arrayinit )
1031+ self .assertEqual (1 , len (new_array .initializer .values ))
1032+ self .assertIsInstance (new_array .initializer .values [0 ], jast .Constant )
1033+ self .assertIsInstance (new_array .initializer .values [0 ].value , jast .IntLiteral )
1034+ self .assertEqual (1 , new_array .initializer .values [0 ].value )
1035+ self ._test_iteration (new_array )
1036+
1037+ def test_NewArray_error (self ):
1038+ self .assertRaises (
1039+ ValueError ,
1040+ jast .NewArray ,
1041+ dims = [jast .dim ()],
1042+ )
1043+ self .assertRaises (
1044+ ValueError ,
1045+ jast .NewArray ,
1046+ type = jast .Int (),
1047+ expr_dims = [jast .Constant (jast .IntLiteral (1 ))],
1048+ initializer = jast .arrayinit (values = [jast .Constant (jast .IntLiteral (1 ))]),
1049+ )
0 commit comments