Exemplo n.º 1
0
def test_duplicates():
    with pytest.raises(SemanticError):
        loads('''
a = 1
b = 2
a = 11
''')
Exemplo n.º 2
0
def test_unused():
    # unused names are ok now
    loads('''
def f():
    x = 1
    return 2
''')
Exemplo n.º 3
0
def test_undefined():
    with pytest.raises(SemanticError):
        loads('''
with_undefined = {
'param': dunno
}
x = another_undefined
''')
Exemplo n.º 4
0
def test_wildcard_overwrite():
    cf = loads('a, b = 1, 2')
    assert cf.dumps(entry_points=['b']) == '_, b = 1, 2\n'

    cf = cf.string_input('a = 5')
    assert cf.dumps() == 'a = 5\n_, b = 1, 2\n'

    cf = cf.string_input('b = 6')
    assert cf.dumps() == 'a = 5\nb = 6\n'

    assert loads('a, _ = b = 1, 2').dumps(entry_points=['b']) == 'b = 1, 2\n'
Exemplo n.º 5
0
def test_exception_type():
    with pytest.raises(KeyError):
        loads('''
a = {'0': 1}[0]
b = a
''').b
    with pytest.raises(KeyError):
        loads('''
a = {'0': 1}[0]
def b(x=a):
    return x
''').b
Exemplo n.º 6
0
def test_cycles():
    with pytest.raises(SemanticError):
        loads('''
a = a
x = {"y": y}
y = {
    'good': t,
    'list': [1, 2, z]
}
z = {'nested': x}
t = 1
''')
Exemplo n.º 7
0
def test_import():
    rm = load('imports/imports.config')
    assert rm.numpy == np
    rm.r
    rm.os
    rm.std
    rm.mean
    rm = loads('import numpy.linalg; inv = numpy.linalg.inv')
    rm.inv
Exemplo n.º 8
0
def test_dumps(subtests, tests_path):
    for path in tests_path.glob('**/*.config'):
        with subtests.test(path=path):
            config = load(path)
            # for each entry point the config must be readable and give the same results
            for name in list(config) + [None, []]:
                source = config.dumps(name)
                assert config.dumps(name) == source, path
                assert loads(source).dumps() == source, path
Exemplo n.º 9
0
def test_local_duplicates():
    assert loads('''
def f():
    x = 1
    x = 2
    return x
    
def g(x):
    x = 1
    return 2
    ''').f() == 2
Exemplo n.º 10
0
def test_wildcards():
    cf = loads('x, y, z = range(3)\nt = x + y + z')
    assert (cf.x, cf.y, cf.z) == (0, 1, 2)
    assert cf.t == 3
    cf = loads('x, y, z = range(3)\nt = x + y + z').string_input('y = 5')
    assert (cf.x, cf.y, cf.z) == (0, 5, 2)
    assert cf.t == 7
    assert cf.dumps() == 'x, _, z = range(3)\ny = 5\nt = x + y + z\n'
    cf = loads('x, *y, z = range(4)')
    assert (cf.x, cf.y, cf.z) == (0, [1, 2], 3)
    cf = loads('x, *y, z = range(4)').string_input('y = 2')
    assert (cf.x, cf.y, cf.z) == (0, 2, 3)
    assert cf.dumps() == 'x, *_, z = range(4)\ny = 2\n'

    # iterables
    cf = loads('x, y, z = map(int, range(3))\nt = x + y + z')
    assert (cf.x, cf.y, cf.z) == (0, 1, 2)
    assert cf.t == 3
    with pytest.raises(ValueError):
        loads('x, y, z = a, b, c = map(int, range(3))\nt = x').t
    with pytest.raises(ValueError):
        loads('x, y, z = a, b, c = map(int, range(3))\nt = a + x').t
Exemplo n.º 11
0
def test_update():
    rm = loads('a = 1').update(a=2)
    rm2 = loads('a = 1').string_input('a = 2')
    assert rm.a == rm2.a

    rm = loads('a = 1').update(a=2).string_input('a = 3')
    assert rm.a == 3

    with pytest.raises(RuntimeError):
        loads('a = 1').update(a=2).dumps()

    with pytest.raises(RuntimeError):
        rm = loads('a = 1')
        rm.a
        rm.update(a=2)
Exemplo n.º 12
0
def test_unpacking():
    rm = load('statements/funcdef.config')
    assert rm.unpack([1, 2]) == 3
    assert rm.nested_unpack([1, [2, 3]]) == (1, 2, 3)
    assert rm.deep_unpack([[[[[[1]]]]], 2]) == (1, 2)
    assert rm.single_unpack([[[1]]]) == [[1]]
    with pytest.raises(TypeError):
        rm.unpack(1)
    with pytest.raises(ValueError):
        rm.unpack([1])
    with pytest.raises(ValueError):
        rm.unpack([1, 2, 3])
    assert loads('def f(x): a, *b = x; return a').f((1, 2, 3)) == 1

    with pytest.raises(SyntaxError):
        loads('_ = 1, 2')
    with pytest.raises(SyntaxError):
        loads('_, _ = 1, 2')
    with pytest.raises(SyntaxError):
        loads('a, a = 1, 2')
Exemplo n.º 13
0
def test_comprehensions():
    rm = load('expressions/comprehensions.config')
    assert rm.everything == [
        list(range(10)), [0, 6], [1, 2, 3], [1, 3], [1, 3]
    ]
    assert rm.set_comp == {i for i in range(10)}
    assert rm.dict_comp == {i: i + 1 for i in range(10)}
    assert list(rm.gen_comp) == list(range(10))
    assert rm.even == list(range(0, 10, 2))

    with pytest.raises(SemanticError):
        loads('v = [x for i in range(1)]')

    with pytest.raises(SemanticError):
        loads('v = [[x, i] for i in range(1)]')

    with pytest.raises(SemanticError):
        loads('v = [i for i in [[2]] if x != 2 for x in i]')
Exemplo n.º 14
0
def test_import_in_string():
    rm = loads('from .expressions.literals import *')
    assert rm.literals[0]
Exemplo n.º 15
0
def test_same_line():
    cf = loads('a = 1; b = 2; c = a + b')
    assert cf.c == 3
    assert cf.dumps() == 'a = 1\nb = 2\nc = a + b'
Exemplo n.º 16
0
def test_read_only():
    with pytest.raises(SemanticError):
        loads('''
__file__ = 1
''')
Exemplo n.º 17
0
def test_bad_shortcut():
    with pytest.raises(ImportError):
        loads('from a.b import *')
Exemplo n.º 18
0
def test_injections():
    with pytest.raises(SemanticError):
        Config(injections={'print': int})

    assert loads('a = 1 + b', injections={'b': 14}).a == 15