#include #include #include #include /** * @brief Test cases for the DuvaHTML namespace. */ SCENARIO("Test HTML", "[HTML]") { auto test1 = []() { docpp::HTML::HTMLDocument doc{}; docpp::HTML::HTMLSection html(docpp::HTML::SECTION_HTML, {}); docpp::HTML::HTMLSection head(docpp::HTML::SECTION_HEAD, {}); docpp::HTML::HTMLSection body(docpp::HTML::SECTION_BODY, {}); docpp::HTML::HTMLSection div(docpp::HTML::SECTION_DIV, {}); docpp::HTML::HTMLSection footer(docpp::HTML::SECTION_FOOTER, {}); head.push_back(docpp::HTML::HTMLElement("title", {}, "Test Title")); body.push_back(docpp::HTML::HTMLElement("h1", {}, "Test Header")); body.push_back(docpp::HTML::HTMLElement("p", {}, "Test Paragraph")); docpp::HTML::HTMLElementProperties prop{}; prop.push_back(docpp::HTML::HTMLProperty(std::pair("id", "test_id"))); body.push_back(docpp::HTML::HTMLElement("p", prop, "Test Paragraph With ID")); div.push_back(docpp::HTML::HTMLElement("p", {}, "Test Paragraph In Div")); body.push_back(div); prop.push_back(docpp::HTML::HTMLProperty(std::pair("class", "class1 class2 class3"))); body.push_back(docpp::HTML::HTMLElement("p", prop, "Test Paragraph With ID And Class")); html.push_back(head); html.push_back(body); html.push_back(footer); doc.set(html); const std::string expected_html{"Test Title

Test Header

Test Paragraph

Test Paragraph With ID

Test Paragraph In Div

Test Paragraph With ID And Class

"}; REQUIRE(doc.get() == expected_html); REQUIRE(doc.get(docpp::HTML::FORMATTING_PRETTY) == "\n\n\nTest Title\n\n\n

Test Header

\n

Test Paragraph

\n

Test Paragraph With ID

\n
\n

Test Paragraph In Div

\n
\n

Test Paragraph With ID And Class

\n\n
\n
\n"); }; auto test2 = []() { docpp::HTML::HTMLSection section(docpp::HTML::SECTION_HTML, {}); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); section.erase(docpp::HTML::HTMLElement("p", {}, "Test 2")); REQUIRE(section.get() == "

Test 1

Test 3

"); REQUIRE(section.get(docpp::HTML::FORMATTING_PRETTY) == "\n

Test 1

\n

Test 3

\n"); }; auto test3 = []() { docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); int pos = section.find(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.insert(pos, docpp::HTML::HTMLElement("p", {}, "Test 2.5")); REQUIRE(section.get() == "

Test 1

Test 2.5

Test 3

"); REQUIRE(section.get(docpp::HTML::FORMATTING_PRETTY) == "\n

Test 1

\n

Test 2.5

\n

Test 3

\n"); }; auto test4 = []() { docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); int pos = section.find(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.erase(pos); REQUIRE(section.get() == "

Test 1

Test 3

"); REQUIRE(section.get(docpp::HTML::FORMATTING_PRETTY) == "\n

Test 1

\n

Test 3

\n"); }; auto test5 = []() { docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); docpp::HTML::HTMLSection subsection(docpp::HTML::SECTION_DIV, {}); subsection.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); docpp::HTML::HTMLSection subsection2(docpp::HTML::SECTION_DIV, {}); subsection2.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); subsection.push_back(subsection2); section.push_back(subsection); docpp::HTML::HTMLDocument doc{}; doc.set(section); REQUIRE(doc.get() == "

Test 1

Test 2

"); REQUIRE(doc.get(docpp::HTML::FORMATTING_PRETTY) == "\n\n
\n

Test 1

\n
\n

Test 2

