Skip to content

Commit cd7799e

Browse files
committed
Add support for serializing objects
1 parent e64e5df commit cd7799e

File tree

7 files changed

+867
-76
lines changed

7 files changed

+867
-76
lines changed

MPack.Tests/Tests.cs renamed to MPack.Tests/BasicTests.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MPack.Tests
66
{
7-
public class Tests
7+
public class BasicTests
88
{
99
[Fact]
1010
public void ReadmeExample()
@@ -83,7 +83,7 @@ public void TestNumRepr()
8383
[Fact]
8484
public void TestNull()
8585
{
86-
Assert.Equal(null, MToken.ParseFromBytes(MToken.Null().EncodeToBytes()).To<object>());
86+
Assert.Null(MToken.ParseFromBytes(MToken.Null().EncodeToBytes()).To<object>());
8787
}
8888

8989
[Fact]
@@ -100,7 +100,9 @@ public void TestString()
100100
};
101101
foreach (var value in tests)
102102
{
103-
Assert.Equal(value, MToken.ParseFromBytes(MToken.From(value).EncodeToBytes()).To<string>());
103+
var token = MToken.From(value);
104+
Assert.Equal(MTokenType.String, token.ValueType);
105+
Assert.Equal(value, MToken.ParseFromBytes(token.EncodeToBytes()).To<string>());
104106
}
105107
}
106108

@@ -124,7 +126,9 @@ public void TestInteger()
124126
};
125127
foreach (var value in tests)
126128
{
127-
Assert.Equal(value, MToken.ParseFromBytes(MToken.From(value).EncodeToBytes()).To<long>());
129+
var token = MToken.From(value);
130+
Assert.Equal(MTokenType.SInt, token.ValueType);
131+
Assert.Equal(value, MToken.ParseFromBytes(token.EncodeToBytes()).To<long>());
128132
}
129133
}
130134

@@ -194,7 +198,9 @@ public void TestUInt64()
194198
};
195199
foreach (var value in tests)
196200
{
197-
Assert.Equal(value, MToken.ParseFromBytes(MToken.From(value).EncodeToBytes()).To<ulong>());
201+
var token = MToken.From(value);
202+
Assert.Equal(MTokenType.UInt, token.ValueType);
203+
Assert.Equal(value, MToken.ParseFromBytes(token.EncodeToBytes()).To<ulong>());
198204
}
199205
}
200206

@@ -223,7 +229,9 @@ public void TestSingle()
223229
};
224230
foreach (var value in tests)
225231
{
226-
Assert.Equal(value, MToken.ParseFromBytes(MToken.From(value).EncodeToBytes()).To<float>());
232+
var token = MToken.From(value);
233+
Assert.Equal(MTokenType.Single, token.ValueType);
234+
Assert.Equal(value, MToken.ParseFromBytes(token.EncodeToBytes()).To<float>());
227235
}
228236
}
229237

@@ -240,7 +248,9 @@ public void TestBinary()
240248
};
241249
foreach (var value in tests)
242250
{
243-
var result = MToken.ParseFromBytes(MToken.From(value).EncodeToBytes()).To<byte[]>();
251+
var token = MToken.From(value);
252+
Assert.Equal(MTokenType.Binary, token.ValueType);
253+
var result = MToken.ParseFromBytes(token.EncodeToBytes()).To<byte[]>();
244254
Assert.True(Enumerable.SequenceEqual(value, result));
245255
}
246256
}

