Skip to content

Commit

Permalink
. (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Sep 21, 2024
1 parent bdfe12a commit 6cb5c44
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 7 deletions.
134 changes: 133 additions & 1 deletion docs/dates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- snippet: Date -->
Expand Down Expand Up @@ -288,3 +287,136 @@ public static void NamedDatesAndTimesGlobal()
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1102-L1113' title='Snippet source file'>snippet source</a> | <a href='#snippet-NamedDatesAndTimesGlobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## Custom Comparers

The following comparers can be overridden


### DateTime

Default Comparer:

<!-- snippet: DateTimeComparer -->
<a id='snippet-DateTimeComparer'></a>
```cs
class DateTimeComparer : IEqualityComparer<DateTime>
{
public bool Equals(DateTime x, DateTime y) =>
x == y &&
x.Kind == y.Kind;

public int GetHashCode(DateTime obj) =>
obj.GetHashCode() + (int) obj.Kind;
}
```
<sup><a href='/src/Verify/Counter_DateTime.cs#L15-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-DateTimeComparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Custom Comparer:

<!-- snippet: CustomDateTimeComparer -->
<a id='snippet-CustomDateTimeComparer'></a>
```cs
[ModuleInitializer]
public static void UseCustomDateTimeComparer() =>
Counter.UseDateTimeComparer(new CustomDateTimeComparer());

public class CustomDateTimeComparer :
IEqualityComparer<DateTime>
{
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();
}
```
<sup><a href='/src/StaticSettingsTests/CustomDateCompareTests.cs#L4-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDateTimeComparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


### DateTimeOffset

Default Comparer:

<!-- snippet: DateTimeOffsetComparer -->
<a id='snippet-DateTimeOffsetComparer'></a>
```cs
class DateTimeOffsetComparer :
IEqualityComparer<DateTimeOffset>
{
public bool Equals(DateTimeOffset x, DateTimeOffset y) =>
x == y && x.Offset == y.Offset;

public int GetHashCode(DateTimeOffset obj) =>
obj.GetHashCode() + (int) obj.Offset.TotalMinutes;
}
```
<sup><a href='/src/Verify/Counter_DateTimeOffset.cs#L15-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-DateTimeOffsetComparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Custom Comparer:

<!-- snippet: CustomDateTimeOffsetComparer -->
<a id='snippet-CustomDateTimeOffsetComparer'></a>
```cs
[ModuleInitializer]
public static void UseCustomDateTimeOffsetComparer() =>
Counter.UseDateTimeOffsetComparer(new CustomDateTimeOffsetComparer());

public class CustomDateTimeOffsetComparer :
IEqualityComparer<DateTimeOffset>
{
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();
}
}
```
<sup><a href='/src/StaticSettingsTests/CustomDateCompareTests.cs#L42-L63' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDateTimeOffsetComparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


### TimeOnly

Default Comparer:

<!-- snippet: TimeComparer -->
<a id='snippet-TimeComparer'></a>
```cs
EqualityComparer<Time>.Default;
```
<sup><a href='/src/Verify/Counter_Time.cs#L14-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-TimeComparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Custom Comparer:

<!-- snippet: CustomTimeComparer -->
<a id='snippet-CustomTimeComparer'></a>
```cs
[ModuleInitializer]
public static void UseCustomTimeComparer() =>
Counter.UseTimeComparer(new CustomTimeComparer());

public class CustomTimeComparer :
IEqualityComparer<Time>
{
public bool Equals(Time x, Time y) =>
new Time(x.Hour, x.Minute, x.Second) ==
new Time(y.Hour, y.Minute, y.Second);

public int GetHashCode(Time date) =>
new Time(date.Hour, date.Minute, date.Second).GetHashCode();
}
```
<sup><a href='/src/StaticSettingsTests/CustomDateCompareTests.cs#L23-L40' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomTimeComparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
41 changes: 39 additions & 2 deletions docs/mdsource/dates.source.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# 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.

snippet: Date
Expand Down Expand Up @@ -91,4 +90,42 @@ snippet: NamedDatesAndTimesFluent

### Globally

snippet: NamedDatesAndTimesGlobal
snippet: NamedDatesAndTimesGlobal


## Custom Comparers

The following comparers can be overridden


### DateTime

Default Comparer:

snippet: DateTimeComparer

Custom Comparer:

snippet: CustomDateTimeComparer


### DateTimeOffset

Default Comparer:

snippet: DateTimeOffsetComparer

Custom Comparer:

snippet: CustomDateTimeOffsetComparer


### TimeOnly

Default Comparer:

snippet: TimeComparer

Custom Comparer:

snippet: CustomTimeComparer
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
Time1: Time_1,
Time2: Time_1,
DateTime1: DateTime_1,
DateTime2: DateTime_1,
DateTimeOffset1: DateTimeOffset_1,
DateTimeOffset2: DateTimeOffset_1
}
107 changes: 107 additions & 0 deletions src/StaticSettingsTests/CustomDateCompareTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
public class CustomDateCompareTests :
BaseTest
{
#region CustomDateTimeComparer

[ModuleInitializer]
public static void UseCustomDateTimeComparer() =>
Counter.UseDateTimeComparer(new CustomDateTimeComparer());

public class CustomDateTimeComparer :
IEqualityComparer<DateTime>
{
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();
}

#endregion

#region CustomTimeComparer

[ModuleInitializer]
public static void UseCustomTimeComparer() =>
Counter.UseTimeComparer(new CustomTimeComparer());

public class CustomTimeComparer :
IEqualityComparer<Time>
{
public bool Equals(Time x, Time y) =>
new Time(x.Hour, x.Minute, x.Second) ==
new Time(y.Hour, y.Minute, y.Second);

public int GetHashCode(Time date) =>
new Time(date.Hour, date.Minute, date.Second).GetHashCode();
}

#endregion

#region CustomDateTimeOffsetComparer

[ModuleInitializer]
public static void UseCustomDateTimeOffsetComparer() =>
Counter.UseDateTimeOffsetComparer(new CustomDateTimeOffsetComparer());

public class CustomDateTimeOffsetComparer :
IEqualityComparer<DateTimeOffset>
{
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();
}
}

