示例#1
0
  def buildApps(self):
    # recreate tmp path
    if os.path.exists(settings.TMP_PATH):
      shutil.rmtree(settings.TMP_PATH)
    os.mkdir(settings.TMP_PATH)

    # build wender client library
    wenderModule = os.path.join(settings.WENDER_PATH, 'client/wender_coffee/wender.module')
    wenderCoffee = generateTmpFilename()
    collectCoffeeModule(wenderModule, wenderCoffee)

    compiler = Compiler()

    # build login app

    for conf in self.confapps:
      module = compiler.compile(':'.join(self.confsite.paths), conf.name)

      # add orm structs
      mainFunc = module.functions.get('main', None)
      if mainFunc:
        ormFn = core.FunctionCallNode('wender.orm.setStructs')
        # add structs param
        ormFn.addParameter(self.structs)
        # insert to main method orm.addStructs
        mainFunc.bodyNodes.insert(0, ormFn)

      # self.genJsApp(conf, module)
      self.compileApp(module, wenderCoffee)
示例#2
0
 def genStructs(self):
   compiler = Compiler()
   module = compiler.compile(':'.join(self.confsite.paths), self.confsite.model)
   wendergen = WenderGen()
   structs = wendergen.createMetaStructs(module)
   return structs
示例#3
0
class CompilerTest(unittest.TestCase):

  def setUp(self):
    self.compiler = Compiler()
    self.paths = data_path

  def testImport(self):
    source = """
    import rb.tasker as tasker;
    """
    pass

  # def testDefine(self):
  #   self.fail('not implemented')

  def testVariable(self):
    module = self.compiler.compile(self.paths, 'variableint')
    self.assertEqual(len(module.variables), 1)
    self.assertEqual(len(module.functions), 0)
    self.assertEqual(len(module.enums), 0)
    self.assertEqual(len(module.structs), 0)
    self.assertEqual(len(module.classes), 0)
    var = module.variables['num']
    self.assertEqual(var.nodetype, 'variable')
    self.assertEqual(matches.tokensToString(var.decltype), 'int')
    self.assertEqual(var.name, 'num')
    self.assertIsNotNone(var.body)
    self.assertEqual(var.body.nodetype, 'value')
    self.assertEqual(var.body.value, '4')

  def testVariableBody(self):
    module = self.compiler.compile(self.paths, 'varbody')
    var = module.variables['num']
    vb = var.body
    self.assertIsNotNone(vb)
    self.assertEqual(vb.nodetype, 'value')
    self.assertEqual(vb.value, '4')
    self.assertEqual(vb.isLitInt, True)

  def testVariableBodyFunctionParams(self):
    module = self.compiler.compile(self.paths, 'varbodyfuncparams')
    var = module.variables.get('result', None)
    self.assertIsNotNone(var)
    self.assertEqual(var.nodetype, 'variable')
    self.assertIsNotNone(var.body)
    vb = var.body
    self.assertEqual(vb.nodetype, 'functioncall')
    self.assertEqual(vb.name, 'getFunctionHash')
    self.assertEqual(len(vb.params), 1)
    vbparam = vb.params[0]
    self.assertEqual(vbparam.nodetype, 'value')
    # self.assertEqual(vbparam.word, 'onOk')

  def testFunction(self):
    module = self.compiler.compile(self.paths, 'emptyfunction')
    self.assertEqual(len(module.variables), 0)
    self.assertEqual(len(module.functions), 1)
    self.assertEqual(len(module.enums), 0)
    self.assertEqual(len(module.structs), 0)
    self.assertEqual(len(module.classes), 0)
    func = module.functions['foo']
    self.assertEqual(func.nodetype, 'function')
    self.assertEqual(matches.tokensToString(func.decltype), 'void')
    self.assertEqual(func.name, 'foo')
    self.assertEqual(len(func.params), 0)

  def testFunctionParams(self):
    # self.fail('not implemented')
    module = self.compiler.compile(self.paths, 'funcparams')
    self.assertEqual(len(module.variables), 0)
    self.assertEqual(len(module.functions), 1)
    self.assertEqual(len(module.enums), 0)
    self.assertEqual(len(module.structs), 0)
    self.assertEqual(len(module.classes), 0)
    func = module.functions['foo']
    self.assertEqual(len(func.params), 2)

  def testFunctionBody(self):
    module = self.compiler.compile(self.paths, 'function')
    func = module.functions['getModerated']
    self.assertEqual(len(func.bodyNodes), 2)
    vn = func.bodyNodes[0]
    rn = func.bodyNodes[1]
    self.assertEqual(vn.nodetype, 'variable')
    self.assertIsNotNone(vn.body)
    self.assertEqual(rn.nodetype, 'return')
    self.assertIsNotNone(rn.body)

  def testEnum(self):
    module = self.compiler.compile(self.paths, 'emptyenum')
    self.assertEqual(len(module.variables), 0)
    self.assertEqual(len(module.functions), 0)
    self.assertEqual(len(module.enums), 1)
    self.assertEqual(len(module.structs), 0)
    self.assertEqual(len(module.classes), 0)
    en = module.enums['KIND']
    self.assertEqual(en.nodetype, 'enum')
    self.assertEqual(en.name, 'KIND')
    self.assertEqual(len(en.members), 0)

  # def testEnumBody(self):
  #   self.fail('not implemented')

  def testStruct(self):
    module = self.compiler.compile(self.paths, 'emptystruct')
    self.assertEqual(len(module.variables), 0)
    self.assertEqual(len(module.functions), 0)
    self.assertEqual(len(module.enums), 0)
    self.assertEqual(len(module.structs), 1)
    self.assertEqual(len(module.classes), 0)
    st = module.structs['User']
    self.assertEqual(st.nodetype, 'struct')
    self.assertEqual(st.name, 'User')
    self.assertIsNone(st.baseName)
    self.assertEqual(len(st.variables), 0)

  # def testStructBody(self):
  #   self.fail('not implemented')

  def testClass(self):
    module = self.compiler.compile(self.paths, 'emptyclass')
    self.assertEqual(len(module.variables), 0)
    self.assertEqual(len(module.functions), 0)
    self.assertEqual(len(module.enums), 0)
    self.assertEqual(len(module.structs), 0)
    self.assertEqual(len(module.classes), 1)
    cl = module.classes['App']
    self.assertEqual(cl.nodetype, 'class')
    self.assertEqual(cl.name, 'App')
    self.assertIsNone(cl.baseName)
    self.assertEqual(len(cl.variables), 0)
    self.assertEqual(len(cl.functions), 0)

  # def testClassBase(self):
  #   self.fail('not implemented')

  # def testClassConstructor(self):
  #   self.fail('not implemented')

  # def testClassBody(self):
  #   self.fail('not implemented')

  def testFunctionReturnSingleTag(self):
    module = self.compiler.compile(self.paths, 'singletag')
    self.assertEqual(len(module.functions), 1)

    func = module.functions['renderTemplate']
    self.assertEqual(len(func.bodyNodes), 1)

    rn = func.bodyNodes[0]
    self.assertIsNotNone(rn.body)
    self.assertEqual(rn.nodetype, 'return')

    tn = rn.body
    self.assertEqual(tn.nodetype, 'tag')
    self.assertEqual(tn.name, 'img')

  def testFeatures(self):
    module = self.compiler.compile(self.paths, 'features')

    # test variables

    varTemplate = module.variables.get('template', None)
    self.assertIsNotNone(varTemplate)
    self.assertEqual(varTemplate.name, 'template')

    # dict body
    varStyle = module.variables.get('style', None)
    self.assertIsNotNone(varStyle)
    self.assertEqual(varStyle.nodetype, 'variable')
    self.assertIsNotNone(varStyle.body)
    vsb = varStyle.body
    self.assertEqual(vsb.nodetype, 'dict_body')
    self.assertEqual(len(vsb.items), 2)
    self.assertTrue("'padding'" in vsb.items)
    self.assertEqual(vsb.items["'padding'"].value, "'0 16px'")
    self.assertTrue("'position'" in vsb.items)
    self.assertEqual(vsb.items["'position'"].value, "'absolute'")

    # tag

    varTemplateTag = varTemplate.body
    self.assertIsNotNone(varTemplateTag)

    self.assertEqual(varTemplateTag.nodetype, 'tag')
    self.assertEqual(len(varTemplateTag.childs), 3)
    self.assertEqual(len(varTemplateTag.attributes), 1)

    varTemplateId = varTemplateTag.attributes.get('id', None)
    self.assertIsNotNone(varTemplateId)
    self.assertEqual(varTemplateId.nodetype, 'value')

    arrayChildAttr = varTemplateTag.childs[1]
    self.assertIsNotNone(arrayChildAttr)
    self.assertEqual(len(arrayChildAttr.attributes), 2)

    classAttr = arrayChildAttr.attributes.get('class', None)
    self.assertIsNotNone(classAttr)
    self.assertEqual(classAttr.nodetype, 'array_body')
    self.assertEqual(len(classAttr.items), 2)
    self.assertEqual(classAttr.items[0].nodetype, 'value')
    self.assertEqual(classAttr.items[0].isLitString, True)
    self.assertEqual(classAttr.items[0].value, "'common-content'")
    self.assertEqual(classAttr.items[1].nodetype, 'value')
    self.assertEqual(classAttr.items[1].isLitString, True)
    self.assertEqual(classAttr.items[1].value, "'main-content'")

    # select from
    sf = module.variables.get('selectFrom', None)
    self.assertIsNotNone(sf)
    self.assertEqual(sf.nodetype, 'variable')
    self.assertIsNotNone(sf.body)
    self.assertEqual(sf.body.nodetype, 'select_from')

    # select concat
    sc = module.variables.get('selectConcat', None)
    self.assertIsNotNone(sc)
    self.assertEqual(sc.nodetype, 'variable')
    self.assertIsNotNone(sc.body)
    self.assertEqual(sc.body.nodetype, 'select_concat')

    # delete from
    df = module.variables.get('deleteFrom', None)
    self.assertIsNotNone(df)
    self.assertEqual(df.nodetype, 'variable')
    self.assertIsNotNone(df.body)
    self.assertEqual(df.body.nodetype, 'delete_from')

    # test functions

    # test enums
    enumDomNodeType = module.enums.get('DomNodeType', None)
    self.assertIsNotNone(enumDomNodeType)
    self.assertEqual(len(enumDomNodeType.members), 2)
    self.assertIsNotNone(enumDomNodeType.members.get('ELEMENT', None))
    self.assertIsNotNone(enumDomNodeType.members['ELEMENT'], '1')
    self.assertIsNotNone(enumDomNodeType.members.get('TEXT', None))
    self.assertIsNotNone(enumDomNodeType.members['TEXT'], '2')

    # test structs

    structUser = module.structs.get('User', None) 
    self.assertIsNotNone(structUser)
    self.assertEqual(len(structUser.variables), 2)
    stuId = structUser.variables.get('id', None)
    self.assertIsNotNone(stuId)
    self.assertEqual(stuId.nodetype, 'struct_variable')
    self.assertEqual(len(stuId.inits), 1)
    self.assertIsNotNone(stuId.inits.get("'validate'", None))
    self.assertEqual(stuId.inits["'validate'"].nodetype, 'value')
    self.assertEqual(stuId.inits["'validate'"].value, 'wender.validateLength')
    stuUsername = structUser.variables.get('username', None)
    self.assertIsNotNone(stuUsername)
    self.assertEqual(stuUsername.nodetype, 'struct_variable')
    self.assertEqual(len(stuUsername.inits), 0)

    # test classes

    classCountManager = module.classes.get('CountManager', None)
    self.assertIsNotNone(classCountManager)

    self.assertIsNotNone(classCountManager.constructor)
    self.assertEqual(classCountManager.name, 'CountManager')

    self.assertEqual(classCountManager.baseName, 'BaseManager')

    # test operator

    fnOper = module.functions.get('oper', None)
  
    self.assertIsNotNone(fnOper)
    self.assertEqual(fnOper.bodyNodes[0].nodetype, 'variable')
    self.assertEqual(fnOper.bodyNodes[1].nodetype, 'variable')
    self.assertEqual(fnOper.bodyNodes[2].nodetype, 'variable')
    self.assertEqual(fnOper.bodyNodes[3].nodetype, 'variable')

    # test if

    ifnode = fnOper.bodyNodes[4]
    self.assertEqual(ifnode.nodetype, 'if')
    ifexpr = ifnode.expr
    self.assertEqual(ifexpr.nodetype, 'functioncall')
    self.assertEqual(ifexpr.name, 'and')
    self.assertEqual(len(ifexpr.params), 2)
    expr1 = ifexpr.params[0]
    expr2 = ifexpr.params[1]

    self.assertEqual(expr1.nodetype, 'functioncall')
    self.assertEqual(len(expr1.params), 2)
    self.assertEqual(expr1.params[0].nodetype, 'value')
    self.assertEqual(expr1.params[0].value, 'val1')
    self.assertEqual(expr1.params[1].nodetype, 'value')
    self.assertEqual(expr1.params[1].value, 'false')

    self.assertEqual(expr2.nodetype, 'functioncall', expr2.name)
    self.assertEqual(len(expr2.params), 2)
    self.assertEqual(expr2.params[0].nodetype, 'value')
    self.assertEqual(expr2.params[0].value, 'val2')
    self.assertEqual(expr2.params[1].nodetype, 'value')
    self.assertEqual(expr2.params[1].value, 'false')

    # test for

    fornode = fnOper.bodyNodes[5]
    self.assertEqual(fornode.nodetype, 'for')
    self.assertIsNotNone(fornode.collName)
    self.assertIsNotNone(fornode.itemName)
    self.assertNotEqual(len(fornode.body), 0)
示例#4
0
 def setUp(self):
   self.compiler = Compiler()
   self.paths = data_path