Beispiel #1
0
    def testIntDiv(self):
        source = """
var const fres: double = 10.0 / 2.0
var const ires: int = 10 / 2
var const la: long = 10
var const lb: long = 2
var const lres: long = la / lb
var const ua: unsigned int = 10
var const ub: unsigned int = 2
var const ures: unsigned int = ua / ub
"""
        expected = """
fres = 10.0 / 2.0
ires = 10 // 2
la = 10
lb = 2
lres = la // lb
ua = 10
ub = 2
ures = ua // ub
"""
        module = PyCore.createModuleFromWpp(source, 'intDiv.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Beispiel #2
0
	def testStatic(self):
		source = """
class public static Hello
	field static public first: int = 123
	field static private second: bool
"""
		expected = """
class Hello:
	first = 123
	second = False
"""
		module = PyCore.createModuleFromWpp(source, 'ststic.wpp')
		out = OutContextMemoryStream()
		module.exportContext(out, style)
		self.assertEqual(str(out), PyCore.strPack(expected))
Beispiel #3
0
    def testIntDivAssign(self):
        source = """
func test
	var a: int = 10
	a /= 2
"""
        expected = """
def test():
	a = 10
	a //= 2
"""
        module = PyCore.createModuleFromWpp(source, 'intDivAssign.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Beispiel #4
0
    def testLogic(self):
        source = """
var const a: int = 5
var const resAnd: bool = a > 0 && a < 10
var const resOr: bool = a < 0 || a > 10
"""
        expected = """
a = 5
resAnd = a > 0 and a < 10
resOr = a < 0 or a > 10
"""
        module = PyCore.createModuleFromWpp(source, 'logic.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Beispiel #5
0
	def testSimple(self):
		source = """
class public static Hello
	field public first: int = 123
	field private second: bool
"""
		expected = """
class Hello:
	__slots__ = ('first', 'second')
	def __init__(self):
		self.first = 123
		self.second = False
"""
		module = PyCore.createModuleFromWpp(source, 'simple.wpp')
		out = OutContextMemoryStream()
		module.exportContext(out, style)
		self.assertEqual(str(out), PyCore.strPack(expected))
Beispiel #6
0
    def testOverload(self):
        source = """
class simple Point
	field public x: double
	field public y: double
	constructor overload
		x = 0
		y = 0
	constructor overload
		altName initPoint
		autoinit x
		autoinit y
	constructor overload
		altName copyPoint
		param src: const ref Point
		x = src.x
		y = src.y
var const first: Point = Point(1, 2)
var const second: Point = Point(first)
"""
        expected = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self):
		self.x = 0
		self.y = 0
	@staticmethod
	def initPoint(x, y):
		_inst = Point()
		_inst.x = x
		_inst.y = y
		return _inst
	@staticmethod
	def copyPoint(src):
		_inst = Point()
		_inst.x = src.x
		_inst.y = src.y
		return _inst
first = Point.initPoint(1, 2)
second = Point.copyPoint(first)
"""
        module = PyCore.createModuleFromWpp(source, 'overload.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Beispiel #7
0
    def testEmpty(self):
        source = """
class simple Point
	field public x: double = 0
var const pt: Point = Point()
var const x: double = pt.x
"""
        expect = """
class Point:
	__slots__ = ('x')
	def __init__(self):
		self.x = 0
pt = Point()
x = pt.x
"""
        module = PyCore.createModuleFromWpp(source, 'empty.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expect))
Beispiel #8
0
    def testArith(self):
        source = """
var const a: double = 1
var const b: double = 2
var const c: double = a + b * 3
var const d: double = (a + b) * c
var const e: double = a + (b * c)
"""
        expected = """
a = 1
b = 2
c = a + b * 3
d = (a + b) * c
e = a + b * c
"""  # В последней строке убираются ненужные скобки
        module = PyCore.createModuleFromWpp(source, 'arith.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Beispiel #9
0
    def testSimple(self):
        source = """
class public static Hello
	method getCount: unsigned int
		return 1234
	method setCount
		param count: unsigned int
		param bUpdate: bool = false
"""
        expected = """
class Hello:
	def getCount(self):
		return 1234
	def setCount(self, count, bUpdate = False):
		pass
"""
        module = PyCore.createModuleFromWpp(source, 'simple.wpp')
        out = OutContextMemoryStream()
        module.exportContext(out, style)
        self.assertEqual(str(out), PyCore.strPack(expected))
Beispiel #10
0
    def testExtends(self):
        source = """
class Top
class Middle
	extends Top
class Bottom
	extends Middle
"""
        expect = """
class Top:
	pass
class Middle(Top):
	pass
class Bottom(Middle):
	pass
"""
        module = PyCore.createModuleFromWpp(source, 'extends.wpp')
        out = OutContextMemoryStream()
        module.exportContext(out, style)
        self.assertEqual(str(out), PyCore.strPack(expect))
Beispiel #11
0
    def testDefaultFieldValues(self):
        source = """
class simple Point
	field x: double = 100
	field y: double = 200
	constructor overload
		altName initPoint
		autoinit x
		autoinit y
	constructor overload
		altName copyPoint
		param src: const ref Point
		x = src.x
		y = src.y
var a: Point = Point()
var b: Point = Point(1, 2)
var c: Point = Point(b)
"""
        with self.assertRaises(ErrorTaxon) as cm:
            module = PyCore.createModuleFromWpp(source,
                                                'defaultFieldValues.wpp')
        self.assertEqual(cm.exception.args[0],
                         'No suitable constructor found for Point()')
    def testRight(self):
        source = """
class simple Value
	field value: int
	constructor
		autoinit value
	operator const right /: Value
		param divident: int
		return Value(divident / value)
var const v: Value = 1000 / Value(22)
"""
        expected = """
class Value:
	__slots__ = ('value')
	def __init__(self, value):
		self.value = value
	def __rtruediv__(self, divident):
		return Value(divident // self.value)
v = 1000 / Value(22)
"""
        module = PyCore.createModuleFromWpp(source, 'right.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Beispiel #13
0
    def testAutoinit(self):
        source = """
class simple Point
	field public x: double
	field public y: double
	constructor
		autoinit x
		autoinit y
var const pt: Point = Point(22, 44)
var const x: double = pt.x
"""
        expect = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self, x, y):
		self.x = x
		self.y = y
pt = Point(22, 44)
x = pt.x
"""
        module = PyCore.createModuleFromWpp(source, 'autoinit.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expect))
Beispiel #14
0
	def testOverride(self):
		source = """
class First
	method virtual getValue: int
		param id: int
		return id * 2
class Second
	extends First
	method override getValue: int
		param id: int
		return super(id) + 1
"""
		expected = """
class First:
	def getValue(self, id):
		return id * 2
class Second(First):
	def getValue(self, id):
		return super().getValue(id) + 1
"""
		module = PyCore.createModuleFromWpp(source, 'superInOver.wpp')
		out = OutContextMemoryStream()
		module.exportContext(out, style)
		self.assertEqual(str(out), PyCore.strPack(expected))