Skip to content

Commit e7e168d

Browse files
committed
tests: add simple test for HTML generation
1 parent 5229441 commit e7e168d

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

code/html_generation.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "../libstriezel/filesystem/directory.hpp"
3535
#include "../libstriezel/filesystem/file.hpp"
3636

37-
int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTMLOptions htmlOptions)
37+
int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTMLOptions htmlOptions, std::string htmlDir)
3838
{
3939
MessageDatabase::Iterator msgIter = mdb.getBegin();
4040
if (msgIter == mdb.getEnd())
@@ -44,7 +44,10 @@ int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTM
4444
}
4545

4646
// directory creation
47-
std::string htmlDir = pmdb::paths::html();
47+
if (htmlDir.empty())
48+
{
49+
htmlDir = pmdb::paths::html();
50+
}
4851
if (!libstriezel::filesystem::directory::exists(htmlDir))
4952
{
5053
std::cout << "Trying to create HTML directory \"" << htmlDir << "\" ...";

code/html_generation.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
* \param mdb the database containing the messages
3131
* \param fm folder mappings for the message database
3232
* \param htmlOptions the options for HTML file generation
33+
* \param htmlDir directory where the HTML files reside
3334
* \return Returns zero, if all HTML files could be created.
3435
* Returns non-zero exit code, if an error occurred.
3536
*/
36-
int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTMLOptions htmlOptions);
37+
int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTMLOptions htmlOptions, std::string htmlDir = "");
3738

3839
#endif // PMDB_HTML_GENERATION_HPP

tests/components/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(component_tests_sources
3232
../../code/bbcode/quotes.cpp
3333
../../code/browser_detection.cpp
3434
../../code/filters/FilterUser.cpp
35+
../../code/html_generation.cpp
3536
../../code/paths.cpp
3637
../../code/templates/defaults.hpp
3738
../../code/templates/functions.cpp
@@ -71,6 +72,7 @@ set(component_tests_sources
7172
bbcode/quotes.cpp
7273
browser_detection.cpp
7374
filter/FilterUser.cpp
75+
html_generation.cpp
7476
names_to_controlsequences.cpp
7577
paths.cpp
7678
templates/defaults.cpp

tests/components/component_tests.cbp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
<Unit filename="../../code/filters/Filter.hpp" />
9191
<Unit filename="../../code/filters/FilterUser.cpp" />
9292
<Unit filename="../../code/filters/FilterUser.hpp" />
93+
<Unit filename="../../code/html_generation.cpp" />
94+
<Unit filename="../../code/html_generation.hpp" />
9395
<Unit filename="../../code/paths.cpp" />
9496
<Unit filename="../../code/paths.hpp" />
9597
<Unit filename="../../code/templates/defaults.hpp" />
@@ -141,6 +143,7 @@
141143
<Unit filename="bbcode/quotes.cpp" />
142144
<Unit filename="browser_detection.cpp" />
143145
<Unit filename="filter/FilterUser.cpp" />
146+
<Unit filename="html_generation.cpp" />
144147
<Unit filename="main.cpp" />
145148
<Unit filename="names_to_controlsequences.cpp" />
146149
<Unit filename="paths.cpp" />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database test suite.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#include "../locate_catch.hpp"
22+
#include <filesystem>
23+
#include "../../code/html_generation.hpp"
24+
25+
TEST_CASE("HTML generation")
26+
{
27+
SECTION("generateHtmlFiles")
28+
{
29+
FolderMap fm;
30+
MessageDatabase mdb;
31+
32+
PrivateMessage pm;
33+
pm.setDatestamp("2007-06-14 12:34");
34+
pm.setTitle("This is the title");
35+
pm.setFromUser("Hermes");
36+
pm.setFromUserID(234);
37+
pm.setToUser("Poseidon");
38+
pm.setMessage("This is a [b]bold[/b] text.");
39+
40+
fm.add(pm.getHash(), "Inbox");
41+
mdb.addMessage(pm);
42+
43+
HTMLOptions options;
44+
options.standard = HTMLStandard::HTML4_01;
45+
46+
std::filesystem::path html_path = std::filesystem::temp_directory_path() / "pmdb_test_html_directory";
47+
48+
int exit_code = generateHtmlFiles(mdb, fm, options, html_path.string());
49+
REQUIRE( exit_code == 0 );
50+
51+
auto message_path = html_path / (pm.getHash().toHexString() + ".html");
52+
REQUIRE( std::filesystem::is_regular_file(message_path) );
53+
54+
auto folder_path = html_path / "folder_94835ea2fcf775cd77cb9c9cee01b5cbd9bc515467aab1215f48a5ade9ca5274.html";
55+
REQUIRE( std::filesystem::is_regular_file(message_path) );
56+
57+
REQUIRE( std::filesystem::remove(message_path) );
58+
REQUIRE( std::filesystem::remove(folder_path) );
59+
REQUIRE( std::filesystem::remove(html_path) );
60+
}
61+
}

0 commit comments

Comments
 (0)