From 36f27ad7442fa00b99685a1d1568abca32b33d4f Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 24 May 2021 20:12:30 +0200 Subject: [PATCH] Add tests for search::search(). --- tests/test.zip | Bin 505 -> 537 bytes tests/test_search.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 tests/test_search.cpp diff --git a/tests/test.zip b/tests/test.zip index 96deb4e4dd98f8cfacf9e3a2f888dd868d52a396..9981d1a3ed575dab3b4016c9a6e2ead17f406ff5 100644 GIT binary patch delta 184 zcmey#Jd0jCC$jd@B*v>014GEdH?_b delta 173 zcmbQq@{@Ujw-*BlbXjf<0yCh5D1!_`NosM4LRx-KN@|flgq@j_8XCgMz-;2UJOPAD zE4UdLSza(RFo21PE2pZ`^9z%)<-ZvK)+w~2`o5}_fi5TnqHnY@rui*f4YGmOq$ L{EQ3?SHSuJu(Kn0 diff --git a/tests/test_search.cpp b/tests/test_search.cpp new file mode 100644 index 0000000..3d23227 --- /dev/null +++ b/tests/test_search.cpp @@ -0,0 +1,128 @@ +#include "fs-compat.hpp" +#include "search.hpp" + +#include + +#include +#include +#include +#include + +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 matches; + epubgrep::search::options opts; + + WHEN("We search for β€˜πŸ“™+\\w?’ using extended regular expressions") + { + try + { + opts.regex = epubgrep::search::regex_kind::extended; + 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; + opts.regex = epubgrep::search::regex_kind::extended; + 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"); + } + } + } + } +}