def test_list_mult_speed(): config = DraconicConfig(max_loops=10000, max_const_len=10000) i = DraconicInterpreter(config=config) expr = """ while True: a = [0] * 10000 """.strip() with pytest.raises(TooManyStatements): i.execute(expr)
def i(): config = DraconicConfig() inter = DraconicInterpreter(config=config) inter.out__ = [] def foo(v): inter.out__.append(v) inter.builtins['print'] = foo inter.builtins['range'] = range return inter
def test_use_func(self): self.s = DraconicInterpreter(builtins={ "list": list, "map": map, "str": str }) self.t('list(map(str, [-1, 0, 1]))', ['-1', '0', '1']) with self.assertRaises(NotDefined): self.s.eval('list(map(bad, [-1, 0, 1]))') with self.assertRaises(NotDefined): self.s.eval('dir(str)') with self.assertRaises(FeatureNotAvailable): self.s.eval('str.__dict__') self.s = DraconicInterpreter(builtins={"dir": dir, "str": str}) self.t('dir(str)', dir(str))
def test_use_func(self): self.s = DraconicInterpreter(builtins={ "list": list, "map": map, "str": str }) self.t("list(map(str, [-1, 0, 1]))", ["-1", "0", "1"]) with self.assertRaises(NotDefined): self.s.eval("list(map(bad, [-1, 0, 1]))") with self.assertRaises(NotDefined): self.s.eval("dir(str)") with self.assertRaises(FeatureNotAvailable): self.s.eval("str.__dict__") self.s = DraconicInterpreter(builtins={"dir": dir, "str": str}) self.t("dir(str)", dir(str))
class DRYTest(unittest.TestCase): """Stuff we need to do every test, let's do here instead.. Don't Repeat Yourself.""" def setUp(self): """initialize a SimpleEval""" self.s = DraconicInterpreter() def assertRaises(self, expected_exception, *args, **kwargs): return utils.raises(expected_exception) def t(self, expr, shouldbe): # pylint: disable=invalid-name """test an evaluation of an expression against an expected answer""" return self.assertEqual(self.s.eval(expr), shouldbe)
def i(): # 1000-size iterables, don't limit us by loops, signed 32b int limit config = DraconicConfig(max_loops=99999999, max_const_len=1000, max_int_size=32) return DraconicInterpreter(config=config)
class TestCompoundTypes(DRYTest): """ Test the compound-types edition of the library """ def test_dict(self): self.t('{}', {}) self.t('{"foo": "bar"}', {'foo': 'bar'}) self.t('{"foo": "bar"}["foo"]', 'bar') self.t('dict()', {}) self.t('dict(a=1)', {'a': 1}) def test_dict_contains(self): self.t('{"a":22}["a"]', 22) with self.assertRaises(KeyError): self.t('{"a":22}["b"]', 22) self.t('{"a": 24}.get("b", 11)', 11) self.t('"a" in {"a": 24}', True) def test_tuple(self): self.t('()', ()) self.t('(1,)', (1, )) self.t('(1, 2, 3, 4, 5, 6)', (1, 2, 3, 4, 5, 6)) self.t('(1, 2) + (3, 4)', (1, 2, 3, 4)) self.t('(1, 2, 3)[1]', 2) self.t('tuple()', ()) self.t('tuple("foo")', ('f', 'o', 'o')) def test_tuple_contains(self): self.t('("a","b")[1]', 'b') with self.assertRaises(IndexError): self.t('("a","b")[5]', 'b') self.t('"a" in ("b","c","a")', True) def test_list(self): self.t('[]', []) self.t('[1]', [1]) self.t('[1, 2, 3, 4, 5]', [1, 2, 3, 4, 5]) self.t('[1, 2, 3][1]', 2) self.t('list()', []) self.t('list("foo")', ['f', 'o', 'o']) def test_list_contains(self): self.t('["a","b"][1]', 'b') with self.assertRaises(IndexError): self.t('("a","b")[5]', 'b') self.t('"b" in ["a","b"]', True) def test_set(self): self.t('{1}', {1}) self.t('{1, 2, 1, 2, 1, 2, 1}', {1, 2}) self.t('set()', set()) self.t('set("foo")', {'f', 'o'}) self.t('2 in {1,2,3,4}', True) self.t('22 not in {1,2,3,4}', True) def test_not(self): self.t('not []', True) self.t('not [0]', False) self.t('not {}', True) self.t('not {0: 1}', False) self.t('not {0}', False) def test_use_func(self): self.s = DraconicInterpreter(builtins={ "list": list, "map": map, "str": str }) self.t('list(map(str, [-1, 0, 1]))', ['-1', '0', '1']) with self.assertRaises(NotDefined): self.s.eval('list(map(bad, [-1, 0, 1]))') with self.assertRaises(NotDefined): self.s.eval('dir(str)') with self.assertRaises(FeatureNotAvailable): self.s.eval('str.__dict__') self.s = DraconicInterpreter(builtins={"dir": dir, "str": str}) self.t('dir(str)', dir(str))
def setUp(self): """ initialize a SimpleEval """ self.s = DraconicInterpreter()
class TestCompoundTypes(DRYTest): """Test the compound-types edition of the library""" def test_dict(self): self.t("{}", {}) self.t('{"foo": "bar"}', {"foo": "bar"}) self.t('{"foo": "bar"}["foo"]', "bar") self.t("dict()", {}) self.t("dict(a=1)", {"a": 1}) def test_dict_contains(self): self.t('{"a":22}["a"]', 22) with self.assertRaises(KeyError): self.t('{"a":22}["b"]', 22) self.t('{"a": 24}.get("b", 11)', 11) self.t('"a" in {"a": 24}', True) def test_tuple(self): self.t("()", ()) self.t("(1,)", (1, )) self.t("(1, 2, 3, 4, 5, 6)", (1, 2, 3, 4, 5, 6)) self.t("(1, 2) + (3, 4)", (1, 2, 3, 4)) self.t("(1, 2, 3)[1]", 2) self.t("tuple()", ()) self.t('tuple("foo")', ("f", "o", "o")) def test_tuple_contains(self): self.t('("a","b")[1]', "b") with self.assertRaises(IndexError): self.t('("a","b")[5]', "b") self.t('"a" in ("b","c","a")', True) def test_list(self): self.t("[]", []) self.t("[1]", [1]) self.t("[1, 2, 3, 4, 5]", [1, 2, 3, 4, 5]) self.t("[1, 2, 3][1]", 2) self.t("list()", []) self.t('list("foo")', ["f", "o", "o"]) def test_list_contains(self): self.t('["a","b"][1]', "b") with self.assertRaises(IndexError): self.t('("a","b")[5]', "b") self.t('"b" in ["a","b"]', True) def test_set(self): self.t("{1}", {1}) self.t("{1, 2, 1, 2, 1, 2, 1}", {1, 2}) self.t("set()", set()) self.t('set("foo")', {"f", "o"}) self.t("2 in {1,2,3,4}", True) self.t("22 not in {1,2,3,4}", True) def test_not(self): self.t("not []", True) self.t("not [0]", False) self.t("not {}", True) self.t("not {0: 1}", False) self.t("not {0}", False) def test_use_func(self): self.s = DraconicInterpreter(builtins={ "list": list, "map": map, "str": str }) self.t("list(map(str, [-1, 0, 1]))", ["-1", "0", "1"]) with self.assertRaises(NotDefined): self.s.eval("list(map(bad, [-1, 0, 1]))") with self.assertRaises(NotDefined): self.s.eval("dir(str)") with self.assertRaises(FeatureNotAvailable): self.s.eval("str.__dict__") self.s = DraconicInterpreter(builtins={"dir": dir, "str": str}) self.t("dir(str)", dir(str))