epubgrep/tests/test_search_epub.cpp
2021-06-07 00:44:42 +02:00

192 lines
7.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "fs-compat.hpp"
#include "options.hpp"
#include "search.hpp"
#include <catch.hpp>
#include <clocale>
#include <exception>
#include <string>
#include <vector>
SCENARIO("Searching EPUB files works")
{
GIVEN("Our test EPUB2 file")
{
fs::path epubfile{"test.epub2"};
std::setlocale(LC_CTYPE,
""); // Needed for utf-8 support in libarchive.
bool exception{false};
REQUIRE(fs::exists(epubfile));
SECTION("search() doesn't fail and returns the right lines")
{
std::vector<epubgrep::search::match> matches;
epubgrep::search::settings opts;
WHEN(R"(We search for test-\w+ using perl regular expressions)")
{
try
{
opts.regex = epubgrep::options::regex_kind::perl;
matches = epubgrep::search::search(epubfile, R"(test-\w+)",
opts);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the match correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
REQUIRE(matches.at(0).text == "test-file");
REQUIRE(matches.at(1).text == "test-suite");
REQUIRE(matches.at(1).headline == "Test for epubgrep");
}
}
WHEN("We search for href with raw = 1 and context = 1.")
{
try
{
opts.raw = true;
opts.context = 1;
matches = epubgrep::search::search(epubfile, "href", opts);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the match correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
REQUIRE(matches.at(0).context.first == "<a ");
REQUIRE(matches.at(0).context.second
== R"(="https://schlomp.space/tastytea/)"
R"(epubgrep">epubgrep</a>. Just)");
REQUIRE(matches.at(1).filepath_inside == "metadata.opf");
REQUIRE(matches.at(1).context.first == "<item ");
REQUIRE(matches.at(1).context.second
== R"(="start.xhtml" id="start")");
REQUIRE(matches.at(2).filepath_inside == "metadata.opf");
REQUIRE(matches.at(2).context.first == "<item ");
REQUIRE(matches.at(2).context.second
== R"(="toc.ncx" id="ncx")");
}
}
}
}
// TODO: Figure out how to do this better.
GIVEN("Our test EPUB3 file")
{
fs::path epubfile{"test.epub3"};
std::setlocale(LC_CTYPE, ""); // Needed for utf-8 support in libarchive.
bool exception{false};
REQUIRE(fs::exists(epubfile));
SECTION("search() doesn't fail and returns the right lines")
{
std::vector<epubgrep::search::match> matches;
epubgrep::search::settings opts;
WHEN(R"(We search for test-\w+ using perl regular expressions)")
{
try
{
opts.regex = epubgrep::options::regex_kind::perl;
matches = epubgrep::search::search(epubfile, R"(test-\w+)",
opts);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the match correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
REQUIRE(matches.at(0).text == "test-file");
REQUIRE(matches.at(1).text == "test-suite");
REQUIRE(matches.at(1).headline == "Test for epubgrep");
}
}
WHEN("We search for href with raw = 1 and context = 1.")
{
try
{
opts.raw = true;
opts.context = 1;
matches = epubgrep::search::search(epubfile, "href", opts);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the match correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
REQUIRE(matches.at(0).context.first == "<a ");
REQUIRE(matches.at(0).context.second
== R"(="https://schlomp.space/tastytea/)"
R"(epubgrep">epubgrep</a>. Just)");
REQUIRE(matches.at(1).filepath_inside == "nav.xhtml");
REQUIRE(matches.at(1).context.first == "<li><a ");
REQUIRE(matches.at(1).context.second
== std::string(R"(="start.xhtml">Start</a></li>)")
+ "\n </ol>");
REQUIRE(matches.at(2).filepath_inside == "metadata.opf");
REQUIRE(matches.at(2).context.first == "<item ");
REQUIRE(matches.at(2).context.second
== R"(="start.xhtml" id="start")");
REQUIRE(matches.at(3).filepath_inside == "metadata.opf");
REQUIRE(matches.at(3).context.first == R"(id="nav" )");
REQUIRE(matches.at(3).context.second
== R"(="nav.xhtml" )"
R"(media-type="application/xhtml+xml")");
}
}
WHEN("We search for for a phrase at the beginning of the file "
"and specify a very high context")
{
try
{
opts.context = 69069;
matches = epubgrep::search::search(epubfile, "Test for",
opts);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the match correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
REQUIRE(matches.at(0).text == "Test for");
REQUIRE(matches.at(0).headline.empty());
REQUIRE(matches.at(0).context.first.empty());
REQUIRE(*matches.at(0).context.second.rbegin() == '.');
}
}
}
}
}