assert
assert
Function
assert is used to assert that a condition is true. When the condition is true, execution continues and returns true; when the condition is false, a runtime error is thrown and the current execution is interrupted.
Syntax
assert(condition) assert(condition, message)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| condition | Any | Yes | None | The condition to be judged is processed according to BT's if truth rule. |
| message | Any | No | None | Customized failure prompt, which will be converted to string output. |
Return value
| Type | Description |
|---|---|
| Bool | Returns true when the condition is true; it will not return when the condition is not true, and a runtime error will be thrown directly. |
Code Example
assert(1 + 1 == 2) name = 'BT' assert(name == 'BT', 'name should be BT') // Output: ok print 'ok'
On failure:
assert(1 + 1 == 3)
will throw an error similar to:
断言失败:assert(1 + 1 == 3)
Notes
-
assertrequires at least 1 argument. - The truth rules are consistent with the
ifcondition,null,empty,0, empty strings, empty arrays, and empty objects are all considered false. - When the assertion fails, the current
assert(...)source code statement will be included in the error message as much as possible; ifmessageis passed in, the custom message will be used as the main error message.