//! Behavio(u)r tests //! //! Check if the macro actually behaves as intended. use log::{self, LevelFilter, Metadata, Record}; use micro_timer::timed; use std::sync::atomic::{AtomicIsize, Ordering}; #[test] fn test_behavior() { struct SimpleLogger { called: AtomicIsize, }; impl log::Log for SimpleLogger { fn enabled(&self, _metadata: &Metadata) -> bool { true } fn log(&self, record: &Record) { self.called.fetch_add(1, Ordering::Relaxed); assert_eq!(record.level(), LevelFilter::Trace); assert!(record .args() .to_string() .starts_with("Duration of `thing`:")) } fn flush(&self) {} } static LOGGER: SimpleLogger = SimpleLogger { called: AtomicIsize::new(0), }; log::set_logger(&LOGGER) .map(|()| log::set_max_level(LevelFilter::Trace)) .unwrap(); #[timed] fn thing() {} assert_eq!(LOGGER.called.load(Ordering::Relaxed), 0); thing(); assert_eq!(LOGGER.called.load(Ordering::Relaxed), 1); #[timed] fn thing_panicking() { panic!("at the disco") } assert_eq!(LOGGER.called.load(Ordering::Relaxed), 1); let res = std::panic::catch_unwind(|| { thing_panicking(); }); assert!(res.is_err()); // Should not print anything if it panics assert_eq!(LOGGER.called.load(Ordering::Relaxed), 1); }