pub fn assert_not_modified<F, Data, Any>(data: &mut Data, test: F)
where
F: FnOnce(&mut Data) -> Any,
Data: Clone + Eq + std::fmt::Debug,
{
let old_data = data.clone();
test(data);
assert_eq!(
&old_data, data,
"Data was modified where it should not have been"
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic(expected = "Data was modified where it should not have been")]
fn test_should_not_modify_data_panics() {
let mut x = 4;
assert_not_modified(&mut x, |x| *x = 1);
}
#[test]
fn test_should_not_modify_data_does_not_panic() {
let mut x = 4;
assert_not_modified(&mut x, |x| *x + 1);
}
}