Skip to content

Commit

Permalink
Add serialization tests for the added JsonConverters
Browse files Browse the repository at this point in the history
  • Loading branch information
Morilli committed Sep 13, 2024
1 parent 2c02817 commit eb1abac
Showing 1 changed file with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Emulation.Common.Json;

namespace BizHawk.Tests.Client.Common.config
{
Expand Down Expand Up @@ -95,5 +97,75 @@ public void TestRoundTripSerialization()
Assert.AreEqual(s, Ser(Deser(s, type)), $"{type} failed serialization round-trip");
}
}

[TestMethod]
[DataRow("0.8")]
[DataRow("1.00000036")]
[DataRow("1.8")]
public void TestRoundTripSerializationFloatConverter(string floatValue)
{
float deserialized = JsonSerializer.Deserialize<float>(floatValue, ConfigService.SerializerOptions);
string serialized = JsonSerializer.Serialize(deserialized, ConfigService.SerializerOptions);
Assert.AreEqual(floatValue, serialized);
}

[TestMethod]
[DataRow("[1,2,3]")]
[DataRow("[]")]
[DataRow("null")]
[DataRow("[255,0,127,128,1]")]
public void TestRoundTripSerializationByteArrayConverter(string byteArrayValue)
{
byte[]? deserialized = JsonSerializer.Deserialize<byte[]>(byteArrayValue, ConfigService.SerializerOptions);
string serialized = JsonSerializer.Serialize(deserialized, ConfigService.SerializerOptions);
Assert.AreEqual(byteArrayValue, serialized);
}

[TestMethod]
public void TestSerializationTypeConverter()
{
var color = Color.FromArgb(200, 255, 13, 42);
string serialized = JsonSerializer.Serialize(color, ConfigService.SerializerOptions);
Assert.AreEqual("\"200; 255; 13; 42\"", serialized);

var newColor = JsonSerializer.Deserialize<Color>(serialized, ConfigService.SerializerOptions);
Assert.AreEqual(color, newColor);
}

private static bool Equals<T>(T[,] array1, T[,] array2)
{
return array1.Rank == array2.Rank
&& Enumerable.Range(0, array1.Rank).All(dimension => array1.GetLength(dimension) == array2.GetLength(dimension))
&& array1.Cast<T>().SequenceEqual(array2.Cast<T>());
}

[TestMethod]
public void TestSerialization2DArrayConverter()
{
var options = new JsonSerializerOptions
{
Converters = { new Array2DJsonConverter<byte>() }
};
var optionsWithByteArrayConverter = new JsonSerializerOptions
{
Converters = { new Array2DJsonConverter<byte>(), new ByteArrayAsNormalArrayJsonConverter() }
};

byte[,] testByteArray =
{
{ 1, 2, 3 },
{ 255, 0, 128 }
};

string serialized = JsonSerializer.Serialize(testByteArray, options);
Assert.AreEqual("[\"AQID\",\"/wCA\"]", serialized);
byte[,] deserialized = JsonSerializer.Deserialize<byte[,]>(serialized, options)!;
Assert.IsTrue(Equals(testByteArray, deserialized));

string serialized2 = JsonSerializer.Serialize(testByteArray, optionsWithByteArrayConverter);
Assert.AreEqual("[[1,2,3],[255,0,128]]", serialized2);
byte[,] deserialized2 = JsonSerializer.Deserialize<byte[,]>(serialized2, optionsWithByteArrayConverter)!;
Assert.IsTrue(Equals(testByteArray, deserialized2));
}
}
}

0 comments on commit eb1abac

Please sign in to comment.