Skip to content

Commit 85a1cd7

Browse files
karlseguinsjorsdonkers
authored andcommitted
Add children node to CDP Node representation
Add Node writer. Different CDP messages want different child depths. For now, only support immediate children, but the new writer should make it easy to support variable.
1 parent f095411 commit 85a1cd7

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/cdp/testing.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ const TestContext = struct {
285285
_ = self.client.?.serialized.orderedRemove(i);
286286
return;
287287
}
288+
288289
std.debug.print("Error not found. Expecting:\n{s}\n\nGot:\n", .{serialized});
289290
for (self.client.?.serialized.items, 0..) |sent, i| {
290291
std.debug.print("#{d}\n{s}\n\n", .{ i, sent });

src/testing.zig

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ pub fn isEqualJson(a: anytype, b: anytype) !bool {
257257
const aa = arena.allocator();
258258
const a_value = try convertToJson(aa, a);
259259
const b_value = try convertToJson(aa, b);
260-
expectJsonValue(a_value, b_value) catch return false;
261-
return true;
260+
return isJsonValue(a_value, b_value);
262261
}
263262

264263
fn convertToJson(arena: Allocator, value: anytype) !std.json.Value {
@@ -308,3 +307,46 @@ fn expectJsonValue(a: std.json.Value, b: std.json.Value) !void {
308307
},
309308
}
310309
}
310+
311+
fn isJsonValue(a: std.json.Value, b: std.json.Value) bool {
312+
if (std.mem.eql(u8, @tagName(a), @tagName(b)) == false) {
313+
return false;
314+
}
315+
316+
// at this point, we know that if a is an int, b must also be an int
317+
switch (a) {
318+
.null => return true,
319+
.bool => return a.bool == b.bool,
320+
.integer => return a.integer == b.integer,
321+
.float => return a.float == b.float,
322+
.number_string => return std.mem.eql(u8, a.number_string, b.number_string),
323+
.string => return std.mem.eql(u8, a.string, b.string),
324+
.array => {
325+
const a_len = a.array.items.len;
326+
const b_len = b.array.items.len;
327+
if (a_len != b_len) {
328+
return false;
329+
}
330+
for (a.array.items, b.array.items) |a_item, b_item| {
331+
if (isJsonValue(a_item, b_item) == false) {
332+
return false;
333+
}
334+
}
335+
return true;
336+
},
337+
.object => {
338+
var it = a.object.iterator();
339+
while (it.next()) |entry| {
340+
const key = entry.key_ptr.*;
341+
if (b.object.get(key)) |b_item| {
342+
if (isJsonValue(entry.value_ptr.*, b_item) == false) {
343+
return false;
344+
}
345+
} else {
346+
return false;
347+
}
348+
}
349+
return true;
350+
},
351+
}
352+
}

0 commit comments

Comments
 (0)