Skip to content

Commit

Permalink
v3.0.0; Add serialization tests with dto
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jun 8, 2024
1 parent ae359c1 commit 1ff9824
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/core-unit/Unit.Tests/Unit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<PropertyGroup>
<NoWarn>$(NoWarn);xUnit1044</NoWarn>
<NoWarn>$(NoWarn);xUnit1044;xUnit1045</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ public static void DeserializeArrayToUnitFromObject_ExpectNoException(
_ = JsonSerializer.Deserialize<Unit>(source, options);
}

[Theory]
[MemberData(nameof(DeserializeArrayToUnit_ExpectNoException_Cases))]
public static void DeserializeDtoWithArrayToDtoWithUnitFromString_ExpectNoException(
JsonArray source,
JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize(BuildDtoWithArbitraryValueNode(source));
_ = JsonSerializer.Deserialize<DtoWithUnit>(sourceString, options);
}

[Theory]
[MemberData(nameof(DeserializeArrayToUnit_ExpectNoException_Cases))]
public static void DeserializeDtoWithArrayToDtoWithUnitFromObject_ExpectNoException(
JsonArray source,
JsonSerializerOptions? options)
{
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithArbitraryValueNode(source), options);
}

public static TheoryData<JsonArray, JsonSerializerOptions?> DeserializeArrayToUnit_ExpectNoException_Cases
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ public static void DeserializeNonEmptyObjectToUnitFromObject_ExpectNoException(
_ = JsonSerializer.Deserialize<Unit>(source, options);
}

[Theory]
[MemberData(nameof(DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases))]
public static void DeserializeDtoWithNonEmptyObjectToDtoWithUnitFromString_ExpectNoException(
JsonObject source,
JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize(BuildDtoWithArbitraryValueNode(source));
_ = JsonSerializer.Deserialize<DtoWithUnit>(sourceString, options);
}

[Theory]
[MemberData(nameof(DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases))]
public static void DeserializeDtoWithNonEmptyObjectToDtoWithUnitFromObject_ExpectNoException(
JsonObject source,
JsonSerializerOptions? options)
{
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithArbitraryValueNode(source), options);
}

public static TheoryData<JsonObject, JsonSerializerOptions?> DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,29 @@ partial class UnitSerializationTests
[MemberData(nameof(JsonSerializerOptionsCases))]
public static void DeserializeNullToUnitFromString_ExpectNoException(JsonSerializerOptions? options)
{
var source = JsonSerializer.Serialize<JsonNode?>(null);
_ = JsonSerializer.Deserialize<Unit>(source, options);
var sourceString = JsonSerializer.Serialize<JsonNode?>(null);
_ = JsonSerializer.Deserialize<Unit>(sourceString, options);
}

[Theory]
[MemberData(nameof(JsonSerializerOptionsCases))]
public static void DeserializeNullToUnitFromNode_ExpectNoException(JsonSerializerOptions? options)
{
JsonNode? source = null;
_ = JsonSerializer.Deserialize<Unit>(source, options);
_ = JsonSerializer.Deserialize<Unit>((JsonNode?)null, options);
}

[Theory]
[MemberData(nameof(JsonSerializerOptionsCases))]
public static void DeserializeDtoWithNullToDtoWithUnitFromString_ExpectNoException(JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize<JsonNode?>(BuildDtoWithNullValueNode());
_ = JsonSerializer.Deserialize<DtoWithUnit>(sourceString, options);
}

[Theory]
[MemberData(nameof(JsonSerializerOptionsCases))]
public static void DeserializeDtoWithNullToDtoWithUnitFromNode_ExpectNoException(JsonSerializerOptions? options)
{
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithNullValueNode(), options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ public static void DeserializeSimpleValueToUnitFromObject_ExpectNoException(
_ = JsonSerializer.Deserialize<Unit>(source, options);
}

[Theory]
[MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))]
public static void DeserializeDtoWithSimpleValueToDtoWithUnitFromString_ExpectNoException(
JsonNode source,
JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize(BuildDtoWithArbitraryValueNode(source));
_ = JsonSerializer.Deserialize<DtoWithUnit>(sourceString, options);
}

[Theory]
[MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))]
public static void DeserializeDtoWithSimpleValueToDtoWithUnitFromObject_ExpectNoException(
JsonNode source,
JsonSerializerOptions? options)
{
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithArbitraryValueNode(source), options);
}

public static TheoryData<JsonNode, JsonSerializerOptions?> DeserializeSimpleValueToUnit_ExpectNoException_Cases
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ public static void DeserializeUnitFromNode_ExpectNoException(JsonSerializerOptio
var source = new JsonObject();
_ = JsonSerializer.Deserialize<Unit>(source, options);
}

[Theory]
[MemberData(nameof(JsonSerializerOptionsCases))]
public static void DeserializeDtoWithUnitFromString_ExpectNoException(JsonSerializerOptions? options)
{
var source = JsonSerializer.Serialize(BuildDtoWithUnitValueNode()); // default options expected
_ = JsonSerializer.Deserialize<DtoWithUnit>(source, options);
}

[Theory]
[MemberData(nameof(JsonSerializerOptionsCases))]
public static void DeserializeDtoWithUnitFromNode_ExpectNoException(JsonSerializerOptions? options)
{
var source = BuildDtoWithUnitValueNode();
_ = JsonSerializer.Deserialize<DtoWithUnit>(source, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
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<JsonSerializerOptions?> JsonSerializerOptionsCases
=>
new(BuildJsonSerializerOptionsCollection().ToArray());
Expand All @@ -17,4 +22,22 @@ public static TheoryData<JsonSerializerOptions?> JsonSerializerOptionsCases
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 BuildDtoWithArbitraryValueNode(JsonNode value)
=>
new() { [ValueName] = value.DeepClone() };

private sealed record DtoWithUnit
{
[JsonPropertyName(ValueName)]
public Unit Value { get; init; }
}
}
2 changes: 1 addition & 1 deletion src/core-unit/Unit/Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Description>PrimeFuncPack Core.Unit is a core library for .NET consisting of Unit type targeted for use in functional programming.</Description>
<RootNamespace>System</RootNamespace>
<AssemblyName>PrimeFuncPack.Core.Unit</AssemblyName>
<Version>3.0.0-rc.1</Version>
<Version>3.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1ff9824

Please sign in to comment.