Skip to content

Commit

Permalink
Manually parameterize tests, add comments about deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Sep 26, 2024
1 parent bc139a6 commit e52cc3a
Showing 1 changed file with 117 additions and 198 deletions.
315 changes: 117 additions & 198 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,216 +371,132 @@ mod test {
}

#[test]
fn from_level_filter() {
// ErrorLevel
let v = Verbosity::<ErrorLevel>::from(LevelFilter::Off);
assert_eq!(v, Verbosity::new(0, 1));

let v = Verbosity::<ErrorLevel>::from(LevelFilter::Error);
assert_eq!(v, Verbosity::new(0, 0));

let v = Verbosity::<ErrorLevel>::from(LevelFilter::Warn);
assert_eq!(v, Verbosity::new(1, 0));

let v = Verbosity::<ErrorLevel>::from(LevelFilter::Info);
assert_eq!(v, Verbosity::new(2, 0));

let v = Verbosity::<ErrorLevel>::from(LevelFilter::Debug);
assert_eq!(v, Verbosity::new(3, 0));

let v = Verbosity::<ErrorLevel>::from(LevelFilter::Trace);
assert_eq!(v, Verbosity::new(4, 0));

// WarnLevel
let v = Verbosity::<WarnLevel>::from(LevelFilter::Off);
assert_eq!(v, Verbosity::new(0, 2));

let v = Verbosity::<WarnLevel>::from(LevelFilter::Error);
assert_eq!(v, Verbosity::new(0, 1));

let v = Verbosity::<WarnLevel>::from(LevelFilter::Warn);
assert_eq!(v, Verbosity::new(0, 0));

let v = Verbosity::<WarnLevel>::from(LevelFilter::Info);
assert_eq!(v, Verbosity::new(1, 0));

let v = Verbosity::<WarnLevel>::from(LevelFilter::Debug);
assert_eq!(v, Verbosity::new(2, 0));
fn from_level_filter_error_level() {
let tests = [
(LevelFilter::Off, Verbosity::<ErrorLevel>::new(0, 1)),
(LevelFilter::Error, Verbosity::<ErrorLevel>::new(0, 0)),
(LevelFilter::Warn, Verbosity::<ErrorLevel>::new(1, 0)),
(LevelFilter::Info, Verbosity::<ErrorLevel>::new(2, 0)),
(LevelFilter::Debug, Verbosity::<ErrorLevel>::new(3, 0)),
(LevelFilter::Trace, Verbosity::<ErrorLevel>::new(4, 0)),
];
for (filter, expected) in tests.iter() {
let verbosity = Verbosity::from(*filter);
assert_eq!(verbosity, *expected,);
assert_eq!(verbosity.log_level_filter(), *filter,);
}
}

let v = Verbosity::<WarnLevel>::from(LevelFilter::Trace);
assert_eq!(v, Verbosity::new(3, 0));
#[test]
fn from_level_filter_warn_level() {
let tests = [
(LevelFilter::Off, Verbosity::<WarnLevel>::new(0, 2)),
(LevelFilter::Error, Verbosity::<WarnLevel>::new(0, 1)),
(LevelFilter::Warn, Verbosity::<WarnLevel>::new(0, 0)),
(LevelFilter::Info, Verbosity::<WarnLevel>::new(1, 0)),
(LevelFilter::Debug, Verbosity::<WarnLevel>::new(2, 0)),
(LevelFilter::Trace, Verbosity::<WarnLevel>::new(3, 0)),
];
for (filter, expected) in tests.iter() {
let verbosity = Verbosity::from(*filter);
assert_eq!(verbosity, *expected,);
assert_eq!(verbosity.log_level_filter(), *filter,);
}
}

// InfoLevel
let v = Verbosity::<InfoLevel>::from(LevelFilter::Off);
assert_eq!(v, Verbosity::new(0, 3));
#[test]
fn from_level_filter_info_level() {
let tests = [
(LevelFilter::Off, Verbosity::<InfoLevel>::new(0, 3)),
(LevelFilter::Error, Verbosity::<InfoLevel>::new(0, 2)),
(LevelFilter::Warn, Verbosity::<InfoLevel>::new(0, 1)),
(LevelFilter::Info, Verbosity::<InfoLevel>::new(0, 0)),
(LevelFilter::Debug, Verbosity::<InfoLevel>::new(1, 0)),
(LevelFilter::Trace, Verbosity::<InfoLevel>::new(2, 0)),
];
for (filter, expected) in tests.iter() {
let verbosity = Verbosity::from(*filter);
assert_eq!(verbosity, *expected,);
assert_eq!(verbosity.log_level_filter(), *filter,);
}
}

let v = Verbosity::<InfoLevel>::from(LevelFilter::Error);
assert_eq!(v, Verbosity::new(0, 2));
#[test]
#[cfg(feature = "serde")]
fn serde_error_level() {
use serde_test::{assert_tokens, Token};

let v = Verbosity::<InfoLevel>::from(LevelFilter::Warn);
assert_eq!(v, Verbosity::new(0, 1));
let tests = [
(Verbosity::<ErrorLevel>::new(0, 1), "OFF"),
(Verbosity::<ErrorLevel>::new(0, 0), "ERROR"),
(Verbosity::<ErrorLevel>::new(1, 0), "WARN"),
(Verbosity::<ErrorLevel>::new(2, 0), "INFO"),
(Verbosity::<ErrorLevel>::new(3, 0), "DEBUG"),
(Verbosity::<ErrorLevel>::new(4, 0), "TRACE"),
];

let v = Verbosity::<InfoLevel>::from(LevelFilter::Info);
assert_eq!(v, Verbosity::new(0, 0));
// check that the verbosity round-trips through serde
for (verbosity, variant) in tests.iter() {
assert_tokens(
verbosity,
&[Token::UnitVariant {
name: "LevelFilter",
variant: variant,

Check failure

Code scanning / clippy

redundant field names in struct initialization Error

redundant field names in struct initialization
}],
);
}
}

let v = Verbosity::<InfoLevel>::from(LevelFilter::Debug);
assert_eq!(v, Verbosity::new(1, 0));
#[test]
#[cfg(feature = "serde")]
fn serde_warn_level() {
use serde_test::{assert_tokens, Token};

let v = Verbosity::<InfoLevel>::from(LevelFilter::Trace);
assert_eq!(v, Verbosity::new(2, 0));
let tests = [
(Verbosity::<WarnLevel>::new(0, 2), "OFF"),
(Verbosity::<WarnLevel>::new(0, 1), "ERROR"),
(Verbosity::<WarnLevel>::new(0, 0), "WARN"),
(Verbosity::<WarnLevel>::new(1, 0), "INFO"),
(Verbosity::<WarnLevel>::new(2, 0), "DEBUG"),
(Verbosity::<WarnLevel>::new(3, 0), "TRACE"),
];

let v = Verbosity::<InfoLevel>::from(LevelFilter::Trace);
assert_eq!(v, Verbosity::new(2, 0));
// check that the verbosity round-trips through serde
for (verbosity, variant) in tests.iter() {
assert_tokens(
verbosity,
&[Token::UnitVariant {
name: "LevelFilter",
variant: variant,

Check failure

Code scanning / clippy

redundant field names in struct initialization Error

redundant field names in struct initialization
}],
);
}
}

#[test]
#[cfg(feature = "serde")]
fn serde() {
fn serde_info_level() {
use serde_test::{assert_tokens, Token};

assert_tokens(
&Verbosity::<ErrorLevel>::new(0, 1),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "OFF",
}],
);

assert_tokens(
&Verbosity::<ErrorLevel>::new(0, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "ERROR",
}],
);

