From 70f34d45154879af4923be9f5a5849d3832f32a6 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 2 Jul 2024 22:21:50 +1000 Subject: [PATCH] UseStrictJson should result in json extension (#1237) --- docs/dates.md | 26 +++++----- docs/guids.md | 14 ++--- docs/named-tuples.md | 4 +- docs/scrubbers.md | 12 ++--- docs/serializer-settings.md | 52 +++++++++---------- src/Directory.Build.props | 2 +- ...ests.VerifyJsonUseStrictJson.verified.json | 3 ++ ...erifyJsonUseStrictJsonFluent.verified.json | 3 ++ ...lizationTests.UseStrictJson.verified.json} | 0 ...onTests.UseStrictJsonFluent.verified.json} | 0 ...ests.VerifyJsonUseStrictJson.verified.json | 3 ++ ...erifyJsonUseStrictJsonFluent.verified.json | 3 ++ .../Serialization/SerializationTests.cs | 25 +++++++++ src/Verify/Serialization/VerifierSettings.cs | 13 ----- src/Verify/Verifier/InnerVerifier_Archive.cs | 2 +- .../Verifier/InnerVerifier_Directory.cs | 2 +- src/Verify/Verifier/InnerVerifier_Inner.cs | 8 +-- src/Verify/Verifier/InnerVerifier_Stream.cs | 4 +- src/Verify/VerifySettings.cs | 13 +++++ 19 files changed, 113 insertions(+), 76 deletions(-) create mode 100644 src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJson.verified.json create mode 100644 src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json rename src/Verify.Tests/Serialization/{SerializationTests.UseStrictJson.verified.txt => SerializationTests.UseStrictJson.verified.json} (100%) rename src/Verify.Tests/Serialization/{SerializationTests.UseStrictJsonFluent.verified.txt => SerializationTests.UseStrictJsonFluent.verified.json} (100%) create mode 100644 src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJson.verified.json create mode 100644 src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json diff --git a/docs/dates.md b/docs/dates.md index 9f9293a21..788fd590a 100644 --- a/docs/dates.md +++ b/docs/dates.md @@ -30,7 +30,7 @@ var target = new DateTimeTarget await Verify(target); ``` -snippet source | anchor +snippet source | anchor Results in the following: @@ -71,7 +71,7 @@ settings.DontScrubDateTimes(); return Verify(target, settings); ``` -snippet source | anchor +snippet source | anchor @@ -88,7 +88,7 @@ var target = new return Verify(target) .DontScrubDateTimes(); ``` -snippet source | anchor +snippet source | anchor @@ -101,7 +101,7 @@ return Verify(target) public static void ModuleInitializer() => VerifierSettings.DontScrubDateTimes(); ``` -snippet source | anchor +snippet source | anchor @@ -125,7 +125,7 @@ settings.DisableDateCounting(); return Verify(target, settings); ``` -snippet source | anchor +snippet source | anchor @@ -142,7 +142,7 @@ var target = new return Verify(target) .DisableDateCounting(); ``` -snippet source | anchor +snippet source | anchor @@ -155,7 +155,7 @@ return Verify(target) public static void ModuleInitializer() => VerifierSettings.DisableDateCounting(); ``` -snippet source | anchor +snippet source | anchor @@ -202,7 +202,7 @@ public Task ScrubInlineDateTimesInstance() settings); } ``` -snippet source | anchor +snippet source | anchor @@ -216,7 +216,7 @@ public Task ScrubInlineDateTimesFluent() => Verify("content 2020-10-20 content") .ScrubInlineDateTimes("yyyy-MM-dd"); ``` -snippet source | anchor +snippet source | anchor @@ -232,7 +232,7 @@ public static class ModuleInitializer VerifierSettings.ScrubInlineDateTimes("yyyy-MM-dd"); } ``` -snippet source | anchor +snippet source | anchor @@ -253,7 +253,7 @@ settings.AddNamedDateTime(new(2030, 1, 2), "instanceNamedDateTime"); settings.AddNamedDateTimeOffset(new DateTime(2030, 1, 2), "instanceNamedTimeOffset"); await Verify(target, settings); ``` -snippet source | anchor +snippet source | anchor @@ -268,7 +268,7 @@ await Verify(target) .AddNamedDateTime(new(2030, 1, 2), "instanceNamedDateTime") .AddNamedDateTimeOffset(new DateTime(2030, 1, 2), "instanceNamedTimeOffset"); ``` -snippet source | anchor +snippet source | anchor @@ -286,5 +286,5 @@ public static void NamedDatesAndTimesGlobal() VerifierSettings.AddNamedDateTimeOffset(new(new(2030, 1, 1)), "namedDateTimeOffset"); } ``` -snippet source | anchor +snippet source | anchor diff --git a/docs/guids.md b/docs/guids.md index b29de73d1..8bd067625 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: @@ -56,7 +56,7 @@ var settings = new VerifySettings(); settings.DontScrubGuids(); await Verify(target, settings); ``` -snippet source | anchor +snippet source | anchor @@ -68,7 +68,7 @@ await Verify(target, settings); await Verify(target) .DontScrubGuids(); ``` -snippet source | anchor +snippet source | anchor @@ -79,7 +79,7 @@ await Verify(target) ```cs VerifierSettings.DontScrubGuids(); ``` -snippet source | anchor +snippet source | anchor @@ -103,7 +103,7 @@ public Task ScrubInlineGuidsInstance() settings); } ``` -snippet source | anchor +snippet source | anchor @@ -117,7 +117,7 @@ public Task ScrubInlineGuidsFluent() => Verify("content 651ad409-fc30-4b12-a47e-616d3f953e4c content") .ScrubInlineGuids(); ``` -snippet source | anchor +snippet source | anchor @@ -133,7 +133,7 @@ public static class ModuleInitializer VerifierSettings.ScrubInlineGuids(); } ``` -snippet source | anchor +snippet source | anchor diff --git a/docs/named-tuples.md b/docs/named-tuples.md index 4ee412eee..7c945023c 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 e09e36c95..2d7bd6704 100644 --- a/docs/scrubbers.md +++ b/docs/scrubbers.md @@ -59,7 +59,7 @@ For example remove lines containing `text`: ```cs verifySettings.ScrubLines(line => line.Contains("text")); ``` -snippet source | anchor +snippet source | anchor @@ -74,7 +74,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). @@ -86,7 +86,7 @@ Case insensitive by default (StringComparison.OrdinalIgnoreCase). ```cs verifySettings.ScrubLinesContaining(StringComparison.Ordinal, "text1", "text2"); ``` -snippet source | anchor +snippet source | anchor @@ -101,7 +101,7 @@ For example converts lines to upper case: ```cs verifySettings.ScrubLinesWithReplace(line => line.ToUpper()); ``` -snippet source | anchor +snippet source | anchor @@ -114,7 +114,7 @@ Replaces `Environment.MachineName` with `TheMachineName`. ```cs verifySettings.ScrubMachineName(); ``` -snippet source | anchor +snippet source | anchor @@ -127,7 +127,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 a2feb5742..0eba3293b 100644 --- a/docs/serializer-settings.md +++ b/docs/serializer-settings.md @@ -246,7 +246,7 @@ To disable this behavior globally use: ```cs VerifierSettings.DontIgnoreEmptyCollections(); ``` -snippet source | anchor +snippet source | anchor @@ -347,7 +347,7 @@ public Task ScopedSerializerFluent() .AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -475,7 +475,7 @@ public Task IgnoreTypeFluent() .IgnoreMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -485,7 +485,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -622,7 +622,7 @@ public Task ScrubTypeFluent() .ScrubMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -632,7 +632,7 @@ Or globally: ```cs VerifierSettings.ScrubMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -711,7 +711,7 @@ public Task AddIgnoreInstanceFluent() .IgnoreInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -721,7 +721,7 @@ Or globally: ```cs VerifierSettings.IgnoreInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -783,7 +783,7 @@ public Task AddScrubInstanceFluent() .ScrubInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -793,7 +793,7 @@ Or globally: ```cs VerifierSettings.ScrubInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -838,7 +838,7 @@ public Task WithObsoleteProp() return Verify(target); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -886,7 +886,7 @@ public Task WithObsoletePropIncludedFluent() .IncludeObsoletes(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -896,7 +896,7 @@ Or globally: ```cs VerifierSettings.IncludeObsoletes(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -957,7 +957,7 @@ public Task IgnoreMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -972,7 +972,7 @@ VerifierSettings.IgnoreMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1032,7 +1032,7 @@ public Task ScrubMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -1047,7 +1047,7 @@ VerifierSettings.ScrubMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1126,7 +1126,7 @@ public Task IgnoreMemberByNameFluent() .IgnoreMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1146,7 +1146,7 @@ VerifierSettings.IgnoreMember("Field"); // For a specific type with expression VerifierSettings.IgnoreMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1221,7 +1221,7 @@ public Task ScrubMemberByNameFluent() .ScrubMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1241,7 +1241,7 @@ VerifierSettings.ScrubMember("Field"); // For a specific type with expression VerifierSettings.ScrubMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1292,7 +1292,7 @@ public Task CustomExceptionPropFluent() .IgnoreMembersThatThrow(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1302,7 +1302,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1339,7 +1339,7 @@ public Task ExceptionMessagePropFluent() .IgnoreMembersThatThrow(_ => _.Message == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1349,7 +1349,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(_ => _.Message == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1507,7 +1507,7 @@ public Task MemberConverterByExpression() return Verify(input); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/Directory.Build.props b/src/Directory.Build.props index f3ae9e08e..cba97938f 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget - 25.1.1 + 25.2.0 enable preview 1.0.0 diff --git a/src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJson.verified.json b/src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJson.verified.json new file mode 100644 index 000000000..37c788a4d --- /dev/null +++ b/src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJson.verified.json @@ -0,0 +1,3 @@ +{ + "fruit": "Apple" +} \ No newline at end of file diff --git a/src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json b/src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json new file mode 100644 index 000000000..37c788a4d --- /dev/null +++ b/src/StrictJsonTests/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json @@ -0,0 +1,3 @@ +{ + "fruit": "Apple" +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.UseStrictJson.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.UseStrictJson.verified.json similarity index 100% rename from src/Verify.Tests/Serialization/SerializationTests.UseStrictJson.verified.txt rename to src/Verify.Tests/Serialization/SerializationTests.UseStrictJson.verified.json diff --git a/src/Verify.Tests/Serialization/SerializationTests.UseStrictJsonFluent.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.UseStrictJsonFluent.verified.json similarity index 100% rename from src/Verify.Tests/Serialization/SerializationTests.UseStrictJsonFluent.verified.txt rename to src/Verify.Tests/Serialization/SerializationTests.UseStrictJsonFluent.verified.json diff --git a/src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJson.verified.json b/src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJson.verified.json new file mode 100644 index 000000000..37c788a4d --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJson.verified.json @@ -0,0 +1,3 @@ +{ + "fruit": "Apple" +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json b/src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json new file mode 100644 index 000000000..37c788a4d --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.VerifyJsonUseStrictJsonFluent.verified.json @@ -0,0 +1,3 @@ +{ + "fruit": "Apple" +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index c6dc84d3a..ca29eaa20 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -642,6 +642,31 @@ await Verify(target) #endregion } + + [Fact] + public async Task VerifyJsonUseStrictJson() + { + var settings = new VerifySettings(); + settings.UseStrictJson(); + await VerifyJson( + """ + { + "fruit": "Apple" + } + """, + settings); + } + + [Fact] + public Task VerifyJsonUseStrictJsonFluent() => + VerifyJson( + """ + { + "fruit": "Apple" + } + """) + .UseStrictJson(); + [Fact] public async Task GuidScrubbingDisabled() { diff --git a/src/Verify/Serialization/VerifierSettings.cs b/src/Verify/Serialization/VerifierSettings.cs index 096176233..76e62270a 100644 --- a/src/Verify/Serialization/VerifierSettings.cs +++ b/src/Verify/Serialization/VerifierSettings.cs @@ -159,19 +159,6 @@ public static void UseStrictJson() public static bool StrictJson { get; private set; } - internal static string TxtOrJson - { - get - { - if (StrictJson) - { - return "json"; - } - - return "txt"; - } - } - internal static bool scrubProjectDir = true; public static void DontScrubProjectDirectory() diff --git a/src/Verify/Verifier/InnerVerifier_Archive.cs b/src/Verify/Verifier/InnerVerifier_Archive.cs index 7d3b8d6b3..23a54b6bb 100644 --- a/src/Verify/Verifier/InnerVerifier_Archive.cs +++ b/src/Verify/Verifier/InnerVerifier_Archive.cs @@ -35,7 +35,7 @@ partial class InnerVerifier { targets.Add( new( - VerifierSettings.TxtOrJson, + settings.TxtOrJson, JsonFormatter.AsJson( settings, counter, diff --git a/src/Verify/Verifier/InnerVerifier_Directory.cs b/src/Verify/Verifier/InnerVerifier_Directory.cs index 9b71c9a4d..b6b3e5a35 100644 --- a/src/Verify/Verifier/InnerVerifier_Directory.cs +++ b/src/Verify/Verifier/InnerVerifier_Directory.cs @@ -70,7 +70,7 @@ partial class InnerVerifier { targets.Add( new( - VerifierSettings.TxtOrJson, + settings.TxtOrJson, JsonFormatter.AsJson( settings, counter, diff --git a/src/Verify/Verifier/InnerVerifier_Inner.cs b/src/Verify/Verifier/InnerVerifier_Inner.cs index ca38e52b9..a227a70d2 100644 --- a/src/Verify/Verifier/InnerVerifier_Inner.cs +++ b/src/Verify/Verifier/InnerVerifier_Inner.cs @@ -63,7 +63,7 @@ async Task<(List extra, Func cleanup)> GetTargets(IEnumerable VerifyStreams(IEnumerable streams, string extens targets.Insert( 0, new( - VerifierSettings.TxtOrJson, + settings.TxtOrJson, JsonFormatter.AsJson(settings, counter, info))); } @@ -101,7 +101,7 @@ public async Task VerifyStream(Stream? stream, string extension, o { targets.Add( new( - VerifierSettings.TxtOrJson, + settings.TxtOrJson, JsonFormatter.AsJson(settings, counter, info))); } diff --git a/src/Verify/VerifySettings.cs b/src/Verify/VerifySettings.cs index a97c5bdd1..033a9fcf5 100644 --- a/src/Verify/VerifySettings.cs +++ b/src/Verify/VerifySettings.cs @@ -2,6 +2,18 @@ namespace VerifyTests; public partial class VerifySettings { + internal string TxtOrJson + { + get + { + if (VerifierSettings.StrictJson || strictJson) + { + return "json"; + } + + return "txt"; + } + } public VerifySettings(VerifySettings? settings) { if (settings is null) @@ -15,6 +27,7 @@ public VerifySettings(VerifySettings? settings) diffEnabled = settings.diffEnabled; methodName = settings.methodName; typeName = settings.typeName; + strictJson = settings.strictJson; appendedFiles = settings.appendedFiles; useUniqueDirectory = settings.useUniqueDirectory; Directory = settings.Directory;