Пример #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_object_literal():
    assert DefaultParser().parse('{foo: "bar", tek: 1+2}') == ObjectLiteral({
        'foo':
        Literal('bar'),
        'tek':
        BinaryExpression(operator=_ops['+'], left=Literal(1), right=Literal(2))
    })
Пример #3
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')})))
Пример #4
0
def test_nested_non_grouped_ternary_expression():
    assert DefaultParser().parse(
        'foo ? bar ? 1 : 2 : 3') == ConditionalExpression(
            test=Identifier('foo'),
            consequent=ConditionalExpression(test=Identifier('bar'),
                                             consequent=Literal(1),
                                             alternate=Literal(2)),
            alternate=Literal(3))
Пример #5
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))
Пример #6
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))
Пример #7
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))
Пример #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_transforms():
    assert DefaultParser().parse(
        'foo|tr1|tr2.baz|tr3({bar:"tek"})') == Transform(
            name='tr3',
            args=[ObjectLiteral({'bar': Literal('tek')})],
            subject=Identifier('baz',
                               subject=Transform(
                                   name='tr2',
                                   args=[],
                                   subject=Transform(
                                       name='tr1',
                                       args=[],
                                       subject=Identifier('foo')))))
Пример #10
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)))
Пример #11
0
def test_literal():
    assert DefaultParser().parse('1') == Literal(1.0)
Пример #12
0
def test_array_literals():
    assert DefaultParser().parse('["foo", 1+2]') == ArrayLiteral([
        Literal('foo'),
        BinaryExpression(operator=_ops['+'], left=Literal(1), right=Literal(2))
    ])
Пример #13
0
def test_object_ternary_expression():
    assert DefaultParser().parse(
        'foo ? {bar: "tek"} : "baz"') == ConditionalExpression(
            test=Identifier('foo'),
            consequent=ObjectLiteral({'bar': Literal('tek')}),
            alternate=Literal('baz'))
Пример #14
0
def test_arbitrary_whitespace():
    assert DefaultParser().parse('\t2\r\n+\n\r3\n\n') == BinaryExpression(
        operator=_ops['+'], left=Literal(2), right=Literal(3))
Пример #15
0
def test_transforms_multiple_arguments():
    assert DefaultParser().parse('foo|bar("tek", 5, true)') == Transform(
        name='bar',
        args=[Literal('tek'), Literal(5),
              Literal(True)],
        subject=Identifier('foo'))
Пример #16
0
def test_ternary_expression():
    assert DefaultParser().parse('foo ? 1 : 0') == ConditionalExpression(
        test=Identifier('foo'), consequent=Literal(1), alternate=Literal(0))
Пример #17
0
def test_identifier_filter_expression():
    assert DefaultParser().parse('foo.bar["baz"]') == FilterExpression(
        expression=Literal('baz'),
        subject=Identifier(value='bar', subject=Identifier(value='foo')))
Пример #18
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))
Пример #19
0
def test_binary_expression():
    assert DefaultParser().parse('1+2') == BinaryExpression(operator=_ops['+'],
                                                            left=Literal(1),
                                                            right=Literal(2))
Пример #20
0
def test_attribute_subexpression():
    assert DefaultParser().parse('("foo" + "bar").length') == Identifier(
        value='length',
        subject=BinaryExpression(operator=_ops['+'],
                                 left=Literal('foo'),
                                 right=Literal('bar')))
Пример #21
0
def test_nexted_array_literals():
    assert DefaultParser().parse('["foo", ["bar", "tek"]]') == ArrayLiteral(
        [Literal('foo'),
         ArrayLiteral([Literal('bar'), Literal('tek')])])
Пример #22
0
def test_attribute_array():
    assert DefaultParser().parse('["foo", "bar"].length') == Identifier(
        value='length', subject=ArrayLiteral([Literal('foo'),
                                              Literal('bar')]))
Пример #23
0
def test_nested_object_literals():
    assert DefaultParser().parse('{foo: {bar: "tek"}}') == ObjectLiteral(
        {'foo': ObjectLiteral({'bar': Literal('tek')})})