forked from gregsdennis/Manatee.Json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinqExtensionsTest.cs
204 lines (203 loc) · 7 KB
/
LinqExtensionsTest.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using Manatee.Json.Serialization;
using Manatee.Json.Tests.Test_References;
using NUnit.Framework;
// ReSharper disable ExpressionIsAlwaysNull
namespace Manatee.Json.Tests
{
[TestFixture]
public class LinqExtensionsTest
{
[Test]
public void ToJson_JsonValueCollection_ReturnsArray()
{
var json = new List<JsonValue> {false, 42, "a string", "another string"};
var expected = new JsonArray {false, 42, "a string", "another string"};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_KeyValuePairCollection_ReturnsObject()
{
var json = new List<KeyValuePair<string, JsonValue>>
{
new KeyValuePair<string, JsonValue>("bool", false),
new KeyValuePair<string, JsonValue>("int", 42),
new KeyValuePair<string, JsonValue>("string", "a string"),
new KeyValuePair<string, JsonValue>("string2", "another string")
};
var expected = new JsonObject {{"bool", false}, {"int", 42}, {"string", "a string"}, {"string2", "another string"}};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_StringArray_ReturnsArray()
{
var json = new List<string> {"a string", "another string"};
JsonValue expected = new JsonArray {"a string", "another string"};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullStringArray_ReturnsNull()
{
var json = (IEnumerable<string>) null;
var expected = JsonValue.Null;
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_BooleanArray_ReturnsArray()
{
var json = new List<bool> {false, true};
JsonValue expected = new JsonArray {false, true};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullBooleanArray_ReturnsNull()
{
var json = (IEnumerable<bool>) null;
var expected = JsonValue.Null;
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullableBooleanArray_ReturnsArray()
{
var json = new List<bool?> {false, true, null};
JsonValue expected = new JsonArray {false, true, JsonValue.Null};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullNullableBooleanArray_ReturnsNull()
{
var json = (IEnumerable<bool?>) null;
var expected = JsonValue.Null;
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NumberArray_ReturnsArray()
{
var json = new List<double> {5, 6};
JsonValue expected = new JsonArray {5, 6};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullNumberArray_ReturnsNull()
{
var json = (IEnumerable<double>) null;
var expected = JsonValue.Null;
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullableNumberArray_ReturnsArray()
{
var json = new List<double?> {5, 6, null};
JsonValue expected = new JsonArray {5, 6, JsonValue.Null};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullNullableNumberArray_ReturnsNull()
{
var json = (IEnumerable<double?>) null;
var expected = JsonValue.Null;
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_JsonArrayArray_ReturnsArray()
{
var json = new List<JsonArray> {new JsonArray {false, true}, new JsonArray {"a string", "another string"}};
JsonValue expected = new JsonArray {new JsonArray {false, true}, new JsonArray {"a string", "another string"}};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullJsonArrayArray_ReturnsNull()
{
var json = (IEnumerable<JsonArray>) null;
var expected = JsonValue.Null;
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_JsonObjectArray_ReturnsArray()
{
var json = new List<JsonObject> {new JsonObject {{"bool", false}, {"int", 42}}, new JsonObject {{"string", "a string"}, {"string2", "another string"}}};
JsonValue expected = new JsonArray {new JsonObject {{"bool", false}, {"int", 42}}, new JsonObject {{"string", "a string"}, {"string2", "another string"}}};
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullJsonObjectArray_ReturnsNull()
{
var json = (IEnumerable<JsonObject>) null;
var expected = JsonValue.Null;
var actual = json.ToJson();
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_SerializableArray_ReturnsArray()
{
var serializer = new JsonSerializer();
var json = new List<JsonSerializableClass> {new JsonSerializableClass("this", 0), new JsonSerializableClass("that", 1)};
JsonValue expected = new JsonArray {new JsonObject {{"StringProp", "this"}, {"IntProp", 0}}, new JsonObject {{"StringProp", "that"}, {"IntProp", 1}}};
var actual = json.ToJson(serializer);
Assert.AreEqual(expected, actual);
}
[Test]
public void ToJson_NullSerializableArray_ReturnsNull()
{
var serializer = new JsonSerializer();
var json = (IEnumerable<JsonSerializableClass>)null;
var expected = JsonValue.Null;
var actual = json.ToJson(serializer);
Assert.AreEqual(expected, actual);
}
[Test]
public void FromJson_SerializableArray_ReturnsArray()
{
var serializer = new JsonSerializer();
var json = new JsonArray { new JsonObject { { "StringProp", "this" }, { "IntProp", 0 } }, new JsonObject { { "StringProp", "that" }, { "IntProp", 1 } } };
var expected = new List<JsonSerializableClass> { new JsonSerializableClass("this", 0), new JsonSerializableClass("that", 1) };
var actual = json.FromJson<JsonSerializableClass>(serializer);
Assert.IsTrue(expected.SequenceEqual(actual));
}
[Test]
public void FromJson_NullSerializableArray_ReturnsNull()
{
Assert.Throws<ArgumentNullException>(() =>
{
var serializer = new JsonSerializer();
var json = (JsonArray) null;
json.FromJson<JsonSerializableClass>(serializer).ToList();
});
}
[Test]
public void FromJson_SerializableObject_ReturnsObject()
{
var serializer = new JsonSerializer();
var json = new JsonObject { { "StringProp", "this" }, { "IntProp", 0 } };
var expected = new JsonSerializableClass("this", 0);
var actual = json.FromJson<JsonSerializableClass>(serializer);
Assert.AreEqual(expected, actual);
}
[Test]
public void FromJson_NullSerializableObject_ReturnsNull()
{
var serializer = new JsonSerializer();
var json = (JsonObject)null;
var expected = (IEnumerable<JsonSerializableClass>)null;
var actual = json.FromJson<JsonSerializableClass>(serializer);
Assert.AreEqual(expected, actual);
}
}
}