Skip to content

Commit

Permalink
redo xml namespacd test and example
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed May 9, 2023
1 parent 78e8c22 commit b92c08a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
31 changes: 21 additions & 10 deletions example/xml_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,28 +317,39 @@ void test_vector() {
std::cout << ss << std::endl;
}

struct item_itunes_t {
iguana::xml::namespace_t<std::string_view> itunes_author;
iguana::xml::namespace_t<std::string_view> itunes_subtitle;
iguana::xml::namespace_t<int> itunes_user;
};
REFLECTION(item_itunes_t, itunes_author, itunes_subtitle, itunes_user);
struct item_t {
iguana::xml::namespace_t itunes_author;
iguana::xml::namespace_t itunes_subtitle;
iguana::xml::namespace_t<item_itunes_t> item_itunes;
};
REFLECTION(item_t, itunes_author, itunes_subtitle);
REFLECTION(item_t, item_itunes);
void test_namespace() {
std::cout << "********** test namespace ************" << std::endl;
std::string str = R"(
<item>
<itunes:author>Jupiter Broadcasting</itunes:author>
<itunes:subtitle>Linux enthusiasts talk top news stories, subtitle</itunes:subtitle>
<item:itunes>
<itunes:author>Jupiter Broadcasting</itunes:author>
<itunes:subtitle>Linux enthusiasts talk top news stories, subtitle</itunes:subtitle>
<itunes:user>10086</itunes:user>
</item:itunes>
</item>
)";
item_t it;
iguana::xml::from_xml(it, str.data());
std::cout << "author : " << it.itunes_author.get<std::string_view>().second
<< std::endl;
std::cout << "subtitle : "
<< it.itunes_subtitle.get<std::string_view>().second << std::endl;
auto itunes = it.item_itunes.get();
std::cout << "author : " << itunes.itunes_author.get() << "\n";
;
std::cout << "subtitle : " << itunes.itunes_subtitle.get() << "\n";
;
std::cout << "user : " << itunes.itunes_user.get() << "\n";
;
std::string ss;
iguana::xml::to_xml(ss, it);
std::cout << "to_xml" << std::endl << ss << std::endl;
std::cout << "to_xml" << std::endl << ss << "\n";
}

struct package_t {
Expand Down
39 changes: 19 additions & 20 deletions test/test_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,40 +321,39 @@ TEST_CASE("test exception") {
iguana::xml::to_xml_pretty(ss, simple2)); // unexpected end of data
}

struct item_itunes_t {
iguana::xml::namespace_t<std::string_view> itunes_author;
iguana::xml::namespace_t<std::string_view> itunes_subtitle;
iguana::xml::namespace_t<int> itunes_user;
};
REFLECTION(item_itunes_t, itunes_author, itunes_subtitle, itunes_user);
struct item_t {
iguana::xml::namespace_t itunes_author;
iguana::xml::namespace_t itunes_subtitle;
iguana::xml::namespace_t itunes_user;
iguana::xml::namespace_t<item_itunes_t> item_itunes;
};
REFLECTION(item_t, itunes_author, itunes_subtitle, itunes_user);
REFLECTION(item_t, item_itunes);
TEST_CASE("test xml namespace") {
item_t item;
std::string str = R"(
<item>
<itunes:author>Jupiter Broadcasting</itunes:author>
<itunes:subtitle>Linux enthusiasts talk top news stories, subtitle</itunes:subtitle>
<itunes:user>10086</itunes:user>
<item:itunes>
<itunes:author>Jupiter Broadcasting</itunes:author>
<itunes:subtitle>Linux enthusiasts talk top news stories, subtitle</itunes:subtitle>
<itunes:user>10086</itunes:user>
</item:itunes>
</item>
)";
item_t it;
iguana::xml::from_xml(it, str.data());
CHECK(it.itunes_author.get<std::string_view>().first);
CHECK(it.itunes_author.get<std::string_view>().second ==
"Jupiter Broadcasting");
CHECK(it.itunes_subtitle.get<std::string_view>().first);
CHECK(it.itunes_user.get<int>().first);
CHECK(it.itunes_user.get<int>().second == 10086);
auto itunes = it.item_itunes.get();
CHECK(itunes.itunes_author.get() == "Jupiter Broadcasting");
CHECK(itunes.itunes_user.get() == 10086);

std::string ss;
iguana::xml::to_xml(ss, it);
item_t it2;
iguana::xml::from_xml(it2, ss.data());
CHECK(it2.itunes_author.get<std::string_view>().first);
CHECK(it2.itunes_author.get<std::string_view>().second ==
"Jupiter Broadcasting");
CHECK(it2.itunes_subtitle.get<std::string_view>().first);
CHECK(it2.itunes_user.get<int>().first);
CHECK(it2.itunes_user.get<int>().second == 10086);
auto itunes2 = it2.item_itunes.get();
CHECK(itunes2.itunes_author.get() == "Jupiter Broadcasting");
CHECK(itunes2.itunes_user.get() == 10086);
}

struct package_t {
Expand Down

0 comments on commit b92c08a

Please sign in to comment.