Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #31 support deserialization of IEnumerable with Add methods. #33

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Binaron.Serializer.Tests/CustomTestCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Binaron.Serializer.Tests
{
public class CustomTestCollection<T> : IEnumerable<T>
{
private List<T> mList = new List<T>();

public T this[int index] => mList[index];

public int Count => mList.Count;

public void Add(T value) => mList.Add(value);


public IEnumerator<T> GetEnumerator() => mList.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => mList.GetEnumerator();

public class TestCollectionObject
{
public T A { get; set; }

public TestCollectionObject()
{
}

public TestCollectionObject(T a)
{
A = a;
}
}
}
}
57 changes: 56 additions & 1 deletion src/Binaron.Serializer.Tests/ListSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -265,5 +265,60 @@ private static TList AddItem<TList, TItem>(TList source, TItem item) where TList
throw new NotSupportedException();
}
}

[Test]
public void CustomCollectionIntTest()
{
CustomTestCollection<int> collection = new CustomTestCollection<int>()
{
0, 1, 2, 3, 4
};

var dest = Tester.TestRoundTrip<CustomTestCollection<int>>(collection);
Assert.AreEqual(5, dest.Count);
Assert.AreEqual(0, dest[0]);
Assert.AreEqual(1, dest[1]);
Assert.AreEqual(2, dest[2]);
Assert.AreEqual(3, dest[3]);
Assert.AreEqual(4, dest[4]);
}

[Test]
public void CustomCollectionStringTest()
{
CustomTestCollection<string> collection = new CustomTestCollection<string>()
{
"0", "1", "2", "3", "4"
};

var dest = Tester.TestRoundTrip<CustomTestCollection<string>>(collection);
Assert.AreEqual(5, dest.Count);
Assert.AreEqual("0", dest[0]);
Assert.AreEqual("1", dest[1]);
Assert.AreEqual("2", dest[2]);
Assert.AreEqual("3", dest[3]);
Assert.AreEqual("4", dest[4]);
}

[Test]
public void CustomCollectionObjectTest()
{
CustomTestCollection<CustomTestCollection<int>.TestCollectionObject> collection = new CustomTestCollection<CustomTestCollection<int>.TestCollectionObject>()
{
new CustomTestCollection<int>.TestCollectionObject(0),
new CustomTestCollection<int>.TestCollectionObject(1),
new CustomTestCollection<int>.TestCollectionObject(2),
new CustomTestCollection<int>.TestCollectionObject(3),
new CustomTestCollection<int>.TestCollectionObject(4),
};

var dest = Tester.TestRoundTrip(collection);
Assert.AreEqual(5, dest.Count);
Assert.AreEqual(0, dest[0].A);
Assert.AreEqual(1, dest[1].A);
Assert.AreEqual(2, dest[2].A);
Assert.AreEqual(3, dest[3].A);
Assert.AreEqual(4, dest[4].A);
}
}
}
Loading