• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 ts from 'typescript';
17import path from 'path';
18import chai from 'chai';
19import {
20  describe,
21  it
22} from 'mocha';
23const expect = chai.expect;
24
25import {
26  validateUISyntax,
27  resetComponentCollection,
28  sourceReplace,
29  componentCollection
30} from '../lib/validate_ui_syntax';
31import { processUISyntax } from '../lib/process_ui_syntax';
32import {
33  componentInfo,
34  readFile
35} from '../lib/utils';
36import { BUILD_ON } from '../lib/pre_define';
37
38function expectActual(name: string, filePath: string) {
39  const content: any = require(filePath);
40  const source: string = content.source;
41  process.env.compiler = BUILD_ON;
42  const afterProcess = sourceReplace(source);
43  validateUISyntax(source, afterProcess, `${name}.ts`);
44  const result: ts.TranspileOutput = ts.transpileModule(afterProcess, {
45    compilerOptions: {
46      "target": ts.ScriptTarget.ES2021
47    },
48    fileName: `${name}.ts`,
49    transformers: { before: [processUISyntax(null, true)] }
50  });
51  componentInfo.id = 0;
52  componentCollection.customComponents.clear();
53  resetComponentCollection();
54  expect(result.outputText).eql(content.expectResult);
55}
56
57describe('compiler', () => {
58  const utPath: string = path.resolve(__dirname, './ut');
59  const utFiles: string[] = [];
60  readFile(utPath, utFiles);
61  utFiles.forEach((item) => {
62    const fileName: string = path.basename(item, '.ts');
63    it(fileName, () => {
64      expectActual(fileName, item);
65    })
66  })
67})
68