Skip to content

Commit 092de3d

Browse files
committed
🔄 synced local 'csharp/' with remote 'csharp/'
Skyvern-AI/rustwright-cloud#186
1 parent 43a3be3 commit 092de3d

1 file changed

Lines changed: 70 additions & 4 deletions

File tree

‎csharp/Runner/Program.cs‎

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Diagnostics;
22
using System.Text.Json;
3-
using System.Text.Json.Nodes;
43
using System.Text.Json.Serialization;
54
using Rustwright;
65
using Rustwright.Runner;
@@ -202,15 +201,82 @@ private static void AssertString(string? actual, JsonElement step, string descri
202201

203202
private static void AssertJsonEqual(object? actual, JsonElement expected)
204203
{
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))
208206
{
209207
throw new InvalidOperationException(
210208
$"expected evaluate result {expected.GetRawText()}, got {JsonSerializer.Serialize(actual)}");
211209
}
212210
}
213211

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+
214280
private static IReadOnlyList<ManifestCase> SelectCases(
215281
IReadOnlyList<ManifestCase> cases,
216282
HashSet<string>? requestedIds)

0 commit comments

Comments
 (0)