-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for serializing and deserializing HTML-encodable cha…
…racters with the Serializer's Newtonsoft methods See #1118
- Loading branch information
1 parent
dae3ece
commit 73ffae9
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
ShopifySharp.Tests/Infrastructure/Serialization/SerializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using FluentAssertions; | ||
using JetBrains.Annotations; | ||
using ShopifySharp.Infrastructure; | ||
using Xunit; | ||
|
||
namespace ShopifySharp.Tests.Infrastructure.Serialization; | ||
|
||
[Trait("Category", "Serialization"), TestSubject(typeof(Serializer))] | ||
public class SerializerTests | ||
{ | ||
#region (Newtonsoft) Serializer.Serialize | ||
|
||
[Theory] | ||
[InlineData("º")] | ||
[InlineData("<")] | ||
[InlineData(">")] | ||
public void Serialize_WhenSerializingHtmlEncodableCharacters_ShouldNotEncodeToHtml(string encodableCharacter) | ||
{ | ||
// Act | ||
var json = Serializer.Serialize(encodableCharacter); | ||
|
||
// Assert | ||
json.Should().Be($"\"{encodableCharacter}\""); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Spiral flute 40º")] | ||
[InlineData("Spiral flute 40<")] | ||
[InlineData("Spiral flute 40>")] | ||
public void Serialize_WhenSerializingProductObjectWithHtmlEncodableCharacters_ShouldNotEncodeToHtml(string productTitle) | ||
{ | ||
// Setup | ||
var expectedJson = $$"""{"title":"{{productTitle}}","published_at":null}"""; | ||
var product = new Product { Title = productTitle }; | ||
|
||
// Act | ||
var json = Serializer.Serialize(product); | ||
|
||
// Assert | ||
json.Should().Be(expectedJson); | ||
} | ||
|
||
#endregion | ||
|
||
#region (Newtonsoft) Serializer.Deserialize(string json, Type objectType) | ||
|
||
[Theory] | ||
[InlineData("º")] | ||
[InlineData("<")] | ||
[InlineData(">")] | ||
public void Deserialize_WhenDeserializingHtmlEncodableStrings_ShouldNotDeserializeToEncodableHtml(string encodableCharacter) | ||
{ | ||
// Setup | ||
var json = $$"""{"foo":"{{encodableCharacter}}"}"""; | ||
|
||
// Act | ||
var result = Serializer.Deserialize(json, typeof(TestFoo)); | ||
|
||
// Assert | ||
result.Should().BeOfType(typeof(TestFoo)); | ||
result.As<TestFoo>().Foo.Should().Be(encodableCharacter); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Spiral flute 40º")] | ||
[InlineData("Spiral flute 40<")] | ||
[InlineData("Spiral flute 40>")] | ||
public void Deserialize_WhenDeserializingProductObjectWithHtmlEncodableCharacters_ShouldNotDeserializeToEncodableHtml(string productTitle) | ||
{ | ||
// Setup | ||
var json = $$"""{"title":"{{productTitle}}","published_at":null}"""; | ||
var expectedProduct = new Product { Title = productTitle, PublishedAt = null }; | ||
|
||
// Act | ||
var result = Serializer.Deserialize(json, typeof(Product)); | ||
|
||
// Assert | ||
result.Should().BeOfType(typeof(Product)); | ||
result.As<Product>().Should().BeEquivalentTo(expectedProduct); | ||
} | ||
|
||
#endregion | ||
|
||
#region (Newtonsoft) Serializer.Deserialize<T>(string json, string rootElementPath = null, DateParseHandling? dateParseHandlingOverride = null)} | ||
|
||
[Theory] | ||
[InlineData("º")] | ||
[InlineData("<")] | ||
[InlineData(">")] | ||
public void Deserialize_T_WhenDeserializingHtmlEncodableStrings_ShouldNotDeserializeToEncodableHtml(string encodableCharacter) | ||
{ | ||
// Setup | ||
var json = $$"""{"foo":"{{encodableCharacter}}"}"""; | ||
|
||
// Act | ||
var result = Serializer.Deserialize<TestFoo>(json); | ||
|
||
// Assert | ||
result.Foo.Should().Be(encodableCharacter); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Spiral flute 40º")] | ||
[InlineData("Spiral flute 40<")] | ||
[InlineData("Spiral flute 40>")] | ||
public void Deserialize_T_WhenDeserializingProductObjectWithHtmlEncodableStrings_ShouldNotDeserializeToEncodableHtml(string productTitle) | ||
{ | ||
// Setup | ||
var json = $$"""{"title":"{{productTitle}}","published_at":null}"""; | ||
var expectedProduct = new Product { Title = productTitle, PublishedAt = null }; | ||
|
||
// Act | ||
var result = Serializer.Deserialize<Product>(json); | ||
|
||
// Assert | ||
result.Should().BeEquivalentTo(expectedProduct); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Spiral flute 40º")] | ||
[InlineData("Spiral flute 40<")] | ||
[InlineData("Spiral flute 40>")] | ||
public void Deserialize_T_WhenDeserializingProductObjectWithHtmlEncodableStrings_AndUsingRootPath_ShouldNotDeserializeToEncodableHtml(string productTitle) | ||
{ | ||
// Setup | ||
var json = $$"""{"title":"{{productTitle}}","published_at":null}"""; | ||
|
||
// Act | ||
var result = Serializer.Deserialize<string>(json, "title"); | ||
|
||
// Assert | ||
result.Should().BeEquivalentTo(productTitle); | ||
} | ||
|
||
#endregion | ||
|
||
private record TestFoo | ||
{ | ||
public string Foo { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters