ln.json/ln.json.tests/JSONTests.cs

210 lines
7.0 KiB
C#

using System;
using System.Reflection;
using ln.json.mapping;
using NUnit.Framework;
namespace ln.json.tests
{
public class JSONTests
{
enum testEnum1 { A, B, C, D, E, F }
enum testEnum2 : ushort {
NONE = 0,
A = 1,
B = 2,
C = 4,
D = 5,
E = 6
}
[Flags]
enum testEnum3 : ushort {
NONE = 0,
A = 1,
B = 2,
C = 4,
D = 5,
E = 6
}
[SetUp]
public void Setup()
{
}
[Test]
public void Test_00_Primitives()
{
JSONValue jsonFloat = JSONParser.Parse("1234.564");
Assert.IsTrue(jsonFloat is JSONNumber);
Assert.AreEqual(1234.564, jsonFloat.ToNative());
JSONValue jsonInteger = JSONParser.Parse("635462");
Assert.IsTrue(jsonFloat is JSONNumber);
Assert.AreEqual(635462, jsonInteger.ToNative());
Assert.Pass();
}
[Test]
public void TestArrayParser()
{
JSONParser.Parse("[]");
JSONParser.Parse("[ ]");
JSONParser.Parse("[\t]");
}
[Test]
public void TestObjectParser()
{
JSONParser.Parse("{}");
JSONParser.Parse("{ }");
JSONParser.Parse("{\t}");
}
[Test]
public void TestEnumMappings()
{
TestEnumSerialization(testEnum1.A);
TestEnumSerialization(testEnum1.B);
TestEnumSerialization(testEnum1.C);
TestEnumSerialization(testEnum1.D);
TestEnumSerialization(testEnum1.E);
TestEnumSerialization(testEnum1.F);
TestEnumSerialization(testEnum2.NONE);
TestEnumSerialization(testEnum2.A);
TestEnumSerialization(testEnum2.B);
TestEnumSerialization(testEnum2.C);
TestEnumSerialization(testEnum2.D);
TestEnumSerialization(testEnum2.E);
TestEnumSerialization(testEnum3.NONE);
TestEnumSerialization(testEnum3.A);
TestEnumSerialization(testEnum3.B);
TestEnumSerialization(testEnum3.C);
TestEnumSerialization(testEnum3.D);
TestEnumSerialization(testEnum3.E);
TestEnumSerialization((testEnum3)23);
}
bool TestEnumSerialization(object enumValue)
{
Type enumType = enumValue.GetType();
Assert.IsTrue(JSONMapper.DefaultMapper.Serialize(enumValue, out JSONValue jsonEnum));
JSONMapper.DefaultMapper.Deserialize(jsonEnum, enumType, out object enunValueDeserialized);
Assert.AreEqual(enumType, enunValueDeserialized.GetType());
Assert.AreEqual(enumValue, enunValueDeserialized);
return true;
}
[Test]
public void TestMapper()
{
TestPrimitiveMapping((byte)0x80);
TestPrimitiveMapping((short)0x80);
TestPrimitiveMapping((ushort)0x80);
TestPrimitiveMapping((int)0x80);
TestPrimitiveMapping((uint)0x80);
TestPrimitiveMapping((long)0x80);
TestPrimitiveMapping((ulong)0x80);
TestDeserialization("4", testEnum3.C);
TestDeserialization("\"C\"", testEnum3.C);
TestDeserialization("4", testEnum2.C);
TestDeserialization("\"C\"", testEnum2.C);
TestDeserialization("7", testEnum3.E | testEnum3.A);
TestDeserialization("\"A,E\"", testEnum3.A | testEnum3.E);
}
[Test]
public void TestDeserialize()
{
Assert.IsTrue(JSONMapper.DefaultMapper.Deserialize("[\"A\",\"B\",\"C\"]", out string[] texts));
Assert.AreEqual(3, texts.Length);
Assert.AreEqual("A", texts[0]);
Assert.AreEqual("B", texts[1]);
Assert.AreEqual("C", texts[2]);
}
[Test]
public void TestParameterMapping()
{
MethodInfo testMethodInfo = GetType().GetMethod("MappingTestMethod");
object[] methodArguments = null;
JSONArray jsonArguments = new JSONArray()
.Add(new JSONString("text parameter value"))
.Add(new JSONNumber(10))
.Add(new JSONNumber(-2))
.Add(new JSONNumber(-3))
;
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonArguments, out object[] arguments));
Assert.AreEqual("text parameter value", arguments[0]);
Assert.AreEqual(10, arguments[1]);
Assert.AreEqual(-2, arguments[2]);
Assert.AreEqual(-3, arguments[3]);
jsonArguments.Remove(3);
Assert.IsFalse(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonArguments, out arguments));
JSONObject jsonNamedArguments = new JSONObject()
.Add("n", new JSONNumber(10))
.Add("m", new JSONNumber(-2))
.Add("a", new JSONNumber(-3))
.Add("t", new JSONString("text parameter value"))
;
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
Assert.AreEqual("text parameter value", arguments[0]);
Assert.AreEqual(10, arguments[1]);
Assert.AreEqual(-2, arguments[2]);
Assert.AreEqual(-3, arguments[3]);
jsonNamedArguments.Remove("m");
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
jsonNamedArguments.Remove("a");
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
jsonNamedArguments.Remove("n");
Assert.IsFalse(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
}
public void MappingTestMethod(string t, int n, short m = 2, long a = 3)
{}
void TestPrimitiveMapping(object primitiveValue)
{
Type primitiveType = primitiveValue.GetType();
Assert.IsTrue(JSONMapper.DefaultMapper.Serialize(primitiveValue, out JSONValue jsonPrimitive));
JSONMapper.DefaultMapper.Deserialize(jsonPrimitive, primitiveType, out object primitiveValue2);
Assert.AreEqual(primitiveValue, primitiveValue2);
Assert.AreEqual(primitiveType, primitiveValue2.GetType());
}
void TestDeserialization(string jsonSource, object targetValue)
{
Type targetType = targetValue.GetType();
JSONValue json = JSONParser.Parse(jsonSource);
Assert.IsTrue(JSONMapper.DefaultMapper.Deserialize(json, targetType, out object value));
Assert.AreEqual(targetType, value.GetType());
Assert.AreEqual(targetValue, value);
}
}
}