1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 14:08:34 +01:00
MLEM/Tests/DataTests.cs

83 lines
2.9 KiB
C#
Raw Normal View History

2021-03-18 18:31:59 +01:00
using System;
using System.IO;
using Microsoft.Xna.Framework;
using MLEM.Data.Json;
using MLEM.Misc;
using Newtonsoft.Json;
using NUnit.Framework;
using Vector2 = Microsoft.Xna.Framework.Vector2;
2021-03-18 18:31:59 +01:00
2022-10-27 10:22:25 +02:00
namespace Tests;
2021-03-18 18:31:59 +01:00
2022-10-27 10:22:25 +02:00
public class DataTests {
2022-10-27 10:22:25 +02:00
private readonly TestObject testObject = new() {
Vec = new Vector2(10, 20),
Point = new Point(20, 30),
Dir = Direction2.Left,
OtherTest = new TestObject {
Vec = new Vector2(70, 30),
Dir = Direction2.Right
}
2022-10-27 10:22:25 +02:00
};
2022-10-27 10:22:25 +02:00
[Test]
public void TestJsonSerializers() {
var serializer = JsonConverters.AddAll(new JsonSerializer());
2021-03-18 18:31:59 +01:00
2022-10-27 10:22:25 +02:00
var writer = new StringWriter();
serializer.Serialize(writer, this.testObject);
var ret = writer.ToString();
2021-03-18 18:31:59 +01:00
2022-10-27 10:22:25 +02:00
Assert.AreEqual(ret, "{\"Vec\":\"10 20\",\"Point\":\"20 30\",\"OtherTest\":{\"Vec\":\"70 30\",\"Point\":\"0 0\",\"OtherTest\":null,\"Dir\":\"Right\"},\"Dir\":\"Left\"}");
var read = serializer.Deserialize<TestObject>(new JsonTextReader(new StringReader(ret)));
Assert.AreEqual(this.testObject, read);
}
2021-03-18 18:31:59 +01:00
2022-10-27 10:22:25 +02:00
[Test]
public void TestJsonTypeSafety() {
var safeData = new JsonTypeSafeGenericDataHolder();
2022-11-22 20:21:25 +01:00
// data holder should wrap the time span to ensure that it stays a time span
2022-10-27 10:22:25 +02:00
safeData.SetData("Time", TimeSpan.FromMinutes(5));
// also check path that creates an instance through reflection
safeData.SetData<object>("Time2", TimeSpan.FromMinutes(15));
2022-11-22 20:21:25 +01:00
var safeRead = DataTests.SerializeAndDeserialize(safeData);
2022-10-27 10:22:25 +02:00
Assert.IsInstanceOf<TimeSpan>(safeRead.GetData<object>("Time"));
Assert.AreEqual(TimeSpan.FromMinutes(5), safeRead.GetData<TimeSpan>("Time"));
Assert.IsInstanceOf<TimeSpan>(safeRead.GetData<object>("Time2"));
Assert.AreEqual(TimeSpan.FromMinutes(15), safeRead.GetData<TimeSpan>("Time2"));
2022-10-27 10:22:25 +02:00
}
2022-11-22 20:21:25 +01:00
private static T SerializeAndDeserialize<T>(T t) {
var serializer = new JsonSerializer {TypeNameHandling = TypeNameHandling.Auto};
2022-10-27 10:22:25 +02:00
var writer = new StringWriter();
serializer.Serialize(writer, t);
return serializer.Deserialize<T>(new JsonTextReader(new StringReader(writer.ToString())));
}
2021-03-18 18:31:59 +01:00
2022-10-27 10:22:25 +02:00
private class TestObject {
2021-03-18 18:31:59 +01:00
2022-10-27 10:22:25 +02:00
public Vector2 Vec;
public Point Point;
public Direction2 Dir { get; set; }
public TestObject OtherTest;
protected bool Equals(TestObject other) {
return this.Vec.Equals(other.Vec) && this.Point.Equals(other.Point) && object.Equals(this.OtherTest, other.OtherTest) && this.Dir == other.Dir;
}
public override bool Equals(object obj) {
return object.ReferenceEquals(this, obj) || obj is TestObject other && this.Equals(other);
}
public override int GetHashCode() {
return HashCode.Combine(this.Vec, this.Point, this.OtherTest, (int) this.Dir);
2021-03-18 18:31:59 +01:00
}
}
2022-10-27 10:22:25 +02:00
2022-06-17 18:23:47 +02:00
}