From d36f0f9919177f22aba8d6f27dceed4beffc5d80 Mon Sep 17 00:00:00 2001 From: zhuoli Date: Tue, 11 Jan 2022 20:49:46 +0800 Subject: [PATCH] Add UT test for stage 1 typeInfo support Signed-off-by: zhuoli Change-Id: I046056142cf2eeb10ac4b7c5981d55ddc5d55c89 --- ts2panda/src/base/literal.ts | 4 + ts2panda/src/compilerDriver.ts | 6 +- ts2panda/tests/types/array.test.ts | 250 +++++++++++++ ts2panda/tests/types/array/array_class.ts | 19 + .../types/array/array_multi_same_class.ts | 20 ++ .../types/array/array_multi_same_primi.ts | 29 ++ ts2panda/tests/types/array/array_object.ts | 18 + .../tests/types/array/array_primitives.ts | 23 ++ ts2panda/tests/types/array/array_union.ts | 19 + ts2panda/tests/types/class.test.ts | 337 ++++++++++++++++++ ts2panda/tests/types/class/class_abstract.ts | 28 ++ .../tests/types/class/class_constr_no_para.ts | 24 ++ .../tests/types/class/class_constr_para.ts | 24 ++ ts2panda/tests/types/class/class_fields.ts | 24 ++ .../tests/types/class/class_implements.ts | 21 ++ ts2panda/tests/types/class/class_methods.ts | 29 ++ .../tests/types/class/class_static_fields.ts | 24 ++ .../tests/types/class/class_static_methods.ts | 30 ++ ts2panda/tests/types/function.test.ts | 202 +++++++++++ .../types/function/function_class_para.ts | 19 + .../types/function/function_class_return.ts | 21 ++ .../types/function/function_multi_para.ts | 20 ++ .../tests/types/function/function_no_para.ts | 18 + ts2panda/tests/types/object.test.ts | 92 +++++ ts2panda/tests/types/object/object_class.ts | 22 ++ ts2panda/tests/types/object/object_primi.ts | 21 ++ ts2panda/tests/types/primitives.test.ts | 197 ++++++++++ .../types/primitives/primitives_in_block.ts | 24 ++ .../types/primitives/primitives_in_class.ts | 27 ++ .../types/primitives/primitives_in_for.ts | 24 ++ .../primitives/primitives_in_function.ts | 25 ++ .../types/primitives/primitives_in_if.ts | 24 ++ ts2panda/tests/types/typeUtils.ts | 138 +++++++ ts2panda/tests/types/union.test.ts | 166 +++++++++ .../types/union/union_multi_same_primi.ts | 19 + .../union/union_multi_userDefinedType.ts | 20 ++ .../tests/types/union/union_primitives.ts | 21 ++ .../types/union/union_userDefinedType.ts | 19 + ts2panda/tests/utils/base.ts | 10 +- 39 files changed, 2052 insertions(+), 6 deletions(-) create mode 100644 ts2panda/tests/types/array.test.ts create mode 100644 ts2panda/tests/types/array/array_class.ts create mode 100644 ts2panda/tests/types/array/array_multi_same_class.ts create mode 100644 ts2panda/tests/types/array/array_multi_same_primi.ts create mode 100644 ts2panda/tests/types/array/array_object.ts create mode 100644 ts2panda/tests/types/array/array_primitives.ts create mode 100644 ts2panda/tests/types/array/array_union.ts create mode 100644 ts2panda/tests/types/class.test.ts create mode 100644 ts2panda/tests/types/class/class_abstract.ts create mode 100644 ts2panda/tests/types/class/class_constr_no_para.ts create mode 100644 ts2panda/tests/types/class/class_constr_para.ts create mode 100644 ts2panda/tests/types/class/class_fields.ts create mode 100644 ts2panda/tests/types/class/class_implements.ts create mode 100644 ts2panda/tests/types/class/class_methods.ts create mode 100644 ts2panda/tests/types/class/class_static_fields.ts create mode 100644 ts2panda/tests/types/class/class_static_methods.ts create mode 100644 ts2panda/tests/types/function.test.ts create mode 100644 ts2panda/tests/types/function/function_class_para.ts create mode 100644 ts2panda/tests/types/function/function_class_return.ts create mode 100644 ts2panda/tests/types/function/function_multi_para.ts create mode 100644 ts2panda/tests/types/function/function_no_para.ts create mode 100644 ts2panda/tests/types/object.test.ts create mode 100644 ts2panda/tests/types/object/object_class.ts create mode 100644 ts2panda/tests/types/object/object_primi.ts create mode 100644 ts2panda/tests/types/primitives.test.ts create mode 100644 ts2panda/tests/types/primitives/primitives_in_block.ts create mode 100644 ts2panda/tests/types/primitives/primitives_in_class.ts create mode 100644 ts2panda/tests/types/primitives/primitives_in_for.ts create mode 100644 ts2panda/tests/types/primitives/primitives_in_function.ts create mode 100644 ts2panda/tests/types/primitives/primitives_in_if.ts create mode 100644 ts2panda/tests/types/typeUtils.ts create mode 100644 ts2panda/tests/types/union.test.ts create mode 100644 ts2panda/tests/types/union/union_multi_same_primi.ts create mode 100644 ts2panda/tests/types/union/union_multi_userDefinedType.ts create mode 100644 ts2panda/tests/types/union/union_primitives.ts create mode 100644 ts2panda/tests/types/union/union_userDefinedType.ts diff --git a/ts2panda/src/base/literal.ts b/ts2panda/src/base/literal.ts index e246cdc4965..804f9413a5b 100755 --- a/ts2panda/src/base/literal.ts +++ b/ts2panda/src/base/literal.ts @@ -52,6 +52,10 @@ export class LiteralBuffer { this.literalBuffer.push(...literals); } + getLiterals() { + return this.literalBuffer; + } + isEmpty() { return this.literalBuffer.length == 0; } diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index b86151953c1..03dc1e86786 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -44,6 +44,7 @@ import { getClassNameForConstructor } from "./statement/classStatement"; import { checkDuplicateDeclaration, checkExportEntries } from "./syntaxChecker"; import { Ts2Panda } from "./ts2panda"; import { TypeRecorder } from "./typeRecorder"; +import { LiteralBuffer } from "./base/literal"; export class PendingCompilationUnit { constructor( @@ -239,13 +240,16 @@ export class CompilerDriver { } } - compileUnitTest(node: ts.SourceFile): void { + compileUnitTest(node: ts.SourceFile, literalBufferArray?: Array): void { let recorder = this.compilePrologue(node, true); for (let i = 0; i < this.pendingCompilationUnits.length; i++) { let unit: PendingCompilationUnit = this.pendingCompilationUnits[i]; this.compileUnitTestImpl(unit.decl, unit.scope, unit.internalName, recorder); } + if (literalBufferArray) { + PandaGen.getLiteralArrayBuffer().forEach(val => literalBufferArray.push(val)); + } PandaGen.clearLiteralArrayBuffer(); } diff --git a/ts2panda/tests/types/array.test.ts b/ts2panda/tests/types/array.test.ts new file mode 100644 index 00000000000..691d80bf98b --- /dev/null +++ b/ts2panda/tests/types/array.test.ts @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import 'mocha'; +import { + compileTsWithType, + createLiteralBufferArray, + compareLiteralBuffer, + createVRegTypePair, + compareVReg2Type +} from "./typeUtils"; +import { PrimitiveType } from '../../src/base/typeSystem'; + +let shift = PrimitiveType._LENGTH; + +describe("array tests in array.test.ts", function() { + it("test array with primitives", function() { + let fileNames = 'tests/types/array/array_primitives.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#num", shift + 1], + ["#4#bool", shift + 2], + ["#5#str", shift + 3], + ["#6#sym", shift + 4], + ["#7#nu", shift + 5], + ["#8#und", shift + 6], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 6], [2, 0] + ], + [ + [2, 5], [2, 1] + ], + [ + [2, 5], [2, 2] + ], + [ + [2, 5], [2, 4] + ], + [ + [2, 5], [2, 5] + ], + [ + [2, 5], [2, 6] + ], + [ + [2, 5], [2, 7] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test array with class", function() { + let fileNames = 'tests/types/array/array_class.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#arrayA", shift + 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 3], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, 51] + ], + [ + [2, 5], [2, 52] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test array with multi same primitive", function() { + let fileNames = 'tests/types/array/array_multi_same_primi.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#num1", shift + 1], + ["#4#num2", shift + 1], + ["#5#bool1", shift + 2], + ["#6#bool2", shift + 2], + ["#7#str1", shift + 3], + ["#8#str2", shift + 3], + ["#9#sym1", shift + 4], + ["#10#sym2", shift + 4], + ["#11#nu1", shift + 5], + ["#12#nu2", shift + 5], + ["#13#und1", shift + 6], + ["#14#und2", shift + 6], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 6], [2, 0] + ], + [ + [2, 5], [2, 1] + ], + [ + [2, 5], [2, 2] + ], + [ + [2, 5], [2, 4] + ], + [ + [2, 5], [2, 5] + ], + [ + [2, 5], [2, 6] + ], + [ + [2, 5], [2, 7] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test array with multi same class", function() { + let fileNames = 'tests/types/array/array_multi_same_class.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#arrayA", shift + 3], + ["#5#arrayB", shift + 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 3], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, 51] + ], + [ + [2, 5], [2, 52] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test array with union", function() { + let fileNames = 'tests/types/array/array_union.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#a", shift + 2], + ["#4#b", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 2], [2, 0] + ], + [ + [2, 4], [2, 2], [2, 4], [2, 1], + ], + [ + [2, 5], [2, 51] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test array with object", function() { + let fileNames = 'tests/types/array/array_object.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#a", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 2], [2, 0] + ], + [ + [2, 6], [2, 2], [5, "element1"], [2, 1], + [5, "element2"], [2, 4], + ], + [ + [2, 5], [2, 51] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); +}); diff --git a/ts2panda/tests/types/array/array_class.ts b/ts2panda/tests/types/array/array_class.ts new file mode 100644 index 00000000000..e86bc503bd2 --- /dev/null +++ b/ts2panda/tests/types/array/array_class.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A{}; + let arrayA: A[]; +} \ No newline at end of file diff --git a/ts2panda/tests/types/array/array_multi_same_class.ts b/ts2panda/tests/types/array/array_multi_same_class.ts new file mode 100644 index 00000000000..995be89466a --- /dev/null +++ b/ts2panda/tests/types/array/array_multi_same_class.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A{}; + let arrayA: A[]; + let arrayB: A[]; +} \ No newline at end of file diff --git a/ts2panda/tests/types/array/array_multi_same_primi.ts b/ts2panda/tests/types/array/array_multi_same_primi.ts new file mode 100644 index 00000000000..c2fb4e9773b --- /dev/null +++ b/ts2panda/tests/types/array/array_multi_same_primi.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let num1: number[]; + let num2: number[]; + let bool1: boolean[]; + let bool2: boolean[]; + let str1: string[]; + let str2: string[]; + let sym1: symbol[]; + let sym2: symbol[]; + let nu1: null[]; + let nu2: null[]; + let und1: undefined[]; + let und2: undefined[]; +} \ No newline at end of file diff --git a/ts2panda/tests/types/array/array_object.ts b/ts2panda/tests/types/array/array_object.ts new file mode 100644 index 00000000000..5bbe958eab5 --- /dev/null +++ b/ts2panda/tests/types/array/array_object.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let a: { element1: number; element2: string }[]; +} \ No newline at end of file diff --git a/ts2panda/tests/types/array/array_primitives.ts b/ts2panda/tests/types/array/array_primitives.ts new file mode 100644 index 00000000000..b9bc5a1bdfb --- /dev/null +++ b/ts2panda/tests/types/array/array_primitives.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let num: number[]; + let bool: boolean[]; + let str: string[]; + let sym: symbol[]; + let nu: null[]; + let und: undefined[]; +} \ No newline at end of file diff --git a/ts2panda/tests/types/array/array_union.ts b/ts2panda/tests/types/array/array_union.ts new file mode 100644 index 00000000000..940aa5030a8 --- /dev/null +++ b/ts2panda/tests/types/array/array_union.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let a: (string | number)[]; + let b: (string | number)[]; +} \ No newline at end of file diff --git a/ts2panda/tests/types/class.test.ts b/ts2panda/tests/types/class.test.ts new file mode 100644 index 00000000000..b0545a03605 --- /dev/null +++ b/ts2panda/tests/types/class.test.ts @@ -0,0 +1,337 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import 'mocha'; +import { + compileTsWithType, + createLiteralBufferArray, + compareLiteralBuffer, + createVRegTypePair, + compareVReg2Type +} from "./typeUtils"; +import { PrimitiveType } from '../../src/base/typeSystem'; + +let shift = PrimitiveType._LENGTH; + +describe("class tests in class.test.ts", function () { + it("test class with no parameter in block", function () { + let fileNames = 'tests/types/class/class_constr_no_para.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#a", shift + 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 3], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], + [2, 0], [2, 1], [5, 'num'], + [2, 1], [2, 0], [2, 0], [2, 1], + [5, 'constructor'], [2, shift + 2], [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], + [5, 'constructor'], [2, 0], [2, 0] + ], + [ + [2, 2], [2, shift + 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test class with parameter in block", function () { + let fileNames = 'tests/types/class/class_constr_para.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#a", shift + 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 3], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 1], [5, 'num'], [2, 1], [2, 0], + [2, 0], [2, 1], [5, 'constructor'], [2, shift + 2], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'constructor'], + [2, 1], [2, 1], [2, 0] + ], + [ + [2, 2], [2, shift + 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test class fields type", function () { + let fileNames = 'tests/types/class/class_fields.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#a", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 2], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 4], [5, 'num1'], [2, 1], [2, 0], [2, 0], + [5, 'str1'], [2, 4], [2, 1], [2, 0], + [5, 'num2'], [2, 1], [2, 2], [2, 0], + [5, 'str2'], [2, 4], [2, 0], [2, 1], + [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, shift + 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test class methods type", function () { + let fileNames = 'tests/types/class/class_methods.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#a", shift + 4], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 1], [5, 'val'], [2, 1], [2, 0], + [2, 0], [2, 2], [5, 'setVal'], [2, 52], + [5, 'getValStr'], [2, 53], [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'setVal'], + [2, 1], [2, 1], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'getValStr'], + [2, 0], [2, 4] + ], + [ + [2, 2], [2, 51] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test class static fields type", function () { + let fileNames = 'tests/types/class/class_static_fields.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#a", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 2], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 2], [5, 'num1'], [2, 1], [2, 0], + [2, 0], [5, 'str1'], [2, 4], [2, 1], + [2, 0], [2, 0], [2, 2], [5, 'num2'], + [2, 1], [2, 0], [2, 0], [5, 'str2'], + [2, 4], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, 51] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test class static methods type", function () { + let fileNames = 'tests/types/class/class_static_methods.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#1#num", 1], + ["#3#A", shift + 1], + ["#4#a", shift + 4], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 1], [5, 'val'], [2, 1], [2, 0], + [2, 0], [2, 1], [5, 'setVal'], [2, shift + 2], + [2, 1], [5, 'str'], [2, 4], [2, 0], + [2, 0], [2, 1], [5, 'getStr'], [2, shift + 3] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'setVal'], + [2, 1], [2, 1], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 1], [5, 'getStr'], + [2, 0], [2, 4] + ], + [ + [2, 2], [2, shift + 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test abstract class type", function () { + let fileNames = 'tests/types/class/class_abstract.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#animal", shift + 2], + ["#4#dog", shift + 1], + ["#5#d", shift + 5], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 5], [2, 0] + ], + [ + [2, 1], [2, 0], [2, shift + 2], [2, 0], + [2, 0], [2, 1], [5, 'constructor'], [2, shift + 4], + [2, 0], [2, 0] + ], + [ + [2, 1], [2, 1], [2, 0], [2, 0], + [2, 0], [2, 1], [5, 'eat'], [2, shift + 3], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'eat'], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'constructor'], + [2, 0], [2, 0] + ], + [ + [2, 2], [2, shift + 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test class implements type", function () { + let fileNames = 'tests/types/class/class_implements.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#base1", shift + 2], + ["#4#base2", shift + 3], + ["#5#A", shift + 1], + ["#6#a", shift + 4], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 2], + [2, shift + 2], [2, shift + 3], [2, 0], [2, 0], + [2, 0], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, shift + 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); +}); diff --git a/ts2panda/tests/types/class/class_abstract.ts b/ts2panda/tests/types/class/class_abstract.ts new file mode 100644 index 00000000000..14b6dfd9e70 --- /dev/null +++ b/ts2panda/tests/types/class/class_abstract.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + abstract class animal { + eat() { }; + } + + class dog extends animal { + constructor() { + super(); + } + } + + let d = new dog(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/class/class_constr_no_para.ts b/ts2panda/tests/types/class/class_constr_no_para.ts new file mode 100644 index 00000000000..bc826bb0b33 --- /dev/null +++ b/ts2panda/tests/types/class/class_constr_no_para.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A { + num: number; + constructor() { + this.num = 3; + } + } + let a = new A(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/class/class_constr_para.ts b/ts2panda/tests/types/class/class_constr_para.ts new file mode 100644 index 00000000000..96746b19392 --- /dev/null +++ b/ts2panda/tests/types/class/class_constr_para.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A { + num: number; + constructor(input: number) { + this.num = input; + } + } + let a = new A(5); +} \ No newline at end of file diff --git a/ts2panda/tests/types/class/class_fields.ts b/ts2panda/tests/types/class/class_fields.ts new file mode 100644 index 00000000000..5933369d650 --- /dev/null +++ b/ts2panda/tests/types/class/class_fields.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A { + public num1: number = 0; + private str1: string = ""; + protected num2: number = 0; + readonly str2: string = ""; + } + let a = new A(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/class/class_implements.ts b/ts2panda/tests/types/class/class_implements.ts new file mode 100644 index 00000000000..06b46826ad3 --- /dev/null +++ b/ts2panda/tests/types/class/class_implements.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class base1 { } + class base2 { } + class A implements base1, base2 { } + let a = new A(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/class/class_methods.ts b/ts2panda/tests/types/class/class_methods.ts new file mode 100644 index 00000000000..2f9aef32820 --- /dev/null +++ b/ts2panda/tests/types/class/class_methods.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A { + val: number = 3; + + setVal(num: number) { + this.val = num; + } + + getValStr(): string { + return "" + this.val; + } + } + let a = new A(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/class/class_static_fields.ts b/ts2panda/tests/types/class/class_static_fields.ts new file mode 100644 index 00000000000..bf7dd434929 --- /dev/null +++ b/ts2panda/tests/types/class/class_static_fields.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A { + public num1: number = 0; + static num2: number = 0; + private str1: string = ""; + static str2: string = ""; + } + let a = new A(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/class/class_static_methods.ts b/ts2panda/tests/types/class/class_static_methods.ts new file mode 100644 index 00000000000..81bfe7eda33 --- /dev/null +++ b/ts2panda/tests/types/class/class_static_methods.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A { + val: number = 3; + static str: string = "static"; + + setVal(num: number) { + this.val = num; + } + + static getStr(): string { + return this.str; + } + } + let a = new A(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/function.test.ts b/ts2panda/tests/types/function.test.ts new file mode 100644 index 00000000000..4de684483b2 --- /dev/null +++ b/ts2panda/tests/types/function.test.ts @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import 'mocha'; +import { + compileTsWithType, + createLiteralBufferArray, + compareLiteralBuffer, + createVRegTypePair, + compareVReg2Type +} from "./typeUtils"; +import { PrimitiveType } from '../../src/base/typeSystem'; + +let shift = PrimitiveType._LENGTH; + +describe("function tests in function.test.ts", function () { + it("test function with no parameter", function () { + let fileNames = 'tests/types/function/function_no_para.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#0#emptyFunc", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 1], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'local'], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'emptyFunc'], + [2, 0], [2, 0] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test function with muti parameter", function () { + let fileNames = 'tests/types/function/function_multi_para.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#0#num", 1], + ["#1#str", 4], + ["#0#emptyFunc", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 2], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'local'], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'emptyFunc'], + [2, 2], [2, 1], [2, 4], [2, 4] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test function with muti parameter", function () { + let fileNames = 'tests/types/function/function_multi_para.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#0#num", 1], + ["#1#str", 4], + ["#0#emptyFunc", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 2], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'local'], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'emptyFunc'], + [2, 2], [2, 1], [2, 4], [2, 4] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test function with class as parameter", function () { + let fileNames = 'tests/types/function/function_class_para.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#0#num", 1], + ["#1#a", shift + 4], + ["#0#foo", shift + 2], + ["#1#A", shift + 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'localClass'], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'foo'], + [2, 2], [2, 1], [2, shift + 4], [2, 0] + ], + [ + + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, shift + 3] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test function with class as return", function () { + let fileNames = 'tests/types/function/function_class_return.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#0#foo", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'localClass'], + [2, 0], [2, 0] + ], + [ + [2, 3], [2, 0], [2, 0], [5, 'foo'], + [2, 0], [2, shift + 4] + ], + [ + + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, shift + 3] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); +}); diff --git a/ts2panda/tests/types/function/function_class_para.ts b/ts2panda/tests/types/function/function_class_para.ts new file mode 100644 index 00000000000..ff5f3b4c35b --- /dev/null +++ b/ts2panda/tests/types/function/function_class_para.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function localClass() { + class A { } + function foo(num: number, a: A) { } +} \ No newline at end of file diff --git a/ts2panda/tests/types/function/function_class_return.ts b/ts2panda/tests/types/function/function_class_return.ts new file mode 100644 index 00000000000..7043cca0348 --- /dev/null +++ b/ts2panda/tests/types/function/function_class_return.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function localClassRet() { + class A { } + function foo(): A { + return new A(); + } +} \ No newline at end of file diff --git a/ts2panda/tests/types/function/function_multi_para.ts b/ts2panda/tests/types/function/function_multi_para.ts new file mode 100644 index 00000000000..f69eecc4754 --- /dev/null +++ b/ts2panda/tests/types/function/function_multi_para.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function multi_local() { + function emptyFunc(num: number, str:string): string { + return str; + } +} \ No newline at end of file diff --git a/ts2panda/tests/types/function/function_no_para.ts b/ts2panda/tests/types/function/function_no_para.ts new file mode 100644 index 00000000000..885f0156aa1 --- /dev/null +++ b/ts2panda/tests/types/function/function_no_para.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function local() { + function emptyFunc(): void { } +} \ No newline at end of file diff --git a/ts2panda/tests/types/object.test.ts b/ts2panda/tests/types/object.test.ts new file mode 100644 index 00000000000..3813fe8dbc2 --- /dev/null +++ b/ts2panda/tests/types/object.test.ts @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import 'mocha'; +import { + compileTsWithType, + createLiteralBufferArray, + compareLiteralBuffer, + createVRegTypePair, + compareVReg2Type +} from "./typeUtils"; +import { PrimitiveType } from '../../src/base/typeSystem'; + +let shift = PrimitiveType._LENGTH; + +describe("object tests in object.test.ts", function() { + it("test object with primitives", function() { + let fileNames = 'tests/types/object/object_primi.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#a", shift + 1], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 1], [2, 0] + ], + [ + [2, 6], [2, 2], [5, 'a'], [2, 1], + [5, 'b'], [2, 4] + ], + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test object with user defined type", function() { + let fileNames = 'tests/types/object/object_class.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 2], + ["#4#a", shift + 1], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 6],[2, 2],[5, 'a'],[2, 53], + [5, 'b'],[2, 54] + ], + [ + [2, 1],[2, 0],[2, 0],[2, 0], + [2, 0],[2, 0],[2, 0],[2, 0] + ], + [ + [2, 2], [2, 52] + ], + [ + [2, 4],[2, 2],[2, 4],[2, 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); +}); \ No newline at end of file diff --git a/ts2panda/tests/types/object/object_class.ts b/ts2panda/tests/types/object/object_class.ts new file mode 100644 index 00000000000..c3a643151b9 --- /dev/null +++ b/ts2panda/tests/types/object/object_class.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A{}; + let a: { + a: A; + b: string | number; + } +} \ No newline at end of file diff --git a/ts2panda/tests/types/object/object_primi.ts b/ts2panda/tests/types/object/object_primi.ts new file mode 100644 index 00000000000..dab2a5125bd --- /dev/null +++ b/ts2panda/tests/types/object/object_primi.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let a: { + a: number; + b: string; + } +} \ No newline at end of file diff --git a/ts2panda/tests/types/primitives.test.ts b/ts2panda/tests/types/primitives.test.ts new file mode 100644 index 00000000000..336db5271fd --- /dev/null +++ b/ts2panda/tests/types/primitives.test.ts @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import 'mocha'; +import { + compileTsWithType, + createLiteralBufferArray, + compareLiteralBuffer, + createVRegTypePair, + compareVReg2Type +} from "./typeUtils"; +import { PrimitiveType } from '../../src/base/typeSystem'; + +let shift = PrimitiveType._LENGTH; + +describe("primitives tests in primitives.test.ts", function () { + it("test primitives in block", function () { + let fileNames = 'tests/types/primitives/primitives_in_block.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#num", 1], + ["#4#bool", 2], + ["#5#str", 4], + ["#6#sym", 5], + ["#7#nu", 6], + ["#8#und", 7], + ["#9#vd", 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], + [2, 0], + [2, 0] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test number in function", function () { + let fileNames = 'tests/types/primitives/primitives_in_function.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("numberFunc"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#0#input", 1], + ["#1#num", 1], + ["#2#bool", 2], + ["#3#str", 4], + ["#4#sym", 5], + ["#5#nu", 6], + ["#6#und", 7], + ["#7#vd", 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], + [2, 1], + [2, 0] + ], + [ + [2, 3], + [2, 0], + [2, 0], + [5, 'numberFunc'], + [2, 1], + [2, 1], + [2, 3] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test primitives in for", function () { + let fileNames = 'tests/types/primitives/primitives_in_for.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#i", 0], + ["#4#num", 1], + ["#5#bool", 2], + ["#6#str", 4], + ["#7#sym", 5], + ["#8#nu", 6], + ["#9#und", 7], + ["#10#vd", 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], + [2, 0], + [2, 0] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test primitives in if", function () { + let fileNames = 'tests/types/primitives/primitives_in_if.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#num", 1], + ["#4#bool", 2], + ["#5#str", 4], + ["#6#sym", 5], + ["#7#nu", 6], + ["#8#und", 7], + ["#9#vd", 3], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], + [2, 0], + [2, 0] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test primitives in class", function () { + let fileNames = 'tests/types/primitives/primitives_in_class.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 1], + ["#4#a", shift + 2], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 2], [2, 0] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 7], [5, 'num'], [2, 1], [2, 0], + [2, 0], [5, 'bool'], [2, 2], [2, 0], [2, 0], + [5, 'str'], [2, 4], [2, 0], [2, 0], + [5, 'sym'], [2, 5], [2, 0], [2, 0], + [5, 'nu'], [2, 6], [2, 0], [2, 0], + [5, 'und'], [2, 7], [2, 0], [2, 0], + [5, 'vd'], [2, 3], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, shift + 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); +}); diff --git a/ts2panda/tests/types/primitives/primitives_in_block.ts b/ts2panda/tests/types/primitives/primitives_in_block.ts new file mode 100644 index 00000000000..170fd20fd92 --- /dev/null +++ b/ts2panda/tests/types/primitives/primitives_in_block.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let num: number = 3; + let bool: boolean = false; + let str: string = "myStr"; + let sym: symbol = Symbol("mySym"); + let nu: null = null; + let und: undefined = undefined; + let vd: void = undefined; +} diff --git a/ts2panda/tests/types/primitives/primitives_in_class.ts b/ts2panda/tests/types/primitives/primitives_in_class.ts new file mode 100644 index 00000000000..422a22d325a --- /dev/null +++ b/ts2panda/tests/types/primitives/primitives_in_class.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A { + num: number = 3; + bool: boolean = false; + str: string = "myStr"; + sym: symbol = Symbol("mySym"); + nu: null = null; + und: undefined = undefined; + vd: void = undefined; + } + let a = new A(); +} \ No newline at end of file diff --git a/ts2panda/tests/types/primitives/primitives_in_for.ts b/ts2panda/tests/types/primitives/primitives_in_for.ts new file mode 100644 index 00000000000..9d1fdded0ce --- /dev/null +++ b/ts2panda/tests/types/primitives/primitives_in_for.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +for (let i = 0; i < 1; i++) { + let num: number = 3; + let bool: boolean = false; + let str: string = "myStr"; + let sym: symbol = Symbol("mySym"); + let nu: null = null; + let und: undefined = undefined; + let vd: void = undefined; +} \ No newline at end of file diff --git a/ts2panda/tests/types/primitives/primitives_in_function.ts b/ts2panda/tests/types/primitives/primitives_in_function.ts new file mode 100644 index 00000000000..41aa901dc1f --- /dev/null +++ b/ts2panda/tests/types/primitives/primitives_in_function.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function numberFunc(input: number): void { + let num: number = 3; + let bool: boolean = false; + let str: string = "myStr"; + let sym: symbol = Symbol("mySym"); + let nu: null = null; + let und: undefined = undefined; + let vd: void = undefined; + return vd; +} diff --git a/ts2panda/tests/types/primitives/primitives_in_if.ts b/ts2panda/tests/types/primitives/primitives_in_if.ts new file mode 100644 index 00000000000..137fc65c908 --- /dev/null +++ b/ts2panda/tests/types/primitives/primitives_in_if.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +if (true) { + let num: number = 3; + let bool: boolean = false; + let str: string = "myStr"; + let sym: symbol = Symbol("mySym"); + let nu: null = null; + let und: undefined = undefined; + let vd: void = undefined; +} \ No newline at end of file diff --git a/ts2panda/tests/types/typeUtils.ts b/ts2panda/tests/types/typeUtils.ts new file mode 100644 index 00000000000..ad23c89b314 --- /dev/null +++ b/ts2panda/tests/types/typeUtils.ts @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import 'mocha'; +import * as ts from "typescript"; +import { CacheExpander } from "../../src/pass/cacheExpander"; +import { + SnippetCompiler +} from "../utils/base"; +import { Pass } from "../../src/pass"; +import { ICPass } from "../../src/pass/ICPass"; +import { RegAlloc } from "../../src/regAllocator"; +import { TypeChecker } from "../../src/typeChecker"; +import { CmdOptions } from "../../src/cmdOptions"; +import { + LiteralBuffer, + Literal +} from "../../src/base/literal"; +import { VReg } from "../../src/irnodes"; + +let options = { + outDir: '../tmp/build', + allowJs: true, + noEmitOnError: true, + noImplicitAny: true, + target: 2, + module: 5, + strictNullChecks: true, + skipLibCheck: true, + alwaysStrict: true +} + +export function compileTsWithType(fileName: string) { + + CmdOptions.parseUserCmd([""]); + let filePath = [fileName]; + let program = ts.createProgram(filePath, options); + let sourceFile = program.getSourceFile(fileName); + let typeChecker = TypeChecker.getInstance(); + typeChecker.setTypeChecker(program.getTypeChecker()); + let passes: Pass[] = [ + new CacheExpander(), + new ICPass(), + new RegAlloc() + ]; + let snippetCompiler = new SnippetCompiler(); + let literalBufferArray: Array = new Array(); + snippetCompiler.compile(sourceFile!.getFullText(), passes, literalBufferArray); + return { + literalBufferArray: literalBufferArray, + snippetCompiler: snippetCompiler + } +} + +export function createVRegTypePair(input: any) { + let id2TypeIndex: Map = new Map(); + for (let rol of input) { + id2TypeIndex.set(rol[0], rol[1]); + } + return id2TypeIndex; +} + +export function compareVReg2Type(expectedMap: Map, generated: VReg[]): boolean { + for (let vreg of generated) { + let name = vreg.getVariableName(); + if (name == "4funcObj" || name == "4newTarget" || name == "this") { + continue; + } + let vregMarker = "#" + vreg.num + "#" + vreg.getVariableName() + if (!expectedMap.has(vregMarker)) { + console.log("verg not found: ", vregMarker) + return false; + } + if (expectedMap.get(vregMarker) != vreg.getTypeIndex()) { + console.log("Unmatched expected type with vreg type:"); + console.log("vreg num:", vregMarker); + console.log("vreg type:", vreg.getTypeIndex()); + console.log("expected type:", expectedMap.get(vregMarker)); + return false; + } + } + return true; +} + +export function createLiteralBufferArray(input: any) { + let literalBufferArray: Array = new Array(); + for (let buff of input) { + let literalBuffer: LiteralBuffer = new LiteralBuffer(); + for (let rol of buff) { + let literal = new Literal(rol[0], rol[1]); + literalBuffer.addLiterals(literal); + } + literalBufferArray.push(literalBuffer); + } + return literalBufferArray; +} + +function printLiteralLog(expected: Literal, generated: Literal) { + console.log("expected literals:"); + console.log(expected); + console.log("unmatched literals:"); + console.log(generated); +} + +export function compareLiteralBuffer(expected: Array, generated: Array): boolean { + let size = expected.length; + for (let j = 0; j < size; j++) { + let expectedLiterals = expected[j].getLiterals(); + let generatedLiterals = generated[j].getLiterals(); + if (expectedLiterals.length != generatedLiterals.length) { + console.log("length miss-match in literals ", j); + return false; + } + for (let i = 0; i < generatedLiterals.length; i++) { + if (expectedLiterals[i].getTag != generatedLiterals[i].getTag) { + printLiteralLog(expectedLiterals[i], generatedLiterals[i]); + return false; + } + if (expectedLiterals[i].getValue != generatedLiterals[i].getValue) { + printLiteralLog(expectedLiterals[i], generatedLiterals[i]); + return false; + } + } + } + return true; +} \ No newline at end of file diff --git a/ts2panda/tests/types/union.test.ts b/ts2panda/tests/types/union.test.ts new file mode 100644 index 00000000000..a07a9e0ef3a --- /dev/null +++ b/ts2panda/tests/types/union.test.ts @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import 'mocha'; +import { + compileTsWithType, + createLiteralBufferArray, + compareLiteralBuffer, + createVRegTypePair, + compareVReg2Type +} from "./typeUtils"; +import { PrimitiveType } from '../../src/base/typeSystem'; + +let shift = PrimitiveType._LENGTH; + +describe("union tests in union.test.ts", function () { + it("test union with primitives", function () { + let fileNames = 'tests/types/union/union_primitives.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#num", shift + 1], + ["#4#str", shift + 2], + ["#5#und", shift + 3], + ["#6#full", shift + 4], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 4], [2, 2], [2, 1], [2, 2] + ], + [ + [2, 4], [2, 2], [2, 4], [2, 5] + ], + [ + [2, 4], [2, 2], [2, 7], [2, 6] + ], + [ + [2, 4], [2, 6], [2, 1], [2, 2], + [2, 4], [2, 5], [2, 7], [2, 6] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test union with user defined type", function () { + let fileNames = 'tests/types/union/union_userDefinedType.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 2], + ["#4#c", shift + 1], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 4], [2, 2], [2, 53], [2, 54] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, 52] + ], + [ + [2, 5], [2, 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test union with multi same primitives", function () { + let fileNames = 'tests/types/union/union_multi_same_primi.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#u1", shift + 1], + ["#4#u2", shift + 1], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 1], [2, 0] + ], + [ + [2, 4], [2, 2], [2, 1], [2, 2] + ], + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); + + it("test union with multi same user defined type", function () { + let fileNames = 'tests/types/union/union_multi_userDefinedType.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#A", shift + 2], + ["#4#c", shift + 1], + ["#5#d", shift + 1], + ] + let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); + expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; + + // check liberalBuffer + let expectedBuffValues = [ + [ + [2, 0], [2, 4], [2, 0] + ], + [ + [2, 4], [2, 2], [2, 53], [2, 54] + ], + [ + [2, 1], [2, 0], [2, 0], [2, 0], + [2, 0], [2, 0], [2, 0], [2, 0] + ], + [ + [2, 2], [2, 52] + ], + [ + [2, 5], [2, 1] + ] + ] + let buff = createLiteralBufferArray(expectedBuffValues); + expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; + }); +}); \ No newline at end of file diff --git a/ts2panda/tests/types/union/union_multi_same_primi.ts b/ts2panda/tests/types/union/union_multi_same_primi.ts new file mode 100644 index 00000000000..bb86cf597da --- /dev/null +++ b/ts2panda/tests/types/union/union_multi_same_primi.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let u1: number | boolean = 3; + let u2: number | boolean = false; +} \ No newline at end of file diff --git a/ts2panda/tests/types/union/union_multi_userDefinedType.ts b/ts2panda/tests/types/union/union_multi_userDefinedType.ts new file mode 100644 index 00000000000..6aa044d0e1e --- /dev/null +++ b/ts2panda/tests/types/union/union_multi_userDefinedType.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A{}; + let c: A | number[]; + let d: A | number[]; +} \ No newline at end of file diff --git a/ts2panda/tests/types/union/union_primitives.ts b/ts2panda/tests/types/union/union_primitives.ts new file mode 100644 index 00000000000..2f4ae407d7f --- /dev/null +++ b/ts2panda/tests/types/union/union_primitives.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + let num: number | boolean = 3; + let str: string | symbol = "myStr"; + let und: undefined | null = null; + let full: number | boolean | string | symbol | undefined | null; +} \ No newline at end of file diff --git a/ts2panda/tests/types/union/union_userDefinedType.ts b/ts2panda/tests/types/union/union_userDefinedType.ts new file mode 100644 index 00000000000..ea8ba462b03 --- /dev/null +++ b/ts2panda/tests/types/union/union_userDefinedType.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + class A{}; + let c: A | number[]; +} \ No newline at end of file diff --git a/ts2panda/tests/utils/base.ts b/ts2panda/tests/utils/base.ts index 78718fbd041..94b1a99f008 100644 --- a/ts2panda/tests/utils/base.ts +++ b/ts2panda/tests/utils/base.ts @@ -30,6 +30,7 @@ import { } from "../../src/scope"; import { setGlobalStrict } from "../../src/strictMode"; import { creatAstFromSnippet } from "./asthelper"; +import { LiteralBuffer } from "../../src/base/literal"; const compileOptions = { outDir: "../tmp/build", @@ -138,7 +139,7 @@ export function checkInstructions(actual: IRNode[], expected: IRNode[], checkFn? return true; } -export function compileAllSnippet(snippet: string, passes?: Pass[]): PandaGen[] { +export function compileAllSnippet(snippet: string, passes?: Pass[], literalBufferArray?: Array): PandaGen[] { let sourceFile = creatAstFromSnippet(snippet); jshelpers.bindSourceFile(sourceFile, {}); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(sourceFile, compileOptions)); @@ -147,9 +148,8 @@ export function compileAllSnippet(snippet: string, passes?: Pass[]): PandaGen[] if (!passes) { passes = []; } - compilerDriver.setCustomPasses(passes); - compilerDriver.compileUnitTest(sourceFile); + compilerDriver.compileUnitTest(sourceFile, literalBufferArray); return compilerDriver.getCompilationUnits(); } @@ -176,8 +176,8 @@ export function getCompileOptions(): ts.CompilerOptions { export class SnippetCompiler { pandaGens: PandaGen[] = []; - compile(snippet: string, passes?: Pass[]) { - this.pandaGens = compileAllSnippet(snippet, passes); + compile(snippet: string, passes?: Pass[], literalBufferArray?: Array) { + this.pandaGens = compileAllSnippet(snippet, passes, literalBufferArray); return this.pandaGens; } -- Gitee