Exemple #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))
Exemple #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))
    })
Exemple #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')})))
Exemple #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))
Exemple #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))
Exemple #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))
Exemple #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))
Exemple #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'))))
Exemple #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')))))
Exemple #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)))
Exemple #11
0
def test_literal():
    assert DefaultParser().parse('1') == Literal(1.0)
Exemple #12
0
def test_array_literals():
    assert DefaultParser().parse('["foo", 1+2]') == ArrayLiteral([
        Literal('foo'),
        BinaryExpression(operator=_ops['+'], left=Literal(1), right=Literal(2))
    ])
Exemple #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'))
Exemple #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))
Exemple #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'))
Exemple #16
0
def test_ternary_expression():
    assert DefaultParser().parse('foo ? 1 : 0') == ConditionalExpression(
        test=Identifier('foo'), consequent=Literal(1), alternate=Literal(0))
Exemple #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')))
Exemple #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))
Exemple #19
0
def test_binary_expression():
    assert DefaultParser().parse('1+2') == BinaryExpression(operator=_ops['+'],
                                                            left=Literal(1),
                                                            right=Literal(2))
Exemple #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')))
Exemple #21
0
def test_nexted_array_literals():
    assert DefaultParser().parse('["foo", ["bar", "tek"]]') == ArrayLiteral(
        [Literal('foo'),
         ArrayLiteral([Literal('bar'), Literal('tek')])])
Exemple #22
0
def test_attribute_array():
    assert DefaultParser().parse('["foo", "bar"].length') == Identifier(
        value='length', subject=ArrayLiteral([Literal('foo'),
                                              Literal('bar')]))
Exemple #23
0
def test_nested_object_literals():
    assert DefaultParser().parse('{foo: {bar: "tek"}}') == ObjectLiteral(
        {'foo': ObjectLiteral({'bar': Literal('tek')})})