Пример #1
0
def test_replace_with_error2():
    '''Check that the replace_with method raises the expected exceptions
    if either node is invalid.

    '''
    parent = Schedule()
    node1 = Statement()
    node2 = Statement()

    with pytest.raises(TypeError) as info:
        node1.replace_with("hello")
    assert ("The argument node in method replace_with in the Node class "
            "should be a Node but found 'str'." in str(info.value))

    with pytest.raises(GenerationError) as info:
        node1.replace_with(node2)
    assert ("This node should have a parent if its replace_with method "
            "is called." in str(info.value))

    node1.parent = parent
    node2.parent = parent
    parent.children = [node1, node2]
    with pytest.raises(GenerationError) as info:
        node1.replace_with(node2)
    assert ("The parent of argument node in method replace_with in the Node "
            "class should be None but found 'Schedule'." in str(info.value))

    node3 = Container("hello")
    with pytest.raises(GenerationError) as info:
        node1.replace_with(node3)
        assert ("Generation Error: Item 'Container' can't be child 0 of "
                "'Schedule'. The valid format is: '[Statement]*'."
                in str(info.value))
Пример #2
0
def test_replace_with():
    '''Check that the replace_with method behaves as expected.'''

    parent_node = Schedule()
    node1 = Statement()
    node2 = Statement()
    node3 = Statement()
    parent_node.children = [node1, node2, node3]
    new_node = Assignment()

    node2.replace_with(new_node)

    assert parent_node.children[1] is new_node
    assert new_node.parent is parent_node
    assert node2.parent is None