コード例 #1
0
    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)
コード例 #2
0
    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)
コード例 #3
0
    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)
コード例 #4
0
    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)
コード例 #5
0
    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)
コード例 #6
0
    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)
コード例 #7
0
    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)
コード例 #8
0
    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)
コード例 #9
0
    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)
コード例 #10
0
    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)
コード例 #11
0
    def test_with(self):
        ex = Executor()
        r = ex.execute(With(Var('a'), Con(5), Eval(Var('a'), '5 + 1')))

        self.assertEqual(6, r)