|
1 | 1 | using System.Diagnostics; |
2 | 2 | using System.Text.Json; |
3 | | -using System.Text.Json.Nodes; |
4 | 3 | using System.Text.Json.Serialization; |
5 | 4 | using Rustwright; |
6 | 5 | using Rustwright.Runner; |
@@ -202,15 +201,82 @@ private static void AssertString(string? actual, JsonElement step, string descri |
202 | 201 |
|
203 | 202 | private static void AssertJsonEqual(object? actual, JsonElement expected) |
204 | 203 | { |
205 | | - var actualNode = JsonSerializer.SerializeToNode(actual); |
206 | | - var expectedNode = JsonNode.Parse(expected.GetRawText()); |
207 | | - if (!JsonNode.DeepEquals(actualNode, expectedNode)) |
| 204 | + var actualJson = JsonSerializer.SerializeToElement(actual); |
| 205 | + if (!JsonValuesEqual(actualJson, expected)) |
208 | 206 | { |
209 | 207 | throw new InvalidOperationException( |
210 | 208 | $"expected evaluate result {expected.GetRawText()}, got {JsonSerializer.Serialize(actual)}"); |
211 | 209 | } |
212 | 210 | } |
213 | 211 |
|
| 212 | + private static bool JsonValuesEqual(JsonElement left, JsonElement right) |
| 213 | + { |
| 214 | + if (left.ValueKind == JsonValueKind.Number && right.ValueKind == JsonValueKind.Number) |
| 215 | + { |
| 216 | + return JsonNumbersEqual(left, right); |
| 217 | + } |
| 218 | + |
| 219 | + if (left.ValueKind != right.ValueKind) |
| 220 | + { |
| 221 | + return false; |
| 222 | + } |
| 223 | + |
| 224 | + switch (left.ValueKind) |
| 225 | + { |
| 226 | + case JsonValueKind.Object: |
| 227 | + if (left.EnumerateObject().Count() != right.EnumerateObject().Count()) |
| 228 | + { |
| 229 | + return false; |
| 230 | + } |
| 231 | + |
| 232 | + foreach (var property in left.EnumerateObject()) |
| 233 | + { |
| 234 | + if (!right.TryGetProperty(property.Name, out var rightValue) || |
| 235 | + !JsonValuesEqual(property.Value, rightValue)) |
| 236 | + { |
| 237 | + return false; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + return true; |
| 242 | + case JsonValueKind.Array: |
| 243 | + if (left.GetArrayLength() != right.GetArrayLength()) |
| 244 | + { |
| 245 | + return false; |
| 246 | + } |
| 247 | + |
| 248 | + var leftItems = left.EnumerateArray(); |
| 249 | + var rightItems = right.EnumerateArray(); |
| 250 | + while (leftItems.MoveNext() && rightItems.MoveNext()) |
| 251 | + { |
| 252 | + if (!JsonValuesEqual(leftItems.Current, rightItems.Current)) |
| 253 | + { |
| 254 | + return false; |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + return true; |
| 259 | + case JsonValueKind.String: |
| 260 | + return string.Equals(left.GetString(), right.GetString(), StringComparison.Ordinal); |
| 261 | + case JsonValueKind.True: |
| 262 | + case JsonValueKind.False: |
| 263 | + case JsonValueKind.Null: |
| 264 | + return true; |
| 265 | + default: |
| 266 | + return false; |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + private static bool JsonNumbersEqual(JsonElement left, JsonElement right) |
| 271 | + { |
| 272 | + if (left.TryGetDecimal(out var leftDecimal) && right.TryGetDecimal(out var rightDecimal)) |
| 273 | + { |
| 274 | + return leftDecimal == rightDecimal; |
| 275 | + } |
| 276 | + |
| 277 | + return left.GetDouble().Equals(right.GetDouble()); |
| 278 | + } |
| 279 | + |
214 | 280 | private static IReadOnlyList<ManifestCase> SelectCases( |
215 | 281 | IReadOnlyList<ManifestCase> cases, |
216 | 282 | HashSet<string>? requestedIds) |
|
0 commit comments