hemplate

Simple XML template engine
git clone git://git.dimitrijedobrota.com/hemplate.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 14c196fa229e9b4baf62db69d4730d64367b769b
parent cac6d73e0f4c4f674875a911c1f4946d7bc690cb
author Dimitrije Dobrota <mail@dimitrijedobrota.com>
date Wed, 23 Apr 2025 15:25:47 +0200

Attribute_list test and bug fixing

Diffstat:
M include/hemplate/attribute.hpp | ++++++++ ------
M include/hemplate/element.hpp | --
M source/attribute.cpp | ++ --------
M test/CMakeLists.txt | +
A test/source/attribute_list.cpp | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

5 files changed, 103 insertions(+), 16 deletions(-)


diff --git a/ include/hemplate/attribute.hpp b/ include/hemplate/attribute.hpp

@@ -26,7 +26,7 @@ public: { }
operator std::string() const // NOLINT *-explicit-constructor
explicit operator std::string() const
{ if (empty()) { return name();

@@ -58,27 +58,29 @@ public: attribute_list add(const attribute_list& list) const; attribute_list add(attribute attr) const;
bool empty() const;
explicit operator std::string() const { std::string res; if (!m_class.empty()) {
res += m_class;
res += std::string(m_class);
res += ' '; } if (!m_style.empty()) {
res += m_style;
res += std::string(m_style);
res += ' '; } for (const auto& attr : m_attributes) {
res += attr;
res += std::string(attr);
res += ' '; }
if (!res.empty()) {
res.pop_back();
}
return res; }

diff --git a/ include/hemplate/element.hpp b/ include/hemplate/element.hpp

@@ -31,8 +31,6 @@ public: const attribute_list& attributes() const { return *this; } private:
using attribute_list::empty;
template<based::string_literal Tag, element::Type MyType> friend class element_builder;

diff --git a/ source/attribute.cpp b/ source/attribute.cpp

@@ -26,20 +26,14 @@ attribute_list::attribute_list(attribute attr) set(std::move(attr)); }
bool attribute_list::empty() const
{
return m_attributes.empty() && m_class.value().empty()
&& m_style.value().empty();
}
attribute_list& attribute_list::set(const attribute_list& list) { for (const auto& attr : list.m_attributes) { set(attr); }
set(m_class);
set(m_style);
set(list.m_class);
set(list.m_style);
return (*this); }

diff --git a/ test/CMakeLists.txt b/ test/CMakeLists.txt

@@ -22,6 +22,7 @@ function(add_test NAME) endfunction() add_test(attribute)
add_test(attribute_list)
# ---- End-of-file commands ----

diff --git a/ test/source/attribute_list.cpp b/ test/source/attribute_list.cpp

@@ -0,0 +1,92 @@
#include "hemplate/attribute.hpp"
#include <catch2/catch_test_macros.hpp>
// NOLINTBEGIN readability-container-size-empty
TEST_CASE("set class", "[attribute_list]")
{
hemplate::attribute_list attrs = hemplate::attribute {"class", "first"};
REQUIRE(std::string(attrs) == R"(class="first")");
SECTION("second")
{
attrs.set({"class", "second"});
REQUIRE(std::string(attrs) == R"(class="first second")");
}
SECTION("random")
{
attrs.set({"test"});
REQUIRE(std::string(attrs) == R"(class="first" test)");
}
}
TEST_CASE("set style", "[attribute_list]")
{
hemplate::attribute_list attrs = hemplate::attribute {"style", "first"};
REQUIRE(std::string(attrs) == R"(style="first")");
SECTION("second")
{
attrs.set({"style", "second"});
REQUIRE(std::string(attrs) == R"(style="first; second")");
}
SECTION("random")
{
attrs.set({"test"});
REQUIRE(std::string(attrs) == R"(style="first" test)");
}
}
TEST_CASE("set list", "[attribute_list]")
{
hemplate::attribute_list attrs {
{"class", "first"},
{"style", "first"},
{"test_first"},
{"class", "second"},
{"style", "second"},
{"test_second"},
};
REQUIRE(
std::string(attrs)
== R"(class="first second" style="first; second" test_first test_second)");
SECTION("set")
{
attrs.set({
{"class", "third"},
{"style", "third"},
{"test_third"},
});
REQUIRE(
std::string(attrs)
== R"(class="first second third" style="first; second; third" test_first test_second test_third)");
}
}
TEST_CASE("add", "[attribute_list]")
{
using namespace std::literals::string_view_literals;
const auto attrs = hemplate::attribute_list {}
.add({"class", "first"})
.add({
{"class"sv, "second"sv},
{"class"sv, "third"sv},
});
REQUIRE(std::string(attrs) == R"(class="first second third")");
}
// NOLINTEND readability-container-size-empty