【发布时间】:2011-11-21 20:48:52
【问题描述】:
如何配置 protobuf-net 类型模型以通过以下示例中的 3 个单元测试? Protobuf 版本是 v2 r470。
我已经简要地查看了 svn 树中的列表测试,但无法发现这与 protobuf-net svn 中的空测试与空测试之间的区别。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ProtoBuf;
using ProtoBuf.Meta;
namespace ProtoCollections
{
[TestFixture]
public class CollectionTests
{
[Test]
public void TestEmptyList()
{
var model = TypeModel.Create();
var orig = new TypeWithReferenceList(Enumerable.Empty<SomeReferenceType>());
var clone = (TypeWithReferenceList)model.DeepClone(orig);
Assert.IsNotNull(clone.List);
Assert.IsEmpty(clone.List);
}
[Test]
public void TestNullList()
{
var model = TypeModel.Create();
var orig = new TypeWithReferenceList(null);
var clone = (TypeWithReferenceList)model.DeepClone(orig);
Assert.IsNull(clone.List);
}
[Test]
public void TestList()
{
var model = TypeModel.Create();
model[typeof (SomeReferenceType)].AsReferenceDefault = true;
SomeReferenceType repeatedItem = new SomeReferenceType(123);
var orig = new TypeWithReferenceList(new []{repeatedItem, repeatedItem});
var clone = (TypeWithReferenceList)model.DeepClone(orig);
Assert.AreEqual(orig.List.Count, clone.List.Count);
Assert.AreSame(orig.List[0], orig.List[1]);
Assert.AreEqual(orig.List[0].Value, clone.List[0].Value);
Assert.AreSame(clone.List[0], clone.List[1]);
}
}
[ProtoContract(ImplicitFields = ImplicitFields.AllFields, SkipConstructor = true)]
public class SomeReferenceType
{
private int value;
public SomeReferenceType(int val)
{
value = val;
}
public int Value { get { return value; } }
}
[ProtoContract(ImplicitFields = ImplicitFields.AllFields, SkipConstructor = true)]
public class TypeWithReferenceList
{
private List<SomeReferenceType> innerList;
public TypeWithReferenceList(IEnumerable<SomeReferenceType> items)
{
innerList = items == null ? null : items.ToList();
}
public List<SomeReferenceType> List { get { return innerList; } }
}
}
【问题讨论】:
标签: .net serialization protobuf-net