13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciimport {before} from 'mocha';
173af6ab5fSopenharmony_ciimport {assert} from 'chai';
183af6ab5fSopenharmony_ciimport {createSourceFile, ScriptTarget, SourceFile} from 'typescript';
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_ciimport {
213af6ab5fSopenharmony_ci  collectExistNames,
223af6ab5fSopenharmony_ci  wildcardTransformer
233af6ab5fSopenharmony_ci} from '../../../src/utils/TransformUtil';
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_cidescribe('test for TransformUtil', function () {
263af6ab5fSopenharmony_ci  let sourceFile: SourceFile;
273af6ab5fSopenharmony_ci
283af6ab5fSopenharmony_ci  before('init ast for source file', function () {
293af6ab5fSopenharmony_ci    const fileContent = `
303af6ab5fSopenharmony_ci    class Demo{
313af6ab5fSopenharmony_ci      constructor(public  title: string, public  content: string, public  mark: number) {
323af6ab5fSopenharmony_ci          this.title = title
333af6ab5fSopenharmony_ci          this.content = content
343af6ab5fSopenharmony_ci          this.mark = mark
353af6ab5fSopenharmony_ci      }
363af6ab5fSopenharmony_ci    }
373af6ab5fSopenharmony_ci    `;
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ci    sourceFile = createSourceFile('demo.js', fileContent, ScriptTarget.ES2015, true);
403af6ab5fSopenharmony_ci  });
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_ci  describe('test for function collectExistNames', function () {
433af6ab5fSopenharmony_ci    it('test collectExistNames', function () {
443af6ab5fSopenharmony_ci      const nameSets = collectExistNames(sourceFile);
453af6ab5fSopenharmony_ci      const targetNames = ['Demo', 'title', 'content', 'mark'];
463af6ab5fSopenharmony_ci
473af6ab5fSopenharmony_ci      assert.strictEqual(nameSets.size, targetNames.length);
483af6ab5fSopenharmony_ci      targetNames.forEach((value) => {
493af6ab5fSopenharmony_ci        assert.isTrue(nameSets.has(value));
503af6ab5fSopenharmony_ci      });
513af6ab5fSopenharmony_ci    });
523af6ab5fSopenharmony_ci  });
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_ci  describe('test for function wildcardTransformer', function () {
553af6ab5fSopenharmony_ci    it('test wildcardTransformer', function () {
563af6ab5fSopenharmony_ci      // special characters: '\', '^', '$', '.', '+', '|', '[', ']', '{', '}', '(', ')'
573af6ab5fSopenharmony_ci      const reserved1 = 'a\\+b*';
583af6ab5fSopenharmony_ci      const reserved2 = '{*}[*](*)';
593af6ab5fSopenharmony_ci      const reserved3 = '*^';
603af6ab5fSopenharmony_ci      const reserved4 = '?$\\..';
613af6ab5fSopenharmony_ci      const reserved5 = '?|123';
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci      const result1 = wildcardTransformer(reserved1);
643af6ab5fSopenharmony_ci      const result2 = wildcardTransformer(reserved2);
653af6ab5fSopenharmony_ci      const result3 = wildcardTransformer(reserved3);
663af6ab5fSopenharmony_ci      const result4 = wildcardTransformer(reserved4);
673af6ab5fSopenharmony_ci      const result5 = wildcardTransformer(reserved5);
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ci      assert.strictEqual(result1, String.raw`a\\\+b.*`);
703af6ab5fSopenharmony_ci      assert.strictEqual(result2, String.raw`\{.*\}\[.*\]\(.*\)`);
713af6ab5fSopenharmony_ci      assert.strictEqual(result3, String.raw`.*\^`);
723af6ab5fSopenharmony_ci      assert.strictEqual(result4, String.raw`.\$\\\.\.`);
733af6ab5fSopenharmony_ci      assert.strictEqual(result5, String.raw`.\|123`);
743af6ab5fSopenharmony_ci    });
753af6ab5fSopenharmony_ci  });
763af6ab5fSopenharmony_ci});