Skip to content

Commit

Permalink
Test verbosity calculation behavior
Browse files Browse the repository at this point in the history
Adds tests for the verbosity calculation behavior, including
characterization tests that show the current behavior is incorrectly
calculated when u8::MAX is passed to either verbose or quiet.
  • Loading branch information
joshka committed Sep 26, 2024
1 parent c621a6a commit 7f435cd
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,130 @@ mod test {
use clap::CommandFactory;
Cli::command().debug_assert();
}

#[test]
fn verbosity_error_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Error), LevelFilter::Error),
(1, 0, Some(Level::Warn), LevelFilter::Warn),
(2, 0, Some(Level::Info), LevelFilter::Info),
(3, 0, Some(Level::Debug), LevelFilter::Debug),
(4, 0, Some(Level::Trace), LevelFilter::Trace),
(5, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, None, LevelFilter::Off),
(0, 2, None, LevelFilter::Off),
(255, 255, Some(Level::Error), LevelFilter::Error),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<ErrorLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}

/// These are characterization tests that show the current behavior
#[test]
fn verbosity_error_level_max_incorrect() {
let v: Verbosity<ErrorLevel> = Verbosity::new(255, 0);
assert_eq!(v.log_level(), None); // Should be Some(Level::Trace)
assert_eq!(v.log_level_filter(), LevelFilter::Off); // Should be LevelFilter::Trace

let v: Verbosity<ErrorLevel> = Verbosity::new(0, 255);
assert_eq!(v.log_level(), Some(Level::Warn)); // Should be None
assert_eq!(v.log_level_filter(), LevelFilter::Warn); // Should be LevelFilter::Off
}

#[test]
fn verbosity_warn_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Warn), LevelFilter::Warn),
(1, 0, Some(Level::Info), LevelFilter::Info),
(2, 0, Some(Level::Debug), LevelFilter::Debug),
(3, 0, Some(Level::Trace), LevelFilter::Trace),
(4, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, Some(Level::Error), LevelFilter::Error),
(0, 2, None, LevelFilter::Off),
(0, 3, None, LevelFilter::Off),
(255, 255, Some(Level::Warn), LevelFilter::Warn),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<WarnLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}

/// These are characterization tests that show the current behavior
#[test]
fn verbosity_warn_level_max_incorrect() {
let v: Verbosity<WarnLevel> = Verbosity::new(255, 0);
assert_eq!(v.log_level(), Some(Level::Error)); // Should be Some(Level::Trace)
assert_eq!(v.log_level_filter(), LevelFilter::Error); // Should be LevelFilter::Trace

let v: Verbosity<WarnLevel> = Verbosity::new(0, 255);
assert_eq!(v.log_level(), Some(Level::Info)); // Should be None
assert_eq!(v.log_level_filter(), LevelFilter::Info); // Should be LevelFilter::Off
}

#[test]
fn verbosity_info_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Info), LevelFilter::Info),
(1, 0, Some(Level::Debug), LevelFilter::Debug),
(2, 0, Some(Level::Trace), LevelFilter::Trace),
(3, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, Some(Level::Warn), LevelFilter::Warn),
(0, 2, Some(Level::Error), LevelFilter::Error),
(0, 3, None, LevelFilter::Off),
(0, 4, None, LevelFilter::Off),
(255, 255, Some(Level::Info), LevelFilter::Info),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<InfoLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}

/// These are characterization tests that show the current behavior
#[test]
fn verbosity_info_level_max_incorrect() {
let v: Verbosity<InfoLevel> = Verbosity::new(255, 0);
assert_eq!(v.log_level(), Some(Level::Warn)); // Should be Some(Level::Trace)
assert_eq!(v.log_level_filter(), LevelFilter::Warn); // Should be LevelFilter::Trace

let v: Verbosity<InfoLevel> = Verbosity::new(0, 255);
assert_eq!(v.log_level(), Some(Level::Debug)); // Should be None
assert_eq!(v.log_level_filter(), LevelFilter::Debug); // Should be LevelFilter::Off
}
}

0 comments on commit 7f435cd

Please sign in to comment.