Exemple #1
0
def test_objects_primary_expression_entity(patch, tree):
    """
    Ensures ExpressionVisitor.primary_expression works with an entity node
    """
    patch.many(ExpressionVisitor, ['entity'])
    tree.child(0).data = 'entity'
    r = ExpressionVisitor().primary_expression(tree)
    ExpressionVisitor.entity.assert_called_with(tree.entity)
    assert r == ExpressionVisitor.entity()
def test_objects_expression_one(patch, tree):
    """
    Ensures ExpressionVisitor.expression works with one node
    """
    patch.many(ExpressionVisitor, ['entity'])
    tree.first_child().data = 'entity'
    tree.children = [1]
    r = ExpressionVisitor().expression(tree)
    ExpressionVisitor.entity.assert_called_with(tree.first_child())
    assert r == ExpressionVisitor.entity()
Exemple #3
0
def test_objects_mul_expression_one(patch, tree):
    """
    Ensures ExpressionVisitor.mul_expression works with one node
    """
    patch.many(ExpressionVisitor, ['unary_expression'])
    tree.child(0).data = 'unary_expression'
    tree.children = [1]
    r = ExpressionVisitor().mul_expression(tree)
    ExpressionVisitor.unary_expression.assert_called_with(tree.child(0))
    assert r == ExpressionVisitor.unary_expression()
Exemple #4
0
def test_objects_primary_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.primary_expression works with a
    or_expression node.
    """
    patch.many(ExpressionVisitor, ['entity', 'or_expression'])
    tree.child(0).data = 'or_expression'
    r = ExpressionVisitor().primary_expression(tree)
    ExpressionVisitor.or_expression.assert_called_with(tree.child(0))
    assert r == ExpressionVisitor.or_expression()
Exemple #5
0
def test_objects_unary_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.nary_expression works with two nodes
    """
    patch.many(ExpressionVisitor, ['nary_expression'])
    tree.child(1).data = 'unary_operator'
    unary_expression = ExpressionVisitor().unary_expression
    patch.object(ExpressionVisitor, 'unary_expression')
    r = unary_expression(tree)
    op = tree.unary_operator.child(0)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree, op, [ExpressionVisitor.unary_expression(tree.child(1))])
    assert r == ExpressionVisitor.nary_expression()
def test_objects_expression_two_as(patch, tree):
    """
    Ensures ExpressionVisitor.expression works with as
    """
    patch.many(ExpressionVisitor, ['as_expression'])
    tree.child(1).data = 'as_operator'
    tree.children = ['!', 1]
    expression = ExpressionVisitor().expression
    patch.object(ExpressionVisitor, 'expression')
    r = expression(tree)
    ExpressionVisitor.as_expression.assert_called_with(
        tree, ExpressionVisitor.expression(tree.first_child()),
    )
    assert r == ExpressionVisitor.as_expression()
def test_objects_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.expression works with two nodes
    """
    patch.many(ExpressionVisitor, ['nary_expression'])
    tree.first_child().data = 'unary_operator'
    tree.children = ['!', 1]
    expression = ExpressionVisitor().expression
    patch.object(ExpressionVisitor, 'expression')
    r = expression(tree)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree, tree.first_child().child(0), [
            ExpressionVisitor.expression(tree.child(1))
        ])
    assert r == ExpressionVisitor.nary_expression()
Exemple #8
0
def test_objects_expression_two_as(patch, tree):
    """
    Ensures ExpressionVisitor.expression works with as
    """
    patch.many(ExpressionVisitor, ["to_expression"])
    tree.child(1).data = "to_operator"
    tree.children = ["!", 1]
    expression = ExpressionVisitor().expression
    patch.object(ExpressionVisitor, "expression")
    r = expression(tree)
    ExpressionVisitor.to_expression.assert_called_with(
        tree,
        ExpressionVisitor.expression(tree.first_child()),
    )
    assert r == ExpressionVisitor.to_expression()
def test_objects_as_expression(patch, tree):
    """
    Ensures that the ExpressionVisitor by default throws AssertionErrors
    for to-be-implemented methods.
    """
    with raises(NotImplementedError):
        ExpressionVisitor().as_expression(None, 0)
Exemple #10
0
def test_objects_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.expression works with two nodes
    """
    patch.many(ExpressionVisitor, ["nary_expression"])
    tree.first_child().data = "unary_operator"
    tree.children = ["!", 1]
    expression = ExpressionVisitor().expression
    patch.object(ExpressionVisitor, "expression")
    r = expression(tree)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree,
        tree.first_child().child(0),
        [ExpressionVisitor.expression(tree.child(1))],
    )
    assert r == ExpressionVisitor.nary_expression()
