235 lines
No EOL
6.8 KiB
C#
235 lines
No EOL
6.8 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using NUnit.Framework;
|
|
|
|
namespace JD.Serialization
|
|
{
|
|
public abstract class SerializerTestsBase
|
|
{
|
|
[Test]
|
|
public void Test_can_read_write_a_single_value()
|
|
{
|
|
var model = new MyModel { MyValue = 153 };
|
|
|
|
var serialized = ReadWrite(model);
|
|
|
|
Assert.AreEqual(153, serialized.MyValue);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_write_an_object_with_multiple_values()
|
|
{
|
|
var model = new MyModel { MyValue = 153, MyString = "ASD" };
|
|
var serialized = ReadWrite(model);
|
|
|
|
Assert.AreEqual(153, serialized.MyValue);
|
|
Assert.AreEqual("ASD", serialized.MyString);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_write_a_list_as_a_property_of_an_object()
|
|
{
|
|
var model = new MyModel
|
|
{
|
|
List = { new MyModel { MyValue = 12 } }
|
|
};
|
|
|
|
var serialized = ReadWrite(model);
|
|
Assert.AreEqual(12, serialized.List[0].MyValue);
|
|
}
|
|
|
|
[Test]
|
|
public virtual void Test_can_read_write_a_list_as_the_root_value()
|
|
{
|
|
var list = new UAFList<MyModel>
|
|
{
|
|
new MyModel { MyValue = 1 },
|
|
new MyModel { MyValue = 2 },
|
|
};
|
|
|
|
var serialized = ReadWrite(list);
|
|
|
|
Assert.AreEqual(2, serialized.Count);
|
|
Assert.AreEqual(1, serialized[0].MyValue);
|
|
Assert.AreEqual(2, serialized[1].MyValue);
|
|
}
|
|
|
|
[Test]
|
|
public virtual void Test_a_list_can_contain_a_child_class_of_the_list_type()
|
|
{
|
|
var list = new UAFList<MyModel>
|
|
{
|
|
new ChildModel { MyValue = 1 },
|
|
};
|
|
|
|
var serialized = ReadWrite(list);
|
|
|
|
Assert.IsInstanceOf<ChildModel>(serialized[0]);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_fields_from_inherited_class_are_serialized()
|
|
{
|
|
var model = new MyModel
|
|
{
|
|
List = new UAFList<MyModel>
|
|
{
|
|
new ChildModel { ChildValue = 999 }
|
|
}
|
|
};
|
|
|
|
var serialized = ReadWrite(model);
|
|
|
|
Assert.AreEqual(999, ((ChildModel)serialized.List[0]).ChildValue);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_write_a_vector_2()
|
|
{
|
|
var model = new MyModel { Vector2 = new Vec2d(18.838869000f, 0.000000f) };
|
|
var serialized = ReadWrite(model);
|
|
|
|
Assert.AreEqual(model.Vector2, serialized.Vector2);
|
|
}
|
|
|
|
[Test]
|
|
public virtual void Test_can_read_write_an_UAF_Dictionary()
|
|
{
|
|
var model = new MyModel();
|
|
model.Dictionary.Add(1, "1");
|
|
var serialized = ReadWrite(model);
|
|
|
|
Assert.AreEqual("1", serialized.Dictionary[1]);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_write_boolean_values()
|
|
{
|
|
Assert.IsTrue(ReadWrite(new MyModel { BooleanValue = true }).BooleanValue);
|
|
Assert.IsFalse(ReadWrite(new MyModel { BooleanValue = false }).BooleanValue);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_write_colors()
|
|
{
|
|
var model = new MyModel { Color = Color.BlanchedAlmond };
|
|
var serialized = ReadWrite(model);
|
|
|
|
Assert.AreEqual(Color.BlanchedAlmond.ToArgb(), serialized.Color.ToArgb());
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_write_ObjectPath()
|
|
{
|
|
var model = new ModelWithPath { MyPath = new ObjectPath("..|Test|Other") };
|
|
var serialized = ReadWrite(model);
|
|
|
|
Assert.AreEqual("..|Test|Other", serialized.MyPath.ToString());
|
|
Assert.AreEqual(2, serialized.MyPath.Levels.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_write_StringId()
|
|
{
|
|
var model = new MyModel { StringId = null };
|
|
var serialized = ReadWrite(model);
|
|
uint serializedId = serialized.StringId;
|
|
Assert.AreEqual(serializedId, StringID.InvalidId);
|
|
|
|
model = new MyModel { StringId = new StringID() };
|
|
serialized = ReadWrite(model);
|
|
Assert.AreEqual(model.StringId.ToString(), serialized.StringId.ToString());
|
|
Assert.AreEqual(model.StringId.getValue(), serialized.StringId.getValue());
|
|
|
|
model = new MyModel { StringId = new StringID("test") };
|
|
serialized = ReadWrite(model);
|
|
Assert.AreEqual(model.StringId.getValue(), serialized.StringId.getValue());
|
|
}
|
|
|
|
protected abstract T ReadWrite<T>(T model)
|
|
where T : new();
|
|
}
|
|
|
|
#region Mocks
|
|
|
|
internal class ModelWithPath
|
|
{
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public ObjectPath MyPath { get; set; }
|
|
}
|
|
|
|
internal class MyModel
|
|
{
|
|
static MyModel()
|
|
{
|
|
ObjectFactory.getFactories().Add(new ModelFactory());
|
|
}
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public int MyValue { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public string MyString { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public bool BooleanValue { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public double DoubleValue { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public UAFList<MyModel> List { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public Vec2d Vector2 { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public Color Color { get; set; }
|
|
|
|
[OriginalPropertyName("__ORIGINAL__UGLY__NAME")]
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public int NewName { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public StringID StringId { get; set; }
|
|
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public UAFDictionary<int, string> Dictionary { get; set; }
|
|
|
|
public MyModel()
|
|
{
|
|
List = new UAFList<MyModel>();
|
|
Dictionary = new UAFDictionary<int, string>();
|
|
}
|
|
}
|
|
|
|
internal class ChildModel : MyModel
|
|
{
|
|
[UAFSerializable(uint.MaxValue)]
|
|
public int ChildValue { get; set; }
|
|
}
|
|
|
|
internal class ModelFactory : ObjectFactory
|
|
{
|
|
public ModelFactory()
|
|
{
|
|
addType((uint)1, typeof(MyModel));
|
|
addType((uint)2, typeof(ChildModel));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Utils
|
|
|
|
internal static class SerializerTestExtensions
|
|
{
|
|
public static string NormalizeLineEndings(this string value)
|
|
{
|
|
return (value ?? "").Replace("\r\n", "\n").Replace("\n", Environment.NewLine);
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
} |