def test_duplicates(): with pytest.raises(SemanticError): loads(''' a = 1 b = 2 a = 11 ''')
def test_unused(): # unused names are ok now loads(''' def f(): x = 1 return 2 ''')
def test_undefined(): with pytest.raises(SemanticError): loads(''' with_undefined = { 'param': dunno } x = another_undefined ''')
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'
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
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 ''')
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
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
def test_local_duplicates(): assert loads(''' def f(): x = 1 x = 2 return x def g(x): x = 1 return 2 ''').f() == 2
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
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)
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')
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]')
def test_import_in_string(): rm = loads('from .expressions.literals import *') assert rm.literals[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'
def test_read_only(): with pytest.raises(SemanticError): loads(''' __file__ = 1 ''')
def test_bad_shortcut(): with pytest.raises(ImportError): loads('from a.b import *')
def test_injections(): with pytest.raises(SemanticError): Config(injections={'print': int}) assert loads('a = 1 + b', injections={'b': 14}).a == 15