Skip to content

Commit

Permalink
Remove unnecessary Result-wrapping in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Jan 28, 2024
1 parent 5f8a8e4 commit b9a2569
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 62 deletions.
29 changes: 10 additions & 19 deletions tests/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::{Duration as StdDuration, SystemTime};

use time::ext::{NumericalDuration, NumericalStdDuration};
use time::macros::{date, datetime, offset, time};
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Result, Weekday};
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Weekday};

#[test]
fn new_utc() {
Expand Down Expand Up @@ -445,7 +445,7 @@ fn replace_offset() {
}

#[test]
fn replace_year() -> Result<()> {
fn replace_year() {
assert_eq!(
datetime!(2022 - 02 - 18 12:00 +01).replace_year(2019),
Ok(datetime!(2019 - 02 - 18 12:00 +01))
Expand All @@ -460,11 +460,10 @@ fn replace_year() -> Result<()> {
.replace_year(1_000_000_000)
.is_err()
); // 1_000_000_000 isn't a valid year
Ok(())
}

#[test]
fn replace_month() -> Result<()> {
fn replace_month() {
assert_eq!(
datetime!(2022 - 02 - 18 12:00 +01).replace_month(Month::January),
Ok(datetime!(2022 - 01 - 18 12:00 +01))
Expand All @@ -474,18 +473,16 @@ fn replace_month() -> Result<()> {
.replace_month(Month::February)
.is_err()
); // 30 isn't a valid day in February
Ok(())
}

#[test]
fn replace_day() -> Result<()> {
fn replace_day() {
assert_eq!(
datetime!(2022 - 02 - 18 12:00 +01).replace_day(1),
Ok(datetime!(2022 - 02 - 01 12:00 +01))
);
assert!(datetime!(2022 - 02 - 18 12:00 +01).replace_day(0).is_err()); // 00 isn't a valid day
assert!(datetime!(2022 - 02 - 18 12:00 +01).replace_day(30).is_err()); // 30 isn't a valid day in February
Ok(())
}

#[test]
Expand All @@ -512,7 +509,7 @@ fn replace_ordinal() {
}

#[test]
fn replace_hour() -> Result<()> {
fn replace_hour() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006 +01).replace_hour(7),
Ok(datetime!(2022 - 02 - 18 07:02:03.004_005_006 +01))
Expand All @@ -522,11 +519,10 @@ fn replace_hour() -> Result<()> {
.replace_hour(24)
.is_err()
); // 24 isn't a valid hour
Ok(())
}

#[test]
fn replace_minute() -> Result<()> {
fn replace_minute() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006 +01).replace_minute(7),
Ok(datetime!(2022 - 02 - 18 01:07:03.004_005_006 +01))
Expand All @@ -536,11 +532,10 @@ fn replace_minute() -> Result<()> {
.replace_minute(60)
.is_err()
); // 60 isn't a valid minute
Ok(())
}

#[test]
fn replace_second() -> Result<()> {
fn replace_second() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006 +01).replace_second(7),
Ok(datetime!(2022 - 02 - 18 01:02:07.004_005_006 +01))
Expand All @@ -550,11 +545,10 @@ fn replace_second() -> Result<()> {
.replace_second(60)
.is_err()
); // 60 isn't a valid second
Ok(())
}

#[test]
fn replace_millisecond() -> Result<()> {
fn replace_millisecond() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006 +01).replace_millisecond(7),
Ok(datetime!(2022 - 02 - 18 01:02:03.007 +01))
Expand All @@ -564,11 +558,10 @@ fn replace_millisecond() -> Result<()> {
.replace_millisecond(1_000)
.is_err()
); // 1_000 isn't a valid millisecond
Ok(())
}

#[test]
fn replace_microsecond() -> Result<()> {
fn replace_microsecond() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006 +01).replace_microsecond(7_008),
Ok(datetime!(2022 - 02 - 18 01:02:03.007_008 +01))
Expand All @@ -578,11 +571,10 @@ fn replace_microsecond() -> Result<()> {
.replace_microsecond(1_000_000)
.is_err()
); // 1_000_000 isn't a valid microsecond
Ok(())
}

#[test]
fn replace_nanosecond() -> Result<()> {
fn replace_nanosecond() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006 +01).replace_nanosecond(7_008_009),
Ok(datetime!(2022 - 02 - 18 01:02:03.007_008_009 +01))
Expand All @@ -592,7 +584,6 @@ fn replace_nanosecond() -> Result<()> {
.replace_nanosecond(1_000_000_000)
.is_err()
); // 1_000_000_000 isn't a valid nanosecond
Ok(())
}

