Ejemplo n.º 1
0
    def test_PyBuiltin_types(self) -> None:
        p = parser.Parser(vocabulary=parser.PyBuiltin())
        peval: AnyOp = lambda expr, *args: p.parse(expr).eval(*args)

        # Builtin Number Types
        self.assertCaseEqual(peval, [
            Case(('bool(x)', 1), {}, True),
            Case(('complex(x)', 1), {}, 1 + 0j),
            Case(('float(x)', 1), {}, 1.),
            Case(('int(x)', 1.), {}, 1.)
        ])

        # Builtin Container Types
        self.assertCaseEqual(peval, [
            Case(('bytearray(x)', 1), {}, bytearray(b'\x00')),
            Case(('bytearray(x, e)', 'x', 'utf8'), {}, bytearray(b'x')),
            Case(('bytes(x)', 1), {}, b'\x00'),
            Case(('bytes(x, e)', 'x', 'utf8'), {}, b'x'),
            Case(('dict(s)', [(1, 1)]), {}, {1: 1}),
            Case(('frozenset(l)', [1, 2]), {}, frozenset([1, 2])),
            Case(('list(a)', (1, 2, 3)), {}, [1, 2, 3]),
            Case(('list(memoryview(x))', b'x'), {}, [120]),
            Case(('str(o)', list()), {}, '[]'),
            Case(('set(l)', [1, 2, 3]), {}, {1, 2, 3}),
            Case(('tuple(l)', [1, 2, 3]), {}, (1, 2, 3))
        ])

        # Further Builtin Types
        # Note: object and type are tested within test_PyBuiltin_oop()
        self.assertCaseEqual(peval, [Case(('slice(n)', 3), {}, slice(3))])
Ejemplo n.º 2
0
    def test_PyBuiltin_constants(self) -> None:
        p = parser.Parser(vocabulary=parser.PyBuiltin())
        peval: AnyOp = lambda expr, *args: p.parse(expr).eval(*args)

        # Builtin Constants
        self.assertCaseEqual(peval, [
            Case(('True', ), {}, True),
            Case(('False', ), {}, False),
            Case(('None', ), {}, None),
            Case(('Ellipsis', ), {}, Ellipsis)
        ])
Ejemplo n.º 3
0
    def test_PyBuiltin_runtime(self) -> None:
        p = parser.Parser(vocabulary=parser.PyBuiltin())
        peval: AnyOp = lambda expr, *args: p.parse(expr).eval(*args)

        # Runtime Evaluation and Meta Programming
        # TODO: Untested functions: breakpoint(), help(), input(), print()
        self.assertCaseEqual(peval, [
            Case(('bool(compile(a, b, c))', '1', '1', 'eval'), {}, True),
            Case(('eval(e)', 'True'), {}, True),
            Case(('exec(e)', 'True'), {}, None),
            Case(('bool(globals())', ), {}, True),
            Case(('id(x)', None), {}, id(None)),
            Case(('bool(locals())', ), {}, True)
        ])
Ejemplo n.º 4
0
    def test_PyBuiltin_math(self) -> None:
        p = parser.Parser(vocabulary=parser.PyBuiltin())
        peval: AnyOp = lambda expr, *args: p.parse(expr).eval(*args)

        # Simple mathematical functions
        self.assertCaseEqual(peval, [
            Case(('abs(x)', -1), {}, 1),
            Case(('divmod(a, b)', 2, 1), {}, (2, 0)),
            Case(('max(l)', [1, 2, 3]), {}, 3),
            Case(('min(l)', [1, 2, 3]), {}, 1),
            Case(('pow(x, y)', 2, 2), {}, 4),
            Case(('round(x)', .6), {}, 1),
            Case(('sum(l)', [1, 2, 3]), {}, 6)
        ])
Ejemplo n.º 5
0
    def test_PyBuiltin_conversion(self) -> None:
        p = parser.Parser(vocabulary=parser.PyBuiltin())
        peval: AnyOp = lambda expr, *args: p.parse(expr).eval(*args)

        # Conversion
        self.assertCaseEqual(peval, [
            Case(('ascii(x)', 1), {}, '1'),
            Case(('bin(x)', 1), {}, '0b1'),
            Case(('chr(x)', 65), {}, 'A'),
            Case(('format(x)', 'A'), {}, 'A'),
            Case(('hex(x)', 1), {}, '0x1'),
            Case(('oct(x)', 1), {}, '0o1'),
            Case(('ord(x)', 'A'), {}, 65)
        ])
Ejemplo n.º 6
0
    def test_PyBuiltin_functional(self) -> None:
        p = parser.Parser(vocabulary=parser.PyBuiltin())
        peval: AnyOp = lambda expr, *args: p.parse(expr).eval(*args)

        # Functional Programming and Iterator functions
        self.assertCaseEqual(peval, [
            Case(('all(l)', [1, 1, 0]), {}, False),
            Case(('any(l)', [1, 1, 0]), {}, True),
            Case(('list(enumerate(l))', [1, 2]), {}, [(0, 1), (1, 2)]),
            Case(('list(filter(None, l))', []), {}, []),
            Case(('list(iter(l))', [1, 2, 3]), {}, [1, 2, 3]),
            Case(('list(map(f, l))', bool, [0]), {}, [False]),
            Case(('next(iter(l))', [1, 2, 3]), {}, 1),
            Case(('list(range(n))', 3), {}, [0, 1, 2]),
            Case(('sorted(l)', [3, 2, 1]), {}, [1, 2, 3]),
            Case(('list(zip(l, l))', range(2)), {}, [(0, 0), (1, 1)])
        ])
Ejemplo n.º 7
0
    def test_PyBuiltin_oop(self) -> None:
        p = parser.Parser(vocabulary=parser.PyBuiltin())
        peval: AnyOp = lambda expr, *args: p.parse(expr).eval(*args)

        # Builtin Types for Object Oriented Programming
        self.assertCaseEqual(peval, [
            Case(('isinstance(object(), c)', object), {}, True),
            Case(('type(o)', object), {}, type)
        ])

        # Builtin functions for Object and Class Tree Organisation
        self.assertCaseEqual(peval, [
            Case(('isinstance(a, b)', 1, int), {}, True),
            Case(('issubclass(a, b)', int, int), {}, True)
        ])

        # Builtin functions for Attribute Organisation
        self.assertCaseEqual(peval, [
            Case(('delattr(o, a)', mock.Mock(), 'a'), {}, None),
            Case(('dir(o)', object), {}, dir(object)),
            Case(('getattr(o, a)', 1j, 'imag'), {}, 1.),
            Case(('hasattr(o, a)', 1j, 'imag'), {}, True),
            Case(('setattr(o, a, v)', mock.Mock(), 'a', 0), {}, None)
        ])

        # Builtin functions for special Methods
        self.assertCaseEqual(peval, [
            Case(('bool(classmethod(o))', object), {}, True),
            Case(("hasattr(property(), 'getter')", ), {}, True),
            Case(("hasattr(staticmethod(f), '__func__')", list), {}, True)
        ])

        # Builtin functions for special Attributes
        self.assertCaseEqual(peval, [
            Case(('callable(o)', object), {}, True),
            Case(('hash(o)', 1), {}, 1),
            Case(('len(o)', [1, 2, 3]), {}, 3),
            Case(('repr(o)', 'x'), {}, "'x'"),
            Case(('sorted(vars(o))', type), {}, sorted(vars(type)))
        ])