Skip to content

Commit e0c59fe

Browse files
authored
Merge pull request #410 from Peefy/test-tool-fail-test-suite
docs: add more error suites for the test tool
2 parents 083a50c + 4de63cf commit e0c59fe

File tree

4 files changed

+120
-0
lines changed
  • docs/tools/cli/kcl
  • i18n/zh-CN/docusaurus-plugin-content-docs
  • versioned_docs/version-0.9/tools/cli/kcl

4 files changed

+120
-0
lines changed

docs/tools/cli/kcl/test.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ schema Person:
1717
name: str = "kcl"
1818
age: int = 1
1919

20+
check:
21+
0 <= age <= 120, "age must be in [0, 120]"
22+
2023
hello = Person {
2124
name = "hello kcl"
2225
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117120
FAIL: 3/3
118121
```
119122

123+
If we want to test error cases correctly and check error messages, we can use the `runtime.catch` function.
124+
125+
```python
126+
import runtime
127+
128+
test_person_age_check_error_message = lambda {
129+
msg = runtime.catch(lambda {
130+
a = Person {age = 123}
131+
})
132+
assert msg == "age must be in [0, 120]"
133+
}
134+
```
135+
136+
Run the command
137+
138+
```shell
139+
kcl test
140+
```
141+
142+
Output:
143+
144+
```shell
145+
test_person_age_check_error_message: PASS (2ms)
146+
--------------------------------------------------------------------------------
147+
PASS: 1/1
148+
```
149+
120150
## Args
121151

122152
- `kcl test path` Execute the test of the specified directory. It can be omitted if it's the same directory that the command is executed

i18n/zh-CN/docusaurus-plugin-content-docs/current/tools/cli/kcl/test.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ schema Person:
1717
name: str = "kcl"
1818
age: int = 1
1919

20+
check:
21+
0 <= age <= 120, "age must be in [0, 120]"
22+
2023
hello = Person {
2124
name = "hello kcl"
2225
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117120
FAIL: 3/3
118121
```
119122

123+
如果我们想要正确测试错误情况并检查错误消息,我们可以使用 `runtime.catch` 函数。
124+
125+
```python
126+
import runtime
127+
128+
test_person_age_check_error_message = lambda {
129+
msg = runtime.catch(lambda {
130+
a = Person {age = 123}
131+
})
132+
assert msg == "age must be in [0, 120]"
133+
}
134+
```
135+
136+
运行命令
137+
138+
```shell
139+
kcl test
140+
```
141+
142+
输出:
143+
144+
```shell
145+
test_person_age_check_error_message: PASS (2ms)
146+
--------------------------------------------------------------------------------
147+
PASS: 1/1
148+
```
149+
120150
## 参数说明
121151

122152
- `kcl test path` 执行指定目录的测试, 当前目录可以省略该参数

i18n/zh-CN/docusaurus-plugin-content-docs/version-0.9/tools/cli/kcl/test.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ schema Person:
1717
name: str = "kcl"
1818
age: int = 1
1919

20+
check:
21+
0 <= age <= 120, "age must be in [0, 120]"
22+
2023
hello = Person {
2124
name = "hello kcl"
2225
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117120
FAIL: 3/3
118121
```
119122

123+
如果我们想要正确测试错误情况并检查错误消息,我们可以使用 `runtime.catch` 函数。
124+
125+
```python
126+
import runtime
127+
128+
test_person_age_check_error_message = lambda {
129+
msg = runtime.catch(lambda {
130+
a = Person {age = 123}
131+
})
132+
assert msg == "age must be in [0, 120]"
133+
}
134+
```
135+
136+
运行命令
137+
138+
```shell
139+
kcl test
140+
```
141+
142+
输出:
143+
144+
```shell
145+
test_person_age_check_error_message: PASS (2ms)
146+
--------------------------------------------------------------------------------
147+
PASS: 1/1
148+
```
149+
120150
## 参数说明
121151

122152
- `kcl test path` 执行指定目录的测试, 当前目录可以省略该参数

versioned_docs/version-0.9/tools/cli/kcl/test.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ schema Person:
1717
name: str = "kcl"
1818
age: int = 1
1919

20+
check:
21+
0 <= age <= 120, "age must be in [0, 120]"
22+
2023
hello = Person {
2124
name = "hello kcl"
2225
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117120
FAIL: 3/3
118121
```
119122

123+
If we want to test error cases correctly and check error messages, we can use the `runtime.catch` function.
124+
125+
```python
126+
import runtime
127+
128+
test_person_age_check_error_message = lambda {
129+
msg = runtime.catch(lambda {
130+
a = Person {age = 123}
131+
})
132+
assert msg == "age must be in [0, 120]"
133+
}
134+
```
135+
136+
Run the command
137+
138+
```shell
139+
kcl test
140+
```
141+
142+
Output:
143+
144+
```shell
145+
test_person_age_check_error_message: PASS (2ms)
146+
--------------------------------------------------------------------------------
147+
PASS: 1/1
148+
```
149+
120150
## Args
121151

122152
- `kcl test path` Execute the test of the specified directory. It can be omitted if it's the same directory that the command is executed

0 commit comments

Comments
 (0)