#endregion

public CustomDateCompareTests()
{
Counter.UseDateTimeComparer(new Compare());
Counter.UseDateTimeOffsetComparer(new Compare());
Counter.UseTimeComparer(new Compare());
}

[Fact]
public Task Run() =>
Verify(new
{
Time1 = new Time(1, 2),
Time2 = new Time(3, 4),
DateTime1 = DateTime.Now,
DateTime2 = DateTime.Now.AddDays(2),
DateTimeOffset1 = new DateTimeOffset(DateTime.Now),
DateTimeOffset2 = new DateTimeOffset(DateTime.Now.AddDays(2)),
});

public class Compare :
IEqualityComparer<DateTime>,
IEqualityComparer<DateTimeOffset>,
IEqualityComparer<Time>
{
public bool Equals(DateTime x, DateTime y) =>
true;

public int GetHashCode(DateTime obj) =>
0;

public bool Equals(DateTimeOffset x, DateTimeOffset y) =>
true;

public int GetHashCode(DateTimeOffset obj) =>
0;

public bool Equals(Time x, Time y) =>
true;

public int GetHashCode(Time obj) =>
0;
}
}
13 changes: 11 additions & 2 deletions src/Verify/Counter_DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

public partial class Counter
{
static DateTimeComparer dateTimeComparer = new();
public static void UseDateTimeComparer(IEqualityComparer<DateTime> comparer)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
dateTimeComparer = comparer;
}

static IEqualityComparer<DateTime> dateTimeComparer = new DateTimeComparer();
Dictionary<DateTime, (int intValue, string stringValue)> dateTimeCache = new(dateTimeComparer);
static Dictionary<DateTime, string> globalNamedDateTimes = [];

#region DateTimeComparer
class DateTimeComparer : IEqualityComparer<DateTime>
{
public bool Equals(DateTime x, DateTime y) =>
x == y && x.Kind == y.Kind;
x == y &&
x.Kind == y.Kind;

public int GetHashCode(DateTime obj) =>
obj.GetHashCode() + (int) obj.Kind;
}
#endregion

int currentDateTime;

Expand Down
10 changes: 9 additions & 1 deletion src/Verify/Counter_DateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

public partial class Counter
{
static DateTimeOffsetComparer dateTimeOffsetComparer = new();
public static void UseDateTimeOffsetComparer(IEqualityComparer<DateTimeOffset> comparer)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
dateTimeOffsetComparer = comparer;
}

static IEqualityComparer<DateTimeOffset> dateTimeOffsetComparer = new DateTimeOffsetComparer();
Dictionary<DateTimeOffset, (int intValue, string stringValue)> dateTimeOffsetCache = new(dateTimeOffsetComparer);
static Dictionary<DateTimeOffset, string> globalNamedDateTimeOffsets = [];

#region DateTimeOffsetComparer
class DateTimeOffsetComparer :
IEqualityComparer<DateTimeOffset>
{
Expand All @@ -15,6 +22,7 @@ public bool Equals(DateTimeOffset x, DateTimeOffset y) =>
public int GetHashCode(DateTimeOffset obj) =>
obj.GetHashCode() + (int) obj.Offset.TotalMinutes;
}
#endregion

int currentDateTimeOffset;

Expand Down
16 changes: 15 additions & 1 deletion src/Verify/Counter_Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ namespace VerifyTests;

public partial class Counter
{
Dictionary<Time, (int intValue, string stringValue)> timeCache = [];
public static void UseTimeComparer(IEqualityComparer<Time> comparer)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
timeComparer = comparer;
}

static IEqualityComparer<Time> timeComparer =

#region TimeComparer

EqualityComparer<Time>.Default;

#endregion

Dictionary<Time, (int intValue, string stringValue)> timeCache = new(timeComparer);
static Dictionary<Time, string> globalNamedTimes = [];
int currentTime;

Expand Down

0 comments on commit 6cb5c44

Please sign in to comment.