mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-03-12 14:51:41 +01:00
more tests for improved coverage
This commit is contained in:
parent
6a87cb7dac
commit
89a9e7b5ac
@ -1,4 +1,7 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Capnproto_test.Capnp.Test;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Capnp.Net.Runtime.Tests
|
||||
{
|
||||
@ -354,5 +357,40 @@ namespace Capnp.Net.Runtime.Tests
|
||||
Assert.AreEqual(double.NegativeInfinity, asListOfDoubles[0]);
|
||||
Assert.AreEqual(double.MaxValue, asListOfDoubles[1]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NestedLists()
|
||||
{
|
||||
var expected = new int[][] {
|
||||
new int[] { 1, 2, 3 },
|
||||
new int[] { 4, 5 },
|
||||
new int[] { 6 } };
|
||||
|
||||
var b = MessageBuilder.Create();
|
||||
var dss = b.CreateObject<DynamicSerializerState>();
|
||||
dss.SetObject(expected);
|
||||
DeserializerState d = dss;
|
||||
var ld = d.RequireList();
|
||||
var result = ld.Cast2D<int>();
|
||||
Assert.AreEqual(3, result.Count);
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
{
|
||||
CollectionAssert.AreEqual(expected[i], result[i].ToArray());
|
||||
}
|
||||
|
||||
Assert.ThrowsException<NotSupportedException>(() => ld.Cast2D<decimal>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void LinearListWrongUse()
|
||||
{
|
||||
var b = MessageBuilder.Create();
|
||||
var dss = b.CreateObject<DynamicSerializerState>();
|
||||
dss.SetObject(new int[] { 1, 2, 3 });
|
||||
DeserializerState d = dss;
|
||||
var ld = d.RequireList();
|
||||
Assert.ThrowsException<NotSupportedException>(() => ld.CastList());
|
||||
Assert.ThrowsException<NotSupportedException>(() => ld.CastCapList<ITestInterface>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -79,6 +79,8 @@ namespace Capnp.Net.Runtime.Tests
|
||||
Assert.ThrowsException<InvalidOperationException>(() => { list[0] = null; });
|
||||
list.Init(5);
|
||||
Assert.ThrowsException<InvalidOperationException>(() => list.Init(1));
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { var _ = list[5]; });
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { list[-1] = null; });
|
||||
var c1 = new Counters();
|
||||
var cap1 = new TestInterfaceImpl(c1);
|
||||
var c2 = new Counters();
|
||||
@ -152,6 +154,8 @@ namespace Capnp.Net.Runtime.Tests
|
||||
list.Init(7);
|
||||
Assert.ThrowsException<InvalidOperationException>(() => list.Init(1));
|
||||
Assert.AreEqual(7, list.Count);
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { var _ = list[-1]; });
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { list[7] = null; });
|
||||
var c1 = new Counters();
|
||||
var cap1 = new TestInterfaceImpl(c1);
|
||||
var obj1 = b.CreateObject<DynamicSerializerState>();
|
||||
@ -234,6 +238,7 @@ namespace Capnp.Net.Runtime.Tests
|
||||
list.Init(4);
|
||||
Assert.ThrowsException<InvalidOperationException>(() => list.Init(1));
|
||||
Assert.AreEqual(4, list.Count);
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { var _ = list[5]; });
|
||||
list[0].SomeText = "0";
|
||||
list[1].SomeText = "1";
|
||||
list[2].SomeText = "2";
|
||||
@ -265,6 +270,8 @@ namespace Capnp.Net.Runtime.Tests
|
||||
list.Init(4);
|
||||
Assert.ThrowsException<InvalidOperationException>(() => list.Init(1));
|
||||
Assert.AreEqual(4, list.Count);
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { var _ = list[5]; });
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { list[-1] = null; });
|
||||
list[0] = "0";
|
||||
list[2] = null;
|
||||
list[3] = "3";
|
||||
@ -724,5 +731,15 @@ namespace Capnp.Net.Runtime.Tests
|
||||
DeserializerState d2 = dss;
|
||||
CollectionAssert.AreEqual(expected, d2.RequireList().CastText2().ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EmptyListContract()
|
||||
{
|
||||
var list = new EmptyList<string>();
|
||||
Assert.AreEqual(0, list.Count);
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { var _ = list[-1]; });
|
||||
Assert.ThrowsException<IndexOutOfRangeException>(() => { var _ = list[0]; });
|
||||
Assert.AreEqual(0, list.ToArray().Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,14 +8,14 @@ namespace Capnp
|
||||
/// <summary>
|
||||
/// Implements an empty <see cref="IReadOnlyList{T}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="T">list element type</typeparam>
|
||||
public class EmptyList<T> : IReadOnlyList<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Always throws an <see cref="ArgumentOutOfRangeException"/>.
|
||||
/// Always throws an <see cref="IndexOutOfRangeException"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">Ignored</param>
|
||||
public T this[int index] => throw new ArgumentOutOfRangeException(nameof(index));
|
||||
public T this[int index] => throw new IndexOutOfRangeException(nameof(index));
|
||||
|
||||
/// <summary>
|
||||
/// Always 0.
|
||||
|
@ -11,6 +11,9 @@ namespace Capnp
|
||||
static class GenericCasts<T>
|
||||
{
|
||||
public static Func<ListDeserializer, T>? CastFunc;
|
||||
|
||||
public static Func<ListDeserializer, T> GetCastFunc() => CastFunc ??
|
||||
throw new NotSupportedException("Requested cast is not supported");
|
||||
}
|
||||
|
||||
static ListDeserializer()
|
||||
@ -45,12 +48,7 @@ namespace Capnp
|
||||
|
||||
T Cast<T>()
|
||||
{
|
||||
var func = GenericCasts<T>.CastFunc;
|
||||
|
||||
if (func == null)
|
||||
throw new NotSupportedException("Requested cast is not supported");
|
||||
|
||||
return func(this);
|
||||
return GenericCasts<T>.GetCastFunc()(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -91,7 +89,7 @@ namespace Capnp
|
||||
/// <exception cref="Rpc.InvalidCapabilityInterfaceException">If <typeparamref name="T"/> does not qualify as capability interface.</exception>
|
||||
public virtual IReadOnlyList<T> CastCapList<T>() where T: class
|
||||
{
|
||||
throw new NotSupportedException("This kind of list does not contain nested lists");
|
||||
throw new NotSupportedException("This kind of list cannot be represented as list of capabilities");
|
||||
}
|
||||
|
||||
object CastND(int n, Func<ListDeserializer, object> func)
|
||||
@ -158,6 +156,7 @@ namespace Capnp
|
||||
/// <exception cref="NotSupportedException">If this list cannot be represented in the desired manner.</exception>
|
||||
public IReadOnlyList<IReadOnlyList<T>> Cast2D<T>()
|
||||
{
|
||||
GenericCasts<IReadOnlyList<T>>.GetCastFunc(); // Probe to avoid lazy NotSupportedException
|
||||
return CastList().LazyListSelect(ld => ld.Cast<IReadOnlyList<T>>());
|
||||
}
|
||||
|
||||
|
@ -11,35 +11,6 @@ namespace Capnp
|
||||
/// </summary>
|
||||
public class ListOfBitsSerializer: SerializerState, IReadOnlyList<bool>
|
||||
{
|
||||
class Enumerator : IEnumerator<bool>
|
||||
{
|
||||
readonly ListOfBitsSerializer _self;
|
||||
int _pos = -1;
|
||||
|
||||
public Enumerator(ListOfBitsSerializer self)
|
||||
{
|
||||
_self = self;
|
||||
}
|
||||
|
||||
public bool Current => _pos >= 0 && _pos < _self.Count ? _self[_pos] : false;
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
return ++_pos < _self.Count;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_pos = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the element at given index.
|
||||
/// </summary>
|
||||
@ -121,10 +92,16 @@ namespace Capnp
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<bool> Enumerate()
|
||||
{
|
||||
for (int i = 0; i < Count; i++)
|
||||
yield return this[i];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements <see cref="IEnumerable{Boolean}"/>
|
||||
/// </summary>
|
||||
public IEnumerator<bool> GetEnumerator() => new Enumerator(this);
|
||||
public IEnumerator<bool> GetEnumerator() => Enumerate().GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user