Exemplo n.º 1
0
def test_attribute():
    e = Evaluator()
    e.symbol_table['dt'] = datetime.now()
    e.run('x = dt.year')
    assert e.symbol_table['x'] == e.symbol_table['dt'].year

    err = 'You can not access `test_test_test` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('y = dt.test_test_test')

    assert 'y' not in e.symbol_table

    err = 'You can not access `asdf` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('z = x.asdf')

    e.symbol_table['math'] = math
    err = 'You can not access `__module__` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('math.__module__')

    e.symbol_table['datetime'] = datetime
    err = 'You can not access `test_test` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('datetime.test_test')
Exemplo n.º 2
0
def test_attribute():
    e = Evaluator()
    e.symbol_table["dt"] = datetime.now()
    e.run("x = dt.year")
    assert e.symbol_table["x"] == e.symbol_table["dt"].year

    err = "You can not access `test_test_test` attribute"
    with pytest.raises(BadSyntax, match=err):
        e.run("y = dt.test_test_test")

    assert "y" not in e.symbol_table

    err = "You can not access `asdf` attribute"
    with pytest.raises(BadSyntax, match=err):
        e.run("z = x.asdf")

    e.symbol_table["math"] = math
    err = "You can not access `__module__` attribute"
    with pytest.raises(BadSyntax, match=err):
        e.run("math.__module__")

    e.symbol_table["datetime"] = datetime
    err = "You can not access `test_test` attribute"
    with pytest.raises(BadSyntax, match=err):
        e.run("datetime.test_test")
Exemplo n.º 3
0
def test_generator_exp():
    e = Evaluator()
    e.symbol_table['r'] = [1, 2, 3]
    err = 'Defining new generator expression is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('x = (i ** 2 for i in r)')
    assert 'x' not in e.symbol_table
Exemplo n.º 4
0
def test_generator_exp():
    e = Evaluator()
    e.symbol_table["r"] = [1, 2, 3]
    err = "Defining new generator expression is not allowed"
    with pytest.raises(BadSyntax, match=err):
        e.run("x = (i ** 2 for i in r)")
    assert "x" not in e.symbol_table
Exemplo n.º 5
0
def test_assert():
    e = Evaluator()
    err = "You can not use assertion syntax"
    with pytest.raises(BadSyntax, match=err):
        e.run("assert True")

    with pytest.raises(BadSyntax, match=err):
        e.run("assert False")
Exemplo n.º 6
0
def test_annassign():
    e = Evaluator()

    err = "You can not use annotation syntax"
    with pytest.raises(BadSyntax, match=err):
        e.run("a: int = 10")

    assert "a" not in e.symbol_table
Exemplo n.º 7
0
def test_annassign():
    e = Evaluator()

    err = 'You can not use annotation syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run('a: int = 10')

    assert 'a' not in e.symbol_table
Exemplo n.º 8
0
def test_assert():
    e = Evaluator()
    err = 'You can not use assertion syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run('assert True')

    with pytest.raises(BadSyntax, match=err):
        e.run('assert False')
Exemplo n.º 9
0
def test_slice():
    e = Evaluator()
    e.symbol_table['obj'] = GetItemSpy()
    e.run('obj[10:20:3]')
    s = e.symbol_table['obj'].queue.pop()
    assert isinstance(s, slice)
    assert s.start == 10
    assert s.stop == 20
    assert s.step == 3
Exemplo n.º 10
0
def test_index():
    e = Evaluator()
    e.symbol_table['obj'] = GetItemSpy()
    e.run('obj[10]')
    index = e.symbol_table['obj'].queue.pop()
    assert index == 10
    e.run('obj["asdf"]')
    index = e.symbol_table['obj'].queue.pop()
    assert index == 'asdf'
Exemplo n.º 11
0
def test_with():
    e = Evaluator()
    err = 'You can not use `with` syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run('''
with some:
    x = 1
