Merge pull request #51 from Browsercore/wpt

WPT tests
This commit is contained in:
Francis Bouvier
2023-10-18 16:49:11 +02:00
committed by GitHub
51 changed files with 9483 additions and 61 deletions

View File

@@ -2,9 +2,16 @@ const std = @import("std");
const parser = @import("../netsurf.zig");
const Node = @import("node.zig").Node;
const Element = @import("element.zig").Element;
const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const Node = @import("node.zig").Node;
const Element = @import("element.zig").Element;
const ElementUnion = @import("element.zig").Union;
// WEB IDL https://dom.spec.whatwg.org/#document
pub const Document = struct {
pub const Self = parser.Document;
pub const prototype = *Node;
@@ -15,20 +22,62 @@ pub const Document = struct {
// return .{};
// }
pub fn getElementById(self: *parser.Document, id: []const u8) ?*parser.Element {
return parser.documentGetElementById(self, id);
}
// JS funcs
// --------
pub fn get_body(_: *parser.Document) ?*parser.Body {
// TODO
return null;
pub fn _getElementById(self: *parser.Document, id: []const u8) ?ElementUnion {
const e = parser.documentGetElementById(self, id) orelse return null;
return Element.toInterface(e);
}
pub fn _getElementById(_: *parser.Document, _: []u8) ?*parser.Element {
// TODO
return null;
pub fn _createElement(self: *parser.Document, tag_name: []const u8) ElementUnion {
const e = parser.documentCreateElement(self, tag_name);
return Element.toInterface(e);
}
};
// Tests
// -----
pub fn testExecFn(
_: std.mem.Allocator,
js_env: *jsruntime.Env,
comptime _: []jsruntime.API,
) !void {
var constructor = [_]Case{
.{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" },
.{ .src = "document.__proto__.__proto__.__proto__.constructor.name", .ex = "Node" },
.{ .src = "document.__proto__.__proto__.__proto__.__proto__.constructor.name", .ex = "EventTarget" },
};
try checkCases(js_env, &constructor);
var getElementById = [_]Case{
.{ .src = "let getElementById = document.getElementById('content')", .ex = "undefined" },
.{ .src = "getElementById.constructor.name", .ex = "HTMLDivElement" },
.{ .src = "getElementById.localName", .ex = "div" },
};
try checkCases(js_env, &getElementById);
const tags = comptime parser.Tag.all();
const elements = comptime parser.Tag.allElements();
comptime var createElements: [(tags.len) * 3]Case = undefined;
inline for (tags, elements, 0..) |tag, element_name, i| {
// if (tag == .undef) {
// continue;
// }
const tag_name = @tagName(tag);
createElements[i * 3] = Case{
.src = "var " ++ tag_name ++ "Elem = document.createElement('" ++ tag_name ++ "')",
.ex = "undefined",
};
createElements[(i * 3) + 1] = Case{
.src = tag_name ++ "Elem.constructor.name",
.ex = "HTML" ++ element_name ++ "Element",
};
createElements[(i * 3) + 2] = Case{
.src = tag_name ++ "Elem.localName",
.ex = tag_name,
};
}
try checkCases(js_env, &createElements);
}

View File

@@ -3,12 +3,19 @@ const std = @import("std");
const parser = @import("../netsurf.zig");
const Node = @import("node.zig").Node;
const HTMLElem = @import("../html/elements.zig");
pub const Union = @import("../html/elements.zig").Union;
// WEB IDL https://dom.spec.whatwg.org/#element
pub const Element = struct {
pub const Self = parser.Element;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub fn toInterface(e: *parser.Element) Union {
return HTMLElem.toInterface(Union, e);
}
// JS funcs
// --------

View File

@@ -9,6 +9,7 @@ const checkCases = jsruntime.test_utils.checkCases;
const Document = @import("../dom/document.zig").Document;
const HTMLElem = @import("elements.zig");
// WEB IDL https://html.spec.whatwg.org/#the-document-object
pub const HTMLDocument = struct {
pub const Self = parser.DocumentHTML;
pub const prototype = *Document;
@@ -20,21 +21,6 @@ pub const HTMLDocument = struct {
pub fn get_body(self: *parser.DocumentHTML) ?*parser.Body {
return parser.documentHTMLBody(self);
}
pub fn _getElementById(self: *parser.DocumentHTML, id: []u8) ?HTMLElem.Union {
const doc = parser.documentHTMLToDocument(self);
const elem_dom = parser.documentGetElementById(doc, id);
if (elem_dom) |elem| {
return HTMLElem.toInterface(HTMLElem.Union, elem);
}
return null;
}
pub fn _createElement(self: *parser.DocumentHTML, tag_name: []const u8) HTMLElem.Union {
const doc_dom = parser.documentHTMLToDocument(self);
const base = parser.documentCreateElement(doc_dom, tag_name);
return HTMLElem.toInterface(HTMLElem.Union, base);
}
};
// Tests
@@ -48,39 +34,7 @@ pub fn testExecFn(
var constructor = [_]Case{
.{ .src = "document.__proto__.constructor.name", .ex = "HTMLDocument" },
.{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" },
.{ .src = "document.__proto__.__proto__.__proto__.constructor.name", .ex = "Node" },
.{ .src = "document.__proto__.__proto__.__proto__.__proto__.constructor.name", .ex = "EventTarget" },
.{ .src = "document.body.localName == 'body'", .ex = "true" },
};
try checkCases(js_env, &constructor);
var getElementById = [_]Case{
.{ .src = "let getElementById = document.getElementById('content')", .ex = "undefined" },
.{ .src = "getElementById.constructor.name", .ex = "HTMLDivElement" },
.{ .src = "getElementById.localName", .ex = "div" },
};
try checkCases(js_env, &getElementById);
const tags = comptime parser.Tag.all();
const elements = comptime parser.Tag.allElements();
comptime var createElements: [(tags.len) * 3]Case = undefined;
inline for (tags, elements, 0..) |tag, element_name, i| {
// if (tag == .undef) {
// continue;
// }
const tag_name = @tagName(tag);
createElements[i * 3] = Case{
.src = "var " ++ tag_name ++ "Elem = document.createElement('" ++ tag_name ++ "')",
.ex = "undefined",
};
createElements[(i * 3) + 1] = Case{
.src = tag_name ++ "Elem.constructor.name",
.ex = "HTML" ++ element_name ++ "Element",
};
createElements[(i * 3) + 2] = Case{
.src = tag_name ++ "Elem.localName",
.ex = tag_name,
};
}
try checkCases(js_env, &createElements);
}

279
src/main_wpt.zig Normal file
View File

@@ -0,0 +1,279 @@
const std = @import("std");
const parser = @import("netsurf.zig");
const jsruntime = @import("jsruntime");
const TPL = jsruntime.TPL;
const Env = jsruntime.Env;
const Loop = jsruntime.Loop;
const DOM = @import("dom.zig");
const HTMLElem = @import("html/elements.zig");
const wpt_dir = "tests/wpt";
// generate APIs
const apis = jsruntime.compile(DOM.Interfaces);
// FileLoader loads files content from the filesystem.
const FileLoader = struct {
const FilesMap = std.StringHashMap([]const u8);
files: FilesMap,
path: []const u8,
alloc: std.mem.Allocator,
fn init(alloc: std.mem.Allocator, path: []const u8) FileLoader {
const files = FilesMap.init(alloc);
return FileLoader{
.path = path,
.alloc = alloc,
.files = files,
};
}
fn get(self: *FileLoader, name: []const u8) ![]const u8 {
if (!self.files.contains(name)) {
try self.load(name);
}
return self.files.get(name).?;
}
fn load(self: *FileLoader, name: []const u8) !void {
const filename = try std.mem.concat(self.alloc, u8, &.{ self.path, name });
defer self.alloc.free(filename);
var file = try std.fs.cwd().openFile(filename, .{});
defer file.close();
const file_size = try file.getEndPos();
const content = try file.readToEndAlloc(self.alloc, file_size);
const namedup = try self.alloc.dupe(u8, name);
try self.files.put(namedup, content);
}
fn deinit(self: *FileLoader) void {
var iter = self.files.iterator();
while (iter.next()) |entry| {
self.alloc.free(entry.key_ptr.*);
self.alloc.free(entry.value_ptr.*);
}
self.files.deinit();
}
};
// TODO For now the WPT tests run is specific to WPT.
// It manually load js framwork libs, and run the first script w/ js content in
// the HTML page.
// Once browsercore will have the html loader, it would be useful to refacto
// this test to use it.
pub fn main() !void {
std.debug.print("Running WPT test suite\n", .{});
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args);
const filter = args[1..];
// initialize VM JS lib.
const vm = jsruntime.VM.init();
defer vm.deinit();
// prepare libraries to load on each test case.
var loader = FileLoader.init(alloc, "tests/wpt");
defer loader.deinit();
// browse the dir to get the tests dynamically.
var list = std.ArrayList([]const u8).init(alloc);
try findWPTTests(alloc, wpt_dir, &list);
defer {
for (list.items) |tc| {
alloc.free(tc);
}
list.deinit();
}
var run: usize = 0;
var failures: usize = 0;
for (list.items) |tc| {
if (filter.len > 0) {
var match = false;
for (filter) |f| {
if (std.mem.startsWith(u8, tc, f)) {
match = true;
break;
}
if (std.mem.endsWith(u8, tc, f)) {
match = true;
break;
}
}
if (!match) {
continue;
}
}
run += 1;
// create an arena and deinit it for each test case.
var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit();
// TODO I don't use testing.expect here b/c I want to execute all the
// tests. And testing.expect stops running test in the first failure.
const res = runWPT(&arena, tc, &loader) catch |err| {
std.debug.print("FAIL\t{s}\n{any}\n", .{ tc, err });
failures += 1;
continue;
};
// no need to call res.deinit() thanks to the arena allocator.
if (!res.success) {
std.debug.print("FAIL\t{s}\n{s}\n", .{ tc, res.stack orelse res.result });
failures += 1;
continue;
}
if (!std.mem.eql(u8, res.result, "Pass")) {
std.debug.print("FAIL\t{s}\n{s}\n", .{ tc, res.stack orelse res.result });
failures += 1;
continue;
}
std.debug.print("PASS\t{s}\n", .{tc});
}
if (failures > 0) {
std.debug.print("{d}/{d} tests failures\n", .{ failures, run });
std.os.exit(1);
}
}
// runWPT parses the given HTML file, starts a js env and run the first script
// tags containing javascript sources.
// It loads first the js libs files.
fn runWPT(arena: *std.heap.ArenaAllocator, f: []const u8, loader: *FileLoader) !jsruntime.JSResult {
const alloc = arena.allocator();
// document
const html_doc = try parser.documentHTMLParseFromFileAlloc(alloc, f);
const doc = parser.documentHTMLToDocument(html_doc);
// create JS env
var loop = try Loop.init(alloc);
defer loop.deinit();
var js_env = try Env.init(arena, &loop);
defer js_env.deinit();
// load APIs in JS env
var tpls: [apis.len]TPL = undefined;
try js_env.load(apis, &tpls);
// start JS env
js_env.start(apis);
defer js_env.stop();
// add document object
try js_env.addObject(apis, html_doc, "document");
// alias global as self and window
try js_env.attachObject(try js_env.getGlobal(), "self", null);
try js_env.attachObject(try js_env.getGlobal(), "window", null);
// thanks to the arena, we don't need to deinit res.
var res: jsruntime.JSResult = undefined;
const init =
\\window.listeners = [];
\\window.document = document;
\\window.parent = window;
\\window.addEventListener = function (type, listener, options) {
\\ window.listeners.push({type: type, listener: listener, options: options});
\\};
\\window.dispatchEvent = function (event) {
\\ len = window.listeners.length;
\\ for (var i = 0; i < len; i++) {
\\ if (window.listeners[i].type == event.target) {
\\ window.listeners[i].listener(event);
\\ }
\\ }
\\ return true;
\\};
\\window.removeEventListener = function () {};
;
res = try evalJS(js_env, alloc, init, "init");
if (!res.success) {
return res;
}
// TODO load <script src> attributes instead of the static list.
res = try evalJS(js_env, alloc, try loader.get("/resources/testharness.js"), "testharness.js");
if (!res.success) {
return res;
}
res = try evalJS(js_env, alloc, try loader.get("/resources/testharnessreport.js"), "testharnessreport.js");
if (!res.success) {
return res;
}
// loop hover the scripts.
const scripts = parser.documentGetElementsByTagName(doc, "script");
const slen = parser.nodeListLength(scripts);
for (0..slen) |i| {
const s = parser.nodeListItem(scripts, @intCast(i)).?;
const src = parser.nodeTextContent(s).?;
res = try evalJS(js_env, alloc, src, "");
// return the first failure.
if (!res.success) {
return res;
}
}
// Mark tests as ready to run.
res = try evalJS(js_env, alloc, "window.dispatchEvent({target: 'load'});", "ready");
if (!res.success) {
return res;
}
// Check the final test status.
res = try evalJS(js_env, alloc, "report.status;", "teststatus");
if (!res.success) {
return res;
}
// If the test failed, return detailed logs intead of the simple status.
if (!std.mem.eql(u8, res.result, "Pass")) {
return try evalJS(js_env, alloc, "report.log", "teststatus");
}
// return the final result.
return res;
}
fn evalJS(env: jsruntime.Env, alloc: std.mem.Allocator, script: []const u8, name: ?[]const u8) !jsruntime.JSResult {
var res = jsruntime.JSResult{};
try env.run(alloc, script, name, &res, null);
return res;
}
// browse the path to find the tests list.
fn findWPTTests(allocator: std.mem.Allocator, path: []const u8, list: *std.ArrayList([]const u8)) !void {
var dir = try std.fs.cwd().openIterableDir(path, .{ .no_follow = true });
defer dir.close();
var walker = try dir.walk(allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (entry.kind != .file) {
continue;
}
if (!std.mem.endsWith(u8, entry.basename, ".html")) {
continue;
}
try list.append(try std.fs.path.join(allocator, &.{ path, entry.path }));
}
}

View File

@@ -228,6 +228,27 @@ pub const NodeType = enum(u4) {
notation = c.DOM_NOTATION_NODE, // historical
};
// NodeList
pub const NodeList = c.dom_nodelist;
pub fn nodeListLength(nodeList: *NodeList) u32 {
var ln: u32 = undefined;
_ = c.dom_nodelist_get_length(nodeList, &ln);
return ln;
}
pub fn nodeListItem(nodeList: *NodeList, index: u32) ?*Node {
var n: [*c]c.dom_node = undefined;
_ = c._dom_nodelist_item(nodeList, index, &n);
if (n == null) {
return null;
}
// cast [*c]c.dom_node into *Node
return @as(*Node, @ptrCast(n));
}
// Node
pub const Node = c.dom_node_internal;
@@ -636,6 +657,12 @@ pub inline fn documentGetElementById(doc: *Document, id: []const u8) ?*Element {
return elem;
}
pub inline fn documentGetElementsByTagName(doc: *Document, tagname: []const u8) *NodeList {
var nlist: ?*NodeList = undefined;
_ = documentVtable(doc).dom_document_get_elements_by_tag_name.?(doc, stringFromData(tagname), &nlist);
return nlist.?;
}
pub inline fn documentCreateElement(doc: *Document, tag_name: []const u8) *Element {
var elem: ?*Element = undefined;
_ = documentVtable(doc).dom_document_create_element.?(doc, stringFromData(tag_name), &elem);

View File

@@ -6,7 +6,8 @@ const generate = @import("generate.zig");
const parser = @import("netsurf.zig");
const DOM = @import("dom.zig");
const docTestExecFn = @import("html/document.zig").testExecFn;
const documentTestExecFn = @import("dom/document.zig").testExecFn;
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
const nodeTestExecFn = @import("dom/node.zig").testExecFn;
const characterDataTestExecFn = @import("dom/character_data.zig").testExecFn;
const textTestExecFn = @import("dom/text.zig").testExecFn;
@@ -45,7 +46,8 @@ fn testsAllExecFn(
comptime apis: []jsruntime.API,
) !void {
const testFns = [_]jsruntime.ContextExecFn{
docTestExecFn,
documentTestExecFn,
HTMLDocumentTestExecFn,
nodeTestExecFn,
characterDataTestExecFn,
textTestExecFn,