Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding line_number test + updating some testing infrastructure #619

Merged
merged 10 commits into from
Mar 26, 2024
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ edition = "2021"
features = ["std", "serde", "kv_std", "kv_sval", "kv_serde"]

[[test]]
name = "filters"
path = "tests/filters.rs"
name = "integration"
path = "tests/integration.rs"
harness = false

[[test]]
Expand Down
46 changes: 32 additions & 14 deletions tests/filters.rs → tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ fn set_boxed_logger(logger: Box<dyn Log>) -> Result<(), log::SetLoggerError> {
}

struct State {
last_log: Mutex<Option<Level>>,
last_log_level: Mutex<Option<Level>>,
last_log_location: Mutex<Option<u32>>,
}

struct Logger(Arc<State>);
Expand All @@ -23,11 +24,11 @@ impl Log for Logger {
}

fn log(&self, record: &Record) {
*self.0.last_log.lock().unwrap() = Some(record.level());
*self.0.last_log_level.lock().unwrap() = Some(record.level());
*self.0.last_log_location.lock().unwrap() = record.line();
}
fn flush(&self) {}
}

#[cfg_attr(lib_build, test)]
fn main() {
// These tests don't really make sense when static
Expand All @@ -48,21 +49,25 @@ fn main() {
)))]
{
let me = Arc::new(State {
last_log: Mutex::new(None),
last_log_level: Mutex::new(None),
last_log_location: Mutex::new(None),
});
let a = me.clone();
set_boxed_logger(Box::new(Logger(me))).unwrap();

test(&a, LevelFilter::Off);
test(&a, LevelFilter::Error);
test(&a, LevelFilter::Warn);
test(&a, LevelFilter::Info);
test(&a, LevelFilter::Debug);
test(&a, LevelFilter::Trace);
test_filter(&a, LevelFilter::Off);
test_filter(&a, LevelFilter::Error);
test_filter(&a, LevelFilter::Warn);
test_filter(&a, LevelFilter::Info);
test_filter(&a, LevelFilter::Debug);
test_filter(&a, LevelFilter::Trace);

test_line_numbers(&a);
}
}

fn test(a: &State, filter: LevelFilter) {
fn test_filter(a: &State, filter: LevelFilter) {
// tests to ensure logs with a level beneath 'max_level' are filtered out
log::set_max_level(filter);
error!("");
last(a, t(Level::Error, filter));
Expand All @@ -82,9 +87,22 @@ fn test(a: &State, filter: LevelFilter) {
None
}
}
fn last(state: &State, expected: Option<Level>) {
let lvl = state.last_log_level.lock().unwrap().take();
assert_eq!(lvl, expected);
}
}

fn last(state: &State, expected: Option<Level>) {
let lvl = state.last_log.lock().unwrap().take();
assert_eq!(lvl, expected);
fn test_line_numbers(state: &State) {
log::set_max_level(LevelFilter::Trace);

info!(""); // ensure check_line function follows log macro
check_log_location(&state);

#[track_caller]
fn check_log_location(state: &State) {
let location = std::panic::Location::caller().line(); // get function calling location
let line_number = state.last_log_location.lock().unwrap().take().unwrap(); // get location of most recent log
assert_eq!(line_number, location - 1);
}
}
4 changes: 2 additions & 2 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#![allow(dead_code)]

#[cfg(test)]
#[path = "../filters.rs"]
mod filters;
#[path = "../integration.rs"]
mod integration;

#[cfg(test)]
#[path = "../macros.rs"]
Expand Down