-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathJsonTestHelper.cs
318 lines (299 loc) · 14.9 KB
/
JsonTestHelper.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json.Serialization.Tests;
using System.Text.Json.Serialization.Tests.Schemas.OrderPayload;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
namespace System.Text.Json
{
internal static partial class JsonTestHelper
{
public static void AssertJsonEqual(string expected, string actual)
{
using JsonDocument expectedDom = JsonDocument.Parse(expected);
using JsonDocument actualDom = JsonDocument.Parse(actual);
AssertJsonEqual(expectedDom.RootElement, actualDom.RootElement);
}
public static void AssertJsonEqual(JsonElement expected, JsonElement actual)
{
AssertJsonEqualCore(expected, actual, new());
}
private static void AssertJsonEqualCore(JsonElement expected, JsonElement actual, Stack<object> path)
{
JsonValueKind valueKind = expected.ValueKind;
AssertTrue(passCondition: valueKind == actual.ValueKind);
switch (valueKind)
{
case JsonValueKind.Object:
var expectedProperties = new List<string>();
foreach (JsonProperty property in expected.EnumerateObject())
{
expectedProperties.Add(property.Name);
}
var actualProperties = new List<string>();
foreach (JsonProperty property in actual.EnumerateObject())
{
actualProperties.Add(property.Name);
}
foreach (var property in expectedProperties.Except(actualProperties))
{
AssertTrue(passCondition: false, $"Property \"{property}\" missing from actual object.");
}
foreach (var property in actualProperties.Except(expectedProperties))
{
AssertTrue(passCondition: false, $"Actual object defines additional property \"{property}\".");
}
foreach (string name in expectedProperties)
{
path.Push(name);
AssertJsonEqualCore(expected.GetProperty(name), actual.GetProperty(name), path);
path.Pop();
}
break;
case JsonValueKind.Array:
JsonElement.ArrayEnumerator expectedEnumerator = expected.EnumerateArray();
JsonElement.ArrayEnumerator actualEnumerator = actual.EnumerateArray();
int i = 0;
while (expectedEnumerator.MoveNext())
{
AssertTrue(passCondition: actualEnumerator.MoveNext(), "Actual array contains fewer elements.");
path.Push(i++);
AssertJsonEqualCore(expectedEnumerator.Current, actualEnumerator.Current, path);
path.Pop();
}
AssertTrue(passCondition: !actualEnumerator.MoveNext(), "Actual array contains additional elements.");
break;
case JsonValueKind.String:
AssertTrue(passCondition: expected.GetString() == actual.GetString());
break;
case JsonValueKind.Number:
case JsonValueKind.True:
case JsonValueKind.False:
case JsonValueKind.Null:
AssertTrue(passCondition: expected.GetRawText() == actual.GetRawText());
break;
default:
Debug.Fail($"Unexpected JsonValueKind: JsonValueKind.{valueKind}.");
break;
}
void AssertTrue(bool passCondition, string? message = null)
{
if (!passCondition)
{
message ??= "Expected JSON does not match actual value";
Assert.Fail($"{message}\nExpected JSON: {expected}\n Actual JSON: {actual}\n in JsonPath: {BuildJsonPath(path)}");
}
// TODO replace with JsonPath implementation for JsonElement
// cf. https://github.com/dotnet/runtime/issues/31068
static string BuildJsonPath(Stack<object> path)
{
var sb = new StringBuilder("$");
foreach (object node in path.Reverse())
{
string pathNode = node is string propertyName
? "." + propertyName
: $"[{(int)node}]";
sb.Append(pathNode);
}
return sb.ToString();
}
}
}
public static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
{
var list = new List<T>();
await foreach (T item in source)
{
list.Add(item);
}
return list;
}
private static readonly Regex s_stripWhitespace = new Regex(@"\s+", RegexOptions.Compiled);
public static string StripWhitespace(this string value)
=> s_stripWhitespace.Replace(value, string.Empty);
internal static List<Order> PopulateLargeObject(int size)
{
List<Order> orders = new List<Order>(size);
for (int i = 0; i < size; i++)
{
Order order = new Order
{
OrderNumber = i,
Customer = new User
{
UserId = "222ffbbb888kkk",
Name = "John Doe",
Username = "johndoe",
CreatedAt = new DateTime(),
ImageId = string.Empty,
UserType = UserType.Customer,
UpdatedAt = new DateTime(),
TwitterId = string.Empty,
FacebookId = "9988998877662222111",
SubscriptionType = 2,
IsNew = true,
IsEmployee = false
},
ShippingInfo = new List<ShippingInfo>
{
new ShippingInfo()
{
OrderNumber = i,
Employee = new User
{
UserId = "222ffbbb888" + i,
Name = "Shipping Coordinator",
Username = "coordinator" + i,
CreatedAt = new DateTime(),
ImageId = string.Empty,
UserType = UserType.Employee,
UpdatedAt = new DateTime(),
TwitterId = string.Empty,
SubscriptionType = 0,
IsEmployee = true
},
CarrierId = "TTT123999MMM",
ShippingType = "Ground",
EstimatedDelivery = new DateTime(),
Tracking = new Uri("http://TestShipCompany.test/track/123" + i),
CarrierName = "TestShipCompany",
HandlingInstruction = "Do cats eat bats? Do cats eat bats. Do cats eat bats? Do cats eat bats. Do cats eat bats? Do cats eat bats. Do cats eat bats? Do cats eat bats",
CurrentStatus = "Out for delivery",
IsDangerous = false
}
},
OneTime = true,
Cancelled = false,
IsGift = i % 2 == 0,
IsGPickUp = i % 5 == 0,
ShippingAddress = new Address()
{
City = "Redmond"
},
PickupAddress = new Address
{
City = "Bellevue"
},
Coupon = SampleEnumInt64.Max,
UserInteractions = new List<Comment>
{
new Comment
{
Id = 200 + i,
OrderNumber = i,
Customer = new User
{
UserId = "222ffbbb888kkk",
Name = "John Doe",
Username = "johndoe",
CreatedAt = new DateTime(),
ImageId = string.Empty,
UserType = UserType.Customer,
UpdatedAt = new DateTime(),
TwitterId = "twitterId" + i,
FacebookId = "9988998877662222111",
SubscriptionType = 2,
IsNew = true,
IsEmployee = false
},
Title = "Green Field",
Message = "Down, down, down. Would the fall never come to an end! 'I wonder how many miles I've fallen by this time. I think-' (for, you see, Alice had learnt several things of this sort in her lessons in the schoolroom, and though this was not a very good opportunity for showing off her knowledge, as there was no one to listen to her, still it was good practice to say it over) '-yes, that's about the right distance-but then I wonder what Latitude or Longitude I've got to",
Responses = new List<Comment>()
}
},
Created = new DateTime(2019, 11, 10),
Confirmed = new DateTime(2019, 11, 11),
ShippingDate = new DateTime(2019, 11, 12),
EstimatedDelivery = new DateTime(2019, 11, 15),
ReviewedBy = new User()
{
UserId = "222ffbbb888" + i,
Name = "Shipping Coordinator",
Username = "coordinator" + i,
CreatedAt = new DateTime(),
ImageId = string.Empty,
UserType = UserType.Employee,
UpdatedAt = new DateTime(),
TwitterId = string.Empty,
SubscriptionType = 0,
IsEmployee = true
}
};
List<Product> products = new List<Product>();
for (int j = 0; j < i % 4; j++)
{
Product product = new Product()
{
ProductId = Guid.NewGuid(),
Name = "Surface Pro",
SKU = "LL123" + j,
Brand = new TestClassWithInitializedProperties(),
ProductCategory = new SimpleTestClassWithNonGenericCollectionWrappers(),
Description = "Down, down, down. Would the fall never come to an end! 'I wonder how many miles I've fallen by this time. I think-' (for, you see, Alice had learnt several things of this sort in her lessons in the schoolroom, and though this was not a very good opportunity for showing off her knowledge, as there was no one to listen to her, still it was good practice to say it over) '-yes, that's about the right distance-but then I wonder what Latitude or Longitude I've got to",
Created = new DateTime(2000, 10, 12),
Title = "Surface Pro 6 for Business - 512GB",
Price = new Price(),
BestChoice = true,
AverageStars = 4.8f,
Featured = true,
ProductRestrictions = new TestClassWithInitializedProperties(),
SalesInfo = new SimpleTestClassWithGenericCollectionWrappers(),
Origin = SampleEnum.One,
Manufacturer = new BasicCompany(),
Fragile = true,
DetailsUrl = new Uri("http://dotnet.test/link/entries/entry/1"),
NetWeight = 2.7m,
GrossWeight = 3.3m,
Length = i,
Height = i + 1,
Width = i + 2,
FeaturedImage = new FeaturedImage(),
PreviewImage = new PreviewImage(),
KeyWords = new List<string> { "surface", "pro", "laptop" },
RelatedImages = new List<Image>(),
RelatedVideo = new Uri("http://dotnet.test/link/entries/entry/2"),
GuaranteeStartsAt = new DateTime(),
GuaranteeEndsAt = new DateTime(),
IsActive = true,
RelatedProducts = new List<Product>()
};
product.SalesInfo.Initialize();
List<Review> reviews = new List<Review>();
for (int k = 0; k < i % 3; k++)
{
Review review = new Review
{
Customer = new User
{
UserId = "333344445555",
Name = "Customer" + i + k,
Username = "cust" + i + k,
CreatedAt = new DateTime(),
ImageId = string.Empty,
UserType = UserType.Customer,
SubscriptionType = k
},
ProductSku = product.SKU,
CustomerName = "Customer" + i + k,
Stars = j + k,
Title = $"Title {i}{j}{k}",
Comment = "",
Images = new List<Uri> { new Uri($"http://dotnet.test/link/images/image/{k}"), new Uri($"http://dotnet.test/link/images/image/{j}") },
ReviewId = i + j + k
};
reviews.Add(review);
}
product.Reviews = reviews;
products.Add(product);
}
order.Products = products;
orders.Add(order);
}
return orders;
}
}
}