epubgrep/tests/test_zip.cpp

73 lines
2.0 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");
}
}
}
}
}