epubgrep/tests/test_zip.cpp

103 lines
2.8 KiB
C++

#include "fs-compat.hpp"
#include "zip.hpp"
#include <catch.hpp>
#include <clocale>
#include <exception>
#include <string>
#include <vector>
SCENARIO("Zip file handling 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("list() doesn't fail and returns the right file list")
{
std::vector<std::string> filelist;
WHEN("We list the file contents")
{
try
{
filelist = epubgrep::zip::list(zipfile);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the TOC correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(filelist.at(0) == "test folder/");
REQUIRE(filelist.at(1) == "test folder/test file");
REQUIRE(filelist.at(2) == "test folder/😊");
}
}
}
SECTION("read_file() doesn't fail and returns the right file contents")
{
std::string filecontents;
WHEN("We list the file contents")
{
try
{
filecontents = epubgrep::zip::read_file(zipfile,
"test folder/😊");
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the file contents correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(filecontents == "📖\n\n📘📗📙\n");
}
}
}
}
}
SCENARIO("Helper functions in epubgrep::zip work")
{
SECTION("urldecode() doesn't fail and returns the decoded string")
{
bool exception{false};
GIVEN("The string test%20folder/%2Afile%5Btest%5D%2A")
{
std::string encoded_text{"test%20folder/%2Afile%5Btest%5D%2A"};
std::string decoded_text{};
try
{
decoded_text = epubgrep::zip::urldecode(encoded_text);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the TOC correctly")
{
REQUIRE_FALSE(exception);
REQUIRE(decoded_text == "test folder/*file[test]*");
}
}
}
}