diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1f897e9..febd11f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,8 @@ include(CTest) file(GLOB sources_tests test_*.cpp) file(COPY "test.zip" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +file(COPY "test.epub2" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +file(COPY "test.epub3" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) find_package(Catch2 CONFIG) diff --git a/tests/test.epub2 b/tests/test.epub2 new file mode 100644 index 0000000..2b3d734 Binary files /dev/null and b/tests/test.epub2 differ diff --git a/tests/test.epub3 b/tests/test.epub3 new file mode 100644 index 0000000..f2df1db Binary files /dev/null and b/tests/test.epub3 differ diff --git a/tests/test_search_epub.cpp b/tests/test_search_epub.cpp new file mode 100644 index 0000000..1c68365 --- /dev/null +++ b/tests/test_search_epub.cpp @@ -0,0 +1,166 @@ +#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 == "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 == "epubgrep. Just)"); + REQUIRE(matches.at(1).filepath == "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 == "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 == "epubgrep. Just)"); + REQUIRE(matches.at(1).filepath == "nav.xhtml"); + REQUIRE(matches.at(1).context.first == "
  • Start
  • )") + + "\n "); + REQUIRE(matches.at(2).filepath == "metadata.opf"); + REQUIRE(matches.at(2).context.first == " #include -SCENARIO("Searching works") +SCENARIO("Searching ZIP files works") { GIVEN("Our test zip file") {