File tree Expand file tree Collapse file tree 4 files changed +120
-0
lines changed
i18n/zh-CN/docusaurus-plugin-content-docs
version-0.9/tools/cli/kcl
versioned_docs/version-0.9/tools/cli/kcl Expand file tree Collapse file tree 4 files changed +120
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ schema Person:
17
17
name: str = " kcl"
18
18
age: int = 1
19
19
20
+ check:
21
+ 0 <= age <= 120 , " age must be in [0, 120]"
22
+
20
23
hello = Person {
21
24
name = " hello kcl"
22
25
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117
120
FAIL: 3/3
118
121
```
119
122
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
+
120
150
## Args
121
151
122
152
- ` 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
Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ schema Person:
17
17
name: str = " kcl"
18
18
age: int = 1
19
19
20
+ check:
21
+ 0 <= age <= 120 , " age must be in [0, 120]"
22
+
20
23
hello = Person {
21
24
name = " hello kcl"
22
25
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117
120
FAIL: 3/3
118
121
```
119
122
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
+
120
150
## 参数说明
121
151
122
152
- ` kcl test path ` 执行指定目录的测试, 当前目录可以省略该参数
Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ schema Person:
17
17
name: str = " kcl"
18
18
age: int = 1
19
19
20
+ check:
21
+ 0 <= age <= 120 , " age must be in [0, 120]"
22
+
20
23
hello = Person {
21
24
name = " hello kcl"
22
25
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117
120
FAIL: 3/3
118
121
```
119
122
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
+
120
150
## 参数说明
121
151
122
152
- ` kcl test path ` 执行指定目录的测试, 当前目录可以省略该参数
Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ schema Person:
17
17
name: str = " kcl"
18
18
age: int = 1
19
19
20
+ check:
21
+ 0 <= age <= 120 , " age must be in [0, 120]"
22
+
20
23
hello = Person {
21
24
name = " hello kcl"
22
25
age = 102
@@ -117,6 +120,33 @@ EvaluationError
117
120
FAIL: 3/3
118
121
```
119
122
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
+
120
150
## Args
121
151
122
152
- ` 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
You can’t perform that action at this time.
0 commit comments