MPack.Tests/EqualityTests.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System;
2+
using System.Linq;
3+
using Xunit;
4+
5+
namespace MPack.Tests
6+
{
7+
public class EqualityTests
8+
{
9+
[Fact]
10+
public void TestDictionaryEquality()
11+
{
12+
var dictionary = new MDict
13+
{
14+
{
15+
"array1", MToken.From(new[]
16+
{
17+
"array1_value1", // implicitly converted string
18+
MToken.From("array1_value2"),
19+
})
20+
},
21+
{"bool1", MToken.From(true)}, //boolean
22+
{"double1", MToken.From(50.5)}, //single-precision float
23+
{"double2", MToken.From(15.2)},
24+
{"int1", 50505}, // implicitly converted integer
25+
{"int2", MToken.From(50)} // integer
26+
};
27+
28+
var dictionary2 = new MDict
29+
{
30+
{
31+
"array1", MToken.From(new[]
32+
{
33+
"array1_value1", // implicitly converted string
34+
MToken.From("array1_value2"),
35+
})
36+
},
37+
{"bool1", MToken.From(true)}, //boolean
38+
{"double1", MToken.From(50.5)}, //single-precision float
39+
{"double2", MToken.From(15.2)},
40+
{"int1", 50505}, // implicitly converted integer
41+
{"int2", MToken.From(50)} // integer
42+
};
43+
44+
var dictionary3 = new MDict
45+
{
46+
{
47+
"array1", MToken.From(new[]
48+
{
49+
"array1_ value1", // implicitly converted string
50+
MToken.From("array1_value2"),
51+
})
52+
},
53+
{"bool1", MToken.From(true)}, //boolean
54+
{"double1", MToken.From(50.5)}, //single-precision float
55+
{"double2", MToken.From(15.2)},
56+
{"int1", 50505}, // implicitly converted integer
57+
{"int2", MToken.From(50)} // integer
58+
};
59+
60+
Assert.Equal(dictionary, dictionary2);
61+
Assert.NotEqual(dictionary, dictionary3);
62+
Assert.NotEqual(dictionary, MToken.From(50.5));
63+
}
64+
65+
[Fact]
66+
public void TestNumberEquality()
67+
{
68+
Assert.Equal(MToken.From(10), MToken.From(10));
69+
Assert.NotEqual(MToken.From(10), MToken.From(-10));
70+
Assert.NotEqual(MToken.From(10), -10);
71+
Assert.Equal(MToken.From(10), 10);
72+
Assert.Equal(10, MToken.From(10));
73+
Assert.Equal((long)10, MToken.From(10));
74+
Assert.Equal(10, MToken.From((long)10));
75+
Assert.Equal(MToken.From((int)10), MToken.From((double)10));
76+
Assert.Equal(MToken.From((decimal)10.12345), MToken.From((double)10.12345));
77+
Assert.NotEqual(MToken.From((decimal)10.12345), MToken.From((double)10.1245));
78+
Assert.Equal(MToken.From((object)10.12345d), MToken.From((object)10.12345m));
79+
}
80+
81+
[Fact]
82+
public void TestStringEquality()
83+
{
84+
Assert.Equal(MToken.From("hello!"), MToken.From("hello!"));
85+
Assert.NotEqual(MToken.From(10), MToken.From("hello!"));
86+
}
87+
88+
[Fact]
89+
public void TestStringCompare()
90+
{
91+
var arr = new MToken[]
92+
{
93+
"a",
94+
9,
95+
"z",
96+
"!",
97+
1,
98+
"b",
99+
};
100+
101+
Array.Sort(arr);
102+
Assert.Equal(1, arr[0]);
103+
Assert.Equal(9, arr[1]);
104+
Assert.Equal("!", arr[2]);
105+
Assert.Equal("a", arr[3]);
106+
Assert.Equal("b", arr[4]);
107+
Assert.Equal("z", arr[5]);
108+
}
109+
110+
[Fact]
111+
public void TestBoolNullEquality()
112+
{
113+
Assert.Equal(MToken.From(true), MToken.From(true));
114+
Assert.NotEqual(MToken.From(true), MToken.From(false));
115+
Assert.NotEqual(MToken.From(true), MToken.From(null));
116+
Assert.Equal(MToken.From(false), MToken.From(false));
117+
Assert.NotEqual(MToken.From(false), MToken.From(null));
118+
Assert.Equal(MToken.From(null), MToken.From(null));
119+
Assert.NotEqual(MToken.From(null), MToken.From(10));
120+
Assert.NotEqual(MToken.From(null), MToken.From("asd"));
121+
}
122+
123+
[Fact]
124+
public void TestArrayEquality()
125+
{
126+
var tests = new object[]
127+
{
128+
(float) 0,
129+
(float) 50505,
130+
1234.92d,
131+
800,
132+
(object)99656,
133+
"hello!",
134+
new[] {1, 2, 3, 4, 5},
135+
};
136+
137+
var m1 = tests.Select(f => MToken.From(f)).ToArray();
138+
var m2 = tests.Select(f => MToken.From(f)).ToArray();
139+
tests[1] = 0;
140+
var m3 = tests.Select(f => MToken.From(f)).ToArray();
141+
142+
Assert.Equal(m1, m2);
143+
Assert.NotEqual(m1, m3);
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)