Skip to content

Commit

Permalink
Fix comparing with line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam committed Oct 30, 2023
1 parent 12817fc commit 9c3c33d
Showing 1 changed file with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@

namespace NJsonSchema.Tests.Generation.SystemTextJson
{
#if NETFRAMEWORK
file static class StringExtensions {
/// <summary>
/// Mimic .NET 6+ String.ReplaceLineEndings
/// </summary>
public static string ReplaceLineEndings(this string content, string lineSeparator = "\n")
{
return string.Join(
lineSeparator,
content.Replace("\r\n", "\n").Split('\r', '\n', '\f', '\u0085', '\u2028', '\u2029')
);
}
}
#endif

public class SystemTextJsonInheritanceTests
{
public class Apple : Fruit
Expand All @@ -30,19 +45,22 @@ public async Task When_using_JsonInheritanceAttribute_and_SystemTextJson_then_sc
{
//// Act
var schema = JsonSchema.FromType<Fruit>();
var data = schema.ToJson();
var data = schema.ToJson().ReplaceLineEndings();

//// Assert
Assert.NotNull(data);
Assert.Contains(@"""a"": """, data);
Assert.Contains(@"""o"": """, data);
Assert.Contains(@"""discriminator"": {
""propertyName"": ""k"",
""mapping"": {
""a"": ""#/definitions/Apple"",
""o"": ""#/definitions/Orange""
}
},", data);
Assert.Contains(
"""
"discriminator": {
"propertyName": "k",
"mapping": {
"a": "#/definitions/Apple",
"o": "#/definitions/Orange"
}
},
""".ReplaceLineEndings(), data);
}

#if !NETFRAMEWORK
Expand Down Expand Up @@ -70,21 +88,24 @@ public async Task When_using_native_attributes_in_SystemTextJson_then_schema_is_
{
//// Act
var schema = JsonSchema.FromType<Fruit2>();
var data = schema.ToJson();
var data = schema.ToJson().ReplaceLineEndings();

//// Assert
Assert.NotNull(data);
Assert.Contains(@"""a"": """, data);
Assert.Contains(@"""o"": """, data);
Assert.Contains(@"""discriminator"": {
""propertyName"": ""k"",
""mapping"": {
""a"": ""#/definitions/Apple2"",
""o"": ""#/definitions/Orange2""
}
},", data);
Assert.Contains(
"""
"discriminator": {
"propertyName": "k",
"mapping": {
"a": "#/definitions/Apple2",
"o": "#/definitions/Orange2"
}
},
""".ReplaceLineEndings(), data);
}

#endif
}
}
}

0 comments on commit 9c3c33d

Please sign in to comment.