Make use of the template functions in the test cases.

This commit is contained in:
Jacob 2024-05-14 10:04:32 +02:00
parent f30f1431fd
commit ceda875213
2 changed files with 38 additions and 38 deletions

View file

@ -2,7 +2,7 @@
cmake_minimum_required(VERSION 3.1...3.29)
project(docpp VERSION 0.0.1
DESCRIPTION "Simple C++ library for generating XML, HTML and CSS."
DESCRIPTION "C++ library for generating XML, HTML and CSS."
HOMEPAGE_URL "https://speedie.site/docpp"
LANGUAGES CXX
)

View file

@ -40,8 +40,8 @@ SCENARIO("Test HTML", "[HTML]") {
const std::string expected_html{"<!DOCTYPE html><html><head><title>Test Title</title></head><body><h1>Test Header</h1><p>Test Paragraph</p><p id=\"test_id\">Test Paragraph With ID</p><div><p>Test Paragraph In Div</p></div><p id=\"test_id\" class=\"class1 class2 class3\">Test Paragraph With ID And Class</p></body><footer></footer></html>"};
REQUIRE(doc.get() == expected_html);
REQUIRE(doc.get(docpp::HTML::FORMATTING_NEWLINE) == "<!DOCTYPE html>\n<html>\n<head>\n<title>Test Title</title>\n</head>\n<body>\n<h1>Test Header</h1>\n<p>Test Paragraph</p>\n<p id=\"test_id\">Test Paragraph With ID</p>\n<div>\n<p>Test Paragraph In Div</p>\n</div>\n<p id=\"test_id\" class=\"class1 class2 class3\">Test Paragraph With ID And Class</p>\n</body>\n<footer>\n</footer>\n</html>");
REQUIRE(doc.get<std::string>() == expected_html);
REQUIRE(doc.get<std::string>(docpp::HTML::FORMATTING_NEWLINE) == "<!DOCTYPE html>\n<html>\n<head>\n<title>Test Title</title>\n</head>\n<body>\n<h1>Test Header</h1>\n<p>Test Paragraph</p>\n<p id=\"test_id\">Test Paragraph With ID</p>\n<div>\n<p>Test Paragraph In Div</p>\n</div>\n<p id=\"test_id\" class=\"class1 class2 class3\">Test Paragraph With ID And Class</p>\n</body>\n<footer>\n</footer>\n</html>");
};
const auto test2 = []() {
@ -53,8 +53,8 @@ SCENARIO("Test HTML", "[HTML]") {
section.erase(docpp::HTML::Element("p", {}, "Test 2"));
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 3</p></html>");
REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "<html>\n<p>Test 1</p>\n<p>Test 3</p>\n</html>");
REQUIRE(section.get<std::string>() == "<html><p>Test 1</p><p>Test 3</p></html>");
REQUIRE(section.get<std::string>(docpp::HTML::FORMATTING_NEWLINE) == "<html>\n<p>Test 1</p>\n<p>Test 3</p>\n</html>");
};
const auto test3 = []() {
@ -67,8 +67,8 @@ SCENARIO("Test HTML", "[HTML]") {
std::size_t pos = section.find(docpp::HTML::Element("p", {}, "Test 2"));
section.insert(pos, docpp::HTML::Element("p", {}, "Test 2.5"));
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 2.5</p><p>Test 3</p></html>");
REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "<html>\n<p>Test 1</p>\n<p>Test 2.5</p>\n<p>Test 3</p>\n</html>");
REQUIRE(section.get<std::string>() == "<html><p>Test 1</p><p>Test 2.5</p><p>Test 3</p></html>");
REQUIRE(section.get<std::string>(docpp::HTML::FORMATTING_NEWLINE) == "<html>\n<p>Test 1</p>\n<p>Test 2.5</p>\n<p>Test 3</p>\n</html>");
};
const auto test4 = []() {
@ -82,8 +82,8 @@ SCENARIO("Test HTML", "[HTML]") {
section.erase(pos);
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 3</p></html>");
REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "<html>\n<p>Test 1</p>\n<p>Test 3</p>\n</html>");
REQUIRE(section.get<std::string>() == "<html><p>Test 1</p><p>Test 3</p></html>");
REQUIRE(section.get<std::string>(docpp::HTML::FORMATTING_NEWLINE) == "<html>\n<p>Test 1</p>\n<p>Test 3</p>\n</html>");
};
const auto test5 = []() {
@ -102,8 +102,8 @@ SCENARIO("Test HTML", "[HTML]") {
docpp::HTML::Document doc{};
doc.set(section);
REQUIRE(doc.get() == "<!DOCTYPE html><html><div><p>Test 1</p><div><p>Test 2</p></div></div></html>");
REQUIRE(doc.get(docpp::HTML::FORMATTING_NEWLINE) == "<!DOCTYPE html>\n<html>\n<div>\n<p>Test 1</p>\n<div>\n<p>Test 2</p>\n</div>\n</div>\n</html>");
REQUIRE(doc.get<std::string>() == "<!DOCTYPE html><html><div><p>Test 1</p><div><p>Test 2</p></div></div></html>");
REQUIRE(doc.get<std::string>(docpp::HTML::FORMATTING_NEWLINE) == "<!DOCTYPE html>\n<html>\n<div>\n<p>Test 1</p>\n<div>\n<p>Test 2</p>\n</div>\n</div>\n</html>");
};
const auto test6 = []() {
@ -112,8 +112,8 @@ SCENARIO("Test HTML", "[HTML]") {
css.push_back(element);
REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}");
REQUIRE(css.get(docpp::CSS::FORMATTING_NEWLINE) == "p {\ncolor: red;\nfont-size: 16px;\nfont-family: Arial;\n}\n");
REQUIRE(css.get<std::string>() == "p {color: red;font-size: 16px;font-family: Arial;}");
REQUIRE(css.get<std::string>(docpp::CSS::FORMATTING_NEWLINE) == "p {\ncolor: red;\nfont-size: 16px;\nfont-family: Arial;\n}\n");
};
const auto test7 = []() {
@ -124,7 +124,7 @@ SCENARIO("Test HTML", "[HTML]") {
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;}");
REQUIRE(css.get<std::string>() == "div {color: blue;font-size: 12px;font-family: Arial;}p {color: red;font-size: 16px;font-family: Arial;}");
};
const auto test8 = []() {
@ -137,7 +137,7 @@ SCENARIO("Test HTML", "[HTML]") {
css.erase(css.find(element2));
REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}");
REQUIRE(css.get<std::string>() == "p {color: red;font-size: 16px;font-family: Arial;}");
};
const auto test9 = []() {
@ -153,11 +153,11 @@ SCENARIO("Test HTML", "[HTML]") {
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;}");
REQUIRE(css.get<std::string>() == "p {color: red;font-size: 16px;font-family: Arial;}div {color: blue;font-size: 12px;font-family: Arial;}");
element.push_front(docpp::CSS::Property("font-weight", "bold"));
REQUIRE(element.get() == "p {font-weight: bold;color: red;font-size: 16px;font-family: Arial;}");
REQUIRE(element.get<std::string>() == "p {font-weight: bold;color: red;font-size: 16px;font-family: Arial;}");
};
const auto test10 = []() {
@ -169,7 +169,7 @@ SCENARIO("Test HTML", "[HTML]") {
section.swap(section.find(docpp::HTML::Element("p", {}, "Test 2")), section.find(docpp::HTML::Element("p", {}, "Test 3")));
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 3</p><p>Test 2</p></html>");
REQUIRE(section.get<std::string>() == "<html><p>Test 1</p><p>Test 3</p><p>Test 2</p></html>");
};
const auto test11 = []() {
@ -181,7 +181,7 @@ SCENARIO("Test HTML", "[HTML]") {
section.swap(docpp::HTML::Element("p", {}, "Test 2"), docpp::HTML::Element("p", {}, "Test 3"));
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 3</p><p>Test 2</p></html>");
REQUIRE(section.get<std::string>() == "<html><p>Test 1</p><p>Test 3</p><p>Test 2</p></html>");
};
const auto test12 = []() {
@ -193,7 +193,7 @@ SCENARIO("Test HTML", "[HTML]") {
section.push_front(docpp::HTML::Element("p", {}, "Test 0"));
REQUIRE(section.get() == "<html><p>Test 0</p><p>Test 1</p><p>Test 2</p><p>Test 3</p></html>");
REQUIRE(section.get<std::string>() == "<html><p>Test 0</p><p>Test 1</p><p>Test 2</p><p>Test 3</p></html>");
};
const auto test13 = []() {
@ -215,7 +215,7 @@ SCENARIO("Test HTML", "[HTML]") {
section.erase(pos);
section.erase(pos2);
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 3</p><p>Test 4</p><p>Test 5</p></html>");
REQUIRE(section.get<std::string>() == "<html><p>Test 1</p><p>Test 3</p><p>Test 4</p><p>Test 5</p></html>");
};
const auto test14 = []() {
@ -231,11 +231,11 @@ SCENARIO("Test HTML", "[HTML]") {
element.erase(red);
REQUIRE(element.get() == "p {font-size: 16px;font-family: Arial;}");
REQUIRE(element.get<std::string>() == "p {font-size: 16px;font-family: Arial;}");
element.insert(red, docpp::CSS::Property("color", "red"));
REQUIRE(element.get() == "p {color: red;font-size: 16px;font-family: Arial;}");
REQUIRE(element.get<std::string>() == "p {color: red;font-size: 16px;font-family: Arial;}");
};
const auto test15 = []() {
@ -244,7 +244,7 @@ SCENARIO("Test HTML", "[HTML]") {
prop.push_back(docpp::HTML::Property(std::pair<std::string, std::string>("id", "test_id")));
prop.push_back(docpp::HTML::Property(std::pair<std::string, std::string>("class", "class1 class2 class3")));
REQUIRE(docpp::HTML::Element("p", prop, {}).get() == "<p id=\"test_id\" class=\"class1 class2 class3\"></p>");
REQUIRE(docpp::HTML::Element("p", prop, {}).get<std::string>() == "<p id=\"test_id\" class=\"class1 class2 class3\"></p>");
const int pos = prop.find("class");
@ -252,8 +252,8 @@ SCENARIO("Test HTML", "[HTML]") {
const int pos2 = prop.find("class2");
REQUIRE(prop.at(pos2).get_key() == "class");
REQUIRE(prop.at(pos2).get_value() == "class1 class2 class3");
REQUIRE(prop.at(pos2).get_key<std::string>() == "class");
REQUIRE(prop.at(pos2).get_value<std::string>() == "class1 class2 class3");
REQUIRE(pos2 != docpp::HTML::Properties::npos);
@ -263,7 +263,7 @@ SCENARIO("Test HTML", "[HTML]") {
prop.erase(pos);
REQUIRE(docpp::HTML::Element("p", prop, {}).get() == "<p id=\"test_id\"></p>");
REQUIRE(docpp::HTML::Element("p", prop, {}).get<std::string>() == "<p id=\"test_id\"></p>");
};
const auto test16 = []() {
@ -281,7 +281,7 @@ SCENARIO("Test HTML", "[HTML]") {
doc.get_section() += docpp::HTML::Element("p", {}, "Test 7");
REQUIRE(doc.get() == "<!DOCTYPE html><html><p>Test 4</p><p>Test 5</p><p>Test 6</p><p>Test 7</p></html>");
REQUIRE(doc.get<std::string>() == "<!DOCTYPE html><html><p>Test 4</p><p>Test 5</p><p>Test 6</p><p>Test 7</p></html>");
};
const auto test17 = []() {
@ -293,7 +293,7 @@ SCENARIO("Test HTML", "[HTML]") {
docpp::HTML::Document doc{section};
REQUIRE(doc.get(docpp::HTML::FORMATTING_PRETTY) == "<!DOCTYPE html>\n<html>\n\t<p>Test 1</p>\n\t<p>Test 2</p>\n\t<p>Test 3</p>\n</html>");
REQUIRE(doc.get<std::string>(docpp::HTML::FORMATTING_PRETTY) == "<!DOCTYPE html>\n<html>\n\t<p>Test 1</p>\n\t<p>Test 2</p>\n\t<p>Test 3</p>\n</html>");
};
const auto test18 = []() {
@ -303,7 +303,7 @@ SCENARIO("Test HTML", "[HTML]") {
section.push_back(docpp::HTML::Element("p", {}, "Test 2"));
section.push_back(docpp::HTML::Element("p", {}, "Test 3"));
REQUIRE(section.get() == "<p>Test 1</p><p>Test 2</p><p>Test 3</p>");
REQUIRE(section.get<std::string>() == "<p>Test 1</p><p>Test 2</p><p>Test 3</p>");
};
const auto test19 = []() {
@ -313,8 +313,8 @@ SCENARIO("Test HTML", "[HTML]") {
section.push_back(docpp::HTML::Element("p", {}, "Test 2"));
section.push_back(docpp::HTML::Element("p", {}, "Test 3"));
REQUIRE(section.front().get() == "<p>Test 1</p>");
REQUIRE(section.back().get() == "<p>Test 3</p>");
REQUIRE(section.front().get<std::string>() == "<p>Test 1</p>");
REQUIRE(section.back().get<std::string>() == "<p>Test 3</p>");
};
const auto test20 = []() {
@ -325,14 +325,14 @@ SCENARIO("Test HTML", "[HTML]") {
prop.push_back(docpp::HTML::Property(std::pair<std::string, std::string>("style", "color: red; font-size: 16px; font-family: Arial;")));
for (const docpp::HTML::Property& p : prop) {
REQUIRE(p.get_key() == "id");
REQUIRE(p.get_value() == "test_id");
REQUIRE(p.get_key<std::string>() == "id");
REQUIRE(p.get_value<std::string>() == "test_id");
break;
}
for (docpp::HTML::Properties::iterator it = ++prop.begin(); it != prop.end(); ++it) {
REQUIRE(it->get_key() == "class");
REQUIRE(it->get_value() == "class1 class2 class3");
REQUIRE(it->get_key<std::string>() == "class");
REQUIRE(it->get_value<std::string>() == "class1 class2 class3");
break;
}
};
@ -345,13 +345,13 @@ SCENARIO("Test HTML", "[HTML]") {
sect.push_back(docpp::HTML::Element("p", {}, "Test 3"));
for (const docpp::HTML::Element& elem : sect) {
REQUIRE(elem.get() == "<p>Test 1</p>");
REQUIRE(elem.get<std::string>() == "<p>Test 1</p>");
break;
}
for (docpp::HTML::Section::iterator it = ++sect.begin(); it != sect.end(); ++it) {
docpp::HTML::Element elem = *it;
REQUIRE(elem.get() == "<p>Test 2</p>");
REQUIRE(elem.get<std::string>() == "<p>Test 2</p>");
break;
}
};