#[test]
Expand Down
29 changes: 10 additions & 19 deletions tests/primitive_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::Ordering;

use time::ext::{NumericalDuration, NumericalStdDuration};
use time::macros::{date, datetime, offset, time};
use time::{Duration, Month, PrimitiveDateTime, Result, Weekday};
use time::{Duration, Month, PrimitiveDateTime, Weekday};

#[test]
fn new() {
Expand Down Expand Up @@ -248,7 +248,7 @@ fn replace_date() {
}

#[test]
fn replace_year() -> Result<()> {
fn replace_year() {
assert_eq!(
datetime!(2022 - 02 - 18 12:00).replace_year(2019),
Ok(datetime!(2019 - 02 - 18 12:00))
Expand All @@ -263,11 +263,10 @@ fn replace_year() -> Result<()> {
.replace_year(1_000_000_000)
.is_err()
); // 1_000_000_000 isn't a valid year
Ok(())
}

#[test]
fn replace_month() -> Result<()> {
fn replace_month() {
assert_eq!(
datetime!(2022 - 02 - 18 12:00).replace_month(Month::January),
Ok(datetime!(2022 - 01 - 18 12:00))
Expand All @@ -277,18 +276,16 @@ fn replace_month() -> Result<()> {
.replace_month(Month::February)
.is_err()
); // 30 isn't a valid day in February
Ok(())
}

#[test]
fn replace_day() -> Result<()> {
fn replace_day() {
assert_eq!(
datetime!(2022 - 02 - 18 12:00).replace_day(1),
Ok(datetime!(2022 - 02 - 01 12:00))
);
assert!(datetime!(2022 - 02 - 18 12:00).replace_day(0).is_err()); // 00 isn't a valid day
assert!(datetime!(2022 - 02 - 18 12:00).replace_day(30).is_err()); // 30 isn't a valid day in February
Ok(())
}

#[test]
Expand All @@ -307,7 +304,7 @@ fn replace_ordinal() {
}

#[test]
fn replace_hour() -> Result<()> {
fn replace_hour() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006).replace_hour(7),
Ok(datetime!(2022 - 02 - 18 07:02:03.004_005_006))
Expand All @@ -317,11 +314,10 @@ fn replace_hour() -> Result<()> {
.replace_hour(24)
.is_err()
); // 24 isn't a valid hour
Ok(())
}

#[test]
fn replace_minute() -> Result<()> {
fn replace_minute() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006).replace_minute(7),
Ok(datetime!(2022 - 02 - 18 01:07:03.004_005_006))
Expand All @@ -331,11 +327,10 @@ fn replace_minute() -> Result<()> {
.replace_minute(60)
.is_err()
); // 60 isn't a valid minute
Ok(())
}

#[test]
fn replace_second() -> Result<()> {
fn replace_second() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006).replace_second(7),
Ok(datetime!(2022 - 02 - 18 01:02:07.004_005_006))
Expand All @@ -345,11 +340,10 @@ fn replace_second() -> Result<()> {
.replace_second(60)
.is_err()
); // 60 isn't a valid second
Ok(())
}

#[test]
fn replace_millisecond() -> Result<()> {
fn replace_millisecond() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006).replace_millisecond(7),
Ok(datetime!(2022 - 02 - 18 01:02:03.007))
Expand All @@ -359,11 +353,10 @@ fn replace_millisecond() -> Result<()> {
.replace_millisecond(1_000)
.is_err()
); // 1_000 isn't a valid millisecond
Ok(())
}

#[test]
fn replace_microsecond() -> Result<()> {
fn replace_microsecond() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006).replace_microsecond(7_008),
Ok(datetime!(2022 - 02 - 18 01:02:03.007_008))
Expand All @@ -373,11 +366,10 @@ fn replace_microsecond() -> Result<()> {
.replace_microsecond(1_000_000)
.is_err()
); // 1_000_000 isn't a valid microsecond
Ok(())
}

#[test]
fn replace_nanosecond() -> Result<()> {
fn replace_nanosecond() {
assert_eq!(
datetime!(2022 - 02 - 18 01:02:03.004_005_006).replace_nanosecond(7_008_009),
Ok(datetime!(2022 - 02 - 18 01:02:03.007_008_009))
Expand All @@ -387,7 +379,6 @@ fn replace_nanosecond() -> Result<()> {
.replace_nanosecond(1_000_000_000)
.is_err()
); // 1_000_000_000 isn't a valid nanosecond
Ok(())
}

#[test]
Expand Down
42 changes: 18 additions & 24 deletions tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,47 +147,43 @@ fn nanosecond() -> Result<()> {
}

#[test]
fn replace_hour() -> Result<()> {
fn replace_hour() {
assert_eq!(
time!(1:02:03.004_005_006).replace_hour(7)?,
time!(7:02:03.004_005_006)
time!(1:02:03.004_005_006).replace_hour(7),
Ok(time!(7:02:03.004_005_006))
);
assert!(time!(1:02:03.004_005_006).replace_hour(24).is_err());
Ok(())
}

#[test]
fn replace_minute() -> Result<()> {
fn replace_minute() {
assert_eq!(
time!(1:02:03.004_005_006).replace_minute(7)?,
time!(1:07:03.004_005_006)
time!(1:02:03.004_005_006).replace_minute(7),
Ok(time!(1:07:03.004_005_006))
);
assert!(time!(1:02:03.004_005_006).replace_minute(60).is_err());
Ok(())
}

#[test]
fn replace_second() -> Result<()> {
fn replace_second() {
assert_eq!(
time!(1:02:03.004_005_006).replace_second(7)?,
time!(1:02:07.004_005_006)
time!(1:02:03.004_005_006).replace_second(7),
Ok(time!(1:02:07.004_005_006))
);
assert!(time!(1:02:03.004_005_006).replace_second(60).is_err());
Ok(())
}

#[test]
fn replace_millisecond() -> Result<()> {
fn replace_millisecond() {
assert_eq!(
time!(1:02:03.004_005_006).replace_millisecond(7)?,
time!(1:02:03.007)
time!(1:02:03.004_005_006).replace_millisecond(7),
Ok(time!(1:02:03.007))
);
assert!(
time!(1:02:03.004_005_006)
.replace_millisecond(1_000)
.is_err()
);
Ok(())
}

#[test]
Expand All @@ -198,31 +194,29 @@ fn replace_millisecond_regression() {
}

#[test]
fn replace_microsecond() -> Result<()> {
fn replace_microsecond() {
assert_eq!(
time!(1:02:03.004_005_006).replace_microsecond(7_008)?,
time!(1:02:03.007_008)
time!(1:02:03.004_005_006).replace_microsecond(7_008),
Ok(time!(1:02:03.007_008))
);
assert!(
time!(1:02:03.004_005_006)
.replace_microsecond(1_000_000)
.is_err()
);
Ok(())
}

#[test]
fn replace_nanosecond() -> Result<()> {
fn replace_nanosecond() {
assert_eq!(
time!(1:02:03.004_005_006).replace_nanosecond(7_008_009)?,
time!(1:02:03.007_008_009)
time!(1:02:03.004_005_006).replace_nanosecond(7_008_009),
Ok(time!(1:02:03.007_008_009))
);
assert!(
time!(1:02:03.004_005_006)
.replace_nanosecond(1_000_000_000)
.is_err()
);
Ok(())
}

#[test]
Expand Down

0 comments on commit b9a2569

Please sign in to comment.