diff --git a/tests/test_search_helpers.cpp b/tests/test_search_helpers.cpp new file mode 100644 index 0000000..3ecba7e --- /dev/null +++ b/tests/test_search_helpers.cpp @@ -0,0 +1,158 @@ +#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"); + } + } + } + + 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"); + } + } + } + } +}