Skip to content

Commit

Permalink
Address lints for tests, benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Aug 29, 2023
1 parent 18cabd1 commit b60a2b2
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 88 deletions.
6 changes: 5 additions & 1 deletion benchmarks/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Benchmarks for `time`.
//!
//! These benchmarks are not very precise, but they're good enough to catch major performance
//! regressions. Run them if you think that may be the case. CI **does not** run benchmarks.
#![allow(
missing_docs,
clippy::missing_docs_in_private_items,
clippy::std_instead_of_core, // irrelevant for benchmarks
clippy::std_instead_of_alloc, // irrelevant for benchmarks
Expand Down
220 changes: 168 additions & 52 deletions tests/date.rs

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions tests/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ macro_rules! assert_cloned_eq {
}

fn component_range_error() -> error::ComponentRange {
Time::from_hms(24, 0, 0).unwrap_err()
Time::from_hms(24, 0, 0).expect_err("24 is not a valid hour")
}

fn invalid_format_description() -> error::InvalidFormatDescription {
format_description::parse("[").unwrap_err()
format_description::parse("[").expect_err("format description is invalid")
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn clone() {
let instant = Instant::now();
Expand Down
1 change: 1 addition & 0 deletions tests/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ fn time_fn() {
assert_eq!(value, 0);
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn display() {
assert_eq!(0.seconds().to_string(), "0s");
Expand Down
20 changes: 6 additions & 14 deletions tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,29 @@ macro_rules! assert_source {
}

fn component_range() -> ComponentRange {
Date::from_ordinal_date(0, 367).unwrap_err()
Date::from_ordinal_date(0, 367).expect_err("367 is not a valid day")
}

fn insufficient_type_information() -> Format {
Time::MIDNIGHT
.format(&time::format_description::well_known::Rfc3339)
.unwrap_err()
.expect_err("missing date and UTC offset")
}

fn unexpected_trailing_characters() -> Parse {
Time::parse("a", format_description!("")).unwrap_err()
Time::parse("a", format_description!("")).expect_err("should fail to parse")
}

// fn unexpected_trailing_characters() -> ParseFromDescription {
// match Time::parse("0", format_description!("[end]")) {
// Err(Parse::ParseFromDescription(
// err @ ParseFromDescription::UnexpectedTrailingCharacters { .. },
// )) => err,
// _ => panic!("unexpected result"),
// }
// }

fn invalid_format_description() -> InvalidFormatDescription {
format_description::parse("[").unwrap_err()
format_description::parse("[").expect_err("format description is invalid")
}

fn io_error() -> io::Error {
io::Error::last_os_error()
}

fn invalid_literal() -> ParseFromDescription {
Parsed::parse_literal(b"a", b"b").unwrap_err()
Parsed::parse_literal(b"a", b"b").expect_err("should fail to parse")
}

#[test]
Expand Down Expand Up @@ -166,6 +157,7 @@ fn component_name() {
assert_eq!(component_range().name(), "ordinal");
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn conversion() {
assert!(ComponentRange::try_from(Error::from(component_range())).is_ok());
Expand Down
1 change: 1 addition & 0 deletions tests/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ fn insufficient_type_information() {
assert_insufficient_type_information(datetime!(2021-001 0:00).format(&Iso8601::DEFAULT));
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn failed_write() -> time::Result<()> {
macro_rules! assert_err {
Expand Down
2 changes: 1 addition & 1 deletion tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ fn format_description_coverage() {
assert_eq!(
format_description!("[ignore count:2]"),
&[FormatItem::Component(Component::Ignore(Ignore::count(
NonZeroU16::new(2).unwrap()
NonZeroU16::new(2).expect("2 is not zero")
)))]
);
assert_eq!(
Expand Down
2 changes: 0 additions & 2 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
clippy::std_instead_of_core, // irrelevant for tests
clippy::std_instead_of_alloc, // irrelevant for tests
clippy::alloc_instead_of_core, // irrelevant for tests
clippy::cognitive_complexity, // TODO split up tests as necessary
clippy::unwrap_used, // TODO convert to expect or better error handling
)]

#[cfg(not(all(
Expand Down
2 changes: 2 additions & 0 deletions tests/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use time::{
UtcOffset, Weekday,
};

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn alignment() {
macro_rules! assert_alignment {
Expand Down Expand Up @@ -89,6 +90,7 @@ fn alignment() {
assert_alignment!(modifier::YearRepr, 1);
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn size() {
macro_rules! assert_size {
Expand Down
2 changes: 2 additions & 0 deletions tests/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn next() {
assert_eq!(December.next(), January);
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn nth_next() {
assert_eq!(January.nth_next(0), January);
Expand Down Expand Up @@ -66,6 +67,7 @@ fn nth_next() {
assert_eq!(December.nth_next(u8::MAX), March);
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn nth_prev() {
assert_eq!(January.nth_prev(0), January);
Expand Down
7 changes: 3 additions & 4 deletions tests/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn now_utc() {
fn now_local() {
use time::util::local_offset::*;

let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();
let _guard = crate::SOUNDNESS_LOCK.lock().expect("lock is poisoned");

// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
// will run into issues with setting an environment variable a few times.
Expand Down Expand Up @@ -73,9 +73,8 @@ fn checked_to_offset() {
assert_eq!(
datetime!(2000-01-01 0:00 UTC)
.checked_to_offset(offset!(-1))
.unwrap()
.year(),
1999,
.map(|odt| odt.year()),
Some(1999),
);
assert_eq!(
PrimitiveDateTime::MAX
Expand Down
3 changes: 3 additions & 0 deletions tests/parse_format_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ fn simple_component() {
);
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn errors() {
use InvalidFormatDescription::*;
Expand Down Expand Up @@ -349,6 +350,7 @@ fn errors() {
}
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn component_with_modifiers() {
for (padding, padding_str) in iterator::padding() {
Expand Down Expand Up @@ -795,6 +797,7 @@ fn nested_error() {
));
}

#[allow(clippy::unwrap_used)] // It's the point of the test.
#[test]
fn error_display() {
assert_eq!(
Expand Down
21 changes: 13 additions & 8 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ fn rfc_2822() -> time::Result<()> {
Ok(())
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn rfc_2822_err() {
// In the first test, the "weekday" component is invalid, we're actually testing the whitespace
Expand Down Expand Up @@ -326,6 +327,7 @@ fn rfc_3339() -> time::Result<()> {
Ok(())
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn rfc_3339_err() {
assert!(matches!(
Expand Down Expand Up @@ -683,6 +685,7 @@ fn parse_time() -> time::Result<()> {
Ok(())
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn parse_time_err() -> time::Result<()> {
assert!(matches!(
Expand Down Expand Up @@ -871,6 +874,7 @@ fn parse_date() -> time::Result<()> {
Ok(())
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn parse_date_err() -> time::Result<()> {
assert!(matches!(
Expand Down Expand Up @@ -1107,6 +1111,7 @@ fn parse_offset_date_time_err() -> time::Result<()> {
Ok(())
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn parse_components() -> time::Result<()> {
macro_rules! parse_component {
Expand Down Expand Up @@ -1389,13 +1394,13 @@ fn parse_components() -> time::Result<()> {
let mut parsed = Parsed::new();
let result = parsed.parse_component(
b"abcdef",
Component::Ignore(Ignore::count(NonZeroU16::new(3).unwrap())),
Component::Ignore(Ignore::count(NonZeroU16::new(3).expect("3 is not zero"))),
)?;
assert_eq!(result, b"def");
let mut parsed = Parsed::new();
let result = parsed.parse_component(
b"abcdef",
Component::Ignore(Ignore::count(NonZeroU16::new(7).unwrap())),
Component::Ignore(Ignore::count(NonZeroU16::new(7).expect("7 is not zero"))),
);
assert!(matches!(
result,
Expand Down Expand Up @@ -1496,6 +1501,7 @@ fn parse_optional() -> time::Result<()> {
Ok(())
}

#[allow(clippy::cognitive_complexity)] // all test the same thing
#[test]
fn parse_first() -> time::Result<()> {
// Ensure the first item is parsed correctly.
Expand Down Expand Up @@ -1576,7 +1582,7 @@ fn parse_first() -> time::Result<()> {
FormatItem::Compound(&fd::parse("x")?),
]),
)
.unwrap_err();
.expect_err("parsing should fail");
assert_eq!(err, error::ParseFromDescription::InvalidComponent("period"));

let mut parsed = Parsed::new();
Expand All @@ -1588,7 +1594,7 @@ fn parse_first() -> time::Result<()> {
FormatItem::Compound(&fd::parse("x")?),
])),
)
.unwrap_err();
.expect_err("parsing should fail");
assert_eq!(err, error::ParseFromDescription::InvalidComponent("period"));

Ok(())
Expand Down Expand Up @@ -1665,11 +1671,10 @@ fn parse_unix_timestamp_err() -> time::Result<()> {
fn issue_601() {
let date = OffsetDateTime::parse(
"1234567890.123",
&fd::parse("[unix_timestamp].[subsecond digits:3]").unwrap(),
)
.unwrap();
&fd::parse("[unix_timestamp].[subsecond digits:3]").expect("format description is valid"),
);

assert_eq!(date, datetime!(2009-02-13 23:31:30.123 +00:00:00));
assert_eq!(date, Ok(datetime!(2009-02-13 23:31:30.123 +00:00:00)));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions tests/utc_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn neg() {
fn local_offset_at() {
use time::util::local_offset::*;

let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();
let _guard = crate::SOUNDNESS_LOCK.lock().expect("lock is poisoned");

// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
// will run into issues with setting an environment variable a few times.
Expand All @@ -164,7 +164,7 @@ fn local_offset_at() {
fn current_local_offset() {
use time::util::local_offset::*;

let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();
let _guard = crate::SOUNDNESS_LOCK.lock().expect("lock is poisoned");

// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
// will run into issues with setting an environment variable a few times.
Expand All @@ -185,7 +185,7 @@ fn current_local_offset() {
ignore
)]
fn local_offset_error_when_multithreaded() {
let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();
let _guard = crate::SOUNDNESS_LOCK.lock().expect("lock is poisoned");

std::thread::spawn(|| {
assert!(UtcOffset::current_local_offset().is_err());
Expand Down
2 changes: 1 addition & 1 deletion tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn weeks_in_year() {
fn local_offset_soundness() {
use time::util::local_offset::*;

let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();
let _guard = crate::SOUNDNESS_LOCK.lock().expect("lock is poisoned");

assert_eq!(get_soundness(), Soundness::Sound);
// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
Expand Down

0 comments on commit b60a2b2

Please sign in to comment.