def test_assignment_semantic_navigation(): '''Test that the Assignment navigation properties reference the expected children''' assignment = Assignment() # lhs should fail if first child is not present with pytest.raises(InternalError) as err: _ = assignment.lhs assert "' malformed or incomplete. It needs at least 1 child to have " \ "a lhs." in str(err.value) ref = Reference(DataSymbol("a", REAL_SINGLE_TYPE), assignment) assignment.addchild(ref) # rhs should fail if second child is not present with pytest.raises(InternalError) as err: _ = assignment.rhs assert " malformed or incomplete. It needs at least 2 children to have " \ "a rhs." in str(err.value) lit = Literal("1", INTEGER_SINGLE_TYPE, assignment) assignment.addchild(lit) assert assignment.lhs is assignment._children[0] assert assignment.rhs is assignment._children[1]
def test_children_validation(): ''' Test that nodes are validated when inserted as children of other nodes. For simplicity we use Node subclasses to test this functionality across a range of possible operations. The specific logic of each validate method will be tested individually inside each Node test file. ''' assignment = Assignment() return_stmt = Return() reference = Reference(DataSymbol("a", INTEGER_TYPE)) assert isinstance(assignment.children, (ChildrenList, list)) # Try adding a invalid child (e.g. a return_stmt into an assingment) with pytest.raises(GenerationError) as error: assignment.addchild(return_stmt) assert "Item 'Return' can't be child 0 of 'Assignment'. The valid format" \ " is: 'DataNode, DataNode'." in str(error.value) # The same behaviour occurs when list insertion operations are used. with pytest.raises(GenerationError): assignment.children.append(return_stmt) with pytest.raises(GenerationError): assignment.children[0] = return_stmt with pytest.raises(GenerationError): assignment.children.insert(0, return_stmt) with pytest.raises(GenerationError): assignment.children.extend([return_stmt]) with pytest.raises(GenerationError): assignment.children = assignment.children + [return_stmt] # Valid nodes are accepted assignment.addchild(reference) # Check displaced items are also be checked when needed start = Literal("0", INTEGER_TYPE) stop = Literal("1", INTEGER_TYPE) step = Literal("2", INTEGER_TYPE) child_node = Assignment.create(Reference(DataSymbol("tmp", REAL_TYPE)), Reference(DataSymbol("i", REAL_TYPE))) loop_variable = DataSymbol("idx", INTEGER_TYPE) loop = Loop.create(loop_variable, start, stop, step, [child_node]) with pytest.raises(GenerationError): loop.children.insert(1, Literal("0", INTEGER_TYPE)) with pytest.raises(GenerationError): loop.children.remove(stop) with pytest.raises(GenerationError): del loop.children[2] with pytest.raises(GenerationError): loop.children.pop(2) with pytest.raises(GenerationError): loop.children.reverse() # But the in the right circumstances they work fine assert isinstance(loop.children.pop(), Schedule) loop.children.reverse() assert loop.children[0].value == "2"