using UnitTestSharp;
using Vellum;
namespace Azimuth.Vellum.UnitTests
{
public class ConverterTests : TestFixture
{
public static SaveSession Session()
{
return SaveConverters.For(typeof(ScalarConverter).Assembly).CreateSession();
}
///
/// Saves the value, writes the save as Lua text, and reads it back, so a value must survive the text.
///
public static ValueType ThroughLuaText(ValueType value)
{
SaveNode save = SaveNode.NewTable();
save.Entries["value"] = Session().ToNode(value);
SaveNode parsed = LuaSaveFormat.Parse(LuaSaveFormat.Format(save));
return Session().FromNode(parsed.RequireEntry("value"));
}
public class ScalarTests : TestFixture
{
public void KeepsEveryBit()
{
Scalar value = 0.1 + 0.2;
CheckEqual((double)value, (double)ThroughLuaText(value));
}
public void KeepsInfinity()
{
CheckTrue(Scalar.IsPositiveInfinity(ThroughLuaText(Scalar.PositiveInfinity)));
}
public void KeepsNegativeInfinity()
{
CheckTrue(Scalar.IsNegativeInfinity(ThroughLuaText(Scalar.NegativeInfinity)));
}
public void KeepsNegativeNumbers()
{
CheckEqual(-2.5, (double)ThroughLuaText((Scalar)(-2.5)));
}
}
public class VectorTests : TestFixture
{
public void KeepsBothCoordinates()
{
Vector loaded = ThroughLuaText(new Vector(1.5, -2.25));
CheckEqual(1.5, (double)loaded.X);
CheckEqual(-2.25, (double)loaded.Y);
}
public void SavesEachCoordinateUnderItsName()
{
SaveNode node = Session().ToNode(new Vector(1, 2));
CheckEqualCommutative(new[] { "X", "Y" }, node.Entries.Keys);
}
}
public class ComplexTests : TestFixture
{
public void KeepsBothParts()
{
Complex loaded = ThroughLuaText(new Complex(3, -4));
CheckEqual(3.0, (double)loaded.Real);
CheckEqual(-4.0, (double)loaded.Imaginary);
}
public void SavesEachPartUnderItsName()
{
SaveNode node = Session().ToNode(new Complex(1, 2));
CheckEqualCommutative(new[] { "Real", "Imaginary" }, node.Entries.Keys);
}
}
public class ForTests : TestFixture
{
public void ThisLibrary_NamesAllThreeConverters()
{
SaveSession session = Session();
CheckEqual(SaveNode.Kinds.Text, session.ToNode((Scalar)1).Kind);
CheckEqual(SaveNode.Kinds.Table, session.ToNode(new Vector(1, 2)).Kind);
CheckEqual(SaveNode.Kinds.Table, session.ToNode(new Complex(1, 2)).Kind);
}
}
}
}