Skip to content

Commit

Permalink
Improve serialization tests: Use a more typed approach for Json nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jun 8, 2024
1 parent 1ff9824 commit 70f8657
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void DeserializeDtoWithArrayToDtoWithUnitFromString_ExpectNoExcept
JsonArray source,
JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize(BuildDtoWithArbitraryValueNode(source));
var sourceString = JsonSerializer.Serialize(BuildDtoWithValueNode(source));
_ = JsonSerializer.Deserialize<DtoWithUnit>(sourceString, options);
}

Expand All @@ -41,7 +41,7 @@ public static void DeserializeDtoWithArrayToDtoWithUnitFromObject_ExpectNoExcept
JsonArray source,
JsonSerializerOptions? options)
{
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithArbitraryValueNode(source), options);
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithValueNode(source), options);
}

public static TheoryData<JsonArray, JsonSerializerOptions?> DeserializeArrayToUnit_ExpectNoException_Cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void DeserializeDtoWithNonEmptyObjectToDtoWithUnitFromString_Expec
JsonObject source,
JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize(BuildDtoWithArbitraryValueNode(source));
var sourceString = JsonSerializer.Serialize(BuildDtoWithValueNode(source));
_ = JsonSerializer.Deserialize<DtoWithUnit>(sourceString, options);
}

Expand All @@ -41,7 +41,7 @@ public static void DeserializeDtoWithNonEmptyObjectToDtoWithUnitFromObject_Expec
JsonObject source,
JsonSerializerOptions? options)
{
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithArbitraryValueNode(source), options);
_ = JsonSerializer.Deserialize<DtoWithUnit>(BuildDtoWithValueNode(source), options);
}

public static TheoryData<JsonObject, JsonSerializerOptions?> DeserializeNonEmptyObjectToUnit_ExpectNoException_Cases
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;

Expand All @@ -9,7 +10,7 @@ partial class UnitSerializationTests
[Theory]
[MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))]
public static void DeserializeSimpleValueToUnitFromString_ExpectNoException(
JsonNode source,
JsonValue source,
JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize(source);
Expand All @@ -19,7 +20,7 @@ public static void DeserializeSimpleValueToUnitFromString_ExpectNoException(
[Theory]
[MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))]
public static void DeserializeSimpleValueToUnitFromObject_ExpectNoException(
JsonNode source,
JsonValue source,
JsonSerializerOptions? options)
{
_ = JsonSerializer.Deserialize<Unit>(source, options);
Expand All @@ -28,47 +29,47 @@ public static void DeserializeSimpleValueToUnitFromObject_ExpectNoException(
[Theory]
[MemberData(nameof(DeserializeSimpleValueToUnit_ExpectNoException_Cases))]
public static void DeserializeDtoWithSimpleValueToDtoWithUnitFromString_ExpectNoException(
JsonNode source,
JsonValue source,
JsonSerializerOptions? options)
{
var sourceString = JsonSerializer.Serialize(BuildDtoWithArbitraryValueNode(source));
var sourceString = JsonSerializer.Serialize(BuildDtoWithValueNode(source));
_ = JsonSerializer.Deserialize<DtoWithUnit>(sourceString, options);
}

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

public static TheoryData<JsonNode, JsonSerializerOptions?> DeserializeSimpleValueToUnit_ExpectNoException_Cases
public static TheoryData<JsonValue, JsonSerializerOptions?> DeserializeSimpleValueToUnit_ExpectNoException_Cases
{
get
{
var values = new JsonNode[]
{
true,
false,
IEnumerable<JsonValue> values =
[
JsonValue.Create(true),
JsonValue.Create(false),

int.MinValue,
-1,
0,
1.1m,
1.2,
int.MaxValue,
JsonValue.Create(int.MinValue),
JsonValue.Create(-1),
JsonValue.Create(0),
JsonValue.Create(1.1m),
JsonValue.Create(1.2),
JsonValue.Create(int.MaxValue),

double.MinValue,
double.MaxValue,
JsonValue.Create(double.MinValue),
JsonValue.Create(double.MaxValue),

"",
"1",
"0AFB2897-BA58-4E10-A083-4C33341B6238"
};
JsonValue.Create(""),
JsonValue.Create("1"),
JsonValue.Create("0AFB2897-BA58-4E10-A083-4C33341B6238")
];

var result = new TheoryData<JsonNode, JsonSerializerOptions?>();
var result = new TheoryData<JsonValue, JsonSerializerOptions?>();

foreach (var value in values)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ private static JsonObject BuildDtoWithNullValueNode()
=>
new() { [ValueName] = null };

private static JsonObject BuildDtoWithArbitraryValueNode(JsonNode value)
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() };

Expand Down

0 comments on commit 70f8657

Please sign in to comment.