From 146b11741c8fccf03b51936b1043e97e1fdeb9ef Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Sun, 8 Dec 2024 23:30:51 +0100 Subject: [PATCH] html2/test: Migrate to etest2 --- html2/character_reference_test.cpp | 46 +- html2/parser_states_test.cpp | 397 ++++++------ html2/token_test.cpp | 50 +- html2/tokenizer_test.cpp | 950 +++++++++++++++-------------- 4 files changed, 723 insertions(+), 720 deletions(-) diff --git a/html2/character_reference_test.cpp b/html2/character_reference_test.cpp index 9b0e69b0..6535abd0 100644 --- a/html2/character_reference_test.cpp +++ b/html2/character_reference_test.cpp @@ -1,53 +1,51 @@ -// SPDX-FileCopyrightText: 2022 Robin Lindén +// SPDX-FileCopyrightText: 2022-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause #include "html2/character_reference.h" -#include "etest/etest.h" +#include "etest/etest2.h" #include using namespace std::literals; -using etest::expect; -using etest::require; - using namespace html2; int main() { - etest::test("no entity found", [] { + etest::Suite s; + s.add_test("no entity found", [](etest::IActions &a) { auto ref = find_named_character_reference_for("A"sv); - expect(!ref.has_value()); + a.expect(!ref.has_value()); }); - etest::test("single-codepoint entity", [] { + s.add_test("single-codepoint entity", [](etest::IActions &a) { auto ref = find_named_character_reference_for("<"sv); - require(ref.has_value()); - expect(ref->name == "<"sv); - expect(ref->first_codepoint == '<'); - expect(!ref->second_codepoint.has_value()); + a.require(ref.has_value()); + a.expect(ref->name == "<"sv); + a.expect(ref->first_codepoint == '<'); + a.expect(!ref->second_codepoint.has_value()); }); - etest::test("double-codepoint entity", [] { + s.add_test("double-codepoint entity", [](etest::IActions &a) { auto ref = find_named_character_reference_for("⪰̸"sv); - require(ref.has_value()); - expect(ref->name == "⪰̸"sv); - expect(ref->first_codepoint == 0x02AB0u); - expect(ref->second_codepoint == 0x00338u); + a.require(ref.has_value()); + a.expect(ref->name == "⪰̸"sv); + a.expect(ref->first_codepoint == 0x02AB0u); + a.expect(ref->second_codepoint == 0x00338u); }); - etest::test("longest prefix is chosen", [] { + s.add_test("longest prefix is chosen", [](etest::IActions &a) { auto ref = find_named_character_reference_for("<"sv); - require(ref.has_value()); - expect(ref->name == "<"sv); // And not < which also matches. + a.require(ref.has_value()); + a.expect(ref->name == "<"sv); // And not < which also matches. }); - etest::test("extra characters are ignored", [] { + s.add_test("extra characters are ignored", [](etest::IActions &a) { auto ref = find_named_character_reference_for("<<&abc;123"sv); - require(ref.has_value()); - expect(ref->name == "<"sv); + a.require(ref.has_value()); + a.expect(ref->name == "<"sv); }); - return etest::run_all_tests(); + return s.run(); } diff --git a/html2/parser_states_test.cpp b/html2/parser_states_test.cpp index 554881cf..f686f79c 100644 --- a/html2/parser_states_test.cpp +++ b/html2/parser_states_test.cpp @@ -4,11 +4,11 @@ #include "html2/parser_states.h" -#include "dom/dom.h" #include "html2/token.h" #include "html2/tokenizer.h" -#include "etest/etest.h" +#include "dom/dom.h" +#include "etest/etest2.h" #include "html/parser_actions.h" #include @@ -22,8 +22,6 @@ using namespace std::literals; -using etest::expect_eq; - using NodeVec = std::vector; namespace { @@ -55,400 +53,400 @@ ParseResult parse(std::string_view html, ParseOptions opts) { return res; } -void initial_tests() { - etest::test("Initial: whitespace before doctype", [] { +void initial_tests(etest::Suite &s) { + s.add_test("Initial: whitespace before doctype", [](etest::IActions &a) { auto res = parse(" ", {}); - expect_eq(res.document.doctype, "html"); + a.expect_eq(res.document.doctype, "html"); res = parse("\t\n\r ", {}); - expect_eq(res.document.doctype, "bad"); + a.expect_eq(res.document.doctype, "bad"); }); - etest::test("Initial: comment", [] { + s.add_test("Initial: comment", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.doctype, "html"); - expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); + a.expect_eq(res.document.doctype, "html"); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("Initial: doctype, sane", [] { + s.add_test("Initial: doctype, sane", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.doctype, "html"); - expect_eq(res.document.mode, dom::Document::Mode::NoQuirks); + a.expect_eq(res.document.doctype, "html"); + a.expect_eq(res.document.mode, dom::Document::Mode::NoQuirks); }); - etest::test("Initial: doctype, sane-ish", [] { + s.add_test("Initial: doctype, sane-ish", [](etest::IActions &a) { auto res = parse(R"()", {}); - expect_eq(res.document.mode, dom::Document::Mode::NoQuirks); + a.expect_eq(res.document.mode, dom::Document::Mode::NoQuirks); }); - etest::test("Initial: doctype, also sane-ish", [] { + s.add_test("Initial: doctype, also sane-ish", [](etest::IActions &a) { auto res = parse(R"()", {}); - expect_eq(res.document.mode, dom::Document::Mode::NoQuirks); + a.expect_eq(res.document.mode, dom::Document::Mode::NoQuirks); }); - etest::test("Initial: doctype, quirky 0", [] { + s.add_test("Initial: doctype, quirky 0", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.doctype, "is_this_the_abyss?"); - expect_eq(res.document.mode, dom::Document::Mode::Quirks); + a.expect_eq(res.document.doctype, "is_this_the_abyss?"); + a.expect_eq(res.document.mode, dom::Document::Mode::Quirks); }); - etest::test("Initial: doctype, quirky 1", [] { + s.add_test("Initial: doctype, quirky 1", [](etest::IActions &a) { auto res = parse(R"()", {}); - expect_eq(res.document.mode, dom::Document::Mode::Quirks); + a.expect_eq(res.document.mode, dom::Document::Mode::Quirks); }); - etest::test("Initial: doctype, quirky 2", [] { + s.add_test("Initial: doctype, quirky 2", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.mode, dom::Document::Mode::Quirks); + a.expect_eq(res.document.mode, dom::Document::Mode::Quirks); }); - etest::test("Initial: doctype, quirky 3", [] { + s.add_test("Initial: doctype, quirky 3", [](etest::IActions &a) { auto res = parse(R"()", {}); - expect_eq(res.document.mode, dom::Document::Mode::Quirks); + a.expect_eq(res.document.mode, dom::Document::Mode::Quirks); }); - etest::test("Initial: doctype, quirky 4", [] { + s.add_test("Initial: doctype, quirky 4", [](etest::IActions &a) { auto res = parse(R"()", {}); - expect_eq(res.document.mode, dom::Document::Mode::Quirks); + a.expect_eq(res.document.mode, dom::Document::Mode::Quirks); }); - etest::test("Initial: doctype, quirky-ish 0", [] { + s.add_test("Initial: doctype, quirky-ish 0", [](etest::IActions &a) { auto res = parse(R"()", {}); - expect_eq(res.document.mode, dom::Document::Mode::LimitedQuirks); + a.expect_eq(res.document.mode, dom::Document::Mode::LimitedQuirks); }); - etest::test("Initial: doctype, quirky-ish 1", [] { + s.add_test("Initial: doctype, quirky-ish 1", [](etest::IActions &a) { auto res = parse(R"()", {}); - expect_eq(res.document.mode, dom::Document::Mode::LimitedQuirks); + a.expect_eq(res.document.mode, dom::Document::Mode::LimitedQuirks); }); } -void before_html_tests() { - etest::test("BeforeHtml: doctype", [] { +void before_html_tests(etest::Suite &s) { + s.add_test("BeforeHtml: doctype", [](etest::IActions &a) { auto res = parse("", {.initial_insertion_mode = html2::BeforeHtml{}}); - expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("BeforeHtml: comment", [] { + s.add_test("BeforeHtml: comment", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {{"foo", "bar"}}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("BeforeHtml: html tag", [] { + s.add_test("BeforeHtml: html tag", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {{"foo", "bar"}}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("BeforeHtml: boring whitespace before html is dropped", [] { + s.add_test("BeforeHtml: boring whitespace before html is dropped", [](etest::IActions &a) { auto res = parse("\t\n\f\r ", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {{"foo", "bar"}}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("BeforeHtml: head end-tag", [] { + s.add_test("BeforeHtml: head end-tag", [](etest::IActions &a) { auto res = parse("", {.initial_insertion_mode = html2::BeforeHtml{}}); - expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("BeforeHtml: dropped end-tag", [] { + s.add_test("BeforeHtml: dropped end-tag", [](etest::IActions &a) { auto res = parse("", {.initial_insertion_mode = html2::BeforeHtml{}}); - expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); }); } -void before_head_tests() { - etest::test("BeforeHead: comment", [] { +void before_head_tests(etest::Suite &s) { + s.add_test("BeforeHead: comment", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {{"foo", "bar"}}}, dom::Element{"body"}}}); }); - etest::test("BeforeHead: doctype", [] { + s.add_test("BeforeHead: doctype", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {{"foo", "bar"}}}, dom::Element{"body"}}}); }); - etest::test("BeforeHead: html tag", [] { + s.add_test("BeforeHead: html tag", [](etest::IActions &a) { auto res = parse("", {}); auto const &head = std::get(res.document.html().children.at(0)); - expect_eq(res.document.html().attributes, dom::AttrMap{{"foo", "bar"}, {"hello", "world"}}); - expect_eq(head, dom::Element{"head"}); + a.expect_eq(res.document.html().attributes, dom::AttrMap{{"foo", "bar"}, {"hello", "world"}}); + a.expect_eq(head, dom::Element{"head"}); }); - etest::test("BeforeHead: head tag", [] { + s.add_test("BeforeHead: head tag", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {{"foo", "bar"}}}, dom::Element{"body"}}}); }); - etest::test("BeforeHead: end-tag fallthrough", [] { + s.add_test("BeforeHead: end-tag fallthrough", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("BeforeHead: ignored end-tag", [] { + s.add_test("BeforeHead: ignored end-tag", [](etest::IActions &a) { auto res = parse("

", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {{"foo", "bar"}}}, dom::Element{"body"}}}); }); - etest::test("BeforeHtml: boring whitespace before head is dropped", [] { + s.add_test("BeforeHtml: boring whitespace before head is dropped", [](etest::IActions &a) { auto res = parse("\t\n\f\r ", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {{"foo", "bar"}}}, dom::Element{"body"}}}); }); } -void in_head_tests() { - etest::test("InHead: comment", [] { +void in_head_tests(etest::Suite &s) { + s.add_test("InHead: comment", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {}, {dom::Element{"meta"}}}, dom::Element{"body"}}}); }); - etest::test("InHead: doctype", [] { + s.add_test("InHead: doctype", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("InHead: end tag parse error", [] { + s.add_test("InHead: end tag parse error", [](etest::IActions &a) { auto res = parse("

", {}); - expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head"}, dom::Element{"body"}}}); }); - etest::test("InHead: html attributes are reparented", [] { + s.add_test("InHead: html attributes are reparented", [](etest::IActions &a) { auto res = parse("", {}); auto const &head = std::get(res.document.html().children.at(0)); - expect_eq(res.document.html().attributes, dom::AttrMap{{"foo", "bar"}, {"hello", "world"}}); - expect_eq(head, dom::Element{"head"}); + a.expect_eq(res.document.html().attributes, dom::AttrMap{{"foo", "bar"}, {"hello", "world"}}); + a.expect_eq(head, dom::Element{"head"}); }); - etest::test("InHead: base, basefont, bgsound, link", [] { + s.add_test("InHead: base, basefont, bgsound, link", [](etest::IActions &a) { auto res = parse(" ", {}); auto head_children = NodeVec{dom::Element{"base"}, dom::Element{"basefont"}, dom::Element{"bgsound"}, dom::Element{"link"}}; auto head = dom::Element{"head", {}, std::move(head_children)}; - expect_eq(res.document.html(), dom::Element{"html", {}, {std::move(head), dom::Element{"body"}}}); + a.expect_eq(res.document.html(), dom::Element{"html", {}, {std::move(head), dom::Element{"body"}}}); }); - etest::test("InHead: meta", [] { + s.add_test("InHead: meta", [](etest::IActions &a) { auto res = parse("", {}); - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {}, {dom::Element{"meta"}}}, dom::Element{"body"}}}); }); - etest::test("InHead: title", [] { + s.add_test("InHead: title", [](etest::IActions &a) { auto res = parse("<body>&", {}); auto title = dom::Element{"title", {}, {dom::Text{"&"}}}; - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {}, {std::move(title)}}, dom::Element{"body"}}}); }); - etest::test("InHead: style", [] { + s.add_test("InHead: style", [](etest::IActions &a) { auto res = parse("", {}); auto style = dom::Element{"style", {}, {dom::Text{"p { color: green; }"}}}; - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {}, {std::move(style)}}, dom::Element{"body"}}}); }); - etest::test("InHead: style, abrupt eof", [] { + s.add_test("InHead: style, abrupt eof", [](etest::IActions &a) { auto res = parse("", {}); auto noscript = dom::Element{"noscript", {}, {dom::Element{"style", {}, {dom::Text{"p { color: green; }"}}}}}; - expect_eq(res.document.html(), + a.expect_eq(res.document.html(), dom::Element{"html", {}, {dom::Element{"head", {}, {std::move(noscript)}}, dom::Element{"body"}}}); }); - etest::test("InHeadNoScript: br", [] { + s.add_test("InHeadNoScript: br", [](etest::IActions &a) { auto res = parse("