示例#1
0
def test_attribute_arithmetic_add_mul():
    result = parse(
        {
            "op": "eq",
            "args": [
                {"property": "attr"},
                {
                    "op": "+",
                    "args": [
                        3,
                        {"op": "*", "args": [5, 2]},
                    ],
                },
            ],
        }
    )
    assert result == ast.Equal(
        ast.Attribute("attr"),
        ast.Add(
            3,
            ast.Mul(
                5,
                2,
            ),
        ),
    )
示例#2
0
def test_arithmetic():
    # test possible optimizations
    result = optimize(parse("attr = 10 + 10"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        20
    )

    result = optimize(parse("attr = 30 - 10"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        20
    )

    result = optimize(parse("attr = 10 * 2"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        20
    )

    result = optimize(parse("attr = 40 / 2"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        20
    )

    # test imppossible optimizations
    result = optimize(parse("attr = other + 10"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Add(
            ast.Attribute('other'), 10
        ),
    )

    result = optimize(parse("attr = other - 10"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Sub(
            ast.Attribute('other'), 10
        ),
    )

    result = optimize(parse("attr = other * 2"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Mul(
            ast.Attribute('other'), 2
        ),
    )

    result = optimize(parse("attr = other / 2"))
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Div(
            ast.Attribute('other'), 2
        ),
    )
示例#3
0
def test_attribute_arithmetic_add():
    result = parse('attr = 5 + 2')
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Add(
            5,
            2,
        ),
    )
示例#4
0
def test_attribute_arithmetic_add():
    result = parse(['==', ['get', 'attr'], ['+', 5, 2]])
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Add(
            5,
            2,
        ),
    )
示例#5
0
def test_attribute_arithmetic_add_mul():
    result = parse('attr = 3 + 5 * 2')
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Add(
            3,
            ast.Mul(
                5,
                2,
            ),
        ),
    )
示例#6
0
def test_attribute_arithmetic_add():
    result = parse({
        "eq": [
            {"property": "attr"},
            {"+": [5, 2]}
        ]
    })
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Add(
            5,
            2,
        ),
    )
示例#7
0
def test_function():
    def myadder(a, b):
        return a + b

    result = optimize(parse("attr = myadder(1, 2)"), {"myadder": myadder})
    assert result == ast.Equal(
        ast.Attribute('attr'),
        3,
    )

    # can't optimize a function referencing an attribute
    result = optimize(parse("attr = myadder(other, 2)"), {"myadder": myadder})
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Function(
            "myadder", [
                ast.Attribute("other"),
                2
            ]
        )
    )
    # can't optimize a function with a nested reference to an attribute
    result = optimize(
        parse("attr = myadder(other + 2, 2)"), {"myadder": myadder}
    )
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Function(
            "myadder", [
                ast.Add(ast.Attribute("other"), 2),
                2
            ]
        )
    )

    # can't optimize an unknown functions
    result = optimize(parse("attr = unkown(1, 2)"), {"myadder": myadder})
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Function(
            "unkown", [
                1,
                2,
            ]
        )
    )
示例#8
0
def test_attribute_arithmetic_add_mul():
    result = parse({
        "eq": [
            {"property": "attr"},
            {"+": [
                3,
                {"*": [5, 2]},
            ]},
        ],
    })
    assert result == ast.Equal(
        ast.Attribute('attr'),
        ast.Add(
            3,
            ast.Mul(
                5,
                2,
            ),
        ),
    )