2021-05-24 20:12:30 +02:00
|
|
|
|
#include "fs-compat.hpp"
|
2021-05-27 17:20:00 +02:00
|
|
|
|
#include "options.hpp"
|
2021-05-24 20:12:30 +02:00
|
|
|
|
#include "search.hpp"
|
|
|
|
|
|
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
|
|
#include <clocale>
|
|
|
|
|
#include <exception>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
SCENARIO("Searching works")
|
|
|
|
|
{
|
|
|
|
|
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("search() doesn't fail and returns the right lines")
|
|
|
|
|
{
|
|
|
|
|
std::vector<epubgrep::search::match> matches;
|
2021-05-27 17:20:00 +02:00
|
|
|
|
epubgrep::search::settings opts;
|
2021-05-24 20:12:30 +02:00
|
|
|
|
|
|
|
|
|
WHEN("We search for ‘📙+\\w?’ using extended regular expressions")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-05-27 17:20:00 +02:00
|
|
|
|
opts.regex = epubgrep::options::regex_kind::extended;
|
2021-05-24 20:12:30 +02:00
|
|
|
|
matches = epubgrep::search::search(zipfile, "📙+\\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 == "test folder/😊");
|
|
|
|
|
REQUIRE(matches.at(0).text == "📙");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WHEN("We search for ‘📗’ with context = 1")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
opts.context = 1;
|
|
|
|
|
matches = epubgrep::search::search(zipfile, "📗", 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 == "test folder/😊");
|
|
|
|
|
REQUIRE(matches.at(0).text == "📗");
|
|
|
|
|
REQUIRE(matches.at(0).context.first == "📖 📘");
|
|
|
|
|
REQUIRE(matches.at(0).context.second == "📙 ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WHEN("We search for ‘ ’ (space) with context = 1.")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
opts.context = 1;
|
|
|
|
|
matches = epubgrep::search::search(zipfile, " ", 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(1).filepath == "test folder/test file");
|
|
|
|
|
REQUIRE(matches.at(1).text == " ");
|
|
|
|
|
REQUIRE(matches.at(1).context.first == "don't");
|
|
|
|
|
REQUIRE(matches.at(1).context.second == "want to");
|
|
|
|
|
REQUIRE(matches.at(10).filepath == "test folder/😊");
|
|
|
|
|
REQUIRE(matches.at(10).text == " ");
|
|
|
|
|
REQUIRE(matches.at(10).context.first == "📖");
|
|
|
|
|
REQUIRE(matches.at(10).context.second == "📘📗📙 ");
|
|
|
|
|
REQUIRE(matches.at(11).filepath == "test folder/😊");
|
|
|
|
|
REQUIRE(matches.at(11).text == " ");
|
|
|
|
|
REQUIRE(matches.at(11).context.first == "📘📗📙");
|
|
|
|
|
REQUIRE(matches.at(11).context.second == "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WHEN(R"(We search for ‘work\s[\w]+\.\W[\w']+\Wstay’ )"
|
|
|
|
|
R"(using Perl regular expressions. context = 1.)")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
opts.context = 1;
|
2021-05-27 17:20:00 +02:00
|
|
|
|
opts.regex = epubgrep::options::regex_kind::extended;
|
2021-05-24 20:12:30 +02:00
|
|
|
|
matches = epubgrep::search::search(
|
|
|
|
|
zipfile, R"(work\s[\w]+\.\W[\w']+\Wstay)", 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 == "test folder/test file");
|
|
|
|
|
REQUIRE(matches.at(0).text == "work today. I'm stay");
|
|
|
|
|
REQUIRE(matches.at(0).context.first == "to ");
|
|
|
|
|
REQUIRE(matches.at(0).context.second == "ing in");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|