use crate::entry::{Entry, Expected};
use crate::result::{BatchRunResult, BatchResult};
use crate::{config::Config, runner::Runner};
use std::cell::RefCell;
use std::path::Path;
use std::thread;
use termcolor::WriteColor;
#[derive(Debug, Default)]
pub struct Batch {
runner: RefCell<Runner>,
has_run: bool,
}
impl Batch {
pub fn new() -> Self {
Batch {
runner: RefCell::new(Runner::new()),
has_run: false,
}
}
pub fn run_match<P: AsRef<Path>>(&self, path: P) {
self.runner
.borrow_mut()
.add_entry(Entry::new(path, Expected::RunMatch));
}
pub fn compile_fail<P: AsRef<Path>>(&self, path: P) {
self.runner
.borrow_mut()
.add_entry(Entry::new(path, Expected::CompileFail));
}
pub fn run(mut self) -> BatchResult {
self.has_run = true;
self.runner.borrow_mut().run()
}
pub fn run_with_config<W: WriteColor>(mut self, cfg: Config<W>) -> BatchResult<BatchRunResult<W>> {
self.has_run = true;
self.runner.borrow_mut().run_with_config(cfg)
}
}
#[doc(hidden)]
impl Drop for Batch {
fn drop(&mut self) {
if !thread::panicking() && !self.has_run {
self.runner
.borrow_mut()
.run()
.map(|_| ())
.unwrap_or_else(|err| println!("{}", err));
}
}
}