2020-12-12 20:16:54 +01:00
|
|
|
using System;
|
2020-12-25 20:31:19 +01:00
|
|
|
using System.Reflection;
|
2020-12-12 20:16:54 +01:00
|
|
|
using ln.json.mapping;
|
2020-11-25 23:23:46 +01:00
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace ln.json.tests
|
|
|
|
{
|
|
|
|
public class JSONTests
|
|
|
|
{
|
2020-12-12 20:16:54 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-25 23:23:46 +01:00
|
|
|
[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();
|
|
|
|
}
|
2020-12-09 14:49:30 +01:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestArrayParser()
|
|
|
|
{
|
|
|
|
JSONParser.Parse("[]");
|
|
|
|
JSONParser.Parse("[ ]");
|
|
|
|
JSONParser.Parse("[\t]");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestObjectParser()
|
|
|
|
{
|
|
|
|
JSONParser.Parse("{}");
|
|
|
|
JSONParser.Parse("{ }");
|
|
|
|
JSONParser.Parse("{\t}");
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:16:54 +01:00
|
|
|
[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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:25:46 +01:00
|
|
|
[Test]
|
|
|
|
public void TestMapper()
|
|
|
|
{
|
2020-12-12 21:59:53 +01:00
|
|
|
TestPrimitiveMapping((byte)0x80);
|
|
|
|
TestPrimitiveMapping((short)0x80);
|
|
|
|
TestPrimitiveMapping((ushort)0x80);
|
|
|
|
TestPrimitiveMapping((int)0x80);
|
|
|
|
TestPrimitiveMapping((uint)0x80);
|
|
|
|
TestPrimitiveMapping((long)0x80);
|
|
|
|
TestPrimitiveMapping((ulong)0x80);
|
2020-12-15 11:30:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-12-12 21:59:53 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 21:17:59 +01:00
|
|
|
[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]);
|
|
|
|
}
|
|
|
|
|
2020-12-25 20:31:19 +01:00
|
|
|
[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)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2020-12-12 21:59:53 +01:00
|
|
|
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());
|
2020-12-15 11:30:47 +01:00
|
|
|
}
|
2020-12-12 21:59:53 +01:00
|
|
|
|
2020-12-15 11:30:47 +01:00
|
|
|
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);
|
2020-12-12 21:25:46 +01:00
|
|
|
}
|
2020-12-12 20:16:54 +01:00
|
|
|
|
2020-12-15 11:30:47 +01:00
|
|
|
|
|
|
|
|
2020-11-25 23:23:46 +01:00
|
|
|
}
|
|
|
|
}
|