Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CA1822;CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget;xUnit1051;NU1608;NU1109</NoWarn>
<Version>31.13.0</Version>
<Version>31.13.1</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
3 changes: 3 additions & 0 deletions src/Verify.Tests/XmlTests.DateOnlyScrubbing.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<root>
<date>Date_1</date>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<node att="Date_1">text</node>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<root>
<date>{Scrubbed}</date>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<root>
<a>Date_1</a>
<b>Date_2</b>
<c>Date_1</c>
</root>
30 changes: 30 additions & 0 deletions src/Verify.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,36 @@ public Task XDocScrubMember() =>
Verify(XDocument.Parse(xml))
.ScrubMember("node");

[Fact]
public Task DateOnlyScrubbing()
{
var date = DateTime.Now.ToString("d");
return VerifyXml($"<root><date>{date}</date></root>");
}

[Fact]
public Task DateOnlyScrubbingDisableCounting()
{
var date = DateTime.Now.ToString("d");
return VerifyXml($"<root><date>{date}</date></root>")
.DisableDateCounting();
}

[Fact]
public Task DateOnlyScrubbingAttribute()
{
var date = DateTime.Now.ToString("d");
return VerifyXml($"""<node att="{date}">text</node>""");
}

[Fact]
public Task DateOnlyScrubbingMultiple()
{
var date1 = new DateTime(2024, 1, 15).ToString("d");
var date2 = new DateTime(2024, 6, 20).ToString("d");
return VerifyXml($"<root><a>{date1}</a><b>{date2}</b><c>{date1}</c></root>");
}

[Fact]
public Task EmptyTag() =>
VerifyXml(
Expand Down
42 changes: 40 additions & 2 deletions src/Verify/Counter_Date.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER
namespace VerifyTests;

public partial class Counter
Expand Down Expand Up @@ -46,4 +46,42 @@ public string NextString(Date input) =>
return (value, "{Scrubbed}");
}
}
#endif
#else
namespace VerifyTests;

public partial class Counter
{
Dictionary<DateTime, (int intValue, string stringValue)> dateCache = new(dateTimeComparer);
int currentDate;

(int intValue, string stringValue) BuildDateValue()
{
var value = Interlocked.Increment(ref currentDate);

if (DateCounting)
{
return (value, $"Date_{value}");
}

return (value, "{Scrubbed}");
}

internal string ConvertDate(DateTime date)
{
if (date.Date == DateTime.MaxValue.Date)
{
return "Date_MaxValue";
}

if (date.Date == DateTime.MinValue.Date)
{
return "Date_MinValue";
}

return dateCache.GetOrAdd(
date,
_ => BuildDateValue())
.stringValue;
}
}
#endif
6 changes: 3 additions & 3 deletions src/Verify/Serialization/Scrubbers/SharedScrubber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ public bool TryConvert(CharSpan value, [NotNullWhen(true)] out string? result)
{
if (TryConvertGuid(value, out result) ||
TryConvertDateTimeOffset(value, out result) ||
TryConvertDateTime(value, out result))
TryConvertDateTime(value, out result) ||
TryConvertDate(value, out result))
{
return true;
}

#if NET6_0_OR_GREATER
if (TryConvertDate(value, out result) ||
TryConvertTime(value, out result))
if (TryConvertTime(value, out result))
{
return true;
}
Expand Down
20 changes: 19 additions & 1 deletion src/Verify/Serialization/Scrubbers/SharedScrubber_Dates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

partial class Counter
{
#if NET6_0_OR_GREATER
internal static List<string> dateFormats = ["d"];
#if NET6_0_OR_GREATER
internal static List<string> timeFormats = ["h:mm tt"];
#endif
internal static List<string> dateTimeFormats = [];
Expand Down Expand Up @@ -112,6 +112,24 @@ string Convert(Time time)
return NextString(time);
}

#else
public bool TryConvertDate(CharSpan value, [NotNullWhen(true)] out string? result)
{
if (ScrubDateTimes)
{
foreach (var format in dateFormats)
{
if (DateTime.TryParseExact(value, format, null, DateTimeStyles.None, out var date))
{
result = ConvertDate(date);
return true;
}
}
}

result = null;
return false;
}
#endif

public bool TryConvert(DateTimeOffset value, [NotNullWhen(true)] out string? result)
Expand Down