def testInClass(self): source = """ class public Point typedef replace Value: double field public x: Value class public Rect typedef replace Value: Point.Value field public x1: Value """ expected = """ export class Point { public x: number; } export class Rect { public x1: number; } """ srcModule = WppCore.createMemModule(source, 'simple.fake') dstModule = srcModule.cloneRoot(TsCore()) Rect = dstModule.dictionary['Rect'] Value = Rect.dictionary['Value'] self.assertEqual(Value.type, 'Typedef') locType = Value.getTypeTaxon() self.assertEqual(locType.type, 'TypePath') outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testArrayReplace(self): source = """ class public A typedef replace Collection: Array String class public B typedef Coll: A.Collection field public a: Coll method init param c: Coll a = c """ expected = """ export class A { } export class B { public a: string[]; public init(c: string[]) { this.a = c; } } """ srcModule = WppCore.createMemModule(source, 'simple.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
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 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 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 testExtern(self): source = """ class public A typedef extern Collection: Array String class public B typedef Coll: A.Collection field a: Coll method init param c: Coll a = c """ expected = """ export type ACollection = string[]; export class A { } export class B { private a: ACollection; public init(c: ACollection) { this.a = c; } } """ srcModule = WppCore.createMemModule(source, 'sextern.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testBase(self): source = """ class Point field x: double field y: double constructor param init x param init y operator +=: ref Point param pt: const ref Point x += pt.x y += pt.y this operator const +: Point param pt: const ref Point Point(x + pt.x, y + pt.y) operator const right *: Point param k: double Point(k * x, k * y) operator const -: Point Point(-x, -y) """ expected = """ export class Point { private x: number; private y: number; public constructor(x: number, y: number) { this.x = x; this.y = y; } public iadd(pt: Point): Point { this.x += pt.x; this.y += pt.y; return this; } public add(pt: Point): Point { return new Point(this.x + pt.x, this.y + pt.y); } public rmul(k: number): Point { return new Point(k * this.x, k * this.y); } public neg(): Point { return new Point(-this.x, -this.y); } } """ 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 = """ typedef TSize: unsigned long """ expected = 'export type TSize = number;' srcModule = WppCore.createMemModule(source, 'simple.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testArrayValue(self): source = """ var const myList: Array String = ["First", "Second", "Third"] """ expected = """ export const myList: string[] = ['First', 'Second', 'Third']; """ srcModule = WppCore.createMemModule(source, 'arrayValue.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testBool(self): source = """ var const public myTrue: bool = true var const public myFalse: bool = false """ expected = """ export const myTrue: boolean = true; export const myFalse: boolean = false; """ srcModule = WppCore.createMemModule(source, 'bool.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), dstModule.strPack(expected))
def testParam(self): source = """ func public getInfo: String param name: String param prefix: String = "Hello, " """ expected = """ export function getInfo(name: string, prefix: string = 'Hello, '): string { } """ srcModule = WppCore.createMemModule(source, 'param.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), dstModule.strPack(expected))
def testBinOp(self): source = """ var const public first: int = 2 + 3 * 4 var const public second: int = (2 + 3) * 4 var const public bin: bool = true == first < second """ expected = """ export const first: number = 2 + 3 * 4; export const second: number = (2 + 3) * 4; export const bin: boolean = (true === first) < second; """ srcModule = WppCore.createMemModule(source, 'A.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), dstModule.strPack(expected))
def testVarExternal(self): """ Variable - member of module """ source = """ var hidden: bool var public visible: double = 1.5 """ expected = """ let hidden: boolean; export let visible: number = 1.5; """ srcModule = WppCore.createMemModule(source, 'var.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testMath(self): source = """ func public main var const a: double = Math.sin(Math.PI * 0.3) var const b: double = Math.min(a, 1) """ expected = """ export function main() { const a: number = Math.sin(Math.PI * 0.3); const b: number = Math.min(a, 1); } """ srcModule = WppCore.createMemModule(source, 'math.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testField(self): source = """ class Hello field count: unsigned long field public value: String = "Hello!" field public static inst: Hello """ expected = """ export class Hello { private count: number; public value: string = 'Hello!'; public static inst: Hello; } """ srcModule = WppCore.createMemModule(source, 'var.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testAngles(self): source = """ func public main var const deg: double = Math.degrees(Math.PI / 2) var const rad: double = Math.radians(270) var const rad2: double = Math.radians(deg + 15) """ expected = """ export function main() { const deg: number = Math.PI / 2 * 57.29577951308232; const rad: number = 4.71238898038469; const rad2: number = (deg + 15) * 0.017453292519943295; } """ srcModule = WppCore.createMemModule(source, 'angles.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testTernaryOp(self): source = """ var const public a: int = 123 var const public b: int = a < 0 ? -1 : 1 var const public c: int = a < b ? 0 : a + b var const public d: int = a < b ? 0 : (a + b) var const public e: int = (a < b ? 0 : a) + b """ expected = """ export const a: number = 123; export const b: number = a < 0 ? -1 : 1; export const c: number = a < b ? 0 : a + b; export const d: number = a < b ? 0 : a + b; export const e: number = (a < b ? 0 : a) + b; """ srcModule = WppCore.createMemModule(source, 'ternaryOp.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), dstModule.strPack(expected))
def testSimple(self): source = """ class TsClassA class public TsClassB # Comment extends TsClassA """ expected = """ class TsClassA { } // Comment export class TsClassB extends TsClassA { } """ srcModule = WppCore.createMemModule(source, 'simple.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testFieldExpr(self): source = """ class FieldTest field myField: double method useField param another: const ref FieldTest var const x: double = another.myField """ expected = """ export class FieldTest { private myField: number; public useField(another: FieldTest) { const x: number = another.myField; } } """ srcModule = WppCore.createMemModule(source, 'fieldExpr.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testThisNull(self): source = """ class public MyTest field value: MyTest = null method work param value: MyTest this.value = value """ expected = """ export class MyTest { private value: MyTest = null; public work(value: MyTest) { this.value = value; } } """ srcModule = WppCore.createMemModule(source, 'thisNull.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testMethods(self): source = """ class Test method private hidden: bool param instance: Test method public static main param title: String """ expected = """ export class Test { private hidden(instance: Test): boolean { } public static main(title: string) { } } """ srcModule = WppCore.createMemModule(source, 'simple.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), expected.strip())
def testMinus(self): source = """ class Point field x: double field y: double constructor param init x param init y operator -: Point param pt: Point Point(x - pt.x, y - pt.y) operator -: Point Point(-x, -y) """ expected = """ export class Point { private x: number; private y: number; public constructor(x: number, y: number) { this.x = x; this.y = y; } public sub(pt: Point): Point { return new Point(this.x - pt.x, this.y - pt.y); } public neg(): Point { return new Point(-this.x, -this.y); } } """ srcModule = WppCore.createMemModule(source, 'minus.fake') dstModule = srcModule.cloneRoot(TsCore()) classPoint = dstModule.dictionary['Point'] minusOver = classPoint.dictionary['-'] self.assertEqual(minusOver.type, 'Overloads') self.assertEqual(len(minusOver.items), 2) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testUnOp(self): source = """ var const public a: int = -20 var const public b: int = -a var const public c: int = ~b var const public d: bool = !c var const public e: int = -a * -b var const public f: int = -(a & b) """ expected = """ export const a: number = -20; export const b: number = -a; export const c: number = ~b; export const d: boolean = !c; export const e: number = -a * -b; export const f: number = -(a & b); """ srcModule = WppCore.createMemModule(source, 'unOp.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), dstModule.strPack(expected))
def testIdExpr(self): source = """ class IdExpr field myField: int = 256 method someFunc param myParam: String var const v1: int = myField var const v2: String = myParam """ expected = """ export class IdExpr { private myField: number = 256; public someFunc(myParam: string) { const v1: number = this.myField; const v2: string = myParam; } } """ srcModule = WppCore.createMemModule(source, 'IdExpr.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testCallAndNew(self): source = """ class A method work func public main var const a: A = A() a.work() """ expected = """ class A { public work() { } } export function main() { const a: A = new A(); a.work(); } """ srcModule = WppCore.createMemModule(source, 'A.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), dstModule.strPack(expected))
def testConstructor(self): source = """ class Point field x: double field y: double constructor param init x = 0 param init y = 0 """ expected = """ export class Point { private x: number; private y: number; public constructor(x: number = 0, y: number = 0) { this.x = x; this.y = y; } } """ srcModule = WppCore.createMemModule(source, 'static.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), WppCore.strPack(expected))
def testCheckThis(self): source = """ class A method first: String "Hello, " method second: String param name: String first() + name """ expected = """ export class A { public first(): string { return 'Hello, '; } public second(name: string): string { return this.first() + name; } } """ srcModule = WppCore.createMemModule(source, 'simple.fake') dstModule = srcModule.cloneRoot(TsCore()) outContext = OutContextMemoryStream() dstModule.export(outContext) self.assertEqual(str(outContext), expected.strip())
import sys sys.path.append(os.path.realpath('../src')) from Wpp.WppCore import WppCore from Python.PyCore import PyCore from TS.TsCore import TsCore from out.OutContextFolder import OutContextFolder from out.deleteTree import deleteTree rootPath = os.path.dirname(os.path.abspath(__file__)) def exportProject(srcRoot, dirName, core): outPath = os.path.join(rootPath, dirName) print('Export to ' + outPath) dstRoot = srcRoot.cloneRoot(core) deleteTree(outPath) os.mkdir(outPath) outContext = OutContextFolder(outPath) dstRoot.export(outContext) wppPath = os.path.join(rootPath, 'wpp') print('Build CharChem. Reading from ' + wppPath) core = WppCore() srcRoot = core.createRootPackage('CharChem', wppPath) # exportProject(srcRoot, 'Python', PyCore()) exportProject(srcRoot, 'TypeScript', TsCore()) print('Success')