1/*
2 * Copyright (c) 2023 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 {before} from 'mocha';
17import {assert} from 'chai';
18import {createSourceFile, ScriptTarget, SourceFile} from 'typescript';
19
20import {
21  collectExistNames,
22  wildcardTransformer
23} from '../../../src/utils/TransformUtil';
24
25describe('test for TransformUtil', function () {
26  let sourceFile: SourceFile;
27
28  before('init ast for source file', function () {
29    const fileContent = `
30    class Demo{
31      constructor(public  title: string, public  content: string, public  mark: number) {
32          this.title = title
33          this.content = content
34          this.mark = mark
35      }
36    }
37    `;
38
39    sourceFile = createSourceFile('demo.js', fileContent, ScriptTarget.ES2015, true);
40  });
41
42  describe('test for function collectExistNames', function () {
43    it('test collectExistNames', function () {
44      const nameSets = collectExistNames(sourceFile);
45      const targetNames = ['Demo', 'title', 'content', 'mark'];
46
47      assert.strictEqual(nameSets.size, targetNames.length);
48      targetNames.forEach((value) => {
49        assert.isTrue(nameSets.has(value));
50      });
51    });
52  });
53
54  describe('test for function wildcardTransformer', function () {
55    it('test wildcardTransformer', function () {
56      // special characters: '\', '^', '$', '.', '+', '|', '[', ']', '{', '}', '(', ')'
57      const reserved1 = 'a\\+b*';
58      const reserved2 = '{*}[*](*)';
59      const reserved3 = '*^';
60      const reserved4 = '?$\\..';
61      const reserved5 = '?|123';
62
63      const result1 = wildcardTransformer(reserved1);
64      const result2 = wildcardTransformer(reserved2);
65      const result3 = wildcardTransformer(reserved3);
66      const result4 = wildcardTransformer(reserved4);
67      const result5 = wildcardTransformer(reserved5);
68
69      assert.strictEqual(result1, String.raw`a\\\+b.*`);
70      assert.strictEqual(result2, String.raw`\{.*\}\[.*\]\(.*\)`);
71      assert.strictEqual(result3, String.raw`.*\^`);
72      assert.strictEqual(result4, String.raw`.\$\\\.\.`);
73      assert.strictEqual(result5, String.raw`.\|123`);
74    });
75  });
76});