Пример #1
0
    def testMethod(self):
        source = """
class A
	method static first
	method const second: int
		param a: A
		param x: double
		"""
        core = WppCore()
        module = core.createRootModule(
            Context.createFromMemory(source, 'A.memory'))
        a = module.dictionary['A']
        self.assertIn('first', a.dictionary)
        over1 = a.dictionary['first']
        self.assertEqual(over1.name, 'first')
        self.assertIn('static', over1.attrs)

        self.assertIn('second', a.dictionary)
        over2 = a.dictionary['second']
        self.assertEqual(over2.name, 'second')
        self.assertEqual(over2.attrs, {'public'})
        fn2 = over2.items[0]
        self.assertEqual(fn2.name, 'second')
        self.assertIn('const', fn2.attrs)
        self.assertIn('a', fn2.dictionary)
        self.assertIn('x', fn2.dictionary)
        paramA = fn2.getParams()[0]
        self.assertEqual(paramA.name, 'a')
        self.assertEqual(paramA.getLocalType().type, 'TypeName')
        self.assertEqual(paramA.getLocalType().getTypeTaxon(), a)
Пример #2
0
    def testFieldInClass(self):
        source = """
class public simple Point
class public Rect
	field public a: Point
		# this is a
	field b: Point
		# this is b
		"""
        core = WppCore()
        module = core.createRootModule(
            Context.createFromMemory(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().getTypeTaxon(), 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')
Пример #3
0
    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')
Пример #4
0
	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)
Пример #5
0
    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())
Пример #6
0
 def testAttributes(self):
     core = WppCore()
     # Error - Incompatible attributes used
     ctx = Context.createFromMemory('class public private A', 'A.memory')
     with self.assertRaises(ErrorTaxon) as ex:
         module = core.createRootModule(ctx)
     ctx = Context.createFromMemory('class static abstract B', 'B.memory')
     with self.assertRaises(ErrorTaxon) as ex:
         module = core.createRootModule(ctx)
     ctx = Context.createFromMemory('class protected C', 'C.memory')
     with self.assertRaises(ErrorTaxon) as ex:
         module = core.createRootModule(ctx)
     ctx = Context.createFromMemory('class static D', 'D.memory')
     module = core.createRootModule(ctx)
     self.assertIn('D', module.dictionary)
     d = module.dictionary['D']
     # Квалификатор доступа должен автоматически установиться в public
     self.assertEqual(d.getAccessLevel(), 'public')
Пример #7
0
	def testWrongLocalVar(self):
		source = """
func localVar
	var tmp: int = 222
	var second: int = tmp1 + 1
		"""
		core = WppCore()
		with self.assertRaises(ErrorTaxon) as ex:
			module = core.createRootModule(Context.createFromMemory(source, 'localVar.fake'))
		self.assertEqual(ex.exception.args[0], 'Name "tmp1" is not defined')
Пример #8
0
    def testInitValue(self):
        source = """
class A
	field counter: int = -1
		"""
        core = WppCore()
        module = core.createRootModule(
            Context.createFromMemory(source, 'First.memory'))
        classA = module.dictionary['A']
        counter = classA.dictionary['counter']
        self.assertEqual(counter.getLocalType().exportString(), 'int')
        v = counter.getValueTaxon()
        self.assertIsNotNone(v)
        self.assertEqual(v.exportString(), '-1')
Пример #9
0
 def testTypeName(self):
     source = """class public A"""
     core = WppCore()
     ctx = Context.createFromMemory(source, 'A.memory')
     module = core.createRootModule(ctx)
     classA = module.dictionary['A']
     self.assertEqual(classA.type, 'Class')
     # Создать таксон типа из строкового описания. Со ссылкой на класс внутри модуля
     typeTaxon = WppType.create('const ref A', ctx)
     self.assertEqual(typeTaxon.type, 'TypeName')
     # Хотя это бессмысленно, но добавить тип в модуль.Чтобы в процессе update была найдена ссылка на класс A
     module.addItem(typeTaxon)
     typeTaxon.fullUpdate()
     declA = typeTaxon.getTypeTaxon()
     self.assertEqual(classA, declA)
Пример #10
0
    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')
Пример #11
0
 def testFuncSimple(self):
     core = WppCore()
     module = core.createRootModule(
         Context.createFromMemory('func simple1', 'simple1.fake'))
     self.assertIn('simple1', module.dictionary)
     over = module.dictionary['simple1']
     self.assertEqual(over.type, 'Overloads')
     self.assertEqual(over.name, 'simple1')
     self.assertEqual(len(over.items), 1)
     self.assertIn('public', over.attrs)  # Autoincluded by module
     func = over.items[0]
     self.assertEqual(func.type, 'Func')
     self.assertEqual(func.name, 'simple1')
     self.assertIsNone(func.getResultType())
     self.assertEqual(func.getParams(), [])
Пример #12
0
    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')
Пример #13
0
    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')