• 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: [
32          123,
33        ],
34        functionName: {
35          name: 'debug',
36          expressionKind: -1,
37          kind: 78,
38        },
39        returnType: {
40          returnKindName: 'void',
41          returnKind: 113,
42        },
43        args: [
44          {
45            paramName: 'message',
46            paramTypeString: 'string',
47            paramTypeKind: 147,
48          },
49          {
50            paramName: 'arguments',
51            paramTypeString: 'any[]',
52            paramTypeKind: 178,
53          },
54        ],
55      },
56    }
57    const mockApi = 'import { TouchObject, KeyEvent, MouseEvent } from \'../component/ets/common\'';
58    const data = 'Console.debug = function(...args) {console.warn(\'The console.debug interface in the Previewer'
59      + ' is a mocked implementation and may behave differently than on a real device.\');\n};';
60    const result = generateStaticFunction(staticMethod, false, sourceFile, mockApi);
61    expect(result).toBe(data);
62  });
63});