#modify #assert #macro #mutate #test

assert-not-modified

Macro which, given a variable and a block of code, executes the block of code and checks that the variable has not changed

4 releases (1 stable)

1.0.0 Nov 30, 2020
0.1.2 Nov 28, 2020
0.1.1 Nov 28, 2020
0.1.0 Nov 28, 2020

#647 in Testing

GPL-3.0 license

14KB

assert-not-modified

Rust macro which, given a variable and a block of code, executes the block of code and checks that the variable has not changed.

For instance, this can check that a function does not have side effects.

The given variable must implement Clone and Debug.

Panics

Panics if data is modified with the message "Data was modified where it should not have been".

Example

#[macro_use] extern crate assert_not_modified;

// This function returns Err but modifies x anyway. This is misleading.
fn modify_x_or_err(x: &mut i32) -> Result<(), String> {
    *x = *x + 1;
    Err("Something wrong happened !".to_owned())
}

// This test will expose the lying function :
assert!(std::panic::catch_unwind(|| {
    let mut x = 3;
    assert_not_modified!(x, modify_x_or_err(&mut x)); // Panics
})
.is_err());

lib.rs:

Macro which, given a variable and a block of code, executes the block of code and checks that the variable has not changed.

For instance, this can check that a function does not have side-effects.

No runtime deps