mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-03-12 23:01:44 +01:00
added tests for CapnpSerializable
This commit is contained in:
parent
9b82ce12fe
commit
ce9260198f
@ -290,5 +290,229 @@ namespace Capnp.Net.Runtime.Tests
|
||||
Assert.IsNull(list3[2]);
|
||||
Assert.AreEqual("3", list3[3]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListBool()
|
||||
{
|
||||
var expected = new bool[] { true, false, true };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfBitsSerializer>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<bool>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListByte()
|
||||
{
|
||||
var expected = new byte[] { 1, 2, 3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<byte>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<byte>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListSByte()
|
||||
{
|
||||
var expected = new sbyte[] { -1, -2, -3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<sbyte>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<sbyte>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListUShort()
|
||||
{
|
||||
var expected = new ushort[] { 1, 2, 3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<ushort>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<ushort>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListShort()
|
||||
{
|
||||
var expected = new short[] { -1, -2, -3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<short>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<short>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListInt()
|
||||
{
|
||||
var expected = new int[] { -1, -2, -3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<int>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<int>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListUInt()
|
||||
{
|
||||
var expected = new uint[] { 1, 2, 3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<uint>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<uint>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListLong()
|
||||
{
|
||||
var expected = new long[] { -1, -2, -3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<long>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<long>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListULong()
|
||||
{
|
||||
var expected = new ulong[] { 1, 2, 3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<ulong>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<ulong>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListFloat()
|
||||
{
|
||||
var expected = new float[] { -1.0f, 2.0f, -3.0f };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<float>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<float>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableListDouble()
|
||||
{
|
||||
var expected = new double[] { -1, -2, -3 };
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<double>>();
|
||||
list.Init(expected);
|
||||
DeserializerState d = list;
|
||||
var list2 = CapnpSerializable.Create<IReadOnlyList<double>>(d);
|
||||
CollectionAssert.AreEqual(expected, list2.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableStruct()
|
||||
{
|
||||
var b = MessageBuilder.Create();
|
||||
var obj = b.CreateObject<SomeStruct.WRITER>();
|
||||
obj.SomeText = "hello";
|
||||
obj.MoreText = "world";
|
||||
DeserializerState d = obj;
|
||||
var obj2 = CapnpSerializable.Create<SomeStruct>(d);
|
||||
Assert.AreEqual("hello", obj2.SomeText);
|
||||
Assert.AreEqual("world", obj2.MoreText);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableCapList()
|
||||
{
|
||||
var b = MessageBuilder.Create();
|
||||
b.InitCapTable();
|
||||
var wr = b.CreateObject<ListOfCapsSerializer<ITestInterface>>();
|
||||
var c1 = new Counters();
|
||||
var cap1 = new TestInterfaceImpl(c1);
|
||||
var c2 = new Counters();
|
||||
var cap2 = new TestInterfaceImpl(c2);
|
||||
wr.Init(new ITestInterface[] { null, cap1, cap2 });
|
||||
DeserializerState d = wr;
|
||||
var list = CapnpSerializable.Create<IReadOnlyList<ITestInterface>>(d);
|
||||
list[1].Foo(123u, true);
|
||||
Assert.AreEqual(1, c1.CallCount);
|
||||
list[2].Foo(123u, true);
|
||||
Assert.AreEqual(1, c2.CallCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableString()
|
||||
{
|
||||
string expected = "1, 2, 3";
|
||||
var b = MessageBuilder.Create();
|
||||
var list = b.CreateObject<ListOfPrimitivesSerializer<byte>>();
|
||||
var bytes = Encoding.UTF8.GetBytes(expected);
|
||||
list.Init(bytes.Length + 1);
|
||||
bytes.CopyTo(list.Data);
|
||||
DeserializerState d = list;
|
||||
var str = CapnpSerializable.Create<string>(d);
|
||||
Assert.AreEqual(expected, str);
|
||||
}
|
||||
|
||||
class Unconstructible1 : ICapnpSerializable
|
||||
{
|
||||
public Unconstructible1(int annoyingParameter)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Deserialize(DeserializerState state)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Serialize(SerializerState state)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
class Unconstructible2 : ICapnpSerializable
|
||||
{
|
||||
public Unconstructible2()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Deserialize(DeserializerState state)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Serialize(SerializerState state)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CapnpSerializableWrongUse()
|
||||
{
|
||||
var d = default(DeserializerState);
|
||||
Assert.ThrowsException<ArgumentException>(() => CapnpSerializable.Create<SerializationTests>(d));
|
||||
Assert.ThrowsException<ArgumentException>(() => CapnpSerializable.Create<IReadOnlyList<SerializationTests>>(d));
|
||||
Assert.ThrowsException<ArgumentException>(() => CapnpSerializable.Create<Unconstructible1>(d));
|
||||
Assert.ThrowsException<ArgumentException>(() => CapnpSerializable.Create<Unconstructible2>(d));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,8 @@ namespace Capnp
|
||||
|
||||
public FromList()
|
||||
{
|
||||
_elementSerializer = (Func<DeserializerState, T>)GetSerializer(typeof(T));
|
||||
var deser = GetSerializer(typeof(T));
|
||||
_elementSerializer = d => (deser(d) as T)!;
|
||||
|
||||
}
|
||||
public object Create(DeserializerState state)
|
||||
@ -147,28 +148,29 @@ namespace Capnp
|
||||
/// <list type="bullet">
|
||||
/// <item><description>Type implementing <see cref="ICapnpSerializable"/>. The type must must have a public parameterless constructor.</description></item>
|
||||
/// <item><description>A capability interface (<seealso cref="Rpc.InvalidCapabilityInterfaceException"/> for further explanation)</description></item>
|
||||
/// <item><description><see cref="String"/></description></item>
|
||||
/// <item><description><code>IReadOnlyList{Boolean}</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{SByte}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{Byte}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{Int16}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{UInt16}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{Int32}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{UInt32}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{Int64}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{UInt64}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{Single}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{Double}"</code></description></item>
|
||||
/// <item><description><code>IReadOnlyList{T}</code> whereby T is one of the things listed here.</description></item>
|
||||
/// <item><description><see cref="string"/></description></item>
|
||||
/// <item><description>IReadOnlyList<bool>, IReadOnlyList<sbyte>, IReadOnlyList<byte></description></item>
|
||||
/// <item><description>IReadOnlyList<short>, IReadOnlyList<ushort>, IReadOnlyList<int></description></item>
|
||||
/// <item><description>IReadOnlyList<uint>, IReadOnlyList<long>, IReadOnlyList<ulong></description></item>
|
||||
/// <item><description>IReadOnlyList<float>, IReadOnlyList<double></description></item>
|
||||
/// <item><description>IReadOnlyList<T> whereby T is one of the things listed here.</description></item>
|
||||
/// </list>
|
||||
/// </typeparam>
|
||||
/// <param name="state">deserializer state to construct from</param>
|
||||
/// <returns>The domain object instance. Nullability note: The returned reference may be null if
|
||||
/// <paramref name="state"/> represents the nil object.</returns>
|
||||
/// <exception cref="ArgumentException">Cannot construct object of type <typeparamref name="T"/></exception>
|
||||
public static T? Create<T>(DeserializerState state)
|
||||
where T: class
|
||||
{
|
||||
return (T?)GetSerializer(typeof(T))(state);
|
||||
try
|
||||
{
|
||||
return (T?)GetSerializer(typeof(T))(state);
|
||||
}
|
||||
catch (TargetInvocationException ex)
|
||||
{
|
||||
throw new ArgumentException("Failed to construct domain object", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -28,7 +28,10 @@ namespace Capnp
|
||||
}
|
||||
}
|
||||
|
||||
Span<T> Data => MemoryMarshal.Cast<ulong, T>(RawData);
|
||||
/// <summary>
|
||||
/// The list's data
|
||||
/// </summary>
|
||||
public Span<T> Data => MemoryMarshal.Cast<ulong, T>(RawData);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the value at given index.
|
||||
|
Loading…
x
Reference in New Issue
Block a user