''')
    assert 'x' not in e.symbol_table
Exemplo n.º 12
0
def test_classdef():
    e = Evaluator()
    err = 'Defining new class via def syntax is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('''
class ABCD:
    pass

''')
    assert 'ABCD' not in e.symbol_table
Exemplo n.º 13
0
def test_functiondef():
    e = Evaluator()
    err = 'Defining new function via def syntax is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('''
def abc():
    pass

''')
    assert 'abc' not in e.symbol_table
Exemplo n.º 14
0
def test_with():
    e = Evaluator()
    err = "You can not use `with` syntax"
    with pytest.raises(BadSyntax, match=err):
        e.run(
            """
with some:
    x = 1
"""
        )
    assert "x" not in e.symbol_table
Exemplo n.º 15
0
def test_asyncfor():
    e = Evaluator()
    e.symbol_table['r'] = 0
    err = 'You can not use `async for` loop syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run('''
async for x in [1, 2, 3, 4]:
    r += x

''')
    assert e.symbol_table['r'] == 0
Exemplo n.º 16
0
def test_try():
    e = Evaluator()
    err = 'You can not use `try` syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run('''
try:
    x = 1
except:
    pass
''')
    assert 'x' not in e.symbol_table
Exemplo n.º 17
0
def test_asyncwith():
    e = Evaluator()
    e.symbol_table['r'] = 0
    err = 'You can not use `async with` syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run('''
async with x():
    r += 100

