1/* 2 * Copyright (c) 2024 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 fs from 'fs'; 17import path from 'path'; 18import {describe, expect, test} from '@jest/globals'; 19import { 20 getAllLegalImports, 21 collectAllLegalImports, 22 getAllFileNameList, 23 collectAllFileName, 24 getClassNameSet, 25 getAllClassDeclaration, 26 firstCharacterToUppercase, 27} from '../common/commonUtils'; 28import { createSourceFile, ScriptTarget } from 'typescript'; 29 30function areSetsEqual(setA, setB) { 31 if (setA.size !== setB.size) { 32 return false; // 大小不同,返回 false 33 } 34 for (let item of setA) { 35 if (!setB.has(item)) { 36 return false; // setB 中没有 setA 的某个元素,返回 false 37 } 38 } 39 return true; // 两个 set 相等 40} 41 42describe('commonUtils.ts file test', (): void => { 43 test('Test the getAllLegalImports function', (): void => { 44 collectAllLegalImports('hello world') 45 const result = getAllLegalImports(); 46 const expectedResult = new Set<string>(); 47 expectedResult.add('hello world'); 48 expect(true).toBe(areSetsEqual(result, expectedResult)); 49 }); 50 51 test('Test the getAllFileNameList function', (): void => { 52 collectAllFileName(path.join(__dirname, 'api/@ohos.ability.ability.d.ts')) 53 collectAllFileName(path.join(__dirname, 'api/@ohos.ability.errorCode.d.ts')) 54 collectAllFileName(path.join(__dirname, 'api/lifecycle.d.ts')) 55 const result = getAllFileNameList(); 56 const expectedResult = new Set<string>(); 57 expectedResult.add('ohos_ability_ability'); 58 expectedResult.add('ohos_ability_errorCode'); 59 expectedResult.add('lifecycle'); 60 expect(true).toBe(areSetsEqual(result, expectedResult)); 61 }); 62 63 test('Test the getClassNameSet function', (): void => { 64 const filePath = path.join(__dirname, './api/@ohos.accessibility.GesturePath.d.ts'); 65 const code = fs.readFileSync(filePath); 66 const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest); 67 getAllClassDeclaration(sourceFile) 68 const result = getClassNameSet(); 69 const expectedResult = new Set<string>(); 70 expectedResult.add('GesturePath'); 71 expect(true).toBe(areSetsEqual(result, expectedResult)); 72 }); 73 74 test('Test the getIsModuleDeclaration function', (): void => { 75 const filePath = path.join(__dirname, './api/@ohos.accessibility.GesturePath.d.ts'); 76 const code = fs.readFileSync(filePath); 77 const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest); 78 getAllClassDeclaration(sourceFile) 79 const result = getClassNameSet(); 80 const expectedResult = new Set<string>(); 81 expectedResult.add('GesturePath'); 82 expect(true).toBe(areSetsEqual(result, expectedResult)); 83 }); 84 85 test('Test the getIsModuleDeclaration function', (): void => { 86 const filePath = path.join(__dirname, './api/@ohos.account.appAccount.d.ts'); 87 const code = fs.readFileSync(filePath); 88 const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest); 89 getAllClassDeclaration(sourceFile) 90 const result = getClassNameSet(); 91 const expectedResult = new Set<string>(); 92 expectedResult.add('GesturePath'); 93 expectedResult.add('Authenticator'); 94 expect(true).toBe(areSetsEqual(result, expectedResult)); 95 }); 96 97 test('Test the firstCharacterToUppercase function', (): void => { 98 const result = firstCharacterToUppercase('helloWorld'); 99 const expectedResult = 'HelloWorld'; 100 expect(result).toBe(expectedResult); 101 }); 102}); 103