Skip to content

Commit

Permalink
Add more DateTime.into tests
Browse files Browse the repository at this point in the history
More tests for combinations of `DateTime::into`.

Follow-up to Pull Request chronotope#271.
  • Loading branch information
jtmoon79 committed Sep 21, 2023
1 parent bc9d070 commit af4d8a4
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,61 @@ where
}
}

#[test]
fn test_add_sub_months() {
let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
assert_eq!(utc_dt + Months::new(15), Utc.with_ymd_and_hms(2019, 12, 5, 23, 58, 0).unwrap());

let utc_dt = Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap();
assert_eq!(utc_dt + Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
assert_eq!(utc_dt + Months::new(2), Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap());

let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
assert_eq!(utc_dt - Months::new(15), Utc.with_ymd_and_hms(2017, 6, 5, 23, 58, 0).unwrap());

let utc_dt = Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap();
assert_eq!(utc_dt - Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
assert_eq!(utc_dt - Months::new(2), Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap());
}

#[test]
fn test_auto_conversion_fixedoffset_into_utc() {
let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
let cdt_dt = FixedOffset::west_opt(5 * 60 * 60)
.unwrap()
.with_ymd_and_hms(2018, 9, 5, 18, 58, 0)
.unwrap();
let utc_dt2: DateTime<Utc> = cdt_dt.into();
assert_eq!(utc_dt, utc_dt2);
}

#[test]
fn test_auto_conversion_utc_into_fixedoffset() {
let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
let cdt_dt = FixedOffset::west_opt(5 * 60 * 60)
.unwrap()
.with_ymd_and_hms(2018, 9, 5, 18, 58, 0)
.unwrap();
let cdt_dt2: DateTime<FixedOffset> = utc_dt.into();
assert_eq!(cdt_dt, cdt_dt2);
}

#[cfg(feature = "clock")]
#[test]
fn test_auto_conversion_local_into_utc() {
let loc_dt = Local.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).unwrap();
let utc_dt: DateTime<Utc> = loc_dt.into();
assert_eq!(utc_dt, loc_dt);
}

#[cfg(feature = "clock")]
#[test]
fn test_auto_conversion_utc_into_local() {
let utc_dt = Utc.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).unwrap();
let loc_dt: DateTime<Local> = utc_dt.into();
assert_eq!(utc_dt, loc_dt);
}

#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
fn test_encodable_json<FUtc, FFixed, E>(to_string_utc: FUtc, to_string_fixed: FFixed)
where
Expand Down

0 comments on commit af4d8a4

Please sign in to comment.