Files
lnreader/modules/nitro-epub/cpp/tests/EpubParserTest.cpp
T
2026-08-04 20:49:38 +05:30

101 lines
3.4 KiB
C++

#include "../import/EpubParser.hpp"
#include <algorithm>
#include <cassert>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
namespace {
void writeFile(const std::filesystem::path& path, const std::string& content) {
std::filesystem::create_directories(path.parent_path());
std::ofstream file(path);
file << content;
}
bool containsPath(const std::vector<std::string>& paths,
const std::filesystem::path& expectedPath) {
return std::find(paths.begin(), paths.end(), expectedPath.string()) !=
paths.end();
}
} // namespace
int main(int argc, char** argv) {
assert(argc == 2);
const std::filesystem::path fixtureDirectory = argv[1];
std::filesystem::remove_all(fixtureDirectory);
writeFile(
fixtureDirectory / "META-INF/container.xml",
R"xml(<?xml version="1.0"?>
<container>
<rootfiles>
<rootfile full-path="OEBPS/content.opf"/>
</rootfiles>
</container>)xml");
writeFile(
fixtureDirectory / "OEBPS/content.opf",
R"xml(<?xml version="1.0"?>
<package version="3.0">
<metadata>
<dc:title>Image fixture</dc:title>
<meta name="cover" content="cover-document"/>
</metadata>
<manifest>
<item id="nav" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="cover-document" href="Text/cover.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter" href="Text/chapter.xhtml" media-type="application/xhtml+xml"/>
<item id="jpeg" href="Images/cover.jpg" media-type="image/jpeg"/>
<item id="png" href="Images/illustration.png" media-type="image/png"/>
<item id="webp" href="Images/illustration.webp" media-type="image/webp"/>
<item id="gif" href="Images/illustration.gif" media-type="image/gif"/>
<item id="svg" href="Images/illustration.svg" media-type="image/svg+xml"/>
</manifest>
<spine>
<itemref idref="cover-document"/>
<itemref idref="chapter"/>
</spine>
</package>)xml");
writeFile(
fixtureDirectory / "OEBPS/toc.ncx",
R"xml(<?xml version="1.0"?>
<ncx>
<navMap>
<navPoint>
<navLabel><text>Chapter</text></navLabel>
<content src="Text/chapter.xhtml"/>
</navPoint>
</navMap>
</ncx>)xml");
writeFile(fixtureDirectory / "OEBPS/Text/chapter.xhtml",
"<html><body>Chapter</body></html>");
writeFile(
fixtureDirectory / "OEBPS/Text/cover.xhtml",
R"xml(<html><body><svg xmlns:xlink="http://www.w3.org/1999/xlink"><image xlink:href="../Images/cover.jpg"/></svg></body></html>)xml");
const EpubMetadata metadata = parseEpub(fixtureDirectory.string());
const std::filesystem::path imageDirectory = fixtureDirectory / "OEBPS/Images";
assert(metadata.imagePaths.size() == 5);
assert(containsPath(metadata.imagePaths, imageDirectory / "cover.jpg"));
assert(containsPath(metadata.imagePaths,
imageDirectory / "illustration.png"));
assert(containsPath(metadata.imagePaths,
imageDirectory / "illustration.webp"));
assert(containsPath(metadata.imagePaths,
imageDirectory / "illustration.gif"));
assert(containsPath(metadata.imagePaths,
imageDirectory / "illustration.svg"));
assert(metadata.cover == (imageDirectory / "cover.jpg").string());
assert(metadata.chapters.size() == 2);
assert(metadata.chapters.front().path ==
(fixtureDirectory / "OEBPS/Text/cover.xhtml").string());
return 0;
}