diff --git a/src/css/css.zig b/src/css/css.zig index 9bc6cca2..fc5e8995 100644 --- a/src/css/css.zig +++ b/src/css/css.zig @@ -37,7 +37,7 @@ pub fn matchAll(s: Selector, node: anytype, m: anytype) !void { if (c == null) break; if (try s.match(c.?)) try m.match(c.?); - try matchFirst(s, c.?, m); + try matchAll(s, c.?, m); c = try c.?.nextSibling(); } } diff --git a/src/css/libdom_test.zig b/src/css/libdom_test.zig index f143c17b..5a952cfc 100644 --- a/src/css/libdom_test.zig +++ b/src/css/libdom_test.zig @@ -28,17 +28,67 @@ const Matcher = struct { test "matchFirst" { const alloc = std.testing.allocator; - const s = try css.parse(alloc, "address", .{}); - defer s.deinit(alloc); + var matcher = Matcher.init(alloc); + defer matcher.deinit(); + + const testcases = [_]struct { + q: []const u8, + html: []const u8, + exp: usize, + }{ + .{ + .q = "address", + .html = "
This address...", + .exp = 1, + }, + }; + + for (testcases) |tc| { + matcher.reset(); + + const doc = try parser.documentHTMLParseFromStr(tc.html); + defer parser.documentHTMLClose(doc) catch {}; + + const s = try css.parse(alloc, tc.q, .{}); + defer s.deinit(alloc); + + const node = Node{ .node = parser.documentHTMLToNode(doc) }; + + _ = try css.matchFirst(s, node, &matcher); + try std.testing.expectEqual(tc.exp, matcher.nodes.items.len); + } +} + +test "matchAll" { + const alloc = std.testing.allocator; var matcher = Matcher.init(alloc); defer matcher.deinit(); - const doc = try parser.documentHTMLParseFromStr("This address..."); - defer parser.documentHTMLClose(doc) catch {}; + const testcases = [_]struct { + q: []const u8, + html: []const u8, + exp: usize, + }{ + .{ + .q = "address", + .html = "This address...", + .exp = 1, + }, + }; - const node = Node{ .node = parser.documentHTMLToNode(doc) }; + for (testcases) |tc| { + matcher.reset(); - _ = try css.matchFirst(s, node, &matcher); - try std.testing.expect(1 == matcher.nodes.items.len); + const doc = try parser.documentHTMLParseFromStr(tc.html); + defer parser.documentHTMLClose(doc) catch {}; + + const s = try css.parse(alloc, tc.q, .{}); + defer s.deinit(alloc); + + const node = Node{ .node = parser.documentHTMLToNode(doc) }; + + _ = try css.matchAll(s, node, &matcher); + try std.testing.expectEqual(tc.exp, matcher.nodes.items.len); + } } diff --git a/src/css/match_test.zig b/src/css/match_test.zig index 90c3cd1a..5df71da6 100644 --- a/src/css/match_test.zig +++ b/src/css/match_test.zig @@ -46,17 +46,57 @@ const Matcher = struct { test "matchFirst" { const alloc = std.testing.allocator; - const s = try css.parse(alloc, "address", .{}); - defer s.deinit(alloc); + var matcher = Matcher.init(alloc); + defer matcher.deinit(); + + const testcases = [_]struct { + q: []const u8, + n: Node, + exp: usize, + }{ + .{ + .q = "address", + .n = .{ .name = "body", .child = &.{ .name = "address" } }, + .exp = 1, + }, + }; + + for (testcases) |tc| { + matcher.reset(); + + const s = try css.parse(alloc, tc.q, .{}); + defer s.deinit(alloc); + + _ = try css.matchFirst(s, &tc.n, &matcher); + try std.testing.expectEqual(tc.exp, matcher.nodes.items.len); + } +} + +test "matchAll" { + const alloc = std.testing.allocator; var matcher = Matcher.init(alloc); defer matcher.deinit(); - const node: Node = .{ - .child = &.{ .name = "address" }, + const testcases = [_]struct { + q: []const u8, + n: Node, + exp: usize, + }{ + .{ + .q = "address", + .n = .{ .name = "body", .child = &.{ .name = "address" } }, + .exp = 1, + }, }; - _ = try css.matchFirst(s, &node, &matcher); - try std.testing.expect(1 == matcher.nodes.items.len); - try std.testing.expect(matcher.nodes.items[0] == node.child); + for (testcases) |tc| { + matcher.reset(); + + const s = try css.parse(alloc, tc.q, .{}); + defer s.deinit(alloc); + + _ = try css.matchAll(s, &tc.n, &matcher); + try std.testing.expectEqual(tc.exp, matcher.nodes.items.len); + } }