2121 CAPABILITY_VERIFICATION ,
2222 MissingCapabilityError ,
2323)
24+ from opensysml import typed
2425from opensysml .connection import Connection
2526from opensysml .enumeration import EnumLiteral
26- from opensysml .errors import ExecutionError , UnsupportedValueError
27+ from opensysml .errors import ExecutionError , TypeMismatchError , UnsupportedValueError
2728from opensysml .proto import sysml_pb2
2829from opensysml .values import (
2930 Array ,
3435 TensorQuantity ,
3536 Unit ,
3637 UnitFactor ,
38+ Vector ,
3739 VectorQuantity ,
3840 value_to_python ,
3941)
@@ -315,9 +317,10 @@ def test_members_that_only_look_alike_are_distinct(elements, expected):
315317 assert 1 not in SetValue ((True ,)) and True not in SetValue ((1 ,))
316318
317319
318- def test_an_unresolved_instance_reference_is_its_id_but_not_an_integer ():
320+ def test_an_unresolved_instance_reference_holds_its_id_but_is_not_an_integer ():
319321 ref = value_to_python (pb_instance (7 ))
320- assert isinstance (ref , InstanceRef ) and ref == 7 and repr (ref ) == "InstanceRef(7)"
322+ assert isinstance (ref , InstanceRef ) and ref .id == 7 and ref == InstanceRef (7 )
323+ assert ref != 7 and not isinstance (ref , int ) and str (ref ) == "instance(7)"
321324 assert ref in SetValue ((InstanceRef (7 ),)) and ref not in SetValue ((7 ,))
322325 assert Array ((1 ,), (ref ,)) != Array ((1 ,), (7 ,))
323326 assert Array ((2 ,), (True , 1 )) != Array ((2 ,), (1 , 1 ))
@@ -327,6 +330,25 @@ def test_an_unresolved_instance_reference_is_its_id_but_not_an_integer():
327330 sent = conn ._python_to_value (SetValue ((ref , 7 )))
328331 assert [e .WhichOneof ("kind" ) for e in sent .set .elements ] == ["instance_id" , "int_value" ]
329332 assert sent .set .elements [0 ].instance_id == 7
333+ assert value_to_python (sent ) == SetValue ((InstanceRef (7 ), 7 ))
334+
335+
336+ def test_an_instance_reference_is_refused_where_a_number_is_meant ():
337+ ref = InstanceRef (7 )
338+ with pytest .raises (ValueError , match = "not a number" ):
339+ Vector ((1 , ref ))
340+ with pytest .raises (ValueError , match = "not a positive integer" ):
341+ Array ((ref ,), (1 ,))
342+ with pytest .raises (ValueError , match = "not a positive integer" ):
343+ TensorQuantity ((ref ,), [Quantity (1.0 , PASCAL )])
344+ with pytest .raises (TypeError ):
345+ Quantity (1.0 , PASCAL ) * ref
346+ with pytest .raises (TypeMismatchError ):
347+ typed .as_int ("n" , ref )
348+ with pytest .raises (TypeMismatchError ):
349+ typed .as_float ("x" , ref )
350+ with pytest .raises (TypeMismatchError ):
351+ typed .as_complex ("z" , ref )
330352
331353
332354def test_a_set_survives_the_wire_bytes ():
0 commit comments