Skip to content

Commit

Permalink
add test;add example
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed Apr 29, 2024
1 parent 1551448 commit 0f17ba2
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 39 deletions.
47 changes: 41 additions & 6 deletions example/xml_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,47 @@ void province_example() {
std::cout << ss1;
}

struct text_t {
using escape_attr_t =
iguana::xml_attr_t<std::string, std::map<std::string_view, std::string>>;
escape_attr_t ID;
std::string DisplayName;
};
REFLECTION(text_t, ID, DisplayName);
void escape_example() {
{
std::string str = R"(
<text_t description="&quot;&lt;'&#x5c0f;&#24378;'&gt;&quot;">
<ID ID'msg='{"msg&apos;reply": "it&apos;s ok"}'>&amp;&lt;&gt;</ID>
<DisplayName>&#x5c0f;&#24378;</DisplayName>
</text_t>
)";
using text_attr_t =
iguana::xml_attr_t<text_t, std::map<std::string_view, std::string>>;
auto validator = [](const text_attr_t& text) {
auto v = text.value();
auto attr = text.attr();
assert(attr["description"] == R"("<'小强'>")");
assert(v.ID.value() == R"(&<>)");
assert(v.ID.attr()["ID'msg"] == R"({"msg'reply": "it's ok"})");
assert(v.DisplayName == "小强");
};
text_attr_t text;
iguana::from_xml(text, str);
validator(text);
std::string ss;
iguana::to_xml<true>(text, ss);
std::cout << ss << std::endl;
}
}

int main(void) {
some_type_example();
lib_example();
package_example();
derived_object();
cdata_example();
province_example();
// some_type_example();
// lib_example();
// package_example();
// derived_object();
// cdata_example();
// province_example();
escape_example();
return 0;
}
2 changes: 1 addition & 1 deletion iguana/xml_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ inline void render_xml_attr(Stream &ss, const T &value, std::string_view name) {
ss.append(name.data(), name.size());
for (auto [k, v] : value) {
ss.push_back(' ');
render_value<true>(ss, k);
render_value(ss, k);
ss.push_back('=');
ss.push_back(XML_ATTR_DELIMITER);
render_value<true>(ss, v);
Expand Down
67 changes: 37 additions & 30 deletions test/test_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,6 @@ struct Owner_t {
}
};
REFLECTION(Owner_t, ID, DisplayName);
TEST_CASE("test escape") {
{
std::string str = R"(
<Owner_t description="&lt;&#x5c0f;&#24378;&gt;">
<ID>&amp;&lt;&gt;</ID>
<DisplayName>&#x5c0f;&#24378;</DisplayName>
</Owner_t>
)";
using Owner_attr_t =
iguana::xml_attr_t<Owner_t, std::map<std::string_view, std::string>>;
auto validator = [](const Owner_attr_t &Owner) {
auto Ow = Owner.value();
auto attr = Owner.attr();
CHECK(attr["description"] == "<小强>");
CHECK(Ow.ID == R"(&<>)");
CHECK(Ow.DisplayName == "小强");
};
Owner_attr_t Owner;
iguana::from_xml(Owner, str);
validator(Owner);

std::string ss;
iguana::to_xml(Owner, ss);
std::cout << ss << std::endl;
Owner_attr_t Owner1;
iguana::from_xml(Owner1, ss);
validator(Owner1);
}
}

struct Contents {
std::string Key;
std::string LastModified;
Expand Down Expand Up @@ -786,6 +756,43 @@ TEST_CASE("test alias") {
CHECK(m1.obj.y == 42);
}

struct text_t {
using escape_attr_t =
iguana::xml_attr_t<std::string, std::map<std::string_view, std::string>>;
escape_attr_t ID;
std::string DisplayName;
};
REFLECTION(text_t, ID, DisplayName);
TEST_CASE("test escape") {
{
std::string str = R"(
<text_t description="&quot;&lt;'&#x5c0f;&#24378;'&gt;&quot;">
<ID ID'msg='{"msg&apos;reply": "it&apos;s ok"}'>&amp;&lt;&gt;</ID>
<DisplayName>&#x5c0f;&#24378;</DisplayName>
</text_t>
)";
using text_attr_t =
iguana::xml_attr_t<text_t, std::map<std::string_view, std::string>>;
auto validator = [](const text_attr_t &text) {
auto v = text.value();
auto attr = text.attr();
CHECK(attr["description"] == R"("<'小强'>")");
CHECK(v.ID.value() == R"(&<>)");
CHECK(v.ID.attr()["ID'msg"] == R"({"msg'reply": "it's ok"})");
CHECK(v.DisplayName == "小强");
};
text_attr_t text;
iguana::from_xml(text, str);
validator(text);
std::string ss;
iguana::to_xml<true>(text, ss);

text_attr_t text1;
iguana::from_xml(text1, ss);
validator(text1);
}
}

// doctest comments
// 'function' : must be 'attribute' - see issue #182
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
Expand Down
44 changes: 42 additions & 2 deletions test/test_xml_nothrow.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h"
#undef THROW_UNKNOWN_KEY
#include "iguana/xml_reader.hpp"
#include "iguana/xml_writer.hpp"
#define XML_ATTR_USE_APOS
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <optional>
#include <vector>

#include "iguana/xml_reader.hpp"
#include "iguana/xml_writer.hpp"

enum class enum_status {
paid,
unpaid,
Expand Down Expand Up @@ -82,6 +84,44 @@ TEST_CASE("test exception") {
CHECK_THROWS(iguana::from_xml(od, str));
}

struct text_t {
using escape_attr_t =
iguana::xml_attr_t<std::string, std::map<std::string_view, std::string>>;
escape_attr_t ID;
std::string DisplayName;
};
REFLECTION(text_t, ID, DisplayName);
TEST_CASE("test escape") {
{
std::string str = R"(
<text_t description="&quot;&lt;'&#x5c0f;&#24378;'&gt;&quot;">
<ID ID'msg='{"msg&apos;reply": "it&apos;s ok"}'>&amp;&lt;&gt;</ID>
<DisplayName>&#x5c0f;&#24378;</DisplayName>
</text_t>
)";
using text_attr_t =
iguana::xml_attr_t<text_t, std::map<std::string_view, std::string>>;
auto validator = [](const text_attr_t &text) {
auto v = text.value();
auto attr = text.attr();
CHECK(attr["description"] == R"("<'小强'>")");
CHECK(v.ID.value() == R"(&<>)");
CHECK(v.ID.attr()["ID'msg"] == R"({"msg'reply": "it's ok"})");
CHECK(v.DisplayName == "小强");
};
text_attr_t text;
iguana::from_xml(text, str);
validator(text);
std::string ss;
iguana::to_xml<true>(text, ss);
std::cout << ss << std::endl;

text_attr_t text1;
iguana::from_xml(text1, ss);
validator(text1);
}
}

// doctest comments
// 'function' : must be 'attribute' - see issue #182
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
Expand Down

0 comments on commit 0f17ba2

Please sign in to comment.