Skip to content

Commit

Permalink
fixup! Add test on parsing of redirect file.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Jul 28, 2023
1 parent 7262d3e commit 2302871
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/zimwriterfs/zimcreatorfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <limits.h>
#include <cassert>


using redirect_handler = std::function<void(std::string, std::string, std::string)>;
void parse_redirectArticles(std::istream& in_stream, redirect_handler handler) {
std::string line;
int line_number = 1;
Expand All @@ -44,10 +42,12 @@ void parse_redirectArticles(std::istream& in_stream, redirect_handler handler) {
);
}

auto path = matches[1].str();
auto title = matches[2].str();
auto redirectUrl = matches[3].str();
handler(path, title, redirectUrl);
Redirect redirect = {
.path= matches[1].str(),
.title = matches[2].str(),
.target = matches[3].str()
};
handler(redirect);
++line_number;
}
}
Expand All @@ -74,7 +74,7 @@ void ZimCreatorFS::add_redirectArticles_from_file(const std::string& path)

in_stream.open(path.c_str());
try {
parse_redirectArticles(in_stream, [this](std::string path, std::string title, std::string redirectUrl) {this->addRedirection(path, title, redirectUrl);});
parse_redirectArticles(in_stream, [this](Redirect redirect) {this->addRedirection(redirect.path, redirect.title, redirect.target);});
} catch(const std::runtime_error& e) {
std::cerr << e.what() << "\nin redirect file " << path << std::endl;
in_stream.close();
Expand Down
8 changes: 8 additions & 0 deletions src/zimwriterfs/zimcreatorfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <vector>
#include <string>
#include <functional>

#include <zim/writer/creator.h>

Expand Down Expand Up @@ -54,4 +55,11 @@ class ZimCreatorFS : public zim::writer::Creator
std::string canonical_basedir;
};

struct Redirect {
std::string path, title, target;
};

using redirect_handler = std::function<void(Redirect)>;
void parse_redirectArticles(std::istream& in_stream, redirect_handler handler);

#endif // OPENZIM_ZIMWRITERFS_ARTICLESOURCE_H
22 changes: 7 additions & 15 deletions test/zimwriterfs-zimcreatorfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <unistd.h>
#include <iostream>
#include <magic.h>
#include <functional>

#include <zim/archive.h>

Expand Down Expand Up @@ -129,18 +128,10 @@ TEST(ZimCreatorFSTest, ThrowsErrorIfDirectoryNotExist)
}, std::invalid_argument );
}

struct Redirect {
std::string path;
std::string title;
std::string target;
};

bool operator==(const Redirect& a, const Redirect& b) {
return a.path == b.path && a.title == b.title && a.target == b.target;
}

void parse_redirectArticles(std::istream& in_stream, std::function<void(std::string, std::string, std::string)> handler);

TEST(ZimCreatorFSTest, ParseRedirect)
{
{
Expand All @@ -151,13 +142,14 @@ TEST(ZimCreatorFSTest, ParseRedirect)
std::vector<Redirect> found;
parse_redirectArticles(
ss,
[&](std::string path, std::string title, std::string target)
{found.push_back({path, title, target});}
[&](Redirect redirect)
{found.push_back(redirect);}
);

std::vector<Redirect> expected;
expected.push_back({"path", "title", "target"});
expected.push_back({"A/path/to/somewhere", "An amazing title", "Another/path"});
const std::vector<Redirect> expected {
{"path", "title", "target"},
{"A/path/to/somewhere", "An amazing title", "Another/path"}
};
EXPECT_EQ(found, expected);
}

Expand All @@ -168,7 +160,7 @@ TEST(ZimCreatorFSTest, ParseRedirect)
EXPECT_THROW({
parse_redirectArticles(
ss,
[&](std::string path, std::string title, std::string target)
[&](Redirect redirect)
{}
);
}, std::runtime_error);
Expand Down

0 comments on commit 2302871

Please sign in to comment.