From dbb83ac8a22398036459e86ef36a84a4569befca Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 28 Dec 2023 07:49:08 +1100 Subject: [PATCH 1/2] . --- docs/dates.md | 2 +- docs/guids.md | 2 +- docs/named-tuples.md | 4 +- docs/scrubbers.md | 12 +-- docs/serializer-settings.md | 52 ++++----- ...sts.DisableDateCountingInline.verified.txt | 1 + ...sts.DisableDateCountingInline.verified.txt | 1 + .../Serialization/SerializationTests.cs | 8 +- src/Verify/Counter.cs | 5 + src/Verify/Counter_Date.cs | 18 +++- src/Verify/Counter_DateTime.cs | 18 +++- src/Verify/Counter_DateTimeOffset.cs | 18 +++- .../Scrubbers/SharedScrubber_Dates.cs | 100 ++---------------- .../Serialization/SerializationSettings.cs | 6 -- src/Verify/Serialization/VerifySettings.cs | 19 ++++ .../VerifySettings_SerializationMaps.cs | 6 -- src/Verify/Verifier/InnerVerifier.cs | 1 + src/Verify/VerifySettings.cs | 1 + 18 files changed, 120 insertions(+), 154 deletions(-) create mode 100644 src/StrictJsonTests/SerializationTests.DisableDateCountingInline.verified.txt create mode 100644 src/Verify.Tests/Serialization/SerializationTests.DisableDateCountingInline.verified.txt diff --git a/docs/dates.md b/docs/dates.md index a72f938a4b..7d39890031 100644 --- a/docs/dates.md +++ b/docs/dates.md @@ -101,7 +101,7 @@ return Verify(target) public static void ModuleInitializer() => VerifierSettings.DontScrubDateTimes(); ``` -snippet source | anchor +snippet source | anchor diff --git a/docs/guids.md b/docs/guids.md index ed0648fba7..5072e96f6c 100644 --- a/docs/guids.md +++ b/docs/guids.md @@ -23,7 +23,7 @@ var target = new GuidTarget await Verify(target); ``` -snippet source | anchor +snippet source | anchor Results in the following: diff --git a/docs/named-tuples.md b/docs/named-tuples.md index e50ccebcaf..feac0bd79e 100644 --- a/docs/named-tuples.md +++ b/docs/named-tuples.md @@ -19,7 +19,7 @@ Given a method that returns a named tuple: static (bool Member1, string Member2, string Member3) MethodWithNamedTuple() => (true, "A", "B"); ``` -snippet source | anchor +snippet source | anchor Can be verified: @@ -29,7 +29,7 @@ Can be verified: ```cs await VerifyTuple(() => MethodWithNamedTuple()); ``` -snippet source | anchor +snippet source | anchor Resulting in: diff --git a/docs/scrubbers.md b/docs/scrubbers.md index 51e6867638..0caf2c7106 100644 --- a/docs/scrubbers.md +++ b/docs/scrubbers.md @@ -58,7 +58,7 @@ For example remove lines containing `text`: ```cs verifySettings.ScrubLines(line => line.Contains("text")); ``` -snippet source | anchor +snippet source | anchor @@ -73,7 +73,7 @@ For example remove lines containing `text1` or `text2` ```cs verifySettings.ScrubLinesContaining("text1", "text2"); ``` -snippet source | anchor +snippet source | anchor Case insensitive by default (StringComparison.OrdinalIgnoreCase). @@ -85,7 +85,7 @@ Case insensitive by default (StringComparison.OrdinalIgnoreCase). ```cs verifySettings.ScrubLinesContaining(StringComparison.Ordinal, "text1", "text2"); ``` -snippet source | anchor +snippet source | anchor @@ -100,7 +100,7 @@ For example converts lines to upper case: ```cs verifySettings.ScrubLinesWithReplace(line => line.ToUpper()); ``` -snippet source | anchor +snippet source | anchor @@ -113,7 +113,7 @@ Replaces `Environment.MachineName` with `TheMachineName`. ```cs verifySettings.ScrubMachineName(); ``` -snippet source | anchor +snippet source | anchor @@ -126,7 +126,7 @@ Replaces `Environment.UserName` with `TheUserName`. ```cs verifySettings.ScrubUserName(); ``` -snippet source | anchor +snippet source | anchor diff --git a/docs/serializer-settings.md b/docs/serializer-settings.md index 300531dd36..c90ab9b29c 100644 --- a/docs/serializer-settings.md +++ b/docs/serializer-settings.md @@ -135,7 +135,7 @@ var settings = new JsonSerializerSettings DefaultValueHandling = DefaultValueHandling.Ignore }; ``` -snippet source | anchor +snippet source | anchor @@ -308,7 +308,7 @@ public Task ScopedSerializerFluent() .AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -436,7 +436,7 @@ public Task IgnoreTypeFluent() .IgnoreMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -446,7 +446,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -583,7 +583,7 @@ public Task ScrubTypeFluent() .ScrubMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -593,7 +593,7 @@ Or globally: ```cs VerifierSettings.ScrubMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -672,7 +672,7 @@ public Task AddIgnoreInstanceFluent() .IgnoreInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -682,7 +682,7 @@ Or globally: ```cs VerifierSettings.IgnoreInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -744,7 +744,7 @@ public Task AddScrubInstanceFluent() .ScrubInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -754,7 +754,7 @@ Or globally: ```cs VerifierSettings.ScrubInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -799,7 +799,7 @@ public Task WithObsoleteProp() return Verify(target); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -847,7 +847,7 @@ public Task WithObsoletePropIncludedFluent() .IncludeObsoletes(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -857,7 +857,7 @@ Or globally: ```cs VerifierSettings.IncludeObsoletes(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -918,7 +918,7 @@ public Task IgnoreMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -933,7 +933,7 @@ VerifierSettings.IgnoreMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -993,7 +993,7 @@ public Task ScrubMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -1008,7 +1008,7 @@ VerifierSettings.ScrubMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1087,7 +1087,7 @@ public Task IgnoreMemberByNameFluent() .IgnoreMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1107,7 +1107,7 @@ VerifierSettings.IgnoreMember("Field"); // For a specific type with expression VerifierSettings.IgnoreMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1182,7 +1182,7 @@ public Task ScrubMemberByNameFluent() .ScrubMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1202,7 +1202,7 @@ VerifierSettings.ScrubMember("Field"); // For a specific type with expression VerifierSettings.ScrubMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1253,7 +1253,7 @@ public Task CustomExceptionPropFluent() .IgnoreMembersThatThrow(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1263,7 +1263,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1300,7 +1300,7 @@ public Task ExceptionMessagePropFluent() .IgnoreMembersThatThrow(_ => _.Message == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1310,7 +1310,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(_ => _.Message == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1468,7 +1468,7 @@ public Task MemberConverterByExpression() return Verify(input); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/StrictJsonTests/SerializationTests.DisableDateCountingInline.verified.txt b/src/StrictJsonTests/SerializationTests.DisableDateCountingInline.verified.txt new file mode 100644 index 0000000000..e1b95041a7 --- /dev/null +++ b/src/StrictJsonTests/SerializationTests.DisableDateCountingInline.verified.txt @@ -0,0 +1 @@ +a {Scrubbed} b \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.DisableDateCountingInline.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.DisableDateCountingInline.verified.txt new file mode 100644 index 0000000000..e1b95041a7 --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.DisableDateCountingInline.verified.txt @@ -0,0 +1 @@ +a {Scrubbed} b \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index 86a9182502..0e9bc788da 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -1742,7 +1742,13 @@ Task DisableDateCountingFluent() #endregion } -/* + [Fact] + Task DisableDateCountingInline() => + Verify("a 2020/10/10 b") + .DisableDateCounting() + .ScrubInlineDateTimes("yyyy/MM/dd"); + + /* #region DontScrubDateTimesGlobal diff --git a/src/Verify/Counter.cs b/src/Verify/Counter.cs index 21496295ba..025c719c02 100644 --- a/src/Verify/Counter.cs +++ b/src/Verify/Counter.cs @@ -9,6 +9,7 @@ public partial class Counter Dictionary namedDateTimes; Dictionary namedGuids; Dictionary namedDateTimeOffsets; + bool dateCounting; static AsyncLocal local = new(); public static Counter Current @@ -26,6 +27,7 @@ public static Counter Current } Counter( + bool dateCounting, #if NET6_0_OR_GREATER Dictionary namedDates, Dictionary namedTimes, @@ -41,9 +43,11 @@ public static Counter Current this.namedDateTimes = namedDateTimes; this.namedGuids = namedGuids; this.namedDateTimeOffsets = namedDateTimeOffsets; + this.dateCounting = dateCounting; } internal static Counter Start( + bool dateCounting = true, #if NET6_0_OR_GREATER Dictionary? namedDates = null, Dictionary? namedTimes = null, @@ -53,6 +57,7 @@ internal static Counter Start( Dictionary? namedDateTimeOffsets = null) { var context = new Counter( + dateCounting, #if NET6_0_OR_GREATER namedDates ?? new(), namedTimes ?? new(), diff --git a/src/Verify/Counter_Date.cs b/src/Verify/Counter_Date.cs index 8daa44ef54..a3e85f3939 100644 --- a/src/Verify/Counter_Date.cs +++ b/src/Verify/Counter_Date.cs @@ -29,11 +29,19 @@ public string NextString(Date input) => return new(0, name); } - return dateCache.GetOrAdd(input, _ => - { - var value = Interlocked.Increment(ref currentDate); - return (value, $"Date_{value}"); - }); + return dateCache.GetOrAdd( + input, + _ => + { + var value = Interlocked.Increment(ref currentDate); + + if (!dateCounting) + { + return (value, "{Scrubbed}"); + } + + return (value, $"Date_{value}"); + }); } } #endif \ No newline at end of file diff --git a/src/Verify/Counter_DateTime.cs b/src/Verify/Counter_DateTime.cs index a7b651d985..8245ee2101 100644 --- a/src/Verify/Counter_DateTime.cs +++ b/src/Verify/Counter_DateTime.cs @@ -38,10 +38,18 @@ public string NextString(DateTime input) => return new(0, name); } - return dateTimeCache.GetOrAdd(input, _ => - { - var value = Interlocked.Increment(ref currentDateTime); - return (value, $"DateTime_{value}"); - }); + return dateTimeCache.GetOrAdd( + input, + _ => + { + var value = Interlocked.Increment(ref currentDateTime); + + if (!dateCounting) + { + return (value, "{Scrubbed}"); + } + + return (value, $"DateTime_{value}"); + }); } } \ No newline at end of file diff --git a/src/Verify/Counter_DateTimeOffset.cs b/src/Verify/Counter_DateTimeOffset.cs index 322b5d9b1c..2d37c5aaed 100644 --- a/src/Verify/Counter_DateTimeOffset.cs +++ b/src/Verify/Counter_DateTimeOffset.cs @@ -39,10 +39,18 @@ public string NextString(DateTimeOffset input) => return new(0, name); } - return dateTimeOffsetCache.GetOrAdd(input, _ => - { - var value = Interlocked.Increment(ref currentDateTimeOffset); - return (value, $"DateTimeOffset_{value}"); - }); + return dateTimeOffsetCache.GetOrAdd( + input, + _ => + { + var value = Interlocked.Increment(ref currentDateTimeOffset); + + if (!dateCounting) + { + return (value, "{Scrubbed}"); + } + + return (value, $"DateTimeOffset_{value}"); + }); } } \ No newline at end of file diff --git a/src/Verify/Serialization/Scrubbers/SharedScrubber_Dates.cs b/src/Verify/Serialization/Scrubbers/SharedScrubber_Dates.cs index d593440f9b..f088fd2f39 100644 --- a/src/Verify/Serialization/Scrubbers/SharedScrubber_Dates.cs +++ b/src/Verify/Serialization/Scrubbers/SharedScrubber_Dates.cs @@ -13,15 +13,7 @@ internal bool TryConvert(Counter counter, DateTime value, [NotNullWhen(true)] ou return false; } - if (dateCountingEnable) - { - result = Convert(counter, value); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, value); return true; } @@ -34,15 +26,7 @@ internal bool TryParseConvertDate(Counter counter, string value, [NotNullWhen(tr { if (Date.TryParseExact(value, format, null, DateTimeStyles.None, out var date)) { - if (dateCountingEnable) - { - result = Convert(counter, date); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, date); return true; } } @@ -60,15 +44,7 @@ internal bool TryConvert(Counter counter, Date value, [NotNullWhen(true)] out st return false; } - if (dateCountingEnable) - { - result = Convert(counter, value); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, value); return true; } @@ -95,15 +71,7 @@ internal bool TryParseConvertTime(Counter counter, string value, [NotNullWhen(tr { if (Time.TryParseExact(value, format, null, DateTimeStyles.None, out var time)) { - if (dateCountingEnable) - { - result = Convert(counter, time); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, time); return true; } } @@ -121,15 +89,7 @@ internal bool TryConvert(Counter counter, Time value, [NotNullWhen(true)] out st return false; } - if (dateCountingEnable) - { - result = Convert(counter, value); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, value); return true; } @@ -158,15 +118,7 @@ internal bool TryConvert(Counter counter, DateTimeOffset value, [NotNullWhen(tru return false; } - if (dateCountingEnable) - { - result = Convert(counter, value); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, value); return true; } @@ -206,15 +158,7 @@ internal bool TryParseConvertDateTime(Counter counter, string value, [NotNullWhe { if (DateTime.TryParseExact(value, "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", null, DateTimeStyles.None, out var dateTime)) { - if (dateCountingEnable) - { - result = Convert(counter, dateTime); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, dateTime); return true; } @@ -222,15 +166,7 @@ internal bool TryParseConvertDateTime(Counter counter, string value, [NotNullWhe { if (DateTime.TryParseExact(value, format, null, DateTimeStyles.None, out dateTime)) { - if (dateCountingEnable) - { - result = Convert(counter, dateTime); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, dateTime); return true; } } @@ -246,15 +182,7 @@ internal bool TryParseConvertDateTimeOffset(Counter counter, string value, [NotN { if (DateTimeOffset.TryParseExact(value, "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", null, DateTimeStyles.None, out var dateTimeOffset)) { - if (dateCountingEnable) - { - result = Convert(counter, dateTimeOffset); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, dateTimeOffset); return true; } @@ -262,15 +190,7 @@ internal bool TryParseConvertDateTimeOffset(Counter counter, string value, [NotN { if (DateTimeOffset.TryParseExact(value, format, null, DateTimeStyles.None, out dateTimeOffset)) { - if (dateCountingEnable) - { - result = Convert(counter, dateTimeOffset); - } - else - { - result = "{Scrubbed}"; - } - + result = Convert(counter, dateTimeOffset); return true; } } diff --git a/src/Verify/Serialization/SerializationSettings.cs b/src/Verify/Serialization/SerializationSettings.cs index a5f305cae7..8ebcd5361a 100644 --- a/src/Verify/Serialization/SerializationSettings.cs +++ b/src/Verify/Serialization/SerializationSettings.cs @@ -54,7 +54,6 @@ public SerializationSettings(SerializationSettings settings) extraSettings = settings.extraSettings.Clone(); ignoreMembersThatThrow = settings.ignoreMembersThatThrow.Clone(); ignoredTypes = settings.ignoredTypes.Clone(); - dateCountingEnable = settings.dateCountingEnable; ignoredInstances = settings.ignoredInstances .ToDictionary( _ => _.Key, @@ -77,11 +76,6 @@ public void DontScrubGuids() => public void DontScrubDateTimes() => scrubDateTimes = false; - bool dateCountingEnable = true; - - public void DisableDateCounting() => - dateCountingEnable = false; - JsonSerializerSettings BuildSettings() { #region defaultSerialization diff --git a/src/Verify/Serialization/VerifySettings.cs b/src/Verify/Serialization/VerifySettings.cs index b6ab17a334..2ac802bf1e 100644 --- a/src/Verify/Serialization/VerifySettings.cs +++ b/src/Verify/Serialization/VerifySettings.cs @@ -18,6 +18,7 @@ void CloneSettings() internal JsonSerializer Serializer => serialization.Serializer; + internal List Appends = []; /// @@ -54,4 +55,22 @@ public void AddExtraSettings(Action action) serialization.AddExtraSettings(action); } + + bool dateCountingEnable = true; + + public bool DateCountingEnable + { + get + { + if (!VerifierSettings.DateCountingEnabled) + { + return false; + } + + return dateCountingEnable; + } + } + + public void DisableDateCounting() => + dateCountingEnable = false; } \ No newline at end of file diff --git a/src/Verify/Serialization/VerifySettings_SerializationMaps.cs b/src/Verify/Serialization/VerifySettings_SerializationMaps.cs index 847519b4de..58e7f3ae1a 100644 --- a/src/Verify/Serialization/VerifySettings_SerializationMaps.cs +++ b/src/Verify/Serialization/VerifySettings_SerializationMaps.cs @@ -14,12 +14,6 @@ public void DontScrubDateTimes() serialization.DontScrubDateTimes(); } - public void DisableDateCounting() - { - CloneSettings(); - serialization.DisableDateCounting(); - } - public void DontSortDictionaries() { CloneSettings(); diff --git a/src/Verify/Verifier/InnerVerifier.cs b/src/Verify/Verifier/InnerVerifier.cs index 5758f82d65..3d8addcebe 100644 --- a/src/Verify/Verifier/InnerVerifier.cs +++ b/src/Verify/Verifier/InnerVerifier.cs @@ -96,6 +96,7 @@ public InnerVerifier(string sourceFile, VerifySettings settings) static Counter StartCounter(VerifySettings settings) => Counter.Start( + settings.DateCountingEnable, #if NET6_0_OR_GREATER settings.namedDates, settings.namedTimes, diff --git a/src/Verify/VerifySettings.cs b/src/Verify/VerifySettings.cs index 8d00239cc3..dc3442c9ae 100644 --- a/src/Verify/VerifySettings.cs +++ b/src/Verify/VerifySettings.cs @@ -9,6 +9,7 @@ public VerifySettings(VerifySettings? settings) return; } + dateCountingEnable = settings.dateCountingEnable; InstanceScrubbers = [..settings.InstanceScrubbers]; ExtensionMappedInstanceScrubbers = new(settings.ExtensionMappedInstanceScrubbers); #if DiffEngine From 8896909206eb1fc78c189357f4a108274be82bee Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 28 Dec 2023 07:58:06 +1100 Subject: [PATCH 2/2] Update Directory.Build.props --- src/Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index d058b9eb17..1be6cb3a14 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters - 22.11.0 + 22.11.1 enable preview 1.0.0