103 lines
2.8 KiB
C#
103 lines
2.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using JD.Core.ProjectManaging;
|
|
using JD.Core.Tests;
|
|
using NUnit.Framework;
|
|
|
|
namespace JD.Serialization
|
|
{
|
|
[TestFixture]
|
|
public class LuaSerializerTests : SerializerTestsBase
|
|
{
|
|
private IDisposable _restoreProject;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_restoreProject = ProjectManager.Instance.SetDataRoot(System.IO.Path.Combine(System.IO.Path.GetPathRoot(Environment.CurrentDirectory), @"JD_DATA\\main\\data"));
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_restoreProject.Dispose();
|
|
}
|
|
|
|
[Test]
|
|
public void Test_the_serializer_produce_the_same_lua_than_the_engine()
|
|
{
|
|
// This is important because we dont want a lot of differences depending on whether the engine or the tool saves a the same file.
|
|
string lua = @"TABLE =
|
|
{
|
|
NAME=""MyModel"",
|
|
MyModel =
|
|
{
|
|
MyValue = 12,
|
|
Vector2 = vector2dNew(25.988279,-0.017099),
|
|
},
|
|
}
|
|
".NormalizeLineEndings();
|
|
|
|
var model = Read<MyModel>(lua);
|
|
string written = Write(model);
|
|
|
|
Assert.AreEqual(lua, written);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_original_name_is_written_instead_of_the_cs_name_when_an_attribute_is_present()
|
|
{
|
|
var lua = Write(new MyModel { NewName = 12 });
|
|
Assert.That(lua.Contains("__ORIGINAL__UGLY__NAME"));
|
|
Assert.AreEqual(12, Read<MyModel>(lua).NewName);
|
|
}
|
|
|
|
[Test]
|
|
public void Test_can_read_a_lua_dictionary()
|
|
{
|
|
const string lua = @"
|
|
TABLE =
|
|
{
|
|
NAME=""MyModel"",
|
|
MyModel =
|
|
{
|
|
Dictionary =
|
|
{
|
|
{ KEY = 1, VAL = ""100"" },
|
|
{ KEY = 2, VAL = ""200"" },
|
|
},
|
|
},
|
|
}";
|
|
|
|
var model = Read<MyModel>(lua);
|
|
Assert.AreEqual(2, model.Dictionary.Count);
|
|
Assert.AreEqual("100", model.Dictionary[1]);
|
|
Assert.AreEqual("200", model.Dictionary[2]);
|
|
}
|
|
|
|
protected override T ReadWrite<T>(T model)
|
|
{
|
|
string content = Write(model);
|
|
return Read<T>(content);
|
|
}
|
|
|
|
private string Write<T>(T model)
|
|
{
|
|
var serializer = new LuaSerializer();
|
|
serializer.RethrowExceptions = true;
|
|
LuaSerializer.AssembliesToSearchForType.Add(GetType().Assembly);
|
|
return serializer.Serialize(model);
|
|
}
|
|
|
|
private T Read<T>(string content)
|
|
where T : new()
|
|
{
|
|
var serializer = new LuaSerializer();
|
|
serializer.RethrowExceptions = true;
|
|
LuaSerializer.AssembliesToSearchForType.Add(GetType().Assembly);
|
|
var toRead = new T();
|
|
serializer.Deserialize(content, toRead);
|
|
return toRead;
|
|
}
|
|
}
|
|
}
|