Skip to content

Instantly share code, notes, and snippets.

@H2CO3
Created October 15, 2017 18:58
Show Gist options
  • Save H2CO3/319d9343075dbd9b5090812657856c04 to your computer and use it in GitHub Desktop.
Save H2CO3/319d9343075dbd9b5090812657856c04 to your computer and use it in GitHub Desktop.
Protect against forgetting to call a test case evaluation function
fn make_test_evaluator() -> Box<Fn(YourTestCaseTypeHere)> {
use std::cell::RefCell;
struct EvalGuard(bool);
impl Drop for EvalGuard {
fn drop(&mut self) {
if !self.0 {
panic!("Did not actually evaluate test cases");
}
}
}
let eval_guard = RefCell::new(EvalGuard(false));
Box::new(move |test_case: YourTestCaseTypeHere| {
assert!(test_case.does_stuff_correctly(), "doesn't do stuff correctly");
eval_guard.borrow_mut().0 = true;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment