Example #1
0
 def test_can_write_hierarchical_package_properly(self):
     EXPECTED = "var com=(function(window,undefined){var example=(function(window,undefined){var deeper=(function(window,undefined){return {};}(window));return {\"deeper\":deeper};}(window));var example2=(function(window,undefined){return {};}(window));return {\"example\":example,\"example2\":example2};}(window));"
     example = Package("example")
     example.packages["deeper"] = Package("deeper")
     self.pack.packages["example"] = example
     self.pack.packages["example2"] = Package("example2")
     assert_that(self.pack.to_js(), equal_to(EXPECTED))
Example #2
0
 def enter_class(self, package: Package, src: SourceFile,
                 ctx: PlayParser.ClassDeclarationContext):
     cls = Class(ctx.IDENTIFIER().getText(), package, src)
     cls.is_interface = ctx.INTERFACE() is not None
     cls.is_native = ctx.NATIVE() is not None
     if cls.is_native and cls.is_interface:
         raise CompileException('interface {} cannot be native'.format(cls))
     Option().nodes[cls] = ctx
     package.put(cls)
     SymbolTable().enter_class(cls)
Example #3
0
 def test_can_write_package_with_class(self):
     EXPECTED = """var com=(function(window,undefined){var Test=function(){};var example=(function(window,undefined){return {};}(window));return {"Test":Test,"example":example};}(window));"""
     com = Package("com")
     com.packages["example"] = Package("example")
     TestClass = mock.Mock(spec=Class)
     TestClass.to_js = mock.Mock(return_value="var Test=function(){};")
     TestClass.visibility = Class.PUBLIC
     com.classes["Test"] = TestClass
     def print_diff():
         print [x for x in difflib.ndiff(EXPECTED,com.to_js())]
     self.assertEqual(EXPECTED,com.to_js(),print_diff());
     assert_that(com.to_js(), equal_to(EXPECTED))
     
Example #4
0
class TestPackage(TestCase):
    def setUp(self):
        self.pack = Package("com")
        
    def test_initial_state(self):
        self.assertEqual(0,len(self.pack.packages))
        self.assertEqual("com",self.pack.name)
        
    def test_cannot_write_to_name(self):
        try:
            self.pack.name = "hello"
            self.fail("Should not be able to write to name field.")
        except AttributeError:
            pass
        
    def test_can_add_subpackages(self):
        pack2 = Package("example")
        self.pack.packages["example"] = pack2
        self.assertEqual(pack2.name,self.pack.packages.keys()[0])
        
    def test_can_write_simple_package_properly(self):
        EXPECTED = "var com=(function(window,undefined){return {};}(window));"
        self.assertEqual(EXPECTED,self.pack.to_js())
        
    def test_can_write_hierarchical_package_properly(self):
        EXPECTED = "var com=(function(window,undefined){var example=(function(window,undefined){var deeper=(function(window,undefined){return {};}(window));return {\"deeper\":deeper};}(window));var example2=(function(window,undefined){return {};}(window));return {\"example\":example,\"example2\":example2};}(window));"
        example = Package("example")
        example.packages["deeper"] = Package("deeper")
        self.pack.packages["example"] = example
        self.pack.packages["example2"] = Package("example2")
        assert_that(self.pack.to_js(), equal_to(EXPECTED))
        
    def test_can_write_package_with_class(self):
        EXPECTED = """var com=(function(window,undefined){var Test=function(){};var example=(function(window,undefined){return {};}(window));return {"Test":Test,"example":example};}(window));"""
        com = Package("com")
        com.packages["example"] = Package("example")
        TestClass = mock.Mock(spec=Class)
        TestClass.to_js = mock.Mock(return_value="var Test=function(){};")
        TestClass.visibility = Class.PUBLIC
        com.classes["Test"] = TestClass
        def print_diff():
            print [x for x in difflib.ndiff(EXPECTED,com.to_js())]
        self.assertEqual(EXPECTED,com.to_js(),print_diff());
        assert_that(com.to_js(), equal_to(EXPECTED))
        
Example #5
0
 def enter_package(self, package: str) -> Package:
     symbol = self.__symbols.get(package)
     if isinstance(symbol, Package):
         return symbol
     elif not symbol:
         owner = self.enter_package(package[:package.rfind('.')] if '.' in
                                    package else '')
         symbol = Package(package, owner)
         owner.put(symbol)
         self.__symbols[package] = symbol
         return symbol
     else:
         raise CompileException(
             "Expect package symbol, but it is {}".format(symbol))
Example #6
0
from ast import Package, Primitive

ROOT_PACKAGE = Package('')
PLAY_PACKAGE = Package('play', ROOT_PACKAGE)
ROOT_PACKAGE.children['play'] = PLAY_PACKAGE

BOOLEAN_TYPE = Primitive("boolean")
BYTE_TYPE = Primitive("byte")
SHORT_TYPE = Primitive("short")
INT_TYPE = Primitive("int")
LONG_TYPE = Primitive("long")
FLOAT_TYPE = Primitive("float")
DOUBLE_TYPE = Primitive("double")
NULL_TYPE = Primitive('null')

INTEGER_NUMBERS = [BYTE_TYPE, SHORT_TYPE, INT_TYPE, LONG_TYPE]
REAL_NUMBERS = [FLOAT_TYPE, DOUBLE_TYPE]
NUMBERS = INTEGER_NUMBERS + REAL_NUMBERS
PRIMITIVES = NUMBERS + [BOOLEAN_TYPE]

BOX_CASTS = {
    'boolean': 'play.Boolean',
    'byte': 'play.Byte',
    'short': 'play.Short',
    'int': 'play.Int',
    'long': 'play.Long',
    'float': 'play.Float',
    'double': 'play.Double',
    'play.Boolean': 'boolean',
    'play.Byte': 'byte',
    'play.Short': 'short',
Example #7
0
 def setUp(self):
     self.pack = Package("com")