Ejemplo n.º 1
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(TypeError) as excinfo:
        visitor("hello")
    assert ("The PSyIR visitor functor method only accepts a PSyIR Node "
            "as argument, but found 'str'." in str(excinfo.value))
    with pytest.raises(VisitorError) as excinfo:
        visitor._visit("hello")
    assert ("Visitor Error: Expected argument to be of type 'Node' but found "
            "'str'." in str(excinfo.value))
Ejemplo n.º 2
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 == "    "
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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))
Ejemplo n.º 8
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)
Ejemplo n.º 9
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
Ejemplo n.º 10
0
def test_psyirvisitor_init_error5():
    '''Check that the expected error is raised if the check_global_constraints
    argument to the PSyIRVisitor constructor is not a bool.

    '''
    with pytest.raises(TypeError) as excinfo:
        _ = PSyIRVisitor(check_global_constraints=-1)
    assert ("check_global_constraints should be a boolean but found 'int'"
            in str(excinfo.value))
Ejemplo n.º 11
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))
Ejemplo n.º 12
0
def test_psyirvisitor_init():
    '''Check the PSyIRVisitor class __init__ arguments are stored.'''
    visitor = PSyIRVisitor(skip_nodes=True,
                           indent_string=" ",
                           initial_indent_depth=1,
                           check_global_constraints=False)
    assert visitor._skip_nodes
    assert visitor._indent == " "
    assert visitor._depth == 1
    assert visitor._validate_nodes is False
Ejemplo n.º 13
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.

    '''
    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', 'statement_node', 'node_node']."
            in str(excinfo.value))
Ejemplo n.º 14
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))
Ejemplo n.º 15
0
def test_reference():
    '''Test that the common reference visitor writes the referenced symbol name
    or raises an error if the node is not Leaf node reference.

    '''
    # Generate PSyIR Reference
    test_visitor = PSyIRVisitor()
    reference = Reference(DataSymbol('a', REAL_TYPE))
    result = test_visitor(reference)
    assert result == "a"

    # Generate PSyIR Array
    reference2 = Array.create(DataSymbol('b', ArrayType(REAL_TYPE, shape=[1])),
                              [reference])
    with pytest.raises(VisitorError) as err:
        result = test_visitor(reference2)
    assert "Expecting a Reference with no children but found: " \
        in str(err.value)