From 512dc55473fe7d3cdfeb9396028df281357aa943 Mon Sep 17 00:00:00 2001 From: zhuoli Date: Mon, 24 Jan 2022 23:04:17 +0800 Subject: [PATCH] Add a few more ut_tests for type Signed-off-by: zhuoli Change-Id: Ibcc32d2c8a5ce9a281ef5cc94444f85be65f2cfa --- ts2panda/tests/types/function.test.ts | 23 ++++--- .../function/function_same_para_and_return.ts | 19 ++++++ ts2panda/tests/types/object/object_class.ts | 2 +- ts2panda/tests/types/primitives.test.ts | 68 +++++++++++++++++-- .../primitives_no_type_annotation.ts | 24 +++++++ .../primitives_only_type_annotation.ts | 24 +++++++ 6 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 ts2panda/tests/types/function/function_same_para_and_return.ts create mode 100644 ts2panda/tests/types/primitives/primitives_no_type_annotation.ts create mode 100644 ts2panda/tests/types/primitives/primitives_only_type_annotation.ts diff --git a/ts2panda/tests/types/function.test.ts b/ts2panda/tests/types/function.test.ts index 4de684483b..c0f1ccd58a 100644 --- a/ts2panda/tests/types/function.test.ts +++ b/ts2panda/tests/types/function.test.ts @@ -89,8 +89,8 @@ describe("function tests in function.test.ts", function () { 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'; + it("test function with same type of paras and return", function () { + let fileNames = 'tests/types/function/function_same_para_and_return.ts'; let result = compileTsWithType(fileNames); let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); let locals = functionPg!.getLocals(); @@ -98,7 +98,10 @@ describe("function tests in function.test.ts", function () { let extectedVRegTypePair = [ ["#0#num", 1], ["#1#str", 4], - ["#0#emptyFunc", shift + 2], + ["#0#num", 1], + ["#1#str", 4], + ["#0#foo", shift + 2], + ["#1#bar", shift + 3], ] let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; @@ -106,15 +109,19 @@ describe("function tests in function.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 2], [2, 0] + [2, 0], [2, 3], [2, 0] ], [ - [2, 3], [2, 0], [2, 0], [5, 'local'], - [2, 0], [2, 0] + [2, 3],[2, 0],[2, 0],[5, 'twoFunctions'], + [2, 0],[2, 0] ], [ - [2, 3], [2, 0], [2, 0], [5, 'emptyFunc'], - [2, 2], [2, 1], [2, 4], [2, 4] + [2, 3],[2, 0],[2, 0],[5, 'foo'], + [2, 2],[2, 1],[2, 4],[2, 3] + ], + [ + [2, 3],[2, 0],[2, 0],[5, 'bar'], + [2, 2],[2, 1],[2, 4],[2, 3] ] ] let buff = createLiteralBufferArray(expectedBuffValues); diff --git a/ts2panda/tests/types/function/function_same_para_and_return.ts b/ts2panda/tests/types/function/function_same_para_and_return.ts new file mode 100644 index 0000000000..193eb15466 --- /dev/null +++ b/ts2panda/tests/types/function/function_same_para_and_return.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 twoFunctions() { + function foo(num: number, str: string): void { } + function bar(num: number, str: string): void { } +} \ No newline at end of file diff --git a/ts2panda/tests/types/object/object_class.ts b/ts2panda/tests/types/object/object_class.ts index c3a643151b..5810fc907e 100644 --- a/ts2panda/tests/types/object/object_class.ts +++ b/ts2panda/tests/types/object/object_class.ts @@ -14,7 +14,7 @@ */ { - class A{}; + class A { }; let a: { a: A; b: string | number; diff --git a/ts2panda/tests/types/primitives.test.ts b/ts2panda/tests/types/primitives.test.ts index 336db5271f..28fb39e58c 100644 --- a/ts2panda/tests/types/primitives.test.ts +++ b/ts2panda/tests/types/primitives.test.ts @@ -26,8 +26,8 @@ import { PrimitiveType } from '../../src/base/typeSystem'; let shift = PrimitiveType._LENGTH; -describe("primitives tests in primitives.test.ts", function () { - it("test primitives in block", function () { +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"); @@ -57,7 +57,7 @@ describe("primitives tests in primitives.test.ts", function () { expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; }); - it("test number in function", function () { + 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"); @@ -97,7 +97,7 @@ describe("primitives tests in primitives.test.ts", function () { expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; }); - it("test primitives in for", function () { + 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"); @@ -128,7 +128,7 @@ describe("primitives tests in primitives.test.ts", function () { expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; }); - it("test primitives in if", function () { + 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"); @@ -158,7 +158,7 @@ describe("primitives tests in primitives.test.ts", function () { expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; }); - it("test primitives in class", function () { + 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"); @@ -194,4 +194,60 @@ describe("primitives tests in primitives.test.ts", function () { let buff = createLiteralBufferArray(expectedBuffValues); expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; }); + + it("test primitives with only type annotations", function() { + let fileNames = 'tests/types/primitives/primitives_only_type_annotation.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 without type annotations", function() { + let fileNames = 'tests/types/primitives/primitives_no_type_annotation.ts'; + let result = compileTsWithType(fileNames); + let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let locals = functionPg!.getLocals(); + // check vreg + let extectedVRegTypePair = [ + ["#3#num", 0], + ["#4#bool", 0], + ["#5#str", 0], + ["#6#sym", 0], + ["#7#nu", 0], + ["#8#und", 0], + ["#9#vd", 0], + ] + 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; + }); }); diff --git a/ts2panda/tests/types/primitives/primitives_no_type_annotation.ts b/ts2panda/tests/types/primitives/primitives_no_type_annotation.ts new file mode 100644 index 0000000000..8cbd78ea65 --- /dev/null +++ b/ts2panda/tests/types/primitives/primitives_no_type_annotation.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 = 3; + let bool = false; + let str = "myStr"; + let sym = Symbol("mySym"); + let nu = null; + let und = undefined; + let vd = undefined; +} diff --git a/ts2panda/tests/types/primitives/primitives_only_type_annotation.ts b/ts2panda/tests/types/primitives/primitives_only_type_annotation.ts new file mode 100644 index 0000000000..9041051faf --- /dev/null +++ b/ts2panda/tests/types/primitives/primitives_only_type_annotation.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; + let bool: boolean; + let str: string; + let sym: symbol; + let nu: null; + let und: undefined; + let vd: void; +} \ No newline at end of file -- Gitee