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 { describe, it } from 'mocha';
17import { TransformerManager } from '../../../src/transformers/TransformerManager';
18import { expect, assert } from 'chai';
19import { IOptions } from '../../../src/configs/IOptions';
20import ts, {
21    TransformerFactory,
22    Node,
23    TransformationContext,
24    factory,
25    SyntaxKind,
26    CallExpression,
27    Expression,
28    CompilerOptions,
29    EmitHelper,
30    EmitHint,
31    FunctionDeclaration,
32    Identifier,
33    Statement,
34    SourceFile,
35    VariableDeclaration,
36  } from 'typescript';
37import { NodeUtils } from '../../../src/utils/NodeUtils';
38import path from 'path';
39import secharmony from '../../../src/transformers/rename/VirtualConstructorTransformer';
40
41describe('Tester Cases for <virtualConstructorTransformerFactory>.', function () {
42  let options: IOptions;
43  let context: TransformationContext = {
44    requestEmitHelper: function (helper: EmitHelper): void {},
45    readEmitHelpers: function (): EmitHelper[] | undefined {
46      return undefined;
47    },
48    enableSubstitution: function (kind: SyntaxKind): void {},
49    isSubstitutionEnabled: function (node: Node): boolean {
50      throw new Error('Function not implemented.');
51    },
52    onSubstituteNode: function (hint: EmitHint, node: Node): Node {
53      throw new Error('Function not implemented.');
54    },
55    enableEmitNotification: function (kind: SyntaxKind): void {},
56    isEmitNotificationEnabled: function (node: Node): boolean {
57      throw new Error('Function not implemented.');
58    },
59    onEmitNode: function (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void {
60      throw new Error('Function not implemented.');
61    },
62    factory: ts.factory,
63    getCompilerOptions: function (): CompilerOptions {
64      throw new Error('Function not implemented.');
65    },
66    startLexicalEnvironment: function (): void {},
67    suspendLexicalEnvironment: function (): void {},
68    resumeLexicalEnvironment: function (): void {},
69    endLexicalEnvironment: function (): Statement[] | undefined {
70      return undefined;
71    },
72    hoistFunctionDeclaration: function (node: FunctionDeclaration): void {},
73    hoistVariableDeclaration: function (node: Identifier): void {}
74  };
75
76  describe('Tester Cases for <virtualConstructorTransformerFactory>.', function () {
77    it('should return virtualConstructorTransformer', function () {
78        const transfomerFactory = secharmony.transformerPlugin.createTransformerFactory(options);
79        expect(transfomerFactory).to.not.be.undefined;
80    });
81  });
82
83  describe('Tester Cases for <virtualConstructorTransformer>.', function () {
84    it('should return node if is not DETSFile', function () {
85      const sourceCode = `let a:number = 1;`;
86      let sourcefile = ts.createSourceFile("a.ts", sourceCode, ts.ScriptTarget.ES2022, false);
87      const transfomerFactory = secharmony.transformerPlugin.createTransformerFactory(options);
88      const node = transfomerFactory(context)(sourcefile);
89      expect(node).to.be.equal(sourcefile);
90    });
91
92    it('should return node if is DETSFile but not StructDeclaration', function () {
93      const sourceCode = `let a:number = 1;`;
94      let sourcefile = ts.createSourceFile("a.d.ets", sourceCode, ts.ScriptTarget.ES2022, false);
95      const transfomerFactory = secharmony.transformerPlugin.createTransformerFactory(options);
96      const node = transfomerFactory(context)(sourcefile);
97      expect(node).to.be.equal(sourcefile);
98    });
99
100    it('should return update node if is StructDeclaration', function () {
101      const sourceCode = `struct Demo{ };`;
102      let sourcefile = ts.createSourceFile("a.d.ets", sourceCode, ts.ScriptTarget.ES2022, false);
103      const transfomerFactory = secharmony.transformerPlugin.createTransformerFactory(options);
104      const node = transfomerFactory(context)(sourcefile);
105      expect(node).to.not.be.equal(sourcefile);
106    });
107  });
108});