• 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 { generatePropertyDeclaration } from '../generate/generatePropertyDeclaration';
21
22const filePath = path.join(__dirname, './api/@ohos.accessibility.d.ts')
23const code = fs.readFileSync(filePath);
24const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest);
25
26describe('generatePropertyDeclaration.ts file test', () => {
27  test('Test propertyDeclaration.kind is SyntaxKind.TypeReference', () => {
28    const rootName = 'EventInfo';
29    const propertyDeclaration = {
30      modifiers: [],
31      propertyName: 'type',
32      propertyTypeName: 'EventType',
33      kind: 173,
34      kinds: -1,
35      isInitializer: false,
36      initializer: '',
37    };
38    const result = generatePropertyDeclaration(rootName, propertyDeclaration, sourceFile, []);
39    expect(result).toBe('this.type = EventType;');
40  });
41
42  test('Test propertyDeclaration.kind is SyntaxKind.StringKeyword', () => {
43    const rootName = 'EventInfo';
44    const propertyDeclaration = {
45      modifiers: [],
46      propertyName: 'bundleName',
47      propertyTypeName: 'string',
48      kind: 147,
49      kinds: -1,
50      isInitializer: false,
51      initializer: '',
52    };
53    const result = generatePropertyDeclaration(rootName, propertyDeclaration, sourceFile, []);
54    expect(result).toBe('this.bundleName = \'\'');
55  });
56
57  test('Test propertyDeclaration.kind is SyntaxKind.NumberKeyword', () => {
58    const rootName = 'EventInfo';
59    const propertyDeclaration = {
60      modifiers: [],
61      propertyName: 'pageId',
62      propertyTypeName: 'number',
63      kind: 144,
64      kinds: -1,
65      isInitializer: false,
66      initializer: '',
67    };
68    const result = generatePropertyDeclaration(rootName, propertyDeclaration, sourceFile, []);
69    expect(result).toBe('this.pageId = 0;');
70  });
71});