Skip to content

Commit

Permalink
Merge pull request #619 from DIvkov575/add-line-number-tests
Browse files Browse the repository at this point in the history
adding line_number test + updating some testing infrastructure
  • Loading branch information
KodrAus authored Mar 26, 2024
2 parents 7bdb8aa + 3aea361 commit 4961162
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
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

0 comments on commit 4961162

Please sign in to comment.