64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "html/html.h"
|
|
|
|
TEST_CASE("html::parse returns elements from valid HTML", "[html]") {
|
|
auto elements =
|
|
html::parse("<html><body><h1>Title</h1><p>Body</p></body></html>");
|
|
|
|
REQUIRE(elements.size() >= 2);
|
|
|
|
bool found_h1 = false;
|
|
bool found_p = false;
|
|
for (const auto &el : elements) {
|
|
if (el.tag == "h1" && el.text == "Title")
|
|
found_h1 = true;
|
|
if (el.tag == "p" && el.text == "Body")
|
|
found_p = true;
|
|
}
|
|
CHECK(found_h1);
|
|
CHECK(found_p);
|
|
}
|
|
|
|
TEST_CASE("html::parse returns empty for empty input", "[html]") {
|
|
auto elements = html::parse("");
|
|
// Empty or minimal — parser may produce an empty body
|
|
REQUIRE(elements.empty());
|
|
}
|
|
|
|
TEST_CASE("html::parse handles nested elements", "[html]") {
|
|
auto elements = html::parse("<div><span>Nested</span></div>");
|
|
|
|
// Parent nodes (body, div) also get text "Nested" via node_text.
|
|
// Just verify that the span element is present among the results.
|
|
bool found_span = false;
|
|
for (const auto &el : elements) {
|
|
if (el.tag == "span" && el.text == "Nested") {
|
|
found_span = true;
|
|
}
|
|
}
|
|
CHECK(found_span);
|
|
}
|
|
|
|
TEST_CASE("html::select finds elements by CSS selector", "[html][select]") {
|
|
auto matches = html::select("<ul><li>A</li><li>B</li><li>C</li></ul>", "li");
|
|
|
|
REQUIRE(matches.size() == 3);
|
|
CHECK(matches[0] == "A");
|
|
CHECK(matches[1] == "B");
|
|
CHECK(matches[2] == "C");
|
|
}
|
|
|
|
TEST_CASE("html::select returns empty for no matches", "[html][select]") {
|
|
auto matches = html::select("<p>Hello</p>", "h1");
|
|
REQUIRE(matches.empty());
|
|
}
|
|
|
|
TEST_CASE("html::select works with class selector", "[html][select]") {
|
|
auto matches = html::select(
|
|
R"(<div><span class="a">X</span><span class="b">Y</span></div>)", ".a");
|
|
|
|
REQUIRE(matches.size() == 1);
|
|
CHECK(matches[0] == "X");
|
|
}
|