• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { createSourceFile, ScriptTarget } from 'typescript';
20import { generateInterfaceDeclaration } from '../generate/generateInterfaceDeclaration';
21
22describe('generateInterfaceDeclaration.ts file test', () => {
23  test('Test the generateInterfaceDeclaration function', () => {
24    const filePath = path.join(__dirname, './api/@ohos.abilityAccessCtrl.d.ts')
25    const code = fs.readFileSync(filePath);
26    const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest);
27    const interfaceEntity = {
28      interfaceName: 'AtManager',
29      typeParameters: [],
30      heritageClauses: [],
31      interfaceConstructors: [],
32      interfaceMethodSignature: new Map(),
33      interfacePropertySignatures: [],
34      callSignatures: [],
35      indexSignature: [],
36      exportModifiers: [],
37    };
38    const isSourceFile = false;
39    const mockApi = 'import { AsyncCallback, Callback } from \'./ohos_base\''
40      + 'import { Permissions } from \'./permissions\''
41      + 'import _Context from \'./application/Context\''
42      + 'import _PermissionRequestResult from \'./security/PermissionRequestResult\''
43      + 'export const PermissionRequestResult = new _PermissionRequestResult();'
44      + 'export const Context = _Context;';
45    const currentSourceInterfaceArray = [
46      {
47        interfaceName: 'AtManager',
48        typeParameters: [],
49        heritageClauses: [],
50        interfaceConstructors: [],
51        interfaceMethodSignature: new Map(),
52        interfacePropertySignatures: [],
53        callSignatures: [],
54        indexSignature: [],
55        exportModifiers: [],
56      },
57      {
58        interfaceName: 'PermissionStateChangeInfo',
59        typeParameters: [],
60        heritageClauses: [],
61        interfaceConstructors: [],
62        interfaceMethodSignature: new Map(),
63        interfacePropertySignatures: [],
64        callSignatures: [],
65        indexSignature: [],
66        exportModifiers: [],
67      },
68    ];
69    const importDeclarations = [
70      {
71        importPath: './@ohos.base',
72        importElements: '{ AsyncCallback, Callback }',
73      },
74      {
75        importPath: './permissions',
76        importElements: '{ Permissions }',
77      },
78      {
79        importPath: './application/Context',
80        importElements: '_Context',
81      },
82      {
83        importPath: './security/PermissionRequestResult',
84        importElements: '_PermissionRequestResult',
85      },
86    ];
87    const extraImport = [];
88    const result = generateInterfaceDeclaration(
89      interfaceEntity,
90      sourceFile,
91      isSourceFile,
92      mockApi,
93      currentSourceInterfaceArray,
94      importDeclarations,
95      extraImport
96    );
97    expect(result).toBe('const AtManager = { \n}\n');
98  });
99});