From ac17b62a5c9d63606e9c161c1533cdbebf2de977 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Fri, 22 Nov 2024 22:53:06 -0500 Subject: [PATCH] Test 12-week increments from 2 to 50 The idea here is to make it so that: 1. If we are using local time, in a time zone with DST adjustments, then no matter when we run the test, at least one "X weeks ago" case will have exactly one DST adjustment between then and now. (Thus, if our local time can observe #1696, it will.) 2. It is easy to verify that this is so. The rules are somewhat variable, and adjustments cannot be approximated as happening every half-year: - In multiple time zones with DST-related adjustments, an adjustment and its reversal can be separated by only four months (November to March). - Countries may institute rules that may be hard for people not familiar with them to anticipate. For example, Morocco used to have DST-related adjustments; when it did, if the (lunar) month of Ramadan occurred during the usual (Gregorian-based) DST period, standard time (rather than DST) was in effect during Ramadan. Thus, in some years, there were four DST-related clock adjustments. It is hard to predict (or, at least, I do not know how to predict) if, or what kind of, new DST adjustments may be put in place somewhere. With that said, the current test probably has more intervals than necessary. Furthermore, if it turns out to be feasible, it may be better to make this a true unit test by using a set time instad of `now` to take "X weeks ago" times relative to, and by somehow specifying or mocking out the time zone, so as to test the tricky cases in exactly the same way no matter where, when, or in what local configuration the test is run. Failure, when it occurs, now looks like: --- STDERR: gix-date::date time::parse::relative::various --- thread 'time::parse::relative::various' panicked at gix-date\tests\time\parse.rs:216:9: assertion failed: `(left == right)`: relative times differ Diff < left / right > : [ 2024-11-09T04:22:21Z, < 2024-08-17T04:22:21Z, < 2024-05-25T04:22:21Z, > 2024-08-17T03:22:21Z, > 2024-05-25T03:22:21Z, 2024-03-02T04:22:21Z, 2023-12-09T04:22:21Z, 2024-11-09T04:22:21Z, 2024-08-17T04:22:21Z, 2024-05-25T04:22:21Z, 2024-03-02T04:22:21Z, 2023-12-09T04:22:21Z, ] Equivalent cases with weeks and minutes were interleaved before, but now all the "weeks" cases are listed before all the "minutes" cases, because that produces more readable output when `pretty_assertions::assert_eq!` is used. --- Cargo.lock | 1 + gix-date/Cargo.toml | 3 ++- gix-date/tests/time/parse.rs | 15 ++++++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dcfc0729ce5..94f8ed1885a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1690,6 +1690,7 @@ dependencies = [ "itoa", "jiff", "once_cell", + "pretty_assertions", "serde", "thiserror 2.0.3", ] diff --git a/gix-date/Cargo.toml b/gix-date/Cargo.toml index 2d911059938..94c98dbcf85 100644 --- a/gix-date/Cargo.toml +++ b/gix-date/Cargo.toml @@ -28,9 +28,10 @@ thiserror = "2.0.0" document-features = { version = "0.2.0", optional = true } [dev-dependencies] +gix-hash = { path = "../gix-hash" } gix-testtools = { path = "../tests/tools" } once_cell = "1.12.0" -gix-hash = { path = "../gix-hash" } +pretty_assertions = "1.4.1" [package.metadata.docs.rs] all-features = true diff --git a/gix-date/tests/time/parse.rs b/gix-date/tests/time/parse.rs index 69a273f5235..bee1ac4c856 100644 --- a/gix-date/tests/time/parse.rs +++ b/gix-date/tests/time/parse.rs @@ -156,6 +156,7 @@ mod relative { use gix_date::time::Sign; use jiff::{ToSpan, Zoned}; + use pretty_assertions::assert_eq; #[test] fn large_offsets() { @@ -182,11 +183,15 @@ mod relative { let cases = [ ("2 weeks ago", 2.weeks()), - ("20160 minutes ago", 20_160.minutes()), // 2 weeks - ("20 weeks ago", 20.weeks()), - ("201600 minutes ago", 201_600.minutes()), // 20 weeks - ("40 weeks ago", 40.weeks()), - ("403200 minutes ago", 403_200.minutes()), // 40 weeks + ("14 weeks ago", 14.weeks()), + ("26 weeks ago", 26.weeks()), + ("38 weeks ago", 38.weeks()), + ("50 weeks ago", 50.weeks()), + ("20160 minutes ago", 20_160.minutes()), // 2 weeks + ("141120 minutes ago", 141_120.minutes()), // 14 weeks + ("262080 minutes ago", 262_080.minutes()), // 26 weeks + ("383040 minutes ago", 383_040.minutes()), // 38 weeks + ("504000 minutes ago", 504_000.minutes()), // 50 weeks ]; let times = cases.map(|(input, _)| gix_date::parse(input, Some(now)).unwrap());