def test_duplicates(): with pytest.raises(SemanticError): read_string(''' a = 1 b = 2 a = 11 ''')
def test_unused(): with pytest.raises(SemanticError): read_string(''' def f(): x = 1 return 2 ''')
def test_undefined(): with pytest.raises(SemanticError): read_string(''' with_undefined = { 'param': dunno } x = another_undefined ''')
def test_cycles(): with pytest.raises(SemanticError): read_string(''' a = a x = {"y": y} y = { 'good': t, 'list': [1, 2, z] } z = {'nested': x} t = 1 ''')
def test_unpacking(): rm = read_config('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]) with pytest.raises(SyntaxError): read_string('a, b = 1, 2') with pytest.raises(SyntaxError): read_string('def f(x): a, *b = x; return a')
def test_entry_points(subtests, tests_path): for path in tests_path.glob('**/*.config'): with subtests.test(filename=path.name): config = read_config(path) # for each entry point the config must be readable and give the same results for name in config._scope.keys(): source = config.render_config(name) assert config.render_config(name) == source assert read_string(source).render_config() == source, path
def test_update(): rm = read_string('a = 1').update(a=2) rm2 = read_string('a = 1').string_input('a = 2') assert rm.a == rm2.a rm = read_string('a = 1').update(a=2).string_input('a = 3') assert rm.a == 3 with pytest.raises(RuntimeError): read_string('a = 1').update(a=2).render_config() with pytest.raises(RuntimeError): rm = read_string('a = 1') rm.a rm.update(a=2)
def test_comprehensions(): rm = read_config('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): read_string('_ = [x for i in range(1)]') with pytest.raises(SemanticError): read_string('_ = [[x, i] for i in range(1)]') with pytest.raises(SemanticError): read_string('_ = [i for i in [[2]] if x != 2 for x in i]')
def test_import_in_string(): rm = read_string('from .expressions.literals import *') assert rm.literals[0]
def test_read_only(): with pytest.raises(SemanticError): read_string(''' __file__ = 1 ''')
def test_exception_type(): with pytest.raises(KeyError): read_string(''' a = {'0': 1}[0] b = a ''').b
def test_bad_import(): with pytest.raises(NameError): read_string('from .expressions.literals import a')
def test_injections(): with pytest.raises(SemanticError): ResourceManager(injections={'print': int}) assert read_string('a = 1 + b', injections={'b': 14}).a == 15
def test_bad_shortcut(): with pytest.raises(ImportError): read_string('from a.b import *')