#include "fs-compat.hpp" #include "options.hpp" #include "search.hpp" #include #include #include #include #include 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 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 = 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_inside == "start.xhtml"); REQUIRE(matches.at(0).context.first == "epubgrep. Just)"); REQUIRE(matches.at(1).filepath_inside == "metadata.opf"); REQUIRE(matches.at(1).context.first == " 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 = true; 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 == "epubgrep. Just)"); REQUIRE(matches.at(1).filepath_inside == "nav.xhtml"); REQUIRE(matches.at(1).context.first == "
  • Start
  • )") + "\n "); REQUIRE(matches.at(2).filepath_inside == "metadata.opf"); REQUIRE(matches.at(2).context.first == "