Exemple #11
0
def test_objects_mul_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.mul_expression works with two nodes
    """
    patch.many(ExpressionVisitor, ['nary_expression',
                                   'unary_expression'])
    tree.child(1).data = 'mul_operator'
    tree.children = [1, '*', 2]
    mul_expression = ExpressionVisitor().mul_expression
    patch.object(ExpressionVisitor, 'mul_expression')
    r = mul_expression(tree)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree, tree.child(1).child(0), [
            ExpressionVisitor.mul_expression(tree.child(0)),
            ExpressionVisitor.unary_expression(tree.child(2))
        ])
    assert r == ExpressionVisitor.nary_expression()
Exemple #12
0
def test_objects_pow_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.pow_expression works with two nodes
    """
    patch.many(ExpressionVisitor, ['nary_expression',
                                   'primary_expression',
                                   'unary_expression'])
    tree.child(1).child(0).type = 'POWER'
    tree.children = [1, '+', 2]
    tree.child(1).data = 'pow_operator'
    r = ExpressionVisitor().pow_expression(tree)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree, tree.child(1).child(0), [
            ExpressionVisitor.primary_expression(tree.child(0)),
            ExpressionVisitor.unary_expression(tree.child(2))
        ])
    assert r == ExpressionVisitor.nary_expression()
Exemple #13
0
def test_objects_cmp_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.and_expression works with two nodes
    """
    patch.many(ExpressionVisitor, ['nary_expression',
                                   'arith_expression'])
    tree.child(1).data = 'cmp_operator'
    tree.children = [1, '==', 2]
    cmp_expression = ExpressionVisitor().cmp_expression
    patch.object(ExpressionVisitor, 'cmp_expression')
    r = cmp_expression(tree)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree, tree.child(1).child(0), [
            ExpressionVisitor.cmp_expression(tree.child(0)),
            ExpressionVisitor.arith_expression(tree.child(2)),
        ])
    assert r == ExpressionVisitor.nary_expression()
Exemple #14
0
def test_objects_and_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.and_expression works with two nodes
    """
    patch.many(ExpressionVisitor, ['nary_expression',
                                   'cmp_expression'])
    tree.child(1).type = 'AND'
    tree.children = [1, 'and', 2]
    and_expression = ExpressionVisitor().and_expression
    patch.object(ExpressionVisitor, 'and_expression')
    r = and_expression(tree)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree, tree.child(1), [
            ExpressionVisitor.and_expression(tree.child(0)),
            ExpressionVisitor.cmp_expression(tree.child(2)),
        ])
    assert r == ExpressionVisitor.nary_expression()
Exemple #15
0
def test_objects_arith_expression_two(patch, tree):
    """
    Ensures ExpressionVisitor.arith_expression works with two nodes
    """
    patch.many(ExpressionVisitor, ['mul_expression', 'nary_expression'])
    tree.child(1).data = 'arith_operator'
    tree.children = [1, '+', 2]
    arith_expression = ExpressionVisitor().arith_expression
    patch.object(ExpressionVisitor, 'arith_expression')
    r = arith_expression(tree)
    op_type = tree.child(1).child(0)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree, op_type, values=[
            ExpressionVisitor.arith_expression(tree.child(0)),
            ExpressionVisitor.mul_expression(tree.children[2])
        ])
    assert r == ExpressionVisitor.nary_expression()
Exemple #16
0
def test_objects_expression_three(patch, tree):
    """
    Ensures ExpressionVisitor.expression works with three nodes
    """
    patch.many(ExpressionVisitor, ["nary_expression"])
    tree.child(1).data = "mul_operator"
    tree.children = [1, "*", 2]
    expression = ExpressionVisitor().expression
    patch.object(ExpressionVisitor, "expression")
    r = expression(tree)
    ExpressionVisitor.nary_expression.assert_called_with(
        tree,
        tree.child(1).child(0),
        [
            ExpressionVisitor.expression(tree.first_child()),
            ExpressionVisitor.expression(tree.child(2)),
        ],
    )
    assert r == ExpressionVisitor.nary_expression()