\n
\n
\n"); }; auto test6 = []() { docpp::CSS::CSSStylesheet css{}; docpp::CSS::CSSElement element{"p", {{"color", "red"}, {"font-size", "16px"}, {"font-family", "Arial"}}}; css.push_back(element); REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); REQUIRE(css.get(docpp::CSS::FORMATTING_PRETTY) == "p {\ncolor: red;\nfont-size: 16px;\nfont-family: Arial;\n}\n"); }; auto test7 = []() { docpp::CSS::CSSStylesheet css = docpp::CSS::CSSStylesheet{}; docpp::CSS::CSSElement element = docpp::CSS::CSSElement{"p", {{"color", "red"}, {"font-size", "16px"}, {"font-family", "Arial"}}}; docpp::CSS::CSSElement element2{"div", {{"color", "blue"}, {"font-size", "12px"}, {"font-family", "Arial"}}}; css.push_back(element); css.push_front(element2); REQUIRE(css.get() == "div {color: blue;font-size: 12px;font-family: Arial;}p {color: red;font-size: 16px;font-family: Arial;}"); }; auto test8 = []() { docpp::CSS::CSSStylesheet css = docpp::CSS::CSSStylesheet{}; docpp::CSS::CSSElement element = docpp::CSS::CSSElement{"p", {{"color", "red"}, {"font-size", "16px"}, {"font-family", "Arial"}}}; docpp::CSS::CSSElement element2{"div", {{"color", "blue"}, {"font-size", "12px"}, {"font-family", "Arial"}}}; css.push_back(element); css.push_front(element2); css.erase(css.find(element2)); REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); }; auto test9 = []() { docpp::CSS::CSSStylesheet css = docpp::CSS::CSSStylesheet{}; docpp::CSS::CSSElement element = docpp::CSS::CSSElement{"p", {{"color", "red"}, {"font-size", "16px"}, {"font-family", "Arial"}}}; docpp::CSS::CSSElement element2{"div", {{"color", "blue"}, {"font-size", "12px"}, {"font-family", "Arial"}}}; css.push_back(element); css.push_front(element2); css.erase(css.find(element2)); css.push_front(element2); css.swap(css.find(element), css.find(element2)); REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}div {color: blue;font-size: 12px;font-family: Arial;}"); element.push_front(docpp::CSS::CSSProperty("font-weight", "bold")); REQUIRE(element.get() == "p {font-weight: bold;color: red;font-size: 16px;font-family: Arial;}"); }; auto test10 = []() { docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); section.swap(section.find(docpp::HTML::HTMLElement("p", {}, "Test 2")), section.find(docpp::HTML::HTMLElement("p", {}, "Test 3"))); REQUIRE(section.get() == "

Test 1

Test 3

Test 2

"); }; auto test11 = []() { docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); section.swap(docpp::HTML::HTMLElement("p", {}, "Test 2"), docpp::HTML::HTMLElement("p", {}, "Test 3")); REQUIRE(section.get() == "

Test 1

Test 3

Test 2

"); }; auto test12 = []() { docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); section.push_front(docpp::HTML::HTMLElement("p", {}, "Test 0")); REQUIRE(section.get() == "

Test 0

Test 1

Test 2

Test 3

"); }; auto test13 = []() { docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 4")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 5")); section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 6")); const int pos{section.find("

Test 2

")}; REQUIRE(pos != docpp::HTML::HTMLSection::npos); const int pos2{section.find("

Test 6

")}; section.erase(pos); section.erase(pos2); REQUIRE(section.get() == "

Test 1

Test 3

Test 4

Test 5

"); }; auto test14 = []() { docpp::CSS::CSSElement element{"p", {{"color", "red"}, {"font-size", "16px"}, {"font-family", "Arial"}}}; const int red = element.find("color"); REQUIRE(red != docpp::CSS::CSSElement::npos); const int blue = element.find("blue"); REQUIRE(blue == docpp::CSS::CSSElement::npos); element.erase(red); REQUIRE(element.get() == "p {font-size: 16px;font-family: Arial;}"); element.insert(red, docpp::CSS::CSSProperty("color", "red")); REQUIRE(element.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); }; auto test15 = []() { docpp::HTML::HTMLElementProperties prop{}; prop.push_back(docpp::HTML::HTMLProperty(std::pair("id", "test_id"))); prop.push_back(docpp::HTML::HTMLProperty(std::pair("class", "class1 class2 class3"))); REQUIRE(docpp::HTML::HTMLElement("p", prop, {}).get() == "

"); const int pos = prop.find("class"); REQUIRE(pos != docpp::HTML::HTMLElementProperties::npos); const int pos2 = prop.find("class2"); REQUIRE(prop.at(pos2).getKey() == "class"); REQUIRE(prop.at(pos2).getValue() == "class1 class2 class3"); REQUIRE(pos2 != docpp::HTML::HTMLElementProperties::npos); const int pos3 = prop.find("class4"); REQUIRE(pos3 == docpp::HTML::HTMLElementProperties::npos); prop.erase(pos); REQUIRE(docpp::HTML::HTMLElement("p", prop, {}).get() == "

"); }; std::vector tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15}; for (const auto& test : tests) { test(); } }