#include "fs-compat.hpp" #include "search.hpp" #include #include #include #include SCENARIO("Searching helpers work as intended") { GIVEN("Our test zip file") { fs::path zipfile{"test.zip"}; std::setlocale(LC_CTYPE, ""); // Needed for utf-8 support in libarchive. bool exception{false}; REQUIRE(fs::exists(zipfile)); SECTION("cleanup_texts() does what it should do") { std::string text; WHEN("Text is a single word") { text = "Moss"; try { epubgrep::search::cleanup_text(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("The text is unchanged") { REQUIRE_FALSE(exception); REQUIRE(text == "Moss"); } } WHEN("Text has 2 \\r next to each other in it.") { text = "šŸ’–\r\ršŸ¦"; try { epubgrep::search::cleanup_text(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("The \\r are removed unchanged") { REQUIRE_FALSE(exception); REQUIRE(text == "šŸ’–šŸ¦"); } } WHEN("Text has 6 \\n after another in it") { text = "Moss\n\n\n\n\n\nis good."; try { epubgrep::search::cleanup_text(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("The 6 \\n are condensed to one space") { REQUIRE_FALSE(exception); REQUIRE(text == "Moss is good."); } } } SECTION("headline() does what it should do") { std::string text; WHEN("We have a text with a h3 headline") { text = "ā€¦

Soup

ā€¦"; try { epubgrep::search::cleanup_text(text); text = epubgrep::search::headline(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("The headline is correctly extracted") { REQUIRE_FALSE(exception); REQUIRE(text == "Soup"); } } WHEN("There is a in the h2 headline") { text = "ā€¦

The long " "road to nowhere

ā€¦"; try { epubgrep::search::cleanup_text(text); text = epubgrep::search::headline(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("The headline is correctly extracted") { REQUIRE_FALSE(exception); REQUIRE(text == "The long road to nowhere"); } } WHEN("There are tags that start with h but are not headlines") { text = "
The long
road to nowhere
"; try { epubgrep::search::cleanup_text(text); text = epubgrep::search::headline(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("No headline is extracted") { REQUIRE_FALSE(exception); REQUIRE(text.empty()); } } } SECTION("page() does what it should do") { std::string text; WHEN("We have text with epub:type pagebreak number 69") { text = R"(ā€¦ ā€¦)"; try { epubgrep::search::cleanup_text(text); text = epubgrep::search::page(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("The page number is correctly extracted") { REQUIRE_FALSE(exception); REQUIRE(text == "69"); } } WHEN("We have text with doc-pagebreak number 69") { text = R"(ā€¦ ā€¦)"; try { epubgrep::search::cleanup_text(text); text = epubgrep::search::page(text); } catch (const std::exception &) { exception = true; } THEN("No exception is thrown") AND_THEN("The page number is correctly extracted") { REQUIRE_FALSE(exception); REQUIRE(text == "69"); } } } } }