def testExtends(self): source = """ class public A class public B extends A class public C extends B """ ctx = Context.createFromMemory(source, 'textExt.memory') core = WppCore() module = core.createRootModule(ctx) self.assertEqual(module.type, 'Module') self.assertEqual(module.name, 'textExt') self.assertIn('A', module.dictionary) self.assertIn('B', module.dictionary) self.assertIn('C', module.dictionary) a = module.dictionary['A'] b = module.dictionary['B'] c = module.dictionary['C'] self.assertEqual(b.getParent(), a) outCtx = OutContextMemory() b.export(outCtx) text = '\n'.join(outCtx.lines) self.assertEqual(text, 'class public B\n\textends A') outCtx = OutContextMemory() c.export(outCtx) text = '\n'.join(outCtx.lines) self.assertEqual(text, 'class public C\n\textends B')
def testFieldInClass(self): source = """ class public simple Point class public Rect field public a: Point # this is a field b: Point # this is b """ module = WppCore.createMemModule(source, 'First.memory') classPoint = module.dictionary['Point'] classRect = module.dictionary['Rect'] self.assertIn('a', classRect.dictionary) self.assertIn('b', classRect.dictionary) a = classRect.dictionary['a'] self.assertEqual(a.type, 'Field') self.assertEqual(a.name, 'a') self.assertEqual(a.getLocalType().typeRef.target, classPoint) self.assertEqual(a.getAccessLevel(), 'public') b = classRect.dictionary['b'] self.assertEqual(b.name, 'b') self.assertEqual(b.getAccessLevel(), 'private') outContext = OutContextMemory() b.export(outContext) self.assertEqual(str(outContext), 'field b: Point\n\t# this is b')
def testConstructor(self): from Wpp.WppFunc import WppConstructor source = """ class public Test field a: int field b: double = 0 constructor param a: int param b: double this.a = a this.b = b """ module = WppCore.createMemModule(source, 'Test.fake') classTest = module.dictionary['Test'] over = classTest.findConstructor() self.assertIsNotNone(over) self.assertEqual(over.type, 'Overloads') c = over.items[0] self.assertEqual(c.type, 'Constructor') self.assertEqual(c.name, WppConstructor.key) self.assertIn('a', c.dictionary) self.assertIn('b', c.dictionary) a = c.dictionary['a'] self.assertEqual(a.type, 'Param') self.assertEqual(a.getLocalType().exportString(), 'int') outCtx = OutContextMemory() classTest.export(outCtx) self.assertEqual(str(outCtx), source.strip())
def testLocalVar(self): source = """ func localVar var tmp: int = 222 var second: int = tmp + 1 """ core = WppCore() module = core.createRootModule(Context.createFromMemory(source, 'localVar.fake')) over = module.dictionary['localVar'] func = over.items[0] block = func.getBody() self.assertEqual(len(block.items), 2) cmd1 = block.items[0] self.assertEqual(cmd1.type, 'Var') outContext = OutContextMemory() cmd1.export(outContext) self.assertEqual(str(outContext), 'var tmp: int = 222') cmd2 = block.items[1] self.assertEqual(cmd2.type, 'Var') self.assertEqual(cmd2.name, 'second') val = cmd2.getValueTaxon() self.assertEqual(val.type, 'BinOp') varExpr = val.getLeft() self.assertEqual(varExpr.type, 'IdExpr') self.assertEqual(varExpr.id, 'tmp') self.assertEqual(varExpr.name, '') d1 = varExpr.getDeclaration() self.assertEqual(d1, cmd1)
def testFuncWithParams(self): source = """ func func03: double # with params param in x: double = 0 param in y: double = -1 """ core = WppCore() module = core.createRootModule( Context.createFromMemory(source, 'func03.fake')) over = module.dictionary['func03'] func = over.items[0] params = func.getParams() self.assertEqual(len(params), 2) self.assertIn('x', func.dictionary) self.assertEqual(params[1].name, 'y') self.assertEqual(params[1].type, 'Param') outContext = OutContextMemory() params[0].export(outContext) self.assertEqual(str(outContext), 'param in x: double = 0') outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), source.strip())
def testComments(self): source = """ class public MyClass # This is comment """ core = WppCore() module = core.createRootModule( Context.createFromMemory(source, 'MyClass.memory')) myClass = module.dictionary['MyClass'] self.assertEqual(myClass.getComment(), ' This is comment') outCtx = OutContextMemory() myClass.export(outCtx) self.assertEqual(str(outCtx), 'class public MyClass\n\t# This is comment')
def testFuncWithResult(self): core = WppCore() module = core.createRootModule( Context.createFromMemory('func pure simple2: int', 'simple2.fake')) over = module.dictionary['simple2'] func = over.items[0] self.assertEqual(func.name, 'simple2') self.assertIn('pure', func.attrs) taxonResult = func.getResultType() self.assertEqual(taxonResult.type, 'TypeName') outContext = OutContextMemory() func.export(outContext) self.assertEqual(str(outContext), 'func pure simple2: int') outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), 'func pure simple2: int')
def testAutoChange(self): source = """ func autoChange: int param x: int x + 1 """ module = WppCore.createMemModule(source, 'autoChange.fake') over = module.items[0] func = over.items[0] block = func.getBody() ret = block.items[0] self.assertEqual(ret.type, 'Return') self.assertEqual(ret.isAutoChange, True) expr = ret.getExpression() self.assertIsNotNone(expr) self.assertEqual(expr.type, 'BinOp') self.assertEqual(expr.exportString(), 'x + 1') outContext = OutContextMemory() func.export(outContext) self.assertEqual(str(outContext), source.strip())
def testVarInModule(self): source = """ class public First var private myVar: First # This is my variable """ core = WppCore() module = core.createRootModule( Context.createFromMemory(source, 'First.memory')) self.assertIn('First', module.dictionary) self.assertIn('myVar', module.dictionary) classFirst = module.dictionary['First'] myVar = module.dictionary['myVar'] self.assertEqual(myVar.name, 'myVar') self.assertEqual(myVar.type, 'Var') localType = myVar.getLocalType() self.assertEqual(localType.type, 'TypeName') self.assertEqual(localType.getTypeTaxon(), classFirst) outContext = OutContextMemory() myVar.export(outContext) self.assertEqual(str(outContext), 'var private myVar: First\n\t# This is my variable')
def testAutoInit(self): source = """ class public A field a: int field b: double constructor param init a param init b = 1.0 """ module = WppCore.createMemModule(source, 'A.fake') classA = module.dictionary['A'] over = classA.findConstructor() self.assertIsNotNone(over) c = over.items[0] a = c.dictionary['a'] self.assertIn('init', a.attrs) self.assertEqual(classA.dictionary['a'], a.refs['field']) autoInits = c.getAutoInits() self.assertEqual(len(autoInits), 2) outCtx = OutContextMemory() classA.export(outCtx) self.assertEqual(str(outCtx), source.strip())