Skip to main content

assert_always

Macro assert_always 

Source
macro_rules! assert_always {
    ($condition:expr, $message:expr$(, $details:expr)?) => { ... };
    ($($rest:tt)*) => { ... };
}
Expand description

Assert that condition is true every time this function is called, and that it is called at least once. The corresponding test property will be viewable in the Antithesis SDK: Always group of your triage report.

§Example

use serde::Serialize;
use antithesis_sdk::{assert_always, random};

// Details can be any Serialize type; non-null scalars and arrays are
// wrapped under "value".
// A borrowed struct avoids serialization on evaluations that are not emitted.
#[derive(Serialize)]
struct Details<'a> {
    max_allowed: u64,
    actual: u64,
    source: &'a str,
}

const MAX_ALLOWED: u64 = 100;
let actual = random::get_random() % 100u64;
let details = Details { max_allowed: MAX_ALLOWED, actual, source: "demo" };
antithesis_sdk::assert_always!(actual < MAX_ALLOWED, "Value in range", &details);

Ensure that non-const-evaluable messages are rejected.

use serde_json::json;
const MESSAGE: &str = concat!("Value", " in range");
antithesis_sdk::assert_always!(true, MESSAGE, &json!({}));

A message computed at runtime is rejected at compile time:

use serde_json::json;
let message = String::from("Value in range");
antithesis_sdk::assert_always!(true, message, &json!({}));
use serde_json::json;
antithesis_sdk::assert_always!(true, format!("{}", "Value in range"), &json!({}));