Skip to content

Commit 5948fc0

Browse files
committed
Add tests
1 parent 96aaa6a commit 5948fc0

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/ContractlessStandardResolverTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public class Person
3333
public object[] /*Address*/ Addresses { get; set; }
3434
}
3535

36+
public class DefaultValueStringKeyClassWithoutExplicitConstructor
37+
{
38+
public const int Prop1Constant = 11;
39+
public const int Prop2Constant = 45;
40+
41+
public int Prop1 { get; set; } = Prop1Constant;
42+
43+
public int Prop2 { get; set; } = Prop2Constant;
44+
}
45+
3646
public class V1
3747
{
3848
public int ABCDEFG1 { get; set; }
@@ -175,6 +185,18 @@ public void SimpleTest()
175185
((string)d2["Street"]).Is("Ave.");
176186
}
177187

188+
[Fact]
189+
public void DefaultValueStringKeyClassWithoutExplicitConstructorTest()
190+
{
191+
var dictionary = new Dictionary<string, int>();
192+
193+
var result = MessagePackSerializer.Serialize(dictionary, Resolvers.ContractlessStandardResolver.Options);
194+
195+
var instance = MessagePackSerializer.Deserialize<DefaultValueStringKeyClassWithoutExplicitConstructor>(result, Resolvers.ContractlessStandardResolver.Options);
196+
instance.Prop1.Is(DefaultValueStringKeyClassWithoutExplicitConstructor.Prop1Constant);
197+
instance.Prop2.Is(DefaultValueStringKeyClassWithoutExplicitConstructor.Prop2Constant);
198+
}
199+
178200
[Fact]
179201
public void Versioning()
180202
{

src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DynamicObjectResolverTests.cs

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Runtime.Serialization;
88
using MessagePack.Resolvers;
9+
using Nerdbank.Streams;
910
using Xunit;
1011
using Xunit.Abstractions;
1112

@@ -65,6 +66,182 @@ private void PrivateMembersInBaseClass_Helper(MessagePackSerializerOptions optio
6566
Assert.Equal(obj.Name, obj2.Name);
6667
}
6768

69+
[Fact]
70+
public void DefaultValueStringKeyClassWithoutExplicitConstructorTest()
71+
{
72+
var seq = new Sequence<byte>();
73+
var writer = new MessagePackWriter(seq);
74+
writer.WriteMapHeader(0);
75+
writer.Flush();
76+
77+
var instance = MessagePackSerializer.Deserialize<DefaultValueStringKeyClassWithoutExplicitConstructor>(seq);
78+
Assert.Equal(DefaultValueStringKeyClassWithoutExplicitConstructor.Prop1Constant, instance.Prop1);
79+
Assert.Equal(DefaultValueStringKeyClassWithoutExplicitConstructor.Prop2Constant, instance.Prop2);
80+
}
81+
82+
[Fact]
83+
public void DefaultValueStringKeyClassWithExplicitConstructorTest()
84+
{
85+
var seq = new Sequence<byte>();
86+
var writer = new MessagePackWriter(seq);
87+
writer.WriteMapHeader(1);
88+
writer.Write(nameof(DefaultValueStringKeyClassWithExplicitConstructor.Prop1));
89+
writer.Write(-1);
90+
writer.Flush();
91+
92+
var instance = MessagePackSerializer.Deserialize<DefaultValueStringKeyClassWithExplicitConstructor>(seq);
93+
Assert.Equal(-1, instance.Prop1);
94+
Assert.Equal(DefaultValueStringKeyClassWithExplicitConstructor.Prop2Constant, instance.Prop2);
95+
}
96+
97+
[Fact]
98+
public void DefaultValueStringKeyStructWithExplicitConstructorTest()
99+
{
100+
var seq = new Sequence<byte>();
101+
var writer = new MessagePackWriter(seq);
102+
writer.WriteMapHeader(1);
103+
writer.Write(nameof(DefaultValueStringKeyStructWithExplicitConstructor.Prop1));
104+
writer.Write(-1);
105+
writer.Flush();
106+
107+
var instance = MessagePackSerializer.Deserialize<DefaultValueStringKeyStructWithExplicitConstructor>(seq);
108+
Assert.Equal(-1, instance.Prop1);
109+
Assert.Equal(DefaultValueStringKeyStructWithExplicitConstructor.Prop2Constant, instance.Prop2);
110+
}
111+
112+
[Fact]
113+
public void DefaultValueIntKeyClassWithoutExplicitConstructorTest()
114+
{
115+
var seq = new Sequence<byte>();
116+
var writer = new MessagePackWriter(seq);
117+
writer.WriteArrayHeader(0);
118+
writer.Flush();
119+
120+
var instance = MessagePackSerializer.Deserialize<DefaultValueIntKeyClassWithoutExplicitConstructor>(seq);
121+
Assert.Equal(DefaultValueIntKeyClassWithoutExplicitConstructor.Prop1Constant, instance.Prop1);
122+
Assert.Equal(DefaultValueIntKeyClassWithoutExplicitConstructor.Prop2Constant, instance.Prop2);
123+
}
124+
125+
[Fact]
126+
public void DefaultValueIntKeyClassWithExplicitConstructorTest()
127+
{
128+
var seq = new Sequence<byte>();
129+
var writer = new MessagePackWriter(seq);
130+
writer.WriteArrayHeader(1);
131+
writer.Write(-1);
132+
writer.Flush();
133+
134+
var instance = MessagePackSerializer.Deserialize<DefaultValueIntKeyClassWithExplicitConstructor>(seq);
135+
Assert.Equal(-1, instance.Prop1);
136+
Assert.Equal(DefaultValueIntKeyClassWithExplicitConstructor.Prop2Constant, instance.Prop2);
137+
}
138+
139+
[Fact]
140+
public void DefaultValueIntKeyStructWithExplicitConstructorTest()
141+
{
142+
var seq = new Sequence<byte>();
143+
var writer = new MessagePackWriter(seq);
144+
writer.WriteArrayHeader(1);
145+
writer.Write(-1);
146+
writer.Flush();
147+
148+
var instance = MessagePackSerializer.Deserialize<DefaultValueIntKeyStructWithExplicitConstructor>(seq);
149+
Assert.Equal(-1, instance.Prop1);
150+
Assert.Equal(DefaultValueIntKeyStructWithExplicitConstructor.Prop2Constant, instance.Prop2);
151+
}
152+
153+
[MessagePackObject(true)]
154+
public class DefaultValueStringKeyClassWithoutExplicitConstructor
155+
{
156+
public const int Prop1Constant = 11;
157+
public const int Prop2Constant = 45;
158+
159+
public int Prop1 { get; set; } = Prop1Constant;
160+
161+
public int Prop2 { get; set; } = Prop2Constant;
162+
}
163+
164+
[MessagePackObject(true)]
165+
public class DefaultValueStringKeyClassWithExplicitConstructor
166+
{
167+
public const int Prop2Constant = 1419;
168+
169+
public int Prop1 { get; set; }
170+
171+
public int Prop2 { get; set; }
172+
173+
public DefaultValueStringKeyClassWithExplicitConstructor(int prop1)
174+
{
175+
Prop1 = prop1;
176+
Prop2 = Prop2Constant;
177+
}
178+
}
179+
180+
[MessagePackObject(true)]
181+
public struct DefaultValueStringKeyStructWithExplicitConstructor
182+
{
183+
public const int Prop2Constant = 198;
184+
185+
public int Prop1 { get; set; }
186+
187+
public int Prop2 { get; set; }
188+
189+
public DefaultValueStringKeyStructWithExplicitConstructor(int prop1)
190+
{
191+
Prop1 = prop1;
192+
Prop2 = Prop2Constant;
193+
}
194+
}
195+
196+
[MessagePackObject]
197+
public class DefaultValueIntKeyClassWithoutExplicitConstructor
198+
{
199+
public const int Prop1Constant = 33;
200+
public const int Prop2Constant = -4;
201+
202+
[Key(0)]
203+
public int Prop1 { get; set; } = Prop1Constant;
204+
205+
[Key(1)]
206+
public int Prop2 { get; set; } = Prop2Constant;
207+
}
208+
209+
[MessagePackObject]
210+
public class DefaultValueIntKeyClassWithExplicitConstructor
211+
{
212+
public const int Prop2Constant = -109;
213+
214+
[Key(0)]
215+
public int Prop1 { get; set; }
216+
217+
[Key(1)]
218+
public int Prop2 { get; set; }
219+
220+
public DefaultValueIntKeyClassWithExplicitConstructor(int prop1)
221+
{
222+
Prop1 = prop1;
223+
Prop2 = Prop2Constant;
224+
}
225+
}
226+
227+
[MessagePackObject]
228+
public struct DefaultValueIntKeyStructWithExplicitConstructor
229+
{
230+
public const int Prop2Constant = 31;
231+
232+
[Key(0)]
233+
public int Prop1 { get; set; }
234+
235+
[Key(1)]
236+
public int Prop2 { get; set; }
237+
238+
public DefaultValueIntKeyStructWithExplicitConstructor(int prop1)
239+
{
240+
Prop1 = prop1;
241+
Prop2 = Prop2Constant;
242+
}
243+
}
244+
68245
[MessagePackObject]
69246
public class TestMessageWithReadOnlyField
70247
{

0 commit comments

Comments
 (0)