css: refacto test

This commit is contained in:
Pierre Tachoire
2024-03-18 11:35:47 +01:00
parent 954a693586
commit 7839f466ea
3 changed files with 105 additions and 15 deletions

View File

@@ -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();
}
}

View File

@@ -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 doc = try parser.documentHTMLParseFromStr("<body><address>This address...</address></body>");
const testcases = [_]struct {
q: []const u8,
html: []const u8,
exp: usize,
}{
.{
.q = "address",
.html = "<body><address>This address...</address></body>",
.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.expect(1 == matcher.nodes.items.len);
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 testcases = [_]struct {
q: []const u8,
html: []const u8,
exp: usize,
}{
.{
.q = "address",
.html = "<body><address>This address...</address></body>",
.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.matchAll(s, node, &matcher);
try std.testing.expectEqual(tc.exp, matcher.nodes.items.len);
}
}

View File

@@ -46,17 +46,57 @@ const Matcher = struct {
test "matchFirst" {
const alloc = std.testing.allocator;
const s = try css.parse(alloc, "address", .{});
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);
}
}