1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { expect } from 'chai'; 17import 'mocha'; 18import { 19 compileTsWithType, 20 createLiteralBufferArray, 21 compareLiteralBuffer, 22 createVRegTypePair, 23 compareVReg2Type 24} from "./typeUtils"; 25import { PrimitiveType } from '../../src/base/typeSystem'; 26 27let shift = PrimitiveType._LENGTH; 28 29describe("object tests in object.test.ts", function() { 30 it("test object with primitives", function() { 31 let fileNames = 'tests/types/object/object_primi.ts'; 32 let result = compileTsWithType(fileNames); 33 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 34 let locals = functionPg!.getLocals(); 35 // check vreg 36 let extectedVRegTypePair = [ 37 ["#3#a", shift + 1], 38 ] 39 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 40 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 41 42 // check liberalBuffer 43 let expectedBuffValues = [ 44 [ 45 [2, 0], [2, 1], [2, 0] 46 ], 47 [ 48 [2, 6], [2, 2], [5, 'a'], [2, 1], 49 [5, 'b'], [2, 4] 50 ], 51 ] 52 let buff = createLiteralBufferArray(expectedBuffValues); 53 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 54 }); 55 56 it("test object with user defined type", function() { 57 let fileNames = 'tests/types/object/object_class.ts'; 58 let result = compileTsWithType(fileNames); 59 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 60 let locals = functionPg!.getLocals(); 61 // check vreg 62 let extectedVRegTypePair = [ 63 ["#3#A", shift + 2], 64 ["#4#a", shift + 1], 65 ] 66 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 67 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 68 69 // check liberalBuffer 70 let expectedBuffValues = [ 71 [ 72 [2, 0], [2, 4], [2, 0] 73 ], 74 [ 75 [2, 6],[2, 2],[5, 'a'],[2, 53], 76 [5, 'b'],[2, 54] 77 ], 78 [ 79 [2, 1],[2, 0],[2, 0],[2, 0], 80 [2, 0],[2, 0],[2, 0],[2, 0] 81 ], 82 [ 83 [2, 2], [2, 52] 84 ], 85 [ 86 [2, 4],[2, 2],[2, 4],[2, 1] 87 ] 88 ] 89 let buff = createLiteralBufferArray(expectedBuffValues); 90 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 91 }); 92});