1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//! The `std::test` module.

use crate::{ContextError, Module, Panic};
use std::fmt;

/// Construct the `std::test` module.
pub fn module() -> Result<Module, ContextError> {
    let mut module = Module::new(&["std", "test"]);
    module.function(&["assert"], assert)?;
    Ok(module)
}

#[derive(Debug)]
struct AssertionFailed(String);

impl fmt::Display for AssertionFailed {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "assertion failed `{}`", self.0)
    }
}

/// Assert that a value is true.
fn assert(value: bool, message: &str) -> Result<(), Panic> {
    if !value {
        return Err(Panic::custom(AssertionFailed(message.to_string())));
    }

    Ok(())
}