• 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 { generateStaticFunction } from '../generate/generateStaticFunction';
21
22describe('generateStaticFunction.ts file test', () => {
23  test('Test the generateStaticFunction function', () => {
24    const filePath = path.join(__dirname, './api/global.d.ts');
25    const code = fs.readFileSync(filePath);
26    const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest);
27
28    const staticMethod = {
29      className: 'console',
30      methodEntity: {
31        modifiers: [123],
32        functionName: {
33          name: 'debug',
34          expressionKind: -1,
35          kind: 78
36        },
37        returnType: {
38          returnKindName: 'void',
39          returnKind: 113
40        },
41        args: [
42          {
43            paramName: 'message',
44            paramTypeString: 'string',
45            paramTypeKind: 147
46          },
47          {
48            paramName: 'arguments',
49            paramTypeString: 'any[]',
50            paramTypeKind: 178
51          }
52        ]
53      }
54    };
55    const mockApi = 'import { TouchObject, KeyEvent, MouseEvent } from \'../component/ets/common\'';
56    const data = 'Console.debug = function(...args) {console.warn(\'The console.debug interface in the Previewer'
57      + ' is a mocked implementation and may behave differently than on a real device.\');\n};';
58    const result = generateStaticFunction(staticMethod, false, sourceFile, mockApi);
59    expect(result).toBe(data);
60  });
61});
62