-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a custom json serializer when deserializing testfiles (#26)
- Loading branch information
Showing
24 changed files
with
470 additions
and
18 deletions.
There are no files selected for viewing
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
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,61 @@ | ||
namespace EasyTestFile.Json; | ||
|
||
using System; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class EasyTestFileSettingsExtension | ||
{ | ||
/// <summary> | ||
/// Set <paramref name="jsonSerializer"/> as the default json serializer when deserializing testfiles. | ||
/// </summary> | ||
/// <param name="settings">The settings.</param> | ||
/// <param name="jsonSerializer">The serializer.</param> | ||
/// <exception cref="ArgumentNullException">Thrown when <paramref name="settings"/> or when <paramref name="jsonSerializer"/> is <c>null</c>.</exception> | ||
/// <exception cref="Exception">Thrown when key cannot be inserted.</exception> | ||
public static void SetNewtonSoftJsonSerializerSettings(this EasyTestFileSettings settings, JsonSerializer jsonSerializer) | ||
{ | ||
if (settings == null) | ||
{ | ||
throw new ArgumentNullException(nameof(settings)); | ||
} | ||
|
||
if (jsonSerializer == null) | ||
{ | ||
throw new ArgumentNullException(nameof(jsonSerializer)); | ||
} | ||
|
||
if (settings.Context.ContainsKey(ExtensionKey.JSON_SERIALIZER)) | ||
{ | ||
settings.Context[ExtensionKey.JSON_SERIALIZER] = jsonSerializer; | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
settings.Context.Add(ExtensionKey.JSON_SERIALIZER, jsonSerializer); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new($"Could not add settings", e); | ||
} | ||
} | ||
|
||
internal static JsonSerializer? GetNewtonSoftJsonSerializerSettings(this EasyTestFileSettings settings) | ||
{ | ||
if (settings == null) | ||
{ | ||
throw new ArgumentNullException(nameof(settings)); | ||
} | ||
|
||
if (!settings.Context.TryGetValue(ExtensionKey.JSON_SERIALIZER, out var value)) | ||
{ | ||
return null; | ||
} | ||
|
||
return value as JsonSerializer; | ||
} | ||
|
||
} |
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,11 @@ | ||
namespace EasyTestFile.Json | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
internal static class ExtensionKey | ||
{ | ||
internal const string JSON_SERIALIZER = "Newtonsoft.Json.JsonSerializer"; | ||
} | ||
} |
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
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
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
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
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
25 changes: 25 additions & 0 deletions
25
tests/EasyTestFile.Json.Tests/EasyTestFile.Json.Tests.csproj
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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5;net6</TargetFrameworks> | ||
<RootNamespace>EasyTestFile.Json.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\EasyTestFile.Json\EasyTestFile.Json.csproj" /> | ||
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" /> | ||
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" /> | ||
<PackageReference Include="Verify.Xunit" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.analyzers" /> | ||
<PackageReference Include="xunit.categories" /> | ||
<PackageReference Include="xunit.runner.console" /> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="xunit.runner.reporters"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
70 changes: 70 additions & 0 deletions
70
tests/EasyTestFile.Json.Tests/EasyTestFileSettingsExtensionTest.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,70 @@ | ||
namespace EasyTestFile.Json.Tests; | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Newtonsoft.Json; | ||
using VerifyXunit; | ||
using Xunit; | ||
using Sut = EasyTestFileSettingsExtension; | ||
|
||
[UsesVerify] | ||
[SuppressMessage("ReSharper", "InvokeAsExtensionMethod", Justification = "Improves readability SUT.")] | ||
public class EasyTestFileSettingsExtensionTest | ||
{ | ||
[Fact] | ||
public void SetNewtonSoftJsonSerializerSettings_ShouldSaveSettingsInContext() | ||
{ | ||
// arrange | ||
var settings = new EasyTestFileSettings(); | ||
var jsonSerializer = new JsonSerializer(); | ||
|
||
// act | ||
Sut.SetNewtonSoftJsonSerializerSettings(settings, jsonSerializer); | ||
|
||
// assert | ||
settings.Context.Should().ContainKey("Newtonsoft.Json.JsonSerializer").WhoseValue.Should().BeSameAs(jsonSerializer); | ||
} | ||
|
||
[Fact] | ||
public void SetNewtonSoftJsonSerializerSettings_ShouldOverrideSettingsInContext_WhenAlreadyStored() | ||
{ | ||
// arrange | ||
var settings = new EasyTestFileSettings(); | ||
var jsonSerializer1 = new JsonSerializer(); | ||
var jsonSerializer2 = new JsonSerializer(); | ||
Sut.SetNewtonSoftJsonSerializerSettings(settings, jsonSerializer1); | ||
|
||
// act | ||
Sut.SetNewtonSoftJsonSerializerSettings(settings, jsonSerializer2); | ||
|
||
// assert | ||
settings.Context.Should().ContainKey("Newtonsoft.Json.JsonSerializer").WhoseValue.Should().BeSameAs(jsonSerializer2); | ||
} | ||
|
||
[Fact] | ||
public void SetNewtonSoftJsonSerializerSettings_ShouldThrow_WhenSettingsArgumentIsNull() | ||
{ | ||
// arrange | ||
|
||
// act | ||
Action act = () => Sut.SetNewtonSoftJsonSerializerSettings(null!, new JsonSerializer()); | ||
|
||
// assert | ||
act.Should().ThrowExactly<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void SetNewtonSoftJsonSerializerSettings_ShouldThrow_WhenJsonSerializerArgumentIsNull() | ||
{ | ||
// arrange | ||
|
||
// act | ||
Action act = () => Sut.SetNewtonSoftJsonSerializerSettings(new EasyTestFileSettings(), null!); | ||
|
||
// assert | ||
act.Should().ThrowExactly<ArgumentNullException>(); | ||
} | ||
} |
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,23 @@ | ||
namespace EasyTestFile.Json.Tests; | ||
|
||
using FluentAssertions; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
|
||
public class EasyTestFileSettingsTest | ||
{ | ||
[Fact] | ||
public void Ctor_ShouldCopyJsonSerializer_WhenSetInBase() | ||
{ | ||
// arrange | ||
var settings1 = new EasyTestFileSettings(); | ||
var jsonSerializer = new JsonSerializer(); | ||
settings1.SetNewtonSoftJsonSerializerSettings(jsonSerializer); | ||
|
||
// act | ||
var settings2 = new EasyTestFileSettings(settings1); | ||
|
||
// assert | ||
settings1.Context.Should().BeEquivalentTo(settings2.Context); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...tonsoft_ShouldDeserializeFile_WhenUsingSettingsContainingSpecificSerializer.testfile.json
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,22 @@ | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.TestEntity, EasyTestFile.Json.Tests", | ||
"Name": "CoenM", | ||
"DateOfBirth": "2020-03-05T04:14:12", | ||
"Subs": { | ||
"$type": "System.Collections.Generic.List`1[[EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests]], System.Private.CoreLib", | ||
"$values": [ | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests", | ||
"Id": "073633d2-edd1-4a1d-93b7-fa4a8b1b1a03" | ||
}, | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests", | ||
"Id": "841e3836-b1b2-4e45-979e-e0b2ad8fad38" | ||
}, | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests", | ||
"Id": "86e2fb0b-30a8-4688-ad24-f7da9385c43c" | ||
} | ||
] | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...t.AsObjectUsingNewtonsoft_ShouldDeserializeFile_WhenUsingSpecificSerializer.testfile.json
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,22 @@ | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.TestEntity, EasyTestFile.Json.Tests", | ||
"Name": "CoenM", | ||
"DateOfBirth": "2020-03-05T04:14:12", | ||
"Subs": { | ||
"$type": "System.Collections.Generic.List`1[[EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests]], System.Private.CoreLib", | ||
"$values": [ | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests", | ||
"Id": "073633d2-edd1-4a1d-93b7-fa4a8b1b1a03" | ||
}, | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests", | ||
"Id": "841e3836-b1b2-4e45-979e-e0b2ad8fad38" | ||
}, | ||
{ | ||
"$type": "EasyTestFile.Json.Tests.TestEntities.SubTestEntity, EasyTestFile.Json.Tests", | ||
"Id": "86e2fb0b-30a8-4688-ad24-f7da9385c43c" | ||
} | ||
] | ||
} | ||
} |
Oops, something went wrong.