assert!
macro assert(Expr condition) -> Code Import
Source: use tests::{assert}; src/tests.hy Fails the enclosing function with an error when condition is false.
assert!(cond) raises the string Assertion: 'cond' failed;
assert!(cond, message) raises message instead. Because it raises rather
than panics, the function it is used in must return a Result (or be a
test block or main, where a raised error fails the test or the program).
Invoke it as assert!(…); the arguments are passed as code (Expr), not values.
Parameters#
| Name | Type |
|---|---|
condition | Expr |
Returns#
Code
Examples#
use tests::{assert};
fn check(int n) -> Result<int, string> {
assert!(n >= 0, "negative input");
return n * 2;
}
fn main() {
assert!(check(4)? == 8);
let failed = match check(-1) {
Result::Err(msg) => msg == "negative input",
Result::Ok(_) => false,
};
assert!(failed);
}