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))
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 { public x: number; public y: number; constructor(x = 0, y = 0) { this.x = x; this.y = y; } } const pt0 = new Point(); const pt1 = new Point(1); const pt2 = new Point(1, 2); """ module = TSCore.createModuleFromWpp(source, 'defaultParams.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expect))
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 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 { public x: number; public y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } const pt = new Point(22, 44); const x = pt.x; """ module = TSCore.createModuleFromWpp(source, 'autoinit.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expect))
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 { public x: number; public y: number; constructor(x0: number, y0: number) { this.x = x0; this.y = y0; } } const pt = new Point(22, 44); const x = pt.x; """ module = TSCore.createModuleFromWpp(source, 'single.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expect))
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 testEq(self): source = """ class simple Count field value: unsigned long constructor autoinit value = 0 operator const ==: bool param n: unsigned long return value == n var const flag: bool = Count(1000) == 1000 """ expected = """ class Count { private value: number; constructor(value = 0) { this.value = value; } eq(n: number): boolean { return this.value === n; } } const flag = new Count(1000).eq(1000); """ module = TSCore.createModuleFromWpp(source, 'eq.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.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 testAlias(self): """Alias of core type""" source = "var const public firstByte: byte = 15" tsModule = TSCore.createModuleFromWpp(source, 'alias.wpp') ctx = OutContextMemoryStream() tsModule.exportContext(ctx, style) # transform byte into number self.assertEqual(str(ctx), 'export const firstByte = 15;')
def testClone(self): srcModule = WppCore.createMemModule('', 'number.mem') self.assertEqual(srcModule.type, 'module') self.assertEqual(srcModule.getName(), 'number') dstCore = TSCore.createInstance() dstModule = srcModule.cloneAll(dstCore) self.assertEqual(dstModule.type, 'module') self.assertEqual(dstModule.getName(), 'number_') self.assertTrue(dstModule.isModule())
def testShortForm(self): source = """ func public shortFunc: double param singleParam: double return singleParam """ expected = 'export const shortFunc = (singleParam: number): number => singleParam;' module = TSCore.createModuleFromWpp(source, 'shortForm.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), expected)
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 org: Point = Point() var const first: Point = Point(1, 2) var const second: Point = Point(first) """ expected = """ class Point { public x: number; public y: number; constructor() { this.x = 0; this.y = 0; } static initPoint(x: number, y: number): Point { const _inst = new Point(); _inst.x = x; _inst.y = y; return _inst; } static copyPoint(src: Point): Point { const _inst = new Point(); _inst.x = src.x; _inst.y = src.y; return _inst; } } const org = new Point(); const first = Point.initPoint(1, 2); const second = Point.copyPoint(first); """ module = TSCore.createModuleFromWpp(source, 'overload.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testComment(self): source = """ typedef public Size = unsigned long # This is comment. # Second line. """ expected = """ // This is comment. // Second line. export type Size = number; """ tsModule = TSCore.createModuleFromWpp(source, 'comment.wpp') ctx = OutContextMemoryStream() tsModule.exportContext(ctx, style) self.assertEqual(str(ctx), expected.strip())
def testCmp(self): source = """ var const a: int = 5 var const eq: bool = a == 0 var const ne: bool = a != 0 """ expected = """ const a = 5; const eq = a === 0; const ne = a !== 0; """ module = TSCore.createModuleFromWpp(source, 'cmp.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testComment(self): source = """ # Header comment. # Second header line. var public const isActive: bool = true """ expected = """ // Header comment. // Second header line. export const isActive = true; """ tsModule = TSCore.createModuleFromWpp(source, 'comment.wpp') ctx = OutContextMemoryStream() tsModule.exportContext(ctx, style) self.assertEqual(str(ctx), expected.strip())
def testComment(self): source = """ var const public bufferSize: size_t = 256 # This is comment. # Second line... """ expected = """ // This is comment. // Second line... export const bufferSize = 256; """ tsModule = TSCore.createModuleFromWpp(source, 'comment.wpp') ctx = OutContextMemoryStream() tsModule.exportContext(ctx, style) self.assertEqual(str(ctx), expected.strip())
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 { public x = 0; } const pt = new Point(); const x = pt.x; """ module = TSCore.createModuleFromWpp(source, 'empty.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expect))
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 = """ const a = 1; const b = 2; const c = a + b * 3; const d = (a + b) * c; const e = a + b * c; """ # В последней строке убираются ненужные скобки module = TSCore.createModuleFromWpp(source, 'arith.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testExtends(self): source = """ class Top class Middle extends Top class Bottom extends Middle """ expected = """ class Top { } class Middle extends Top { } class Bottom extends Middle { } """ module = TSCore.createModuleFromWpp(source, 'extends.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testSimpleField(self): source = """ class Value field public value: double = 1.0 method setValue param v: double value = v """ expected = """ class Value { public value = 1.0; setValue(v: number): void { this.value = v; } } """ module = TSCore.createModuleFromWpp(source, 'simple.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testStaticField(self): source = """ class Value field public value: double = 1.0 field private static epsilon: double = 0.001 method is0: bool return value < epsilon """ expected = """ class Value { public value = 1.0; private static epsilon = 0.001; is0(): boolean { return this.value < Value.epsilon; } } """ module = TSCore.createModuleFromWpp(source, 'static.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testSimple(self): tsModule = TSCore.createModuleFromWpp( "var public const pi: double = 3.14", 'module.wpp') self.assertEqual(tsModule.type, 'module') self.assertEqual(len(tsModule.items), 1) txVarDecl = tsModule.items[0] self.assertEqual(txVarDecl.type, 'var') self.assertIn('hiddenType', txVarDecl.attrs) txTypeExpr = txVarDecl.getTypeTaxon() self.assertEqual(txTypeExpr.type, '@typeExprName') txType = txTypeExpr.getTypeTaxon() self.assertEqual(txType.type, 'scalar') self.assertEqual(txType.getName(), 'number') # 'double' in TS translated to 'number' txVal = txVarDecl.getValueTaxon() self.assertIsNot(txVal, None) self.assertEqual(txVal.type, 'const') code = tsModule.exportText(style)[0] self.assertEqual(code, 'export const pi = 3.14;')
def testShift(self): source = """ var const s: int = 128 var const u: unsigned int = 128 var const sl: int = s << 2 var const ul: unsigned int = u << 2 var const sr: int = s >> 2 var const ur: unsigned int = u >> 2 """ expected = """ const s = 128; const u = 128; const sl = s << 2; const ul = u << 2; const sr = s >> 2; const ur = u >>> 2; """ module = TSCore.createModuleFromWpp(source, 'shift.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def testOverloadRight(self): source = """ class simple Point field public x: double field public y: double constructor autoinit x = 0 autoinit y = 0 operator const overload *: Point param right: double return Point(x * right, y * right) operator const overload right *: Point param left: double return Point(left * x, left * y) var const a: Point = Point(11, 22) * 33 var const b: Point = 1.1 * Point(0, -1) """ expected = """ class Point { public x: number; public y: number; constructor(x = 0, y = 0) { this.x = x; this.y = y; } mul(right: number): Point { return new Point(this.x * right, this.y * right); } rmul(left: number): Point { return new Point(left * this.x, left * this.y); } } const a = new Point(11, 22).mul(33); const b = new Point(0, -1).rmul(1.1); """ module = TSCore.createModuleFromWpp(source, 'overloadR.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
def exportTS(module): from TS.TSCore import TSCore from TS.style import style print('-- TypeScript') tsModule = TSCore.createFromSource(module) outCtx = OutContextMemoryStream() tsModule.exportContext(outCtx, style) printCtx(outCtx) # Debug output lexems to JSON lexems = [] tsModule.exportLexems(lexems, style) rows = ['['] for lex in lexems: rows.append(' ["%s", "%s"],' % lex) rows[-1] = rows[-1][0:-1] rows.append(']') f = open('lexems.json', 'w') for s in rows: f.write("%s\n" % s) f.close() with open("style.json", "w") as write_file: json.dump(style, write_file, ensure_ascii=False, indent=4)
def testIntDiv(self): source = """ var const sA: int = 10 var const sB: int = 3 var const s1: int = sA / sB var const s2: int = sA / sB + 1 var const s3: int = sA / 3 var const s4: int = 1000 / sB """ expected = """ const sA = 10; const sB = 3; const s1 = sA / sB | 0; const s2 = (sA / sB | 0) + 1; const s3 = sA / 3 | 0; const s4 = 1000 / sB | 0; """ # sA / sB => sA / sB | 0 Необходимо использовать | 0, т.к. результат должен быть целым числом 10 // 3 = 3 # sA / sB + 1 => (sA / sB | 0) + 1 Применяются скобки, т.к. приоритет операции | ниже, чем + module = TSCore.createModuleFromWpp(source, 'intDiv.wpp') ctx = OutContextMemoryStream() module.exportContext(ctx, style) self.assertEqual(str(ctx), module.strPack(expected))
import unittest from TS.TSExpression import TSConst from Wpp.WppExpression import WppConst from TS.TSCore import TSCore singleStyle = {'singleQuote': True} doubleStyle = {'singleQuote': False} tsCore = TSCore.createInstance() def fromWpp(stringValue): wpp = WppConst.create(stringValue) return wpp.clone(tsCore) class TestTSConst(unittest.TestCase): def testString(self): self.assertEqual(TSConst.makeString('Hello', singleStyle), "'Hello'") self.assertEqual(TSConst.makeString('Hello', doubleStyle), '"Hello"') self.assertEqual(TSConst.makeString('\t"Hello"\r\n', singleStyle), """'\\t"Hello"\\r\\n'""") self.assertEqual(TSConst.makeString('\t"Hello"\r\n', doubleStyle), '''"\\t\\"Hello\\"\\r\\n"''') def testClone(self): wppInt = WppConst.create('123') self.assertEqual(wppInt.constType, 'int') self.assertEqual(wppInt.value, 123) tsInt = wppInt.clone(tsCore) self.assertEqual(tsInt.type, 'const')
def testReservedWords(self): core = TSCore.createInstance() self.assertEqual(core.getSafeName('myName'), 'myName') self.assertEqual(core.getSafeName('class'), 'class_') self.assertEqual(core.getSafeName('from'), 'from_')