diff --git a/src/core-unit/Unit.Tests/Unit.Tests.csproj b/src/core-unit/Unit.Tests/Unit.Tests.csproj index eb4c6c0a..ba90b0b7 100644 --- a/src/core-unit/Unit.Tests/Unit.Tests.csproj +++ b/src/core-unit/Unit.Tests/Unit.Tests.csproj @@ -16,7 +16,7 @@ - $(NoWarn);xUnit1044 + $(NoWarn);xUnit1044;xUnit1045 diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeArrayToUnit.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeArrayToUnit.cs index b3a4803d..69fc0edd 100644 --- a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeArrayToUnit.cs +++ b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeArrayToUnit.cs @@ -25,6 +25,25 @@ public static void DeserializeArrayToUnitFromObject_ExpectNoException( _ = JsonSerializer.Deserialize(source, options); } + [Theory] + [MemberData(nameof(DeserializeArrayToUnit_ExpectNoException_Cases))] + public static void DeserializeDtoWithArrayToDtoWithUnitFromString_ExpectNoException( + JsonArray source, + JsonSerializerOptions? options) + { + var sourceString = JsonSerializer.Serialize(BuildDtoWithValueNode(source)); + _ = JsonSerializer.Deserialize(sourceString, options); + } + + [Theory] + [MemberData(nameof(DeserializeArrayToUnit_ExpectNoException_Cases))] + public static void DeserializeDtoWithArrayToDtoWithUnitFromObject_ExpectNoException( + JsonArray source, + JsonSerializerOptions? options) + { + _ = JsonSerializer.Deserialize(BuildDtoWithValueNode(source), options); + } + public static TheoryData DeserializeArrayToUnit_ExpectNoException_Cases { get @@ -39,7 +58,7 @@ public static void DeserializeArrayToUnitFromObject_ExpectNoException( foreach (var array in arrays) { - foreach (var options in BuildJsonSerializerOptionsCollection()) + foreach (var options in EnumerateJsonSerializerOptionsCases()) { result.Add(array, options); } diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNonEmptyObjectToUnit.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNonEmptyObjectToUnit.cs index bd16e33c..d25fdb3e 100644 --- a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNonEmptyObjectToUnit.cs +++ b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNonEmptyObjectToUnit.cs @@ -25,6 +25,25 @@ public static void DeserializeNonEmptyObjectToUnitFromObject_ExpectNoException( _ = JsonSerializer.Deserialize(source, options); } + [Theory] + [MemberData(nameof(DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases))] + public static void DeserializeDtoWithNonEmptyObjectToDtoWithUnitFromString_ExpectNoException( + JsonObject source, + JsonSerializerOptions? options) + { + var sourceString = JsonSerializer.Serialize(BuildDtoWithValueNode(source)); + _ = JsonSerializer.Deserialize(sourceString, options); + } + + [Theory] + [MemberData(nameof(DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases))] + public static void DeserializeDtoWithNonEmptyObjectToDtoWithUnitFromObject_ExpectNoException( + JsonObject source, + JsonSerializerOptions? options) + { + _ = JsonSerializer.Deserialize(BuildDtoWithValueNode(source), options); + } + public static TheoryData DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases { get @@ -58,7 +77,7 @@ public static void DeserializeNonEmptyObjectToUnitFromObject_ExpectNoException( foreach (var obj in objects) { - foreach (var options in BuildJsonSerializerOptionsCollection()) + foreach (var options in EnumerateJsonSerializerOptionsCases()) { result.Add(obj, options); } diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNullToUnit.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNullToUnit.cs index 1efc4a2d..1ceb51ab 100644 --- a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNullToUnit.cs +++ b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeNullToUnit.cs @@ -10,15 +10,29 @@ partial class UnitSerializationTests [MemberData(nameof(JsonSerializerOptionsCases))] public static void DeserializeNullToUnitFromString_ExpectNoException(JsonSerializerOptions? options) { - var source = JsonSerializer.Serialize(null); - _ = JsonSerializer.Deserialize(source, options); + var sourceString = JsonSerializer.Serialize(null); + _ = JsonSerializer.Deserialize(sourceString, options); } [Theory] [MemberData(nameof(JsonSerializerOptionsCases))] public static void DeserializeNullToUnitFromNode_ExpectNoException(JsonSerializerOptions? options) { - JsonNode? source = null; - _ = JsonSerializer.Deserialize(source, options); + _ = JsonSerializer.Deserialize((JsonNode?)null, options); + } + + [Theory] + [MemberData(nameof(JsonSerializerOptionsCases))] + public static void DeserializeDtoWithNullToDtoWithUnitFromString_ExpectNoException(JsonSerializerOptions? options) + { + var sourceString = JsonSerializer.Serialize(BuildDtoWithNullValueNode()); + _ = JsonSerializer.Deserialize(sourceString, options); + } + + [Theory] + [MemberData(nameof(JsonSerializerOptionsCases))] + public static void DeserializeDtoWithNullToDtoWithUnitFromNode_ExpectNoException(JsonSerializerOptions? options) + { + _ = JsonSerializer.Deserialize(BuildDtoWithNullValueNode(), options); } } diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeSimpleValueToUnit.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeSimpleValueToUnit.cs deleted file mode 100644 index 7cb8fd94..00000000 --- a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeSimpleValueToUnit.cs +++ /dev/null @@ -1,65 +0,0 @@ -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(sourceString, options); - } - - [Theory] - [MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))] - public static void DeserializeSimpleValueToUnitFromObject_ExpectNoException( - JsonNode source, - JsonSerializerOptions? options) - { - _ = JsonSerializer.Deserialize(source, options); - } - - public static TheoryData 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(); - - foreach (var value in values) - { - foreach (var options in BuildJsonSerializerOptionsCollection()) - { - result.Add(value, options); - } - } - - return result; - } - } -} diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeUnit.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeUnit.cs index 68fc55d1..ec956461 100644 --- a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeUnit.cs +++ b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeUnit.cs @@ -21,4 +21,20 @@ public static void DeserializeUnitFromNode_ExpectNoException(JsonSerializerOptio var source = new JsonObject(); _ = JsonSerializer.Deserialize(source, options); } + + [Theory] + [MemberData(nameof(JsonSerializerOptionsCases))] + public static void DeserializeDtoWithUnitFromString_ExpectNoException(JsonSerializerOptions? options) + { + var source = JsonSerializer.Serialize(BuildDtoWithUnitValueNode()); // default options expected + _ = JsonSerializer.Deserialize(source, options); + } + + [Theory] + [MemberData(nameof(JsonSerializerOptionsCases))] + public static void DeserializeDtoWithUnitFromNode_ExpectNoException(JsonSerializerOptions? options) + { + var source = BuildDtoWithUnitValueNode(); + _ = JsonSerializer.Deserialize(source, options); + } } diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeValueToUnit.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeValueToUnit.cs new file mode 100644 index 00000000..52cdc0cd --- /dev/null +++ b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.DeserializeValueToUnit.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace PrimeFuncPack.Core.Tests; + +partial class UnitSerializationTests +{ + [Theory] + [MemberData(nameof(DeserializeValueToUnit_ExpectNoException_Cases))] + public static void DeserializeValueToUnitFromString_ExpectNoException( + JsonValue source, + JsonSerializerOptions? options) + { + var sourceString = JsonSerializer.Serialize(source); + _ = JsonSerializer.Deserialize(sourceString, options); + } + + [Theory] + [MemberData(nameof(DeserializeValueToUnit_ExpectNoException_Cases))] + public static void DeserializeValueToUnitFromObject_ExpectNoException( + JsonValue source, + JsonSerializerOptions? options) + { + _ = JsonSerializer.Deserialize(source, options); + } + + [Theory] + [MemberData(nameof(DeserializeValueToUnit_ExpectNoException_Cases))] + public static void DeserializeDtoWithValueToDtoWithUnitFromString_ExpectNoException( + JsonValue source, + JsonSerializerOptions? options) + { + var sourceString = JsonSerializer.Serialize(BuildDtoWithValueNode(source)); + _ = JsonSerializer.Deserialize(sourceString, options); + } + + [Theory] + [MemberData(nameof(DeserializeValueToUnit_ExpectNoException_Cases))] + public static void DeserializeDtoWithValueToDtoWithUnitFromObject_ExpectNoException( + JsonValue source, + JsonSerializerOptions? options) + { + _ = JsonSerializer.Deserialize(BuildDtoWithValueNode(source), options); + } + + public static TheoryData DeserializeValueToUnit_ExpectNoException_Cases + { + get + { + var result = new TheoryData(); + + foreach (var value in EnumerateValues()) + { + foreach (var options in EnumerateJsonSerializerOptionsCases()) + { + result.Add(value, options); + } + } + + return result; + + static IEnumerable EnumerateValues() + { + yield return JsonValue.Create(true); + yield return JsonValue.Create(false); + + yield return JsonValue.Create(int.MinValue); + yield return JsonValue.Create(-1); + yield return JsonValue.Create(0); + yield return JsonValue.Create(1.1m); + yield return JsonValue.Create(1.2); + yield return JsonValue.Create(int.MaxValue); + + yield return JsonValue.Create(double.MinValue); + yield return JsonValue.Create(double.MaxValue); + + yield return JsonValue.Create(""); + yield return JsonValue.Create("1"); + yield return JsonValue.Create("0AFB2897-BA58-4E10-A083-4C33341B6238"); + } + } + } +} diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.SerializeUnit.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.SerializeUnit.cs index c4d8b9a1..3fc7d161 100644 --- a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.SerializeUnit.cs +++ b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.SerializeUnit.cs @@ -23,4 +23,22 @@ public static void SerializeUnitToNode_ExpectEmptyJsonObject(JsonSerializerOptio var expected = new JsonObject(); Assert.Equal(expected, actual); } + + [Theory] + [MemberData(nameof(JsonSerializerOptionsCases))] + public static void SerializeDtoWithUnitToString_ExpectEmptyJsonObject(JsonSerializerOptions? options) + { + var actual = JsonSerializer.Serialize(new DtoWithUnit(), options); + var expected = JsonSerializer.Serialize(BuildDtoWithUnitValueNode()); // default options expected + Assert.Equal(expected, actual); + } + + [Theory] + [MemberData(nameof(JsonSerializerOptionsCases))] + public static void SerializeDtoWithUnitToNode_ExpectEmptyJsonObject(JsonSerializerOptions? options) + { + var actual = JsonSerializer.SerializeToNode(new DtoWithUnit(), options); + var expected = BuildDtoWithUnitValueNode(); + Assert.Equal(expected, actual); + } } diff --git a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.cs b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.cs index f31b6cbc..08ca3f64 100644 --- a/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.cs +++ b/src/core-unit/Unit.Tests/UnitSerializationTests/UnitSerializationTests.cs @@ -1,20 +1,55 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; namespace PrimeFuncPack.Core.Tests; public static partial class UnitSerializationTests { + private const string ValueName = "value"; + public static TheoryData JsonSerializerOptionsCases => - new(BuildJsonSerializerOptionsCollection().ToArray()); + new(EnumerateJsonSerializerOptionsCases().ToArray()); - private static IEnumerable BuildJsonSerializerOptionsCollection() + private static IEnumerable EnumerateJsonSerializerOptionsCases() { yield return null; yield return JsonSerializerOptions.Default; yield return new(JsonSerializerDefaults.General); yield return new(JsonSerializerDefaults.Web); } + + private static JsonObject BuildDtoWithUnitValueNode() + => + new() { [ValueName] = new JsonObject() }; + + private static JsonObject BuildDtoWithNullValueNode() + => + new() { [ValueName] = null }; + + private static JsonObject BuildDtoWithValueNode(JsonObject value) + => + InnerBuildDtoWithValueNode(value); + + private static JsonObject BuildDtoWithValueNode(JsonArray value) + => + InnerBuildDtoWithValueNode(value); + + private static JsonObject BuildDtoWithValueNode(JsonValue value) + => + InnerBuildDtoWithValueNode(value); + + private static JsonObject InnerBuildDtoWithValueNode(JsonNode value) + => + new() { [ValueName] = value.DeepClone() }; + + private sealed record DtoWithUnit + { + [JsonPropertyName(ValueName)] + public Unit Value { get; init; } + } } diff --git a/src/core-unit/Unit/Unit.csproj b/src/core-unit/Unit/Unit.csproj index b2b2047d..1ee763a9 100644 --- a/src/core-unit/Unit/Unit.csproj +++ b/src/core-unit/Unit/Unit.csproj @@ -18,7 +18,7 @@ PrimeFuncPack Core.Unit is a core library for .NET consisting of Unit type targeted for use in functional programming. System PrimeFuncPack.Core.Unit - 3.0.0-rc.1 + 3.0.0