Skip to content

Commit

Permalink
Handle bad newlines written by converter (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Aug 9, 2022
1 parent cf2faa0 commit 91a72a2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 25 deletions.
30 changes: 15 additions & 15 deletions docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public Task ScopedSerializerFluent()
.AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2387-L2414' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2412-L2439' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -602,7 +602,7 @@ public Task IgnoreTypeFluent()
.IgnoreMembersWithType<ToIgnoreStruct>();
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1627-L1732' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1652-L1757' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -612,7 +612,7 @@ Or globally:
```cs
VerifierSettings.IgnoreMembersWithType<ToIgnore>();
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1620-L1624' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretypeglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1645-L1649' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretypeglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -683,7 +683,7 @@ public Task AddIgnoreInstanceFluent()
.IgnoreInstance<Instance>(_ => _.Property == "Ignore");
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1548-L1587' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1573-L1612' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -693,7 +693,7 @@ Or globally:
```cs
VerifierSettings.IgnoreInstance<Instance>(_ => _.Property == "Ignore");
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1541-L1545' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstanceglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1566-L1570' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstanceglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -736,7 +736,7 @@ public Task WithObsoleteProp()
return Verify(target);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2353-L2373' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2378-L2398' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -784,7 +784,7 @@ public Task WithObsoletePropIncludedFluent()
.IncludeObsoletes();
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2324-L2351' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2349-L2376' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -794,7 +794,7 @@ Or globally:
```cs
VerifierSettings.IncludeObsoletes();
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2317-L2321' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2342-L2346' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -855,7 +855,7 @@ public Task IgnoreMemberByExpressionFluent()
_ => _.PropertyThatThrows);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1912-L1951' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1937-L1976' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally
Expand All @@ -870,7 +870,7 @@ VerifierSettings.IgnoreMembers<IgnoreExplicitTarget>(
_ => _.GetOnlyProperty,
_ => _.PropertyThatThrows);
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1900-L1909' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpressionglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1925-L1934' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpressionglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -944,7 +944,7 @@ public Task IgnoreMemberByNameFluent()
.IgnoreMember<IgnoreExplicitTarget>(_ => _.PropertyThatThrows);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2004-L2057' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2029-L2082' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -964,7 +964,7 @@ VerifierSettings.IgnoreMember<IgnoreExplicitTarget>("Field");
// For a specific type with expression
VerifierSettings.IgnoreMember<IgnoreExplicitTarget>(_ => _.PropertyThatThrows);
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1987-L2001' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbynameglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2012-L2026' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbynameglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -1011,7 +1011,7 @@ public Task CustomExceptionPropFluent()
.IgnoreMembersThatThrow<CustomException>();
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2209-L2228' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2234-L2253' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -1021,7 +1021,7 @@ Or globally:
```cs
VerifierSettings.IgnoreMembersThatThrow<CustomException>();
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2202-L2206' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2227-L2231' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -1234,7 +1234,7 @@ public Task MemberConverterByExpression()
return Verify(input);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1953-L1977' title='Snippet source file'>snippet source</a> | <a href='#snippet-memberconverter' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1978-L2002' title='Snippet source file'>snippet source</a> | <a href='#snippet-memberconverter' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public Task VerifyJsonJToken()
return VerifyJson(target);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2133-L2165' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2158-L2190' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Property1:


A


B


,
Property2:


A


B


27 changes: 26 additions & 1 deletion src/Verify.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
Expand All @@ -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<ConverterWithBadNewlineTarget>
{
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()
{
Expand Down
4 changes: 4 additions & 0 deletions src/Verify/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public static List<T> Clone<T>(this List<T> 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)
Expand Down
1 change: 1 addition & 0 deletions src/Verify/Serialization/JsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static StringBuilder AsJson(object? input, List<ToAppend> appends, Verify
var builder = new StringBuilder();
using var writer = new VerifyJsonWriter(builder, settings, counter);
settings.Serializer.Serialize(writer, input);
builder.FixNewlines();
return builder;
}
}
10 changes: 2 additions & 8 deletions src/Verify/Serialization/VerifyJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void WriteRawValueWithScrubbers(string? value)
base.WriteRawValue(value);
return;
}
value = ReplaceNewlinesAndScrub(value);
value = ApplyScrubbers.ApplyForPropertyValue(value, settings);
base.WriteRawValue(value);
}

Expand All @@ -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);
Expand All @@ -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 "")
Expand Down

0 comments on commit 91a72a2

Please sign in to comment.