def test_attrs_0(self): ex = Executor(should_trace=True) r = ex.execute(Con([1, 2, 3])[1:2]) self.assertEqual([2], r) r = ex.execute(Con([1, 2, 3]).len) self.assertEqual(3, r)
def test_fil_1(self): ex = Executor(should_trace=True) inp = [1, 2, 3] r = ex.execute(Fil( Var('x'), Var('x') > Con(1), Con(inp), )) self.assertEqual([x for x in inp if x > 1], r)
def test_fil_err_0(self): ex = Executor(should_trace=True) try: r = ex.execute(Fil( Var('x'), Var('x') > Con(1), Con(1), )) except ExecError as e: self.assertIsInstance(e.e, OpError) self.assertEqual('Returned iterable `1` is not a list', e.e.reason) self.assertEqual(Loc.from_frame_idx(2).shift(-10), e.e.loc)
def test_match_2(self): ex = Executor() r = ex.execute( Match( Var('x'), Con(5), Case(Eval(Var('x'), lambda x: x == 1), Con('a')), Case(Eval(Var('x'), lambda x: x == 2), Con('b')), Case(Eval(Var('x'), lambda x: x == 5), Con('c')), )) self.assertEqual('c', r)
def test_fun_0(self): ex = Executor() r = ex.execute( With( Var('fn'), Fun( Var('x'), Var('y'), Var('z'), Eval(Var('x'), Var('y'), Var('z'), lambda x, y, z: [x, y, z]), ), Call(Var('fn'), Con(5), Con(6), Con(7)))) self.assertEqual([5, 6, 7], r)
def test_image(self): expr = With(Con('unix:///var/run/docker.sock'), lambda docker: Seq(ImagePull('alpine:3.5'))) r = Executor(should_trace=True).execute(expr) print(r)
def test_callable_wrapped_0(self): ex = With('a', 'b', 'c', lambda x, y, z: With(lambda: Con(x), lambda w: w)) ret = Executor(should_trace=True).execute(ex) self.assertEqual('a', ret)
def test_callable_1(self): ex = With( 'a', 'b', 'c', lambda x, y, z: Seq(With('d', lambda d: Seq(lambda: Con(d))))) ret = Executor(should_trace=True).execute(ex) self.assertEqual('d', ret)
def test_map_0(self): ex = Executor(should_trace=True) inp = [1, 2, 3] r = ex.execute(Map( lambda x: x * x, Con(inp), )) self.assertEqual([x * x for x in inp], r)
def test_fil_0(self): ex = Executor(should_trace=True) inp = [1, 2, 3] r = ex.execute(Fil( lambda b: b > 1, Con(inp), )) self.assertEqual([x for x in inp if x > 1], r)
def test_err_0(self): ex = Executor(should_trace=True) try: ex.execute( Match( Var('m'), Con(5), Case(Eval(Var('m'), lambda m: m == 5), Err('No matching branches found %s', Var('m'))))) except ExecError as e: self.assertIsInstance(e.e, OpError) self.assertEqual('No matching branches found 5', e.e.reason) self.assertEqual(Loc.from_frame_idx(2).shift(-7), e.e.loc)
def test_fil_02(self): ex = Executor(should_trace=True) inp = [1, 2, 3] try: r = ex.execute(Fil( lambda b: lambda: True, Con(inp), )) except ExecError as e: self.assertIsInstance(e.e, OpError) self.assertEqual('`True` returned to Eval is not an Op', e.e.reason) self.assertEqual(Loc.from_frame_idx(2).shift(-6), e.e.loc)
def test_seq_0(self): # assume a variable generated is always unique. # a context. # a) save all of the variable mappings and then pass them down the context # b) allow only global variables and therefore allow to directly depend upon them # c) prog = Iter( Var('id'), Con(0), Eval(Var('id'), '(a, a + 1) if a < 10 else (a, None)'), With( Var('a'), Eval(Var('id'), 'a + 1 if a < 10 else None'), Eval(Var('a'), '0'), )) self.assertEqual(0, Executor(should_trace=True).execute(prog))
def test_iter_0(self): last_items = [] def last_item_updater(x): last_items.append(x) return x ex = Executor() r = ex.execute( Iter( Var('x'), Con(0), Eval(Var('x'), lambda x: ((x, x + 1) if x < 5 else (x, None))), Eval(Var('x'), last_item_updater), )) self.assertEqual(4, r) self.assertEqual([0, 1, 2, 3, 4], last_items)
def test_callable_2_1(self): ex = Map(lambda x: Seq(lambda: Con(x), ), [1, 2, 3]) self.assertEqual([1, 2, 3], Executor(should_trace=True).execute(ex))
def test_callable_2_0(self): with self.assertRaises(KeyError): ex = Map(lambda x: lambda: Con(x), [1, 2, 3]) self.assertEqual([1, 2, 3], Executor(should_trace=True).execute(ex))
def test_par_3(self): self.assertEqual([1, 2, 3], executor(Par(Con(1), Con(2), Con(3))))
def test_seq_2(self): self.assertEqual(2, executor(Seq(Con(1), Con(2))))
def test_seq_3(self): self.assertEqual(3, executor(Seq(Con(1), Con(2), Con(3))))
def test_with(self): ex = Executor() r = ex.execute(With(Var('a'), Con(5), Eval(Var('a'), '5 + 1'))) self.assertEqual(6, r)
def test_par_1(self): self.assertEqual([1], executor(Par(Con(1))))
def test_par_2(self): self.assertEqual([1, 2], executor(Par(Con(1), Con(2))))
def test_seq_1(self): self.assertEqual(1, executor(Seq(Con(1))))
def test_eval(self): self.assertEqual(5, executor(Eval(Con(2), Con(3), 'a + b'))) self.assertEqual(5, executor(Eval(Con(2), Con(3), lambda a, b: a + b))) self.assertEqual(5, executor(Eval(Log(Eval(Con(2), Con(3), lambda a, b: Con(a + b))))))