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_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_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_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_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_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_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_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 setUp(self): Executor(should_trace=True).execute( With( 'unix:///var/run/docker.sock', lambda docker: Map( lambda x: Seq(lambda: ContainerRemove(x)), Fil(lambda x: x, ContainerList(filters={'label': 'test'}))) ))
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_callable_00(self): ex = With('a', 'b', 'c', lambda x, y, z: Seq(lambda: x)) try: ret = Executor(should_trace=True).execute(ex) except ExecError as e: self.assertIsInstance(e.e, OpError) self.assertEquals('`a` returned to Eval is not an Op', e.e.reason) self.assertEqual(Loc.from_frame_idx(2).shift(-9), e.e.loc)
def test_with(self): ex = With( 'a', # anything that is not a variable must be matched to a Con 'b', 'c', lambda x, y, z: Seq(x + y + z)) ret = Executor(should_trace=True).execute(ex) self.assertEqual('abc', ret)
def test_fun(self): ex = Fun( # we need to understand that lambdas are executed immediately upon object construction, # so there would be no real hell in debugging this lambda x, y, z: Seq(x, y, z)) ex = ex('a', 'b', 'c') ret = Executor(should_trace=True).execute(ex) self.assertEqual('c', ret)
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_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_start(self): cmd = ['sh', '-c', 'echo asd'] expr = With( 'unix:///var/run/docker.sock', lambda docker: With( ImagePull('alpine:3.5'), lambda i: With( lambda: ContainerCreate( i, 'test_simple', cmd, ContainerConfig(labels={'test': '1'})), lambda c: Seq( With( Match( lambda: Map( lambda x: x, Fil( lambda x: x['Names'][0] == '/test_simple', ContainerList(), )), lambda m: [ Case(m.len == 1, m[0]), Case( True, Err('Wrong number of containers: %s', m .len)), ]), lambda container: Match( container, lambda m: [ Case(m['Command'] == cmd, m), Case(True, Err('Command is incorrect: "%s"', m)) ])), # notice the difference between dynamically generated steps from bound variables lambda: ContainerStart(c), lambda: Seq(lambda: Eval(lambda: sleep(1)), ), # todo this will hang if the container managed to stop before it's called lambda: ContainerWait(c), lambda: ContainerLogs(c), lambda: ContainerRemove(c), # and the return value of the actual variable c)))) r = Executor(should_trace=True).execute(expr) print(r)
def test_match(self): ex = Match( 'a', # anything that is not a variable must be matched to a Con lambda m: [ Case( m.len > 1, Err('Can not be longer than 1 %s', m), ), Case( m.len == 1, m[0], ), Case( m.len == 0, Err('Can not be empty %s', m), ), ]) ret = Executor(should_trace=True).execute(ex) self.assertEqual(ret, 'a')
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_assert_single_0(self): expr = assert_single([1]) ret = Executor().execute(expr) self.assertEqual(1, ret)
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_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 executor(body): return Executor(should_trace=True).execute(body)