From 91a72a2d565e2a2117b3f817576c347b48a83585 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 9 Aug 2022 10:46:04 +1000 Subject: [PATCH] Handle bad newlines written by converter (#580) --- docs/serializer-settings.md | 30 +++++++++---------- readme.md | 2 +- ...s.TestConverterWithBadNewline.verified.txt | 19 ++++++++++++ .../Serialization/SerializationTests.cs | 27 ++++++++++++++++- src/Verify/Extensions.cs | 4 +++ src/Verify/Serialization/JsonFormatter.cs | 1 + src/Verify/Serialization/VerifyJsonWriter.cs | 10 ++----- 7 files changed, 68 insertions(+), 25 deletions(-) create mode 100644 src/Verify.Tests/Serialization/SerializationTests.TestConverterWithBadNewline.verified.txt diff --git a/docs/serializer-settings.md b/docs/serializer-settings.md index c7a31c6e1d..4f3e327418 100644 --- a/docs/serializer-settings.md +++ b/docs/serializer-settings.md @@ -474,7 +474,7 @@ public Task ScopedSerializerFluent() .AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -602,7 +602,7 @@ public Task IgnoreTypeFluent() .IgnoreMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -612,7 +612,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -683,7 +683,7 @@ public Task AddIgnoreInstanceFluent() .IgnoreInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -693,7 +693,7 @@ Or globally: ```cs VerifierSettings.IgnoreInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -736,7 +736,7 @@ public Task WithObsoleteProp() return Verify(target); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -784,7 +784,7 @@ public Task WithObsoletePropIncludedFluent() .IncludeObsoletes(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -794,7 +794,7 @@ Or globally: ```cs VerifierSettings.IncludeObsoletes(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -855,7 +855,7 @@ public Task IgnoreMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -870,7 +870,7 @@ VerifierSettings.IgnoreMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -944,7 +944,7 @@ public Task IgnoreMemberByNameFluent() .IgnoreMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -964,7 +964,7 @@ VerifierSettings.IgnoreMember("Field"); // For a specific type with expression VerifierSettings.IgnoreMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1011,7 +1011,7 @@ public Task CustomExceptionPropFluent() .IgnoreMembersThatThrow(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1021,7 +1021,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1234,7 +1234,7 @@ public Task MemberConverterByExpression() return Verify(input); } ``` -snippet source | anchor +snippet source | anchor diff --git a/readme.md b/readme.md index 56772a74f6..08e2fe4b5c 100644 --- a/readme.md +++ b/readme.md @@ -349,7 +349,7 @@ public Task VerifyJsonJToken() return VerifyJson(target); } ``` -snippet source | anchor +snippet source | anchor Results in: diff --git a/src/Verify.Tests/Serialization/SerializationTests.TestConverterWithBadNewline.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.TestConverterWithBadNewline.verified.txt new file mode 100644 index 0000000000..301a103e66 --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.TestConverterWithBadNewline.verified.txt @@ -0,0 +1,19 @@ +Property1: + + +A + + +B + + +, +Property2: + + +A + + +B + + diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index cc302f42dd..145bd2aa1a 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -1469,7 +1469,8 @@ public Task TestEnumerableWithExistingConverter() { "Value" }; - return Verify(target).AddExtraSettings(_ => _.Converters.Add(new EnumerableWithExistingConverter())); + return Verify(target) + .AddExtraSettings(_ => _.Converters.Add(new EnumerableWithExistingConverter())); } class EnumerableWithExistingConverterTarget: List @@ -1483,6 +1484,30 @@ public override void Write(VerifyJsonWriter writer, EnumerableWithExistingConver writer.Serialize("Content"); } + [Fact] + public Task TestConverterWithBadNewline() + { + var target = new ConverterWithBadNewlineTarget(); + return Verify(target) + .AddExtraSettings(_ => _.Converters.Add(new ConverterWithBadNewline())); + } + + class ConverterWithBadNewlineTarget + { + } + + class ConverterWithBadNewline: + WriteOnlyJsonConverter + { + public override void Write(VerifyJsonWriter writer, ConverterWithBadNewlineTarget target) + { + writer.WritePropertyName("Property1"); + writer.WriteRawValue("\n\r\r\nA\n\r\r\nB\n\r\r\n"); + writer.WritePropertyName("Property2"); + writer.WriteValue("\n\r\r\nA\n\r\r\nB\n\r\r\n"); + } + } + [Fact] public Task TestEnumerableWithExistingItemConverter() { diff --git a/src/Verify/Extensions.cs b/src/Verify/Extensions.cs index df8e9468fe..53973a6fef 100644 --- a/src/Verify/Extensions.cs +++ b/src/Verify/Extensions.cs @@ -11,6 +11,10 @@ public static List Clone(this List original) => return attribute?.Configuration; } + public static string FixNewlines(this string value) => + value.Replace("\r\n", "\n") + .Replace('\r', '\n'); + public static char? FirstChar(this StringBuilder builder) { if (builder.Length > 0) diff --git a/src/Verify/Serialization/JsonFormatter.cs b/src/Verify/Serialization/JsonFormatter.cs index f30ef7259b..fb6010de27 100644 --- a/src/Verify/Serialization/JsonFormatter.cs +++ b/src/Verify/Serialization/JsonFormatter.cs @@ -22,6 +22,7 @@ public static StringBuilder AsJson(object? input, List appends, Verify var builder = new StringBuilder(); using var writer = new VerifyJsonWriter(builder, settings, counter); settings.Serializer.Serialize(writer, input); + builder.FixNewlines(); return builder; } } \ No newline at end of file diff --git a/src/Verify/Serialization/VerifyJsonWriter.cs b/src/Verify/Serialization/VerifyJsonWriter.cs index e8bf37ea43..f2f5e04580 100644 --- a/src/Verify/Serialization/VerifyJsonWriter.cs +++ b/src/Verify/Serialization/VerifyJsonWriter.cs @@ -35,7 +35,7 @@ public void WriteRawValueWithScrubbers(string? value) base.WriteRawValue(value); return; } - value = ReplaceNewlinesAndScrub(value); + value = ApplyScrubbers.ApplyForPropertyValue(value, settings); base.WriteRawValue(value); } @@ -53,7 +53,7 @@ public override void WriteValue(string? value) return; } - value = ReplaceNewlinesAndScrub(value); + value = ApplyScrubbers.ApplyForPropertyValue(value, settings); if (VerifierSettings.StrictJson) { base.WriteValue(value); @@ -78,12 +78,6 @@ public override void WriteValue(string? value) WriteRawValue(value); } - string ReplaceNewlinesAndScrub(string value) - { - value = value.Replace("\r\n", "\n").Replace('\r', '\n'); - return ApplyScrubbers.ApplyForPropertyValue(value, settings); - } - public void WriteSingleLineNoScrubbing(string value) { if (value is "")