assert_tokens(
&Verbosity::<ErrorLevel>::new(1, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "WARN",
}],
);

assert_tokens(
&Verbosity::<ErrorLevel>::new(2, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "INFO",
}],
);

assert_tokens(
&Verbosity::<ErrorLevel>::new(3, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "DEBUG",
}],
);

assert_tokens(
&Verbosity::<ErrorLevel>::new(4, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "TRACE",
}],
);

assert_tokens(
&Verbosity::<WarnLevel>::new(0, 2),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "OFF",
}],
);

assert_tokens(
&Verbosity::<WarnLevel>::new(0, 1),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "ERROR",
}],
);

assert_tokens(
&Verbosity::<WarnLevel>::new(0, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "WARN",
}],
);

assert_tokens(
&Verbosity::<WarnLevel>::new(1, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "INFO",
}],
);

assert_tokens(
&Verbosity::<WarnLevel>::new(2, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "DEBUG",
}],
);

assert_tokens(
&Verbosity::<WarnLevel>::new(3, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "TRACE",
}],
);

assert_tokens(
&Verbosity::<InfoLevel>::new(0, 3),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "OFF",
}],
);

assert_tokens(
&Verbosity::<InfoLevel>::new(0, 2),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "ERROR",
}],
);

assert_tokens(
&Verbosity::<InfoLevel>::new(0, 1),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "WARN",
}],
);

assert_tokens(
&Verbosity::<InfoLevel>::new(0, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "INFO",
}],
);

assert_tokens(
&Verbosity::<InfoLevel>::new(1, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "DEBUG",
}],
);

assert_tokens(
&Verbosity::<InfoLevel>::new(2, 0),
&[Token::UnitVariant {
name: "LevelFilter",
variant: "TRACE",
}],
);
let tests = [
(Verbosity::<InfoLevel>::new(0, 3), "OFF"),
(Verbosity::<InfoLevel>::new(0, 2), "ERROR"),
(Verbosity::<InfoLevel>::new(0, 1), "WARN"),
(Verbosity::<InfoLevel>::new(0, 0), "INFO"),
(Verbosity::<InfoLevel>::new(1, 0), "DEBUG"),
(Verbosity::<InfoLevel>::new(2, 0), "TRACE"),
];

// check that the verbosity round-trips through serde
for (verbosity, variant) in tests.iter() {
assert_tokens(
verbosity,
&[Token::UnitVariant {
name: "LevelFilter",
variant: variant,

Check failure

Code scanning / clippy

redundant field names in struct initialization Error

redundant field names in struct initialization
}],
);
}
}

#[test]
Expand All @@ -596,13 +512,16 @@ mod test {
verbose: Verbosity<InfoLevel>,
}

// round-trips
// round-trips through serde. The default serialization is the uppercase name of the variant
// as this is how log::LevelFilter is serialized.
let toml = "meaning_of_life = 42\nverbose = \"DEBUG\"\n";
let cli: Cli = toml::from_str(toml).unwrap();
assert_eq!(cli.verbose.log_level_filter(), LevelFilter::Debug);
assert_eq!(toml::to_string(&cli).unwrap(), toml);

// is case-insensitive
// Checks that deserializing a lowercase level also works fine. Serializing is likely less
// frequent than deserializing from a config file, so this is a more common case than the
// above serialize then deserialize test.
let toml = "meaning_of_life = 42\nverbose = \"debug\"\n";
let cli: Cli = toml::from_str(toml).unwrap();
assert_eq!(cli.verbose.log_level_filter(), LevelFilter::Debug);
Expand Down

0 comments on commit e52cc3a

Please sign in to comment.