''')
    assert e.symbol_table['r'] == 0
Exemplo n.º 18
0
def test_classdef():
    e = Evaluator()
    err = "Defining new class via def syntax is not allowed"
    with pytest.raises(BadSyntax, match=err):
        e.run(
            """
class ABCD:
    pass

"""
        )
    assert "ABCD" not in e.symbol_table
Exemplo n.º 19
0
def test_functiondef():
    e = Evaluator()
    err = "Defining new function via def syntax is not allowed"
    with pytest.raises(BadSyntax, match=err):
        e.run(
            """
def abc():
    pass

"""
        )
    assert "abc" not in e.symbol_table
Exemplo n.º 20
0
def test_asyncfor():
    e = Evaluator()
    e.symbol_table["r"] = 0
    err = "You can not use `async for` loop syntax"
    with pytest.raises(BadSyntax, match=err):
        e.run(
            """
async for x in [1, 2, 3, 4]:
    r += x

"""
        )
    assert e.symbol_table["r"] == 0
Exemplo n.º 21
0
def test_try():
    e = Evaluator()
    err = "You can not use `try` syntax"
    with pytest.raises(BadSyntax, match=err):
        e.run(
            """
try:
    x = 1
except:
    pass
"""
        )
    assert "x" not in e.symbol_table
Exemplo n.º 22
0
def test_extslice():
    e = Evaluator()
    e.symbol_table['obj'] = GetItemSpy()
    e.run('obj[1,2:3,4]')
    es = e.symbol_table['obj'].queue.pop()
    assert isinstance(es, tuple)
    assert len(es) == 3
    assert es[0] == 1
    assert isinstance(es[1], slice)
    assert es[1].start == 2
    assert es[1].stop == 3
    assert es[1].step is None
    assert es[2] == 4
Exemplo n.º 23
0
def test_asyncwith():
    e = Evaluator()
    e.symbol_table["r"] = 0
    err = "You can not use `async with` syntax"
    with pytest.raises(BadSyntax, match=err):
        e.run(
            """
async with x():
    r += 100

"""
        )
    assert e.symbol_table["r"] == 0
Exemplo n.º 24
0
def test_listcomp():
    e = Evaluator()
    assert e.run('[x ** 2 for x in [1, 2, 3]]') == [1, 4, 9]
    assert 'x' not in e.symbol_table
    assert e.run('[x ** 2 + y for x in [1, 2, 3] for y in [10, 20, 30]]') == ([
        x**2 + y for x in [1, 2, 3] for y in [10, 20, 30]
    ])
    assert 'x' not in e.symbol_table
    assert 'y' not in e.symbol_table
    assert e.run('[y ** 2 for x in [1, 2, 3] for y in [x+1, x+3, x+5]]') == ([
        y**2 for x in [1, 2, 3] for y in [x + 1, x + 3, x + 5]
    ])
    assert 'x' not in e.symbol_table
    assert 'y' not in e.symbol_table
Exemplo n.º 25
0
def test_setcomp():
    e = Evaluator()
    assert e.run("{x ** 2 for x in [1, 2, 3, 3]}") == {1, 4, 9}
    assert "x" not in e.symbol_table
    assert e.run("{x ** 2 + y for x in [1, 2, 3] for y in [10, 20, 30]}") == (
        {x**2 + y for x in [1, 2, 3] for y in [10, 20, 30]}
    )
    assert "x" not in e.symbol_table
    assert "y" not in e.symbol_table
    assert e.run("{y ** 2 for x in [1, 2, 3] for y in [x+1, x+3, x+5]}") == (
        {y**2 for x in [1, 2, 3] for y in [x + 1, x + 3, x + 5]}
    )
    assert "x" not in e.symbol_table
    assert "y" not in e.symbol_table
Exemplo n.º 26
0
def test_compare():
    e = Evaluator()
    assert e.run("1 == 2") == (1 == 2)
    assert e.run("3 > 2") == (3 > 2)
    assert e.run("3 >= 2") == (3 >= 2)
    assert e.run('"A" in "America"') == ("A" in "America")
    assert e.run('"E" not in "America"') == ("E" not in "America")
    assert e.run("1 is 2") == (1 is 2)  # noqa
    assert e.run("1 is not 2") == (1 is not 2)  # noqa
    assert e.run("3 < 2") == (3 < 2)
    assert e.run("3 <= 2") == (3 <= 2)
Exemplo n.º 27
0
def test_nameconstant():
    e = Evaluator()
    assert e.run('True') is True
    assert e.run('False') is False
    assert e.run('None') is None
    e.run('x = True')
    e.run('y = False')
    e.run('z = None')
    assert e.symbol_table['x'] is True
    assert e.symbol_table['y'] is False
    assert e.symbol_table['z'] is None
Exemplo n.º 28
0
def test_nameconstant():
    e = Evaluator()
    assert e.run("True") is True
    assert e.run("False") is False
    assert e.run("None") is None
    e.run("x = True")
    e.run("y = False")
    e.run("z = None")
    assert e.symbol_table["x"] is True
    assert e.symbol_table["y"] is False
    assert e.symbol_table["z"] is None
Exemplo n.º 29
0
def test_compare():
    e = Evaluator()
    assert e.run('1 == 2') == (1 == 2)
    assert e.run('3 > 2') == (3 > 2)
    assert e.run('3 >= 2') == (3 >= 2)
    assert e.run('"A" in "America"') == ('A' in 'America')
    assert e.run('"E" not in "America"') == ('E' not in 'America')
    assert e.run('1 is 2') == (1 is 2)  # noqa
    assert e.run('1 is not 2') == (1 is not 2)  # noqa
    assert e.run('3 < 2') == (3 < 2)
    assert e.run('3 <= 2') == (3 <= 2)
Exemplo n.º 30
0
def test_binop():
    e = Evaluator()
    assert e.run('1 + 2') == 1 + 2
    assert e.run('3 & 2') == 3 & 2
    assert e.run('1 | 2') == 1 | 2
    assert e.run('3 ^ 2') == 3 ^ 2
    assert e.run('3 / 2') == 3 / 2
    assert e.run('3 // 2') == 3 // 2
    assert e.run('3 << 2') == 3 << 2
    with pytest.raises(TypeError):
        e.run('2 @ 3')
    assert e.run('3 * 2') == 3 * 2
    assert e.run('33 % 4') == 33 % 4
    assert e.run('3 ** 2') == 3**2
    assert e.run('100 >> 2') == 100 >> 2
    assert e.run('3 - 1') == 3 - 1