2021-05-29 23:35:39 +02:00
|
|
|
|
#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);
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
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
|
|
|
|
|
{
|
2021-06-07 00:44:42 +02:00
|
|
|
|
opts.raw = true;
|
2021-05-29 23:35:39 +02:00
|
|
|
|
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);
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
REQUIRE(matches.at(0).context.first == "<a ");
|
|
|
|
|
REQUIRE(matches.at(0).context.second
|
|
|
|
|
== R"(="https://schlomp.space/tastytea/)"
|
|
|
|
|
R"(epubgrep">epubgrep</a>. Just)");
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(1).filepath_inside == "metadata.opf");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
REQUIRE(matches.at(1).context.first == "<item ");
|
|
|
|
|
REQUIRE(matches.at(1).context.second
|
|
|
|
|
== R"(="start.xhtml" id="start")");
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(2).filepath_inside == "metadata.opf");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
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"};
|
2021-06-07 00:05:33 +02:00
|
|
|
|
std::setlocale(LC_CTYPE, ""); // Needed for utf-8 support in libarchive.
|
2021-05-29 23:35:39 +02:00
|
|
|
|
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);
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
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
|
|
|
|
|
{
|
2021-06-01 19:15:00 +02:00
|
|
|
|
opts.raw = true;
|
2021-06-07 00:44:42 +02:00
|
|
|
|
opts.context = 1;
|
2021-05-29 23:35:39 +02:00
|
|
|
|
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);
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(0).filepath_inside == "start.xhtml");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
REQUIRE(matches.at(0).context.first == "<a ");
|
|
|
|
|
REQUIRE(matches.at(0).context.second
|
|
|
|
|
== R"(="https://schlomp.space/tastytea/)"
|
|
|
|
|
R"(epubgrep">epubgrep</a>. Just)");
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(1).filepath_inside == "nav.xhtml");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
REQUIRE(matches.at(1).context.first == "<li><a ");
|
|
|
|
|
REQUIRE(matches.at(1).context.second
|
|
|
|
|
== std::string(R"(="start.xhtml">Start</a></li>)")
|
|
|
|
|
+ "\n </ol>");
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(2).filepath_inside == "metadata.opf");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
REQUIRE(matches.at(2).context.first == "<item ");
|
|
|
|
|
REQUIRE(matches.at(2).context.second
|
|
|
|
|
== R"(="start.xhtml" id="start")");
|
2021-06-01 19:15:00 +02:00
|
|
|
|
REQUIRE(matches.at(3).filepath_inside == "metadata.opf");
|
2021-05-30 13:48:11 +02:00
|
|
|
|
REQUIRE(matches.at(3).context.first == R"(id="nav" )");
|
2021-05-29 23:35:39 +02:00
|
|
|
|
REQUIRE(matches.at(3).context.second
|
2021-05-30 13:48:11 +02:00
|
|
|
|
== R"(="nav.xhtml" )"
|
2021-05-29 23:35:39 +02:00
|
|
|
|
R"(media-type="application/xhtml+xml")");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-07 00:05:33 +02:00
|
|
|
|
|
|
|
|
|
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() == '.');
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-29 23:35:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|