-
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.
Implement Unit deserialization; Add the tests
- Loading branch information
Showing
13 changed files
with
351 additions
and
7 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 @@ | ||
global using NUnit.Framework; |
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 @@ | ||
global using Xunit; |
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,39 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
<LangVersion>latest</LangVersion> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
<Authors>Andrei Sergeev, Pavel Moskovoy</Authors> | ||
<Copyright>Copyright © 2020-2024 Andrei Sergeev, Pavel Moskovoy</Copyright> | ||
<RootNamespace>PrimeFuncPack.Core.Tests</RootNamespace> | ||
<AssemblyName>PrimeFuncPack.Core.Unit.Tests</AssemblyName> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<NoWarn>$(NoWarn);xUnit1044</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" /> | ||
<PackageReference Include="xunit" Version="2.8.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Unit\Unit.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
51 changes: 51 additions & 0 deletions
51
...e-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeArrayToUnit.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,51 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
partial class UnitSerializationTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(DeserializeArrayToUnit_ExpectNoException_Cases))] | ||
public static void DeserializeArrayToUnitFromString_ExpectNoException( | ||
JsonArray source, | ||
JsonSerializerOptions? options) | ||
{ | ||
var sourceString = JsonSerializer.Serialize(source); | ||
_ = JsonSerializer.Deserialize<Unit>(sourceString, options); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(DeserializeArrayToUnit_ExpectNoException_Cases))] | ||
public static void DeserializeArrayToUnitFromObject_ExpectNoException( | ||
JsonArray source, | ||
JsonSerializerOptions? options) | ||
{ | ||
_ = JsonSerializer.Deserialize<Unit>(source, options); | ||
} | ||
|
||
public static TheoryData<JsonArray, JsonSerializerOptions?> DeserializeArrayToUnit_ExpectNoException_Cases | ||
{ | ||
get | ||
{ | ||
var arrays = new JsonArray[] | ||
{ | ||
[], | ||
new(new JsonArray(null, true, false, -1, 0, 1.1m, 1.2, "2")) | ||
}; | ||
|
||
var result = new TheoryData<JsonArray, JsonSerializerOptions?>(); | ||
|
||
foreach (var array in arrays) | ||
{ | ||
foreach (var options in BuildJsonSerializerOptionsCollection()) | ||
{ | ||
result.Add(array, options); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...it.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNonEmptyObjectToUnit.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 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
partial class UnitSerializationTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases))] | ||
public static void DeserializeNonEmptyObjectToUnitFromString_ExpectNoException( | ||
JsonObject source, | ||
JsonSerializerOptions? options) | ||
{ | ||
var sourceString = JsonSerializer.Serialize(source); | ||
_ = JsonSerializer.Deserialize<Unit>(sourceString, options); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases))] | ||
public static void DeserializeNonEmptyObjectToUnitFromObject_ExpectNoException( | ||
JsonObject source, | ||
JsonSerializerOptions? options) | ||
{ | ||
_ = JsonSerializer.Deserialize<Unit>(source, options); | ||
} | ||
|
||
public static TheoryData<JsonObject, JsonSerializerOptions?> DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases | ||
{ | ||
get | ||
{ | ||
var objects = new JsonObject[] | ||
{ | ||
new() | ||
{ | ||
["id"] = "0AFB2897-BA58-4E10-A083-4C33341B6238" | ||
}, | ||
new() | ||
{ | ||
["id"] = "0AFB2897-BA58-4E10-A083-4C33341B6238", | ||
["body"] = new JsonObject | ||
{ | ||
["foo"] = 1, | ||
["bar"] = "2", | ||
["baz"] = new JsonArray(null, true, 1, "2"), | ||
}, | ||
["qux"] = new JsonArray(false, -1, 0, 1.1m, 1.2, "3"), | ||
["quux"] = new JsonArray(), | ||
["corge"] = -1.1m, | ||
["grault"] = -1.2, | ||
["garply"] = true, | ||
["waldo"] = false, | ||
["fred"] = null, | ||
} | ||
}; | ||
|
||
var result = new TheoryData<JsonObject, JsonSerializerOptions?>(); | ||
|
||
foreach (var obj in objects) | ||
{ | ||
foreach (var options in BuildJsonSerializerOptionsCollection()) | ||
{ | ||
result.Add(obj, options); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...re-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNullToUnit.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,24 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
partial class UnitSerializationTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(JsonSerializerOptionsCases))] | ||
public static void DeserializeNullToUnitFromString_ExpectNoException(JsonSerializerOptions? options) | ||
{ | ||
var source = JsonSerializer.Serialize<JsonNode?>(null); | ||
_ = JsonSerializer.Deserialize<Unit>(source, options); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(JsonSerializerOptionsCases))] | ||
public static void DeserializeNullToUnitFromNode_ExpectNoException(JsonSerializerOptions? options) | ||
{ | ||
JsonNode? source = null; | ||
_ = JsonSerializer.Deserialize<Unit>(source, options); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
.../Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeSimpleValueToUnit.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,65 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
partial class UnitSerializationTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))] | ||
public static void DeserializeSimpleValueToUnitFromString_ExpectNoException( | ||
JsonNode source, | ||
JsonSerializerOptions? options) | ||
{ | ||
var sourceString = JsonSerializer.Serialize(source); | ||
_ = JsonSerializer.Deserialize<Unit>(sourceString, options); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))] | ||
public static void DeserializeSimpleValueToUnitFromObject_ExpectNoException( | ||
JsonNode source, | ||
JsonSerializerOptions? options) | ||
{ | ||
_ = JsonSerializer.Deserialize<Unit>(source, options); | ||
} | ||
|
||
public static TheoryData<JsonNode, JsonSerializerOptions?> DeserializeSimpleValueToUnit_ExpectNoException_Cases | ||
{ | ||
get | ||
{ | ||
var values = new JsonNode[] | ||
{ | ||
true, | ||
false, | ||
|
||
int.MinValue, | ||
-1, | ||
0, | ||
1.1m, | ||
1.2, | ||
int.MaxValue, | ||
|
||
double.MinValue, | ||
double.MaxValue, | ||
|
||
"", | ||
"1", | ||
"0AFB2897-BA58-4E10-A083-4C33341B6238" | ||
}; | ||
|
||
var result = new TheoryData<JsonNode, JsonSerializerOptions?>(); | ||
|
||
foreach (var value in values) | ||
{ | ||
foreach (var options in BuildJsonSerializerOptionsCollection()) | ||
{ | ||
result.Add(value, options); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeUnit.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,24 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
partial class UnitSerializationTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(JsonSerializerOptionsCases))] | ||
public static void DeserializeUnitFromString_ExpectNoException(JsonSerializerOptions? options) | ||
{ | ||
var source = JsonSerializer.Serialize(new JsonObject()); // default options expected | ||
_ = JsonSerializer.Deserialize<Unit>(source, options); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(JsonSerializerOptionsCases))] | ||
public static void DeserializeUnitFromNode_ExpectNoException(JsonSerializerOptions? options) | ||
{ | ||
var source = new JsonObject(); | ||
_ = JsonSerializer.Deserialize<Unit>(source, options); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.SerializeUnit.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,26 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
partial class UnitSerializationTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(JsonSerializerOptionsCases))] | ||
public static void SerializeUnitToString_ExpectEmptyJsonObject(JsonSerializerOptions? options) | ||
{ | ||
var actual = JsonSerializer.Serialize(default(Unit), options); | ||
var expected = JsonSerializer.Serialize(new JsonObject()); // default options expected | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(JsonSerializerOptionsCases))] | ||
public static void SerializeUnitToNode_ExpectEmptyJsonObject(JsonSerializerOptions? options) | ||
{ | ||
var actual = JsonSerializer.SerializeToNode(default(Unit), options); | ||
var expected = new JsonObject(); | ||
Assert.Equal(expected, actual); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.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,20 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
public static partial class UnitSerializationTests | ||
{ | ||
public static TheoryData<JsonSerializerOptions?> JsonSerializerOptionsCases | ||
=> | ||
new(BuildJsonSerializerOptionsCollection().ToArray()); | ||
|
||
private static IEnumerable<JsonSerializerOptions?> BuildJsonSerializerOptionsCollection() | ||
{ | ||
yield return null; | ||
yield return JsonSerializerOptions.Default; | ||
yield return new(JsonSerializerDefaults.General); | ||
yield return new(JsonSerializerDefaults.Web); | ||
} | ||
} |
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