Add test for search helpers.

- search::cleanup_text()
- search::headline()
- search::page
This commit is contained in:
tastytea 2021-05-24 21:38:44 +02:00
parent 36f27ad744
commit 72e2482028
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,158 @@
#include "fs-compat.hpp"
#include "search.hpp"
#include <catch.hpp>
#include <clocale>
#include <exception>
#include <string>
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 = "… <h3>Soup</h3> …";
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"(… <span epub:type="pagebreak" … title="69"/> …)";
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"(… <span role="doc-pagebreak" … aria-label="69"/> …)";
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");
}
}
}
}
}