示例#1
0
def test_subexpression():
    assert DefaultParser().parse('(2+3)*4') == BinaryExpression(
        operator=_ops['*'],
        left=BinaryExpression(operator=_ops['+'],
                              left=Literal(2),
                              right=Literal(3)),
        right=Literal(4))
示例#2
0
def test_nested_subexpression():
    assert DefaultParser().parse('(4*(2+3))/5') == BinaryExpression(
        operator=_ops['/'],
        left=BinaryExpression(operator=_ops['*'],
                              left=Literal(4),
                              right=BinaryExpression(operator=_ops['+'],
                                                     left=Literal(2),
                                                     right=Literal(3))),
        right=Literal(5))
示例#3
0
def test_binary_expression_priority_left():
    assert DefaultParser().parse('2*3+4') == BinaryExpression(
        operator=_ops['+'],
        left=BinaryExpression(
            operator=_ops['*'],
            left=Literal(2),
            right=Literal(3),
        ),
        right=Literal(4))
示例#4
0
def test_unary_operator():
    assert DefaultParser().parse('1*!!true-2') == BinaryExpression(
        operator=_ops['-'],
        left=BinaryExpression(operator=_ops['*'],
                              left=Literal(1),
                              right=UnaryExpression(operator=_ops['!'],
                                                    right=UnaryExpression(
                                                        operator=_ops['!'],
                                                        right=Literal(True)))),
        right=Literal(2))
示例#5
0
def test_binary_expression_encapsulation():
    assert DefaultParser().parse('2+3*4==5/6-7') == BinaryExpression(
        operator=_ops['=='],
        left=BinaryExpression(
            operator=_ops['+'],
            left=Literal(2),
            right=BinaryExpression(operator=_ops['*'],
                                   left=Literal(3),
                                   right=Literal(4)),
        ),
        right=BinaryExpression(operator=_ops['-'],
                               left=BinaryExpression(operator=_ops['/'],
                                                     left=Literal(5),
                                                     right=Literal(6)),
                               right=Literal(7)))
示例#6
0
def test_object_literal():
    assert DefaultParser().parse('{foo: "bar", tek: 1+2}') == ObjectLiteral({
        'foo':
        Literal('bar'),
        'tek':
        BinaryExpression(operator=_ops['+'], left=Literal(1), right=Literal(2))
    })
示例#7
0
def test_attribute_all_operands():
    assert DefaultParser().parse(
        '"foo".length + {foo: "bar"}.foo') == BinaryExpression(
            operator=_ops['+'],
            left=Identifier('length', subject=Literal('foo')),
            right=Identifier(value='foo',
                             subject=ObjectLiteral({'foo': Literal('bar')})))
示例#8
0
def test_filters():
    assert DefaultParser().parse('foo[1][.bar[0]=="tek"].baz') == Identifier(
        value='baz',
        subject=FilterExpression(
            relative=True,
            expression=BinaryExpression(operator=_ops['=='],
                                        left=FilterExpression(
                                            relative=False,
                                            expression=Literal(0),
                                            subject=Identifier('bar',
                                                               relative=True)),
                                        right=Literal('tek')),
            subject=FilterExpression(relative=False,
                                     expression=Literal(1),
                                     subject=Identifier('foo'))))
示例#9
0
def test_arbitrary_whitespace():
    assert DefaultParser().parse('\t2\r\n+\n\r3\n\n') == BinaryExpression(
        operator=_ops['+'], left=Literal(2), right=Literal(3))
示例#10
0
def test_complex_binary_operator_balancing():
    assert DefaultParser().parse('a.b == c.d') == BinaryExpression(
        operator=_ops['=='],
        left=Identifier('b', subject=Identifier('a')),
        right=Identifier('d', subject=Identifier('c')))
示例#11
0
def test_attribute_subexpression():
    assert DefaultParser().parse('("foo" + "bar").length') == Identifier(
        value='length',
        subject=BinaryExpression(operator=_ops['+'],
                                 left=Literal('foo'),
                                 right=Literal('bar')))
示例#12
0
def test_binary_expression():
    assert DefaultParser().parse('1+2') == BinaryExpression(operator=_ops['+'],
                                                            left=Literal(1),
                                                            right=Literal(2))
示例#13
0
def test_chained_identifiers():
    assert DefaultParser().parse('foo.bar.baz + 1') == BinaryExpression(
        operator=_ops['+'],
        left=Identifier('baz',
                        subject=Identifier('bar', subject=Identifier('foo'))),
        right=Literal(1))
示例#14
0
def test_array_literals():
    assert DefaultParser().parse('["foo", 1+2]') == ArrayLiteral([
        Literal('foo'),
        BinaryExpression(operator=_ops['+'], left=Literal(1), right=Literal(2))
    ])