• 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 { generatePropertySignatureDeclaration } from '../generate/generatePropertySignatureDeclaration';
21
22const filePath = path.join(__dirname, './api/@ohos.abilityAccessCtrl.d.ts')
23const code = fs.readFileSync(filePath);
24const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest);
25
26describe('generatePropertySignatureDeclaration.ts file test', () => {
27  test('Test propertySignature.kind is SyntaxKind.TypeReference', () => {
28    const rootName = 'PermissionStateChangeInfo';
29    const propertySignature = {
30      modifiers: [],
31      propertyName: 'change',
32      propertyTypeName: 'PermissionStateChangeType',
33      kind: 173,
34    };
35    const mockApi = 'import { AsyncCallback, Callback } from \'./ohos_base\''
36      + 'import { Permissions } from \'./permissions\''
37      + 'import _Context from \'./application/Context\''
38      + 'import _PermissionRequestResult from \'./security/PermissionRequestResult\''
39      + 'export const PermissionRequestResult = new _PermissionRequestResult();'
40      + 'export const Context = _Context;';
41    const result = generatePropertySignatureDeclaration(rootName, propertySignature, sourceFile, mockApi);
42    expect(result).toBe('change: PermissionStateChangeType,');
43  });
44  test('Test propertySignature.kind is SyntaxKind.NumberKeyword', () => {
45    const rootName = 'PermissionStateChangeInfo';
46    const propertySignature = {
47      modifiers: [],
48      propertyName: 'tokenID',
49      propertyTypeName: 'number',
50      kind: 144,
51    };
52    const mockApi = 'import { AsyncCallback, Callback } from \'./ohos_base\''
53      + 'import { Permissions } from \'./permissions\''
54      + 'import _Context from \'./application/Context\''
55      + 'import _PermissionRequestResult from \'./security/PermissionRequestResult\''
56      + 'export const PermissionRequestResult = new _PermissionRequestResult();'
57      + 'export const Context = _Context;';
58    const result = generatePropertySignatureDeclaration(rootName, propertySignature, sourceFile, mockApi);
59    expect(result).toBe('tokenID: 0,');
60  });
61});