Beispiel #1
0
    def test_parentCompiler(self):
        """
        You can provide a fallback compiler
        """
        fallback = Compiler()
        compiler = Compiler([fallback])

        @compiler.when(str)
        def foo(x, state):
            return x + ' compiled', state


        @fallback.when(str)
        def bar(x, state):
            return x + ' fallback', state


        @fallback.when(list)
        def baz(x, state):
            return x + ['fallback'], state


        state = State()
        self.assertEqual(compiler.compile('something', state),
                         ('something compiled', state),
                         "Should use the immediate compiler")
        self.assertEqual(compiler.compile([], state),
                         (['fallback'], state),
                         "Should use the fallback compiler but make the "
                         "original compiler available to the compile function")
        self.assertRaises(CompileError, compiler.compile, {})
Beispiel #2
0
    def test_subclass(self):
        """
        Subclasses will use the parent compiler unless another has been defined
        """
        class Parent(object):
            pass

        class Child1(Parent):
            pass

        class Child2(Parent):
            pass

        compiler = Compiler()

        @compiler.when(Parent)
        def parent(x, state):
            return 'parent', state


        @compiler.when(Child2)
        def child2(x, state):
            return 'child2', state

        state = State()
        self.assertEqual(compiler.compile(Parent(), state), ('parent', state))
        self.assertEqual(compiler.compile(Child1(), state), ('parent', state))
        self.assertEqual(compiler.compile(Child2(), state), ('child2', state))
Beispiel #3
0
    def test_type(self):
        """
        You can define functions to compile types
        """
        compiler = Compiler()

        @compiler.when(list)
        def foo(x, state):
            return x + ['hey']

        self.assertEqual(compiler.compile(['10']), ['10', 'hey'])
Beispiel #4
0
    def test_shared(self):
        """
        You can specify a list of things to that are compiled by the same
        function
        """
        compiler = Compiler()

        @compiler.when(list, str, dict)
        def foo(x, state):
            return 'NaN'

        self.assertEqual(compiler.compile('hey'), 'NaN')
        self.assertEqual(compiler.compile({}), 'NaN')
        self.assertEqual(compiler.compile([]), 'NaN')
Beispiel #5
0
    def test_class(self):
        """
        You can define functions to compile classes.
        """
        compiler = Compiler()

        class Foo(object):
            pass

        @compiler.when(Foo)
        def func(x, state):
            return (x, state, 'hey')


        foo = Foo()
        state = State()
        r = compiler.compile(foo, state)
        self.assertEqual(r, (foo, state, 'hey'),
                         "Should have used the class compiler")