epubgrep/tests/test_search_epub.cpp

167 lines
6.3 KiB
C++
Raw Normal View History

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);
REQUIRE(matches.at(0).filepath == "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 = 1;
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 == "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 == "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 == "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 == "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 = 1;
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 == "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 == "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 == "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 == "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")");
}
}
}
}
}