def test_member_constructor_errors(): ''' Test the validation checks in the constructor. ''' with pytest.raises(TypeError) as err: nodes.Member("hello", parent="wrong") assert ("parent of a Member must be either a " "(ArrayOf)Structure(s)Reference or (ArrayOf)Structure(s)Member " "but found 'str'" in str(err.value))
def test_asmr_create(): ''' Test the create method of ArrayOfStructuresMember. ''' asmr = nodes.ArrayOfStructuresMember.create( "regions", [nodes.Literal("3", symbols.INTEGER_TYPE)], nodes.Member("sub_mesh")) assert len(asmr.children) == 2 assert isinstance(asmr.children[0], nodes.Member) assert asmr.children[1].value == "3"
def test_sm_child_validate(): ''' Check the _validate_child() method of StructureMember. ''' smr = nodes.StructureMember("area") with pytest.raises(GenerationError) as err: smr.addchild("hello") assert "'str' can't be child 0 of 'StructureMember'" in str(err.value) # StructureMember is only permitted to have a single child which must # be a Member smr.addchild(nodes.Member("nx")) assert smr.children[0].name == "nx" # Attempting to add a second child should fail with pytest.raises(GenerationError) as err: smr.addchild(None) assert "'NoneType' can't be child 1 of" in str(err.value)
def test_member_can_be_copied(): ''' Test that a Member node can be copied. ''' member = nodes.Member("name1") member1 = member.copy() assert isinstance(member1, nodes.Member) assert member1 is not member assert member1.name == "name1" # Modifying the new member does not affect the original member1._component_name = "name2" assert member1.name == "name2" assert member.name == "name1"
def test_asmr_validate_child(): ''' Test the _validate_child method of ArrayOfStructuresMember. ''' asmr = nodes.ArrayOfStructuresMember("regions") with pytest.raises(GenerationError) as err: asmr.addchild("wrong") assert ("'str' can't be child 0 of 'ArrayOfStructuresMember'" in str(err.value)) asmr.addchild(nodes.Member("sub_mesh")) assert isinstance(asmr.children[0], nodes.Member) with pytest.raises(GenerationError) as err: asmr.addchild("2") assert ("'str' can't be child 1 of 'ArrayOfStructuresMember'" in str(err.value)) idx = nodes.Reference(symbols.DataSymbol("idx", symbols.INTEGER_TYPE)) asmr.addchild(idx) assert asmr.children[1] is idx
def test_asmr_indices(): ''' Test the indices property of ArrayOfStructuresMember. ''' asmr = nodes.ArrayOfStructuresMember.create( "regions", [nodes.Literal("3", symbols.INTEGER_TYPE)], nodes.Member("sub_mesh")) indices = asmr.indices assert len(indices) == 1 assert isinstance(indices[0], nodes.Literal) assert indices[0].value == "3" # Break the children of the node to check that we get the expected # error. asmr._children = [asmr._children[0]] with pytest.raises(InternalError) as err: asmr.indices assert ("must have one or more children representing array-index " "expressions but found none" in str(err.value)) asmr._children = [asmr._children[0], "hello"] with pytest.raises(InternalError) as err: asmr.indices assert ("malformed or incomplete: child 1 must represent an array-index " "expression but found 'str' instead of psyir.nodes.DataNode or " "Range" in str(err.value))
def test_member_constructor(): ''' Test that we can construct an instance of Member. ''' mem = nodes.Member("fred") assert mem.name == "fred" assert mem.children == []