Macro in tests coil-stdlib

assert!

macro assert(Expr condition) -> Code
Import
use tests::{assert};
Source: 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#

NameType
conditionExpr

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);
}

See also#

  • Result
  • not!: Expands to !expression.
  • eq!: Expands to lhs == rhs.
  • matches!: Returns whether expr matches the pattern ty, as a bool.
  • type!: Expands to typeof ty: the type of an expression, as a value that can be compared.
  • and!: Expands to lhs && rhs.
↑ ↓ navigate ↵ open