1/* 2 * Copyright (c) 2023 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 {assert} from 'chai'; 17import {before, describe} from 'mocha'; 18import {createSourceFile, ScriptTarget, SourceFile} from 'typescript'; 19 20import {TypeUtils} from '../../../src/utils/TypeUtils'; 21import { FileUtils } from '../../../src/utils/FileUtils'; 22import { Extension, PathAndExtension } from '../../../src/common/type'; 23 24describe('test for TypeUtils', function () { 25 let sourceFile: SourceFile; 26 let jsSourceFile: SourceFile; 27 28 before('init sourceFile', function () { 29 const fileContent = ` 30 //This is a comment 31 class Demo{ 32 constructor(public title: string, public content: string, public mark: number) { 33 this.title = title 34 this.content = content 35 this.mark = mark 36 } 37 } 38 `; 39 const jsFileContent = ` 40 //This is a comment 41 class Demo{ 42 constructor(public title: string, public content: string, public mark: number) { 43 this.title = title 44 this.content = content 45 this.mark = mark 46 } 47 } 48 `; 49 50 sourceFile = createSourceFile('demo.ts', fileContent, ScriptTarget.ES2015, true); 51 jsSourceFile = createSourceFile('demo.js', jsFileContent, ScriptTarget.ES2015, true); 52 }); 53 54 describe('test for method createObfSourceFile', function () { 55 it('js test', function () { 56 const ast = TypeUtils.createObfSourceFile(jsSourceFile.fileName, jsSourceFile.text); 57 const astExtension: PathAndExtension = FileUtils.getFileSuffix(ast.fileName); 58 const astFileSuffix = astExtension.ext; 59 60 assert.strictEqual(jsSourceFile.text, ast.text); 61 assert.strictEqual(Extension.TS, astFileSuffix); 62 }); 63 it('ts test', function () { 64 const ast = TypeUtils.createObfSourceFile(sourceFile.fileName, sourceFile.text); 65 const astExtension: PathAndExtension = FileUtils.getFileSuffix(ast.fileName); 66 const astFileSuffix = astExtension.ext; 67 68 assert.strictEqual(sourceFile.text, ast.text); 69 assert.strictEqual(Extension.TS, astFileSuffix); 70 }); 71 }); 72 73 describe('test for function createChecker', function () { 74 it('functional test', function () { 75 const checker = TypeUtils.createChecker(sourceFile); 76 assert.notEqual(checker, undefined); 77 78 const checker1 = TypeUtils.createChecker(jsSourceFile); 79 assert.notEqual(checker1, undefined); 80 }); 81 82 describe('test for function tsToJs', function () { 83 it('functional test', function () { 84 TypeUtils.tsToJs(sourceFile); 85 assert.strictEqual(sourceFile.fileName, 'demo.js'); 86 }); 87 }); 88 }); 89});