From 6cb5c44cef1ac988fc8db70975c2882b95c4a55e Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 21 Sep 2024 22:55:11 +1000 Subject: [PATCH] . (#1299) --- docs/dates.md | 134 +++++++++++++++++- docs/mdsource/dates.source.md | 41 +++++- .../CustomDateCompareTests.Run.verified.txt | 8 ++ .../CustomDateCompareTests.cs | 107 ++++++++++++++ src/Verify/Counter_DateTime.cs | 13 +- src/Verify/Counter_DateTimeOffset.cs | 10 +- src/Verify/Counter_Time.cs | 16 ++- 7 files changed, 322 insertions(+), 7 deletions(-) create mode 100644 src/StaticSettingsTests/CustomDateCompareTests.Run.verified.txt create mode 100644 src/StaticSettingsTests/CustomDateCompareTests.cs diff --git a/docs/dates.md b/docs/dates.md index 0c3d976e2c..fcbc5b234b 100644 --- a/docs/dates.md +++ b/docs/dates.md @@ -7,7 +7,6 @@ To change this file edit the source file and then run MarkdownSnippets. # Dates - By default dates and times (`DateTime`, `DateTimeOffset`, `DateOnly`, and `TimeOnly`) are sanitized during verification. This is done by finding each date and taking a counter based that that specific date. That counter is then used replace the date values. This allows for repeatable tests when date values are changing. @@ -288,3 +287,136 @@ public static void NamedDatesAndTimesGlobal() ``` snippet source | anchor + + +## Custom Comparers + +The following comparers can be overridden + + +### DateTime + +Default Comparer: + + + +```cs +class DateTimeComparer : IEqualityComparer +{ + public bool Equals(DateTime x, DateTime y) => + x == y && + x.Kind == y.Kind; + + public int GetHashCode(DateTime obj) => + obj.GetHashCode() + (int) obj.Kind; +} +``` +snippet source | anchor + + +Custom Comparer: + + + +```cs +[ModuleInitializer] +public static void UseCustomDateTimeComparer() => + Counter.UseDateTimeComparer(new CustomDateTimeComparer()); + +public class CustomDateTimeComparer : + IEqualityComparer +{ + public bool Equals(DateTime x, DateTime y) => + new DateTime(x.Year, x.Month, x.Day) == + new DateTime(y.Year, y.Month, y.Day); + + public int GetHashCode(DateTime date) => + new DateTime(date.Year, date.Month, date.Day).GetHashCode(); +} +``` +snippet source | anchor + + + +### DateTimeOffset + +Default Comparer: + + + +```cs +class DateTimeOffsetComparer : + IEqualityComparer +{ + public bool Equals(DateTimeOffset x, DateTimeOffset y) => + x == y && x.Offset == y.Offset; + + public int GetHashCode(DateTimeOffset obj) => + obj.GetHashCode() + (int) obj.Offset.TotalMinutes; +} +``` +snippet source | anchor + + +Custom Comparer: + + + +```cs +[ModuleInitializer] +public static void UseCustomDateTimeOffsetComparer() => + Counter.UseDateTimeOffsetComparer(new CustomDateTimeOffsetComparer()); + +public class CustomDateTimeOffsetComparer : + IEqualityComparer +{ + public bool Equals(DateTimeOffset x, DateTimeOffset y) => + new DateTimeOffset(new(x.Year, x.Month, x.Day)) == + new DateTimeOffset(new(y.Year, y.Month, y.Day)); + + public int GetHashCode(DateTimeOffset date) + { + var dateTime = new DateTime(date.Year, date.Month, date.Day); + return new DateTimeOffset(dateTime) + .GetHashCode(); + } +} +``` +snippet source | anchor + + + +### TimeOnly + +Default Comparer: + + + +```cs +EqualityComparer