added formatter inject_html test

This commit is contained in:
2026-01-08 12:32:04 +01:00
parent 1754c9fbab
commit cf3b502509
5 changed files with 41 additions and 4 deletions

View File

@@ -3,7 +3,8 @@
#include <sstream>
#include <string>
bool replace(std::string &str, const std::string &from, const std::string &to) {
static bool replace(std::string &str, const std::string &from,
const std::string &to) {
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
@@ -11,8 +12,6 @@ bool replace(std::string &str, const std::string &from, const std::string &to) {
return true;
}
// Change template from file into html
// Return html in string
std::string inject_html_into_template(const std::string &FILE_NAME,
const std::string &TEMPLATE,
const std::string &HTML) {

View File

@@ -0,0 +1,7 @@
#include <string>
// Change template from file into html
// Return html in string
std::string inject_html_into_template(const std::string &FILE_NAME,
const std::string &TEMPLATE,
const std::string &HTML);

View File

@@ -22,7 +22,7 @@ markdown.o: markdown.cpp markdown.hpp
$(CXX) $(CXXFLAGS) -c markdown.cpp
formatter.o: formatter.cpp formatter.hpp
$(CXX) $(CXXFLAGS) -c formatter.hpp
$(CXX) $(CXXFLAGS) -c formatter.cpp
tests.o: $(TEST_DIR)/tests.cpp config.hpp
$(CXX) $(CXXFLAGS) -c $(TEST_DIR)/tests.cpp

11
src/tests/formatter.html Normal file
View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{replace_me}}
</body>
</html>

View File

@@ -1,4 +1,5 @@
#include "../config.hpp"
#include "../formatter.hpp"
#include "../markdown.hpp"
#include "../utils.hpp"
#include <catch2/catch_all.hpp>
@@ -83,3 +84,22 @@ int main() {
REQUIRE(get_html(INPUT) == EXPECTED_OUTPUT);
}
TEST_CASE("formatter injection test") {
const std::string FILE_NAME = "./tests/formatter.html";
const std::string EXPECTED_OUTPUT = R"(<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
I'm replaced!
</body>
</html>
)";
const std::string output =
inject_html_into_template(FILE_NAME, "{{replace_me}}", "I'm replaced!");
REQUIRE(EXPECTED_OUTPUT == output);
}