def testRightSimple(self): source = """ class simple Point field x: double field y: double constructor autoinit x = 0 autoinit y = 0 operator const right *: Point param k: double return Point(k * x, k * y) var const p: Point = 10 * Point(1, 2) """ expected = """ class Point { private x: number; private y: number; constructor(x = 0, y = 0) { this.x = x; this.y = y; } rmul(k: number): Point { return new Point(k * this.x, k * this.y); } } const p = new Point(1, 2).rmul(10); """ module = TSCore.createModuleFromWpp(source, 'simpleRight.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testFunc(self): source = """ func public empty: double param x: double param y: double param z: double = 1 var a: double = x """ module = WppCore.createMemModule(source, 'func.wpp') ctx = OutContextMemoryStream() module.export(ctx) self.assertEqual(str(ctx), source.strip()) func = module.findItem('empty') self.assertEqual(func.type, 'func') x = func.findParam('z') self.assertEqual(x.type, 'param') body = func.getBody() self.assertEqual(body.type, 'body') y = body.startFindUp('y') self.assertIsNotNone(y) self.assertEqual(y.type, 'param') self.assertEqual(y.name, 'y')
def testCallFunc(self): source = """ func public first: double param x: double param y: double x * x + y func public second: double param a: double param b: double first(a + 1, b) """ module = WppCore.createMemModule(source, 'callFunc.fake') secondOver = module.dictionary['second'] self.assertEqual(secondOver.type, 'Overloads') secondFunc = secondOver.items[0] self.assertEqual(secondFunc.type, 'Func') cmd = secondFunc.getBody().items[-1] self.assertEqual(cmd.type, 'Return') expr = cmd.getExpression() self.assertEqual(expr.type, 'Call') caller = expr.getCaller() self.assertEqual(caller.type, 'IdExpr') self.assertEqual(caller.id, 'first') args = expr.getArguments() self.assertEqual(len(args), 2) outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), module.strPack(source))
def testDefaultParams(self): source = """ class simple Point field public x: double field public y: double constructor autoinit x = 0 autoinit y = 0 var const pt0: Point = Point() var const pt1: Point = Point(1) var const pt2: Point = Point(1, 2) """ expect = """ class Point: __slots__ = ('x', 'y') def __init__(self, x = 0, y = 0): self.x = x self.y = y pt0 = Point() pt1 = Point(1) pt2 = Point(1, 2) """ module = PyCore.createModuleFromWpp(source, 'defaultParams.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expect))
def testSimple(self): source = """ class simple Point field public x: double field public y: double constructor autoinit x = 0 autoinit y = 0 operator const +: Point param right: const ref Point return Point(x + right.x, y + right.y) var const a: Point = Point(11, 22) var const b: Point = a + Point(0, -1) """ expected = """ class Point: __slots__ = ('x', 'y') def __init__(self, x = 0, y = 0): self.x = x self.y = y def __add__(self, right): return Point(self.x + right.x, self.y + right.y) a = Point(11, 22) b = a + Point(0, -1) """ module = PyCore.createModuleFromWpp(source, 'simple.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testString(self): source = """ func public main var s: String = "Hello!" var n: unsigned long = s.length """ expected = """ def main(): s = 'Hello!' n = len(s) """ srcModule = WppCore.createMemModule(source, 'string.fake') dstModule = srcModule.cloneRoot(PyCore()) mainOver = dstModule.dictionary['main'] mainFunc = mainOver.items[0] cmd2 = mainFunc.getBody().items[1] self.assertEqual(cmd2.type, 'Var') expr = cmd2.getValueTaxon() self.assertEqual(expr.type, 'Call') lenId = expr.getCaller() self.assertEqual(lenId.type, 'IdExpr') self.assertEqual(lenId.id, 'len') lenDecl = lenId.getDeclaration() self.assertEqual(lenDecl.type, 'Overloads') self.assertEqual(lenDecl.name, 'len') outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), expected.strip())
def testSimple(self): source = """ var const public myInt: int = 22 var const public myLong: long = -123456 var public myFloat: float = 1.23 var public myDouble: double = -1.23E+04 var myTrue: bool = true var myFalse: bool = false var defInt: int var defULong: unsigned long var defFloat: float var defDouble: double var defBool: bool """ expected = """ myInt = 22 myLong = -123456 myFloat = 1.23 myDouble = -1.23E+04 myTrue = True myFalse = False defInt = 0 defULong = 0 defFloat = 0.0 defDouble = 0.0 defBool = False """ module = PyCore.createModuleFromWpp(source, 'pyVar.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), expected.strip())
def testUpdateShortStatic(self): source = """ class public StClass field static inst: StClass method getInstShort: StClass inst method getInstFull: StClass StClass.inst """ expected = """ class public StClass field static inst: StClass method getInstShort: StClass StClass.inst method getInstFull: StClass StClass.inst """ module = WppCore.createMemModule(source, 'fake') stClass = module.dictionary['StClass'] getInstShort = stClass.dictionary['getInstShort'].items[0] cmd = getInstShort.getBody().items[0] self.assertEqual(cmd.type, 'Return') exp = cmd.getExpression() self.assertEqual(exp.type, 'IdExpr') self.assertEqual(exp.checkShortStatic(), stClass) # Replace short form by full exp.updateShortStatic(stClass) outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), expected.strip())
def testStatic(self): source = """ class static MyMath field public Pi: double = 3.1415926 method abs: double param x: double x < 0 ? -x : x func public main var const x: double = MyMath.abs(MyMath.Pi * 123) """ expected = """ class MyMath { public static Pi: number = 3.1415926; public static abs(x: number): number { return x < 0 ? -x : x; } } export function main() { const x: number = MyMath.abs(MyMath.Pi * 123); } """ srcModule = WppCore.createMemModule(source, 'static.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testBool(self): source = """ func public boolName: String param value: bool value ? "Yes" : "No" func public trueName: String boolName(true) func public falseName: String boolName(false) """ module = WppCore.createMemModule(source, 'bool.fake') trueNameOver = module.dictionary['trueName'] trueName = trueNameOver.items[0] cmd = trueName.getBody().items[0] self.assertEqual(cmd.type, 'Return') expr = cmd.getExpression() self.assertEqual(expr.type, 'Call') params = expr.getArguments() p1 = params[0] self.assertEqual(p1.type, 'True') outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), module.strPack(source))
def testRightOver(self): source = """ class simple Point field public x: double field public y: double constructor autoinit x = 0 autoinit y = 0 operator const overload right *: Point altName rightMul param left: double return Point(left * x, left * y) operator const overload *: Point altName leftMul param right: double return Point(x * right, y * right) var const a: Point = 1.23 * Point(11, 22) var const b: Point = Point(1.1, 2.2) * 3.3 """ module = WppCore.createMemModule(source, 'right.wpp') a = module.findItem('a') decl = a.getValueTaxon().getDeclaration() self.assertEqual(TaxonAltName.getAltName(decl), 'rightMul') b = module.findItem('b') decl = b.getValueTaxon().getDeclaration() self.assertEqual(TaxonAltName.getAltName(decl), 'leftMul') ctx = OutContextMemoryStream() module.export(ctx) self.assertEqual(str(ctx), module.strPack(source))
def testExport(self): inCtx = Context.createFromMemory(['']) txif = WppIf() txif.addItem(WppExpression.parse('first', inCtx)) txBody = txif.addItem(WppBody()) txRet = txBody.addItem(WppReturn()) txRet.addItem(WppExpression.parse('1', inCtx)) txif.addItem(WppExpression.parse('second', inCtx)) txBody = txif.addItem(WppBody()) txRet = txBody.addItem(WppReturn()) txRet.addItem(WppExpression.parse('2', inCtx)) txBody = txif.addItem(WppBody()) txRet = txBody.addItem(WppReturn()) txRet.addItem(WppExpression.parse('0', inCtx)) outCtx = OutContextMemoryStream() txif.export(outCtx) expected = """ if first return 1 elif second return 2 else return 0 """ self.assertEqual(str(outCtx), expected.strip())
def testCast(self): source = """ class A field name: String constructor param init name cast const: String name func public main var const a: A = A("Hello!") var const s: String = String(a) """ expected = """ class A { private name: string; public constructor(name: string) { this.name = name; } public toString(): string { return this.name; } } export function main() { const a: A = new A('Hello!'); const s: string = String(a); } """ srcModule = WppCore.createMemModule(source, 'simple.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testSimple(self): source = """ class simple Point field public x: double field public y: double constructor autoinit x = 0 autoinit y = 0 operator const +: Point param right: const ref Point return Point(x + right.x, y + right.y) var const a: Point = Point(11, 22) var const b: Point = a + Point(0, -1) """ expected = """ class Point { public x: number; public y: number; constructor(x = 0, y = 0) { this.x = x; this.y = y; } add(right: Point): Point { return new Point(this.x + right.x, this.y + right.y); } } const a = new Point(11, 22); const b = a.add(new Point(0, -1)); """ module = TSCore.createModuleFromWpp(source, 'simpleOp.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testWithReplace(self): source = """ func myMul: double param x: double param y: double return x * y """ dst = """ func myMul: double param x: double param y: double x * y """ module = WppCore.createMemModule(source, 'root.fake') myMulOver = module.dictionary['myMul'] self.assertEqual(myMulOver.type, 'Overloads') myMulFn = myMulOver.items[0] self.assertEqual(myMulFn.type, 'Func') block = myMulFn.getBody() lastCmd = block.items[-1] self.assertEqual(lastCmd.type, 'Return') self.assertTrue(lastCmd.isAutoChange) # Т.к. при экспорте return не выводится outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), module.strPack(dst))
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))
def testStringStr(self): source = """ var s: String = String(15) """ srcModule = WppCore.createMemModule(source, 'toa.fake') s = srcModule.dictionary['s'] v = s.getValueTaxon() self.assertEqual(v.type, 'New') c = v.getCaller() self.assertEqual(c.type, 'IdExpr') wString = c.refs['decl'] self.assertEqual(wString.name + ':' + wString.type, 'String:Class') dstModule = srcModule.cloneRoot(PyCore()) s1 = dstModule.dictionary['s'] v1 = s1.getValueTaxon() self.assertEqual(v1.type, 'New') c1 = v1.getCaller() self.assertEqual(c1.type, 'IdExpr') pString = c1.refs['decl'] self.assertEqual(pString.name, 'String') outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), 's = str(15)')
def testLength(self): source = """ func test: String param value: String value.length == 0 ? "Empty" : "Full" """ module = WppCore.createMemModule(source, 'length.fake') funcOver = module.dictionary['test'] func = funcOver.items[0] cmd = func.getBody().items[0] self.assertEqual(cmd.type, 'Return') expr = cmd.getExpression() self.assertEqual(expr.type, 'TernaryOp') cond = expr.getCondition() self.assertEqual(cond.type, 'BinOp') self.assertEqual(cond.opCode, '==') pt = cond.getLeft() self.assertEqual(pt.type, 'BinOp') self.assertEqual(pt.opCode, '.') length = pt.getRight() self.assertEqual(length.type, 'FieldExpr') self.assertEqual(length.id, 'length') lengthDecl = length.getDeclaration() self.assertEqual(lengthDecl.type, 'Field') self.assertEqual(lengthDecl.cloneScheme, 'Owner') self.assertEqual(lengthDecl.owner.type, 'Class') outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), source.strip())
def testOverloads(self): self.maxDiff = None source = """ class Point field public x: double field public y: double constructor param init x param init y class public Rect field public A: Point field public B: Point constructor altname fromNums param x1: double param y1: double param x2: double param y2: double A = Point(x1, y1) B = Point(x2, y2) constructor altname fromPoints param init A param init B func public main var const r1: Rect = Rect(10, 10, 60, 40) var const r2: Rect = Rect(Point(10, 10), Point(60, 40)) """ expected = '' srcModule = WppCore.createMemModule(source, 'over.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testSuper(self): source = """ class public A field public index: int constructor public param init index class public B extends A field public mass: double constructor public param index: int param init mass super(index) """ srcModule = WppCore.createMemModule(source, 'binOp.fake') dstModule = srcModule.cloneRoot(PyCore()) outStream = OutContextMemoryStream() dstModule.export(outStream) expected = """ class A: __slots__ = ('index') def __init__(self, index): self.index = index class B(A): __slots__ = ('mass') def __init__(self, index, mass): super().__init__(index) self.mass = mass """ self.assertEqual(str(outStream), WppCore.strPack(expected))
def testSingle(self): source = """ class simple Point field public x: double field public y: double constructor param x0: double param y0: double x = x0 y = y0 var const pt: Point = Point(22, 44) var const x: double = pt.x """ expect = """ class Point: __slots__ = ('x', 'y') def __init__(self, x0, y0): self.x = x0 self.y = y0 pt = Point(22, 44) x = pt.x """ module = PyCore.createModuleFromWpp(source, 'empty.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expect))
def testUnary(self): source = """ class public Unary field public first: int field public second: int method public init param value: int first = -value * -10 second = -first method public cloneNeg param src: Unary first = -src.first + 1 second = -(src.second + 1) """ expected0 = '' expected = """ class Unary: __slots__ = ('first', 'second') def __init__(self): self.first = 0 self.second = 0 def init(self, value): self.first = -value * -10 self.second = -self.first def cloneNeg(self, src): self.first = -src.first + 1 self.second = -(src.second + 1) """ srcModule = WppCore.createMemModule(source, 'unary.fake') dstModule = srcModule.cloneRoot(PyCore()) outStream = OutContextMemoryStream() dstModule.export(outStream) self.assertEqual(str(outStream), WppCore.strPack(expected))
def testOverload(self): source = """ class simple Point field x: double field y: double constructor autoinit x autoinit y operator const overload +: Point param p: Point return Point(x + p.x, y + p.y) operator const overload +: Point param k: double return Point(x + k, y + k) method const overload plus: Point altName plusPt param p: const ref Point return Point(x + p.x, y + p.y) method const overload plus: Point altName plusN param k: double return Point(x + k, y + k) var const a: Point = Point(11, 22) + 3 var const b: Point = a + Point(-1, -2) var const a1: Point = Point(11, 22).plus(3) var const b1: Point = a1.plus(Point(-1, -2)) """ expected = """ """ module = PyCore.createModuleFromWpp(source, 'overload.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testConstructor(self): source = """ class First field primary: int constructor autoinit primary class Second extends First field secondary: double constructor param primary: int autoinit secondary super(primary) """ expected = """ class First: __slots__ = ('primary') def __init__(self, primary): self.primary = primary class Second(First): __slots__ = ('secondary') def __init__(self, primary, secondary): super().__init__(primary) self.secondary = secondary """ module = PyCore.createModuleFromWpp(source, 'superInCon.wpp') out = OutContextMemoryStream() module.exportContext(out, style) self.assertEqual(str(out), PyCore.strPack(expected))
def testConstructor(self): source = """ class Parent field first: int constructor autoinit first class Child extends Parent field second: double constructor param first: int autoinit second super(first) """ module = WppCore.createMemModule(source, 'superCon.wpp') Child = module.findItem('Child') con = Child.findConstructor() body = con.getBody() txSuper = body.items[0].getCaller() self.assertEqual(txSuper.type, 'super') self.assertTrue(txSuper.isConstructor()) self.assertFalse(txSuper.isOverride()) ctx = OutContextMemoryStream() module.export(ctx) self.assertEqual(str(ctx), module.strPack(source))
def testExport(self): source = 'typedef public Size = unsigned long' tsModule = TSCore.createModuleFromWpp(source, 'export.wpp') ctx = OutContextMemoryStream() tsModule.exportContext(ctx, style) # TypeScript is not maintain unsigned types self.assertEqual(str(ctx), 'export type Size = number;')
def testMethod(self): source = """ class public Point field public x: double field public y: double operator +=: ref Point param pt: const ref Point x += pt.x y += pt.y this """ module = WppCore.createMemModule(source, 'method.fake') classPoint = module.dictionary['Point'] self.assertEqual(classPoint.type, 'Class') opOver = classPoint.dictionary['+='] self.assertEqual(opOver.type, 'Overloads') self.assertEqual(opOver.name, '+=') op = opOver.items[0] self.assertEqual(op.type, 'Operator') self.assertEqual(op.name, '+=') self.assertTrue(op.isBinary()) outContext = OutContextMemoryStream() module.export(outContext) self.assertEqual(str(outContext), source.strip())
def testSimple(self): source = """ func public funcIf: double param cond: bool param positive: double param negative: double if cond return positive return negative var const good: double = funcIf(true, 1.1, 3.3) var const bad: double = funcIf(false, -1, -100) """ module = WppCore.createMemModule(source, 'simpleCall.wpp') good = module.findItem('good') self.assertEqual(good.type, 'var') goodVal = good.getValueTaxon() self.assertEqual(goodVal.type, 'call') goodCallQT = goodVal.buildQuasiType() self.assertEqual(goodCallQT.type, 'scalar') self.assertEqual(goodCallQT.taxon.name, 'double') outCtx = OutContextMemoryStream() module.export(outCtx) self.assertEqual(str(outCtx), WppCore.strPack(source))
def testParent(self): source = """ class public A class public B # Class B extends A """ expected = """ class A: pass class B(A): \"\"\" Class B \"\"\" pass """ srcModule = WppCore.createMemModule(source, 'ext.fake') pyCore = PyCore() dstModule = srcModule.clone(pyCore) dstModule.updateRefs() classA = dstModule.dictionary['A'] classB = dstModule.dictionary['B'] self.assertEqual(classB.type, 'Class') self.assertTrue(classB.canBeStatic) self.assertEqual(classB.name, 'B') self.assertEqual(classB.core, pyCore) self.assertEqual(classB.getParent(), classA) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext).strip(), WppCore.strPack(expected))
def testIntDiv(self): source = """ class simple Count field value: unsigned long constructor autoinit value = 0 operator const /: Count param n: unsigned long return Count(value / n) var const c1: Count = Count(1000) / 2 """ expected = """ class Count { private value: number; constructor(value = 0) { this.value = value; } div(n: number): Count { return new Count(this.value / n | 0); } } const c1 = new Count(1000).div(2); """ module = TSCore.createModuleFromWpp(source, 'intDiv.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))