My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ProtoBuf;
using System.IO;
using ProtoBuf.Meta;

namespace Examples.Issues
{
[TestFixture]
public class SO11656439
{
[Test]
public void BasicStringListShouldRoundTrip()
{
var list = new List<string> {"abc"};
var clone = Serializer.DeepClone(list);
Assert.AreEqual(1, clone.Count);
Assert.AreEqual("abc", clone[0]);
}

public class MyList : List<string>{}

[Test]
public void ListSubclassShouldRoundTrip()
{
var list = new MyList { "abc" };
var clone = Serializer.DeepClone(list);
Assert.AreEqual(1, clone.Count);
Assert.AreEqual("abc", clone[0]);
}

[ProtoContract]
public class MyContractList : List<string> { }

[Test]
public void ContractListSubclassShouldRoundTrip()
{
// this test is larger because it wasn't working; neeeded
// to make it more granular
var model = TypeModel.Create();
model.AutoCompile = false;
CheckContractSubclass(model, "Runtime");
model.CompileInPlace();
CheckContractSubclass(model, "CompileInPlace");
CheckContractSubclass(model.Compile(), "CompileInPlace");
model.Compile("ContractListSubclassShouldRoundTrip", "ContractListSubclassShouldRoundTrip.dll");
PEVerify.AssertValid("ContractListSubclassShouldRoundTrip.dll");
}

private void CheckContractSubclass(TypeModel model, string caption)
{
var list = new MyContractList { "abc" };
using (var ms = new MemoryStream())
{
model.Serialize(ms, list);
Assert.Greater(2, 0, "sanity check:" + caption);
Assert.Greater(ms.Length, 0, "data should be written:" + caption);
ms.Position = 0;
var clone = (MyContractList) model.Deserialize(ms,null, typeof(MyContractList));
Assert.AreEqual(1, clone.Count, "count:" + caption);
Assert.AreEqual("abc", clone[0], "payload:" + caption);
}
}

[ProtoContract]
public class ListWrapper
{
[ProtoMember(1)]
public List<string> BasicList { get; set; }
[ProtoMember(2)]
public MyList MyList { get; set; }
[ProtoMember(3)]
public MyContractList MyContractList { get; set; }
}

[Test]
public void TestBasicListAsMember()
{
var obj = new ListWrapper { BasicList = new List<string> { "abc" } };
var clone = Serializer.DeepClone(obj);
Assert.IsNull(clone.MyList);
Assert.IsNull(clone.MyContractList);
Assert.AreEqual(1, clone.BasicList.Count);
Assert.AreEqual("abc", clone.BasicList[0]);
}

[Test]
public void TestMyListAsMember()
{
var obj = new ListWrapper { MyList = new MyList { "abc" } };
var clone = Serializer.DeepClone(obj);
Assert.IsNull(clone.BasicList);
Assert.IsNull(clone.MyContractList);
Assert.AreEqual(1, clone.MyList.Count);
Assert.AreEqual("abc", clone.MyList[0]);
}

[Test]
public void TestMyContractListAsMember()
{
var obj = new ListWrapper { MyContractList = new MyContractList { "abc" } };
var clone = Serializer.DeepClone(obj);
Assert.IsNull(clone.BasicList);
Assert.IsNull(clone.MyList);
Assert.AreEqual(1, clone.MyContractList.Count);
Assert.AreEqual("abc", clone.MyContractList[0]);
}

[Test]
public void SanityCheckListWrapper()
{
var model = TypeModel.Create();
model.Add(typeof (ListWrapper), true);

var schema = model.GetSchema(null);

Assert.AreEqual(@"package Examples.Issues;

message ListWrapper {
repeated string BasicList = 1;
repeated string MyList = 2;
repeated string MyContractList = 3;
}
", schema);
model.Compile("SanityCheckListWrapper", "SanityCheckListWrapper.dll");
PEVerify.AssertValid("SanityCheckListWrapper.dll");
}

}
}

Change log

r585 by marc.gravell on Aug 29, 2012   Diff
GetSchema fixes: don't work for lists;
expand generic type-names via
GetSchemaTypeName; add bcl.proto import
when necessary
Go to: 
Project members, sign in to write a code review

Older revisions

r555 by marc.gravell on Jul 27, 2012   Diff
FIX: SO11656439 - lists as outermost
would drop data
All revisions of this file

File info

Size: 4601 bytes, 133 lines
Powered by Google Project Hosting