forked from protobuf-net/protobuf-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListAsInterfaceTests.cs
76 lines (67 loc) · 2.35 KB
/
ListAsInterfaceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ProtoBuf;
namespace Examples
{
[ProtoContract]
class DataWithListUninitialized : IDataWithList
{
[ProtoMember(1)]
public IList<string> Data { get; internal set; } // non-public just for fun
}
[ProtoContract]
class DataWithListInitialized : IDataWithList
{
private readonly IList<string> data = new List<string>();
[ProtoMember(1)]
public IList<string> Data { get { return data; } }
}
interface IDataWithList
{
IList<string> Data { get; }
}
[TestFixture]
public class ListAsInterfaceTests
{
[Test]
public void TestInitialized()
{
TestList<DataWithListInitialized>(new DataWithListInitialized());
}
[Test]
public void TestUninitialized()
{
TestList<DataWithListUninitialized>(new DataWithListUninitialized { Data = new List<string>() });
}
[Test]
public void TestDefaultListType()
{
IList<string> data = new List<string>();
data.Add("abc");
var clone = Serializer.DeepClone(data);
Assert.IsNotNull(clone);
Assert.AreNotSame(data, clone);
Assert.AreEqual(1, clone.Count);
Assert.AreEqual("abc", clone[0]);
}
static void TestList<T>(T original) where T : class, IDataWithList
{
Assert.IsNotNull(original, "original should be initialized");
Assert.IsNotNull(original.Data, "original.Data should be initialized");
Assert.AreEqual(0, original.Data.Count, "original.Data should be empty");
original.Data.Add("abc");
original.Data.Add("def");
original.Data.Add("ghi");
original.Data.Add("jkl");
var clone = Serializer.DeepClone<T>(original);
Assert.IsNotNull(clone, "clone");
Assert.AreNotSame(original, clone);
Assert.IsNotNull(clone.Data, "clone.Data");
Assert.AreEqual(original.Data.Count, clone.Data.Count, "clone.Data.Count");
Assert.IsTrue(Enumerable.SequenceEqual(original.Data, clone.Data), "SequenceEqual");
}
}
}