README.md
1# ArkTS Syntax Constraint Rule Set Test Cases
2## Project Description
3This project aims to validate the constraint rules in [TypeScript to ArkTS Cookbook](https://gitee.com/openharmony/docs/blob/master/en/application-dev/quick-start/typescript-to-arkts-migration-guide.md) by script and output the test result report of the cases.
4
5The test cases are stored in the default directory called "testcase", and the results are stored in the "test_results" directory.
6
7## Project Requirements
81. Provide a test case set for the ArkTS syntax constraint rule set.
92. The framework should be able to test the cases in the test case set and return the test results, including details of successful and failed cases. It should also support optional case blocking.
103. The test case set should cover all the ArkTS syntax rule sets, with at least 10 cases for each rule, including positive and negative examples.
11
12## Getting Started
13
141. compile build typescript lib(pwd:third_party_typescript)
15 ```shell
16 npm install
17 npm run build
18 npm run release
19 npm pack
20 ```
21 must run this step everytime if you change your code.
22
232. Install project dependencies by running(pwd:third_party_typescript/tests/arkTSTest):
24 ```shell
25 rm -r ./node_modules/ ./package-lock.json # must run everytime
26 npm install # must run everytime
27 ```
283. Place the test cases in the "testcase" folder. It is recommended to use the constraint name as the test case directory, such as "arkts-no-any-unknown".
294. Run the "run.js" script to perform the code testing.
30
31 ```nodejs
32 node run.js -v1.0
33 node run.js -v1.1
34 ```
35Run with a specified test case folder:
36
37```shell
38node run.js -P:testcase/arkts-identifiers-as-prop-names // You can modify the test case to the specified directory in the current path
39```
40To get more details,you can use the command:
41```shell
42node run.js --detail
43```
44Ignore a case directory or specific case:
45
46```shell
47node run.js --ignore-list:testcase/arkts-identifiers-as-prop-names/case1.ets,testcase/arkts-no-any-unknown # Specify via --ignore-list or add to ignorecase.json
48```
49Example of `ignorecase.json`:
50```json
51{
52 "ignoreCase":["testcase/arkts-identifiers-as-prop-names/xxxx.ets", "testcase/arkts-no-any-unknown"]
53}
54```
55Generate test result files:
56
57```shell
58node run.js -e
59```
605. The generated test results are stored in the "test_results" directory, and basic test information is printed on the command line, such as:
61
62```plain
63Total number of test cases:2 Number of use cases passed:1 The number of use cases that failed:1 Total running time:371ms
64Failed test cases:
65testcase/arkts-no-any-unknown/test2.ets
66```
67## Usage Instructions
68
69### Print details of failed test cases
70Support printing specific details of failed cases by using the `--detail` or `-D` command, such as:
71
72```shell
73node run.js -D
74```
75Return details of failed cases:
76
77```plain
78==> Expect the error in Line null The null character. Expect exception rules:null Actual error line 20 The 7character. Actual exception rules:Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) Comparison Result:Fail!
79Total number of test cases:2 Number of use cases passed:1 The number of use cases that failed:1 Total running time:371ms
80Failed test cases:
81testcase/arkts-no-any-unknown/test2.ets
82```
83
84
85### Block specific test cases
861. Support blocking test case directories or specific cases by using the --ignore-list parameter, such as:
87```shell
88# Ignore a single case
89node run.js --ignore-list:testcase/arkts-no-any-unknown/test2.ets
90```
91
92Return the test results:
93
94```
95Total number of test cases:44 Number of use cases passed:43 The number of use cases that failed:1 Total running time:6342ms
96Ignored test cases:
97testcase/arkts-no-any-unknown/test2.ets
98```
992. Ignore multiple cases, separated by commas (support blocking directories):
100```shell
101node run.js --ignore-list:testcase/arkts-no-any-unknown/test2.ets,testcase/arkts-identifiers-as-prop-names
102```
1033. Support importing blocked cases through a configuration file:
104Rename the "ignorecase.json.example" file in the root directory to "ignorecase.json" and configure the "ignoreCase" field for blocked cases, such as:
105
106```json
107# ignorecase.json
108{
109"ignoreCase":["testcase/arkts-no-any-unknown/test2.ets","testcase/arkts-identifiers-as-prop-names"]
110}
111```
112Run the test case command:
113
114```shell
115node run.js -D
116```
117Running results:
118
119```plain
120// It can be observed that the configuration use case is properly masked
121Total number of test cases:0 Number of use cases passed:0 The number of use cases that failed:0 Total running time:342ms
122Ignored test cases:
123testcase/arkts-no-any-unknown/test2.ets
124testcase/arkts-identifiers-as-prop-names
125```
126#### Running Test Cases Script with Other Code
127Taking Python code as an example, refer to the file `run_test.py`:
128
129```python
130import subprocess
131
132p = subprocess.Popen("node run.js --detail", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
133out, err = p.communicate()
134print(out.decode('utf-8'), err.decode('utf-8'))
135
136return_code = p.returncode
137print(return_code)
138```
139If there are any failed cases, an exception with the status code 1 will be thrown. If all cases pass, the correct status code 0 will be returned.
140
README.zh-cn.md
1# ArkTS语法约束规则集测试用例
2
3## 项目说明
4
5本项目旨在对[从TypeScript到ArkTS的适配规则](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/typescript-to-arkts-migration-guide.md)中的约束规则通过脚本方式进行验证并输出用例的测试结果报告。
6
7其中测试用例默认存放在`testcase`目录,运行结果存放在`test_results`目录。
8
9## 项目要求
10
111. 提供对ArkTS语法约束规则集的测试用例集
122. 框架能对用例集中的用例进行测试,并返回测试结果,成功/失败用例详情。并支持可选屏蔽用例
133. 用例集覆盖所有arkTS语法规则集,每条规则要有正例和反例覆盖,平均用例数不少于10个
14
15## 快速开始
16
171. 编译构建typescript包(工作目录:third_party_typescript)
18 ```shell
19 npm install
20 npm run build
21 npm run release
22 npm pack
23 ```
24 每次修改代码必须执行以上步骤。
25
262. 安装项目依赖,执行(工作目录:third_party_typescript/tests/arkTSTest)
27 ```shell
28 rm -r ./node_modules/ ./package-lock.json # 每次必须执行
29 npm install # 每次必须执行
30 ```
313. 将测试用例放至 testcase 文件夹,建议使用约束名称作为测试用例目录,如`arkts-no-any-unknown`
324. 运行`run.js`,进行代码测试
33
34 ```nodejs
35 node run.js -v1.0
36 node run.js -v1.1
37 ```
38
39 指定测试用例文件夹运行:
40
41 ```shell
42 node run.js -P:testcase/arkts-identifiers-as-prop-names // 可修改为当前路径下指定目录的测试用例
43 ```
44
45 打印详情:
46
47 ```shell
48 node run.js --detail
49 ```
50
51 屏蔽用例目录或具体用例:
52
53 ```shell
54 node run.js --ignore-list:testcase/arkts-identifiers-as-prop-names/case1.ets,testcase/arkts-no-any-unknown # 通过--ignore-list指定或添加至 ignorecase.json
55 ```
56
57 `ignorecase.json`示例:
58
59 ```json
60 {
61 "ignoreCase":["testcase/arkts-identifiers-as-prop-names/xxxx.ets", "testcase/arkts-no-any-unknown"]
62 }
63 ```
64
65 生成测试结果文件:
66
67 ```shell
68 node run.js -e
69 ```
70
715. 生成的测试结果存放至 `test_results` 目录,命令行打印基本的测试信息,如:
72
73 ```plain
74 Total number of test cases:2 Number of use cases passed:1 The number of use cases that failed:1 Total running time:371ms
75 Failed test cases:
76 testcase/arkts-no-any-unknown/test2.ets
77 ```
78
79## 用法说明
80
81### 打印失败测试用例详情
82
83支持通过`--detail`或者`-D`指令打印具体失败用例细节,如运行指令:
84
85```shell
86node run.js -D
87```
88返回未通过用例详情:
89```plain
90==> Expect the error in Line null The null character. Expect exception rules:null Actual error line 20 The 7character. Actual exception rules:Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) Comparison Result:Fail!
91Total number of test cases:2 Number of use cases passed:1 The number of use cases that failed:1 Total running time:371ms
92Failed test cases:
93testcase/arkts-no-any-unknown/test2.ets
94```
95
96### 屏蔽指定测试用例
97
981. 支持使用 `--ignore-list` 参数传入屏蔽的用例目录或具体用例,如运行指令:
99
100```shell
101# 忽略单个用例
102node run.js --ignore-list:testcase/arkts-no-any-unknown/test2.ets
103```
104返回用例测试结果:
105```
106Total number of test cases:1 Number of use cases passed:1 The number of use cases that failed:0 Total running time:342ms
107Ignored test cases:
108testcase/arkts-no-any-unknown/test2.ets
109```
1102. 忽略多个用例,请使用`,`分割输入(支持屏蔽目录)
111
112```shell
113node run.js --ignore-list:testcase/arkts-no-any-unknown/test2.ets,testcase/arkts-identifiers-as-prop-names
114```
1153. 支持通过配置文件导入屏蔽用例:
116
117修改根目录下的`ignorecase.json.example`为`ignorecase.json`,配置`ignoreCase`字段为屏蔽用例字段,如:
118```json
119# ignorecase.json
120{
121"ignoreCase":["testcase/arkts-no-any-unknown/test2.ets","testcase/arkts-identifiers-as-prop-names"]
122}
123```
124
125执行测试用例指令:
126
127```shell
128node run.js -D
129```
130运行结果:
131```plain
132// It can be observed that the configuration use case is properly masked
133Total number of test cases:0 Number of use cases passed:0 The number of use cases that failed:0 Total running time:342ms
134Ignored test cases:
135testcase/arkts-no-any-unknown/test2.ets
136testcase/arkts-identifiers-as-prop-names
137```
138
139### 使用其他代码运行测试用例脚本
140
141以`Python`代码为例,参考文件`run_test.py`:
142```python
143import subprocess
144
145p = subprocess.Popen("node run.js --detail", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
146out, err = p.communicate()
147print(out.decode('utf-8'), err.decode('utf-8'))
148
149# 获取返回值
150return_code = p.returncode
151print(return_code)
152```
153预期如果有用例不通过,则抛出异常状态码为`1`,如果用例全部通过,则返回正确状态码为`0`。
154