mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
css: refacto test
This commit is contained in:
@@ -37,7 +37,7 @@ pub fn matchAll(s: Selector, node: anytype, m: anytype) !void {
|
|||||||
if (c == null) break;
|
if (c == null) break;
|
||||||
|
|
||||||
if (try s.match(c.?)) try m.match(c.?);
|
if (try s.match(c.?)) try m.match(c.?);
|
||||||
try matchFirst(s, c.?, m);
|
try matchAll(s, c.?, m);
|
||||||
c = try c.?.nextSibling();
|
c = try c.?.nextSibling();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,17 +28,67 @@ const Matcher = struct {
|
|||||||
test "matchFirst" {
|
test "matchFirst" {
|
||||||
const alloc = std.testing.allocator;
|
const alloc = std.testing.allocator;
|
||||||
|
|
||||||
const s = try css.parse(alloc, "address", .{});
|
var matcher = Matcher.init(alloc);
|
||||||
defer s.deinit(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.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);
|
var matcher = Matcher.init(alloc);
|
||||||
defer matcher.deinit();
|
defer matcher.deinit();
|
||||||
|
|
||||||
const doc = try parser.documentHTMLParseFromStr("<body><address>This address...</address></body>");
|
const testcases = [_]struct {
|
||||||
defer parser.documentHTMLClose(doc) catch {};
|
q: []const u8,
|
||||||
|
html: []const u8,
|
||||||
|
exp: usize,
|
||||||
|
}{
|
||||||
|
.{
|
||||||
|
.q = "address",
|
||||||
|
.html = "<body><address>This address...</address></body>",
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const node = Node{ .node = parser.documentHTMLToNode(doc) };
|
for (testcases) |tc| {
|
||||||
|
matcher.reset();
|
||||||
|
|
||||||
_ = try css.matchFirst(s, node, &matcher);
|
const doc = try parser.documentHTMLParseFromStr(tc.html);
|
||||||
try std.testing.expect(1 == matcher.nodes.items.len);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,17 +46,57 @@ const Matcher = struct {
|
|||||||
test "matchFirst" {
|
test "matchFirst" {
|
||||||
const alloc = std.testing.allocator;
|
const alloc = std.testing.allocator;
|
||||||
|
|
||||||
const s = try css.parse(alloc, "address", .{});
|
var matcher = Matcher.init(alloc);
|
||||||
defer s.deinit(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);
|
var matcher = Matcher.init(alloc);
|
||||||
defer matcher.deinit();
|
defer matcher.deinit();
|
||||||
|
|
||||||
const node: Node = .{
|
const testcases = [_]struct {
|
||||||
.child = &.{ .name = "address" },
|
q: []const u8,
|
||||||
|
n: Node,
|
||||||
|
exp: usize,
|
||||||
|
}{
|
||||||
|
.{
|
||||||
|
.q = "address",
|
||||||
|
.n = .{ .name = "body", .child = &.{ .name = "address" } },
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
_ = try css.matchFirst(s, &node, &matcher);
|
for (testcases) |tc| {
|
||||||
try std.testing.expect(1 == matcher.nodes.items.len);
|
matcher.reset();
|
||||||
try std.testing.expect(matcher.nodes.items[0] == node.child);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user