예제 #1
0
def test_psyirvisitor_nindent():
    '''Check that the PSyIRVisitor _nindent method returns the product of
    the supplied depth and indent values.

    '''
    visitor = PSyIRVisitor(indent_string=" ", initial_indent_depth=4)
    assert visitor._nindent == "    "
예제 #2
0
def test_psyirvisitor_init():
    '''Check the PSyIRVisitor class __init__ arguments are stored.'''
    visitor = PSyIRVisitor(skip_nodes=True,
                           indent_string=" ",
                           initial_indent_depth=1)
    assert visitor._skip_nodes
    assert visitor._indent == " "
    assert visitor._depth == 1
예제 #3
0
def test_psyirvisitor_visit_no_method2():
    '''Check that an exception is not raised if the method for the Node class
    does not exist and skip_nodes is set to True.

    '''
    visitor = PSyIRVisitor(skip_nodes=True)
    result = visitor(Node())
    assert result is None
예제 #4
0
def test_psyirvisitor_init_error4():
    '''Check that the expected error is raised if the PSyIRVisitor class
    __init__ initial_indent_depth argument is negative.

    '''
    with pytest.raises(TypeError) as excinfo:
        _ = PSyIRVisitor(initial_indent_depth=-1)
    assert "initial_indent_depth should not be negative, but found '-1'." \
        in str(excinfo.value)
예제 #5
0
def test_psyirvisitor_init_error3():
    '''Check that the expected error is raised if the PSyIRVisitor class
    __init__ initial_indent_depth argument is not an integer.

    '''
    with pytest.raises(TypeError) as excinfo:
        _ = PSyIRVisitor(initial_indent_depth="2")
    assert "initial_indent_depth should be an integer but found 'str'." \
        in str(excinfo.value)
예제 #6
0
def test_psyirvisitor_init_error2():
    '''Check that the expected error is raised if the PSyIRVisitor class
    __init__ indent_string argument is not a string.

    '''
    with pytest.raises(TypeError) as excinfo:
        _ = PSyIRVisitor(indent_string=True)
    assert ("indent_string should be a str but found "
            "'bool'." in str(excinfo.value))
예제 #7
0
def test_psyirvisitor_init_error1():
    '''Check that the expected error is raised if the PSyIRVisitor class
    __init__ skip_node argument is not a boolean.

    '''
    with pytest.raises(TypeError) as excinfo:
        _ = PSyIRVisitor(skip_nodes="hello")
    assert "skip_nodes should be a boolean but found 'str'." \
        in str(excinfo.value)
예제 #8
0
def test_psyirvisitor_defaults():
    '''Check the PSyIRVisitor class can be instantiated and the default
    values are set appropriately.

    '''
    visitor = PSyIRVisitor()
    assert not visitor._skip_nodes
    assert visitor._indent == "  "
    assert visitor._depth == 0
예제 #9
0
def test_psyirvisitor_visit_no_method1():
    '''Check that an exception is raised if the method for the Node class
    does not exist.

    '''
    visitor = PSyIRVisitor()
    with pytest.raises(VisitorError) as excinfo:
        visitor(Node())
    assert ("Visitor Error: Unsupported node 'Node' found: method names "
            "attempted were ['node_node']." in str(excinfo.value))
예제 #10
0
def test_psyirvisitor_visit_arg_error():
    '''Check that an exception is raised if the argument to the
    PSyIRVisitor visit method is not a Node or subclass of Node.

    '''
    visitor = PSyIRVisitor(indent_string=" ", initial_indent_depth=4)
    with pytest.raises(VisitorError) as excinfo:
        visitor("hello")
    assert ("Visitor Error: Expected argument to be of type 'Node' but found "
            "'str'." in str(excinfo.value))
예제 #11
0
def test_psyirvisitor_visit_return_node():
    '''Check that when a return PSyIR node is found the actual method
    called is 'return_node'. This is done to avoid clashing with the
    Python keyword.

    '''
    from psyclone.psyGen import Return
    return_node = Return()
    test_visitor = PSyIRVisitor()
    with pytest.raises(VisitorError) as excinfo:
        _ = test_visitor(return_node)
    assert ("Visitor Error: Unsupported node 'Return' found: method names "
            "attempted were ['return_node', 'node_node']."
            "" in str(excinfo))
예제 #12
0
def test_psyirvisitor_visit_all_parents():
    '''Check that the names of the class and all ancestors of the class
    are tried when looking to find a method.

    '''
    class Unsupported(Node):
        '''Subclass of node used to check that the names of all ancestors of a
        node are called as methods, in method resolution order (mro).

        '''

    visitor = PSyIRVisitor()
    with pytest.raises(VisitorError) as excinfo:
        visitor(Unsupported())
    assert (
        "Visitor Error: Unsupported node 'Unsupported' found: method names "
        "attempted were ['unsupported_node', 'node_node']."
        "" in str(excinfo.value))