Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed Jul 11, 2023
1 parent ab3b29d commit 51045b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
8 changes: 3 additions & 5 deletions iguana/xml_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ IGUANA_INLINE void skip_object_value(It &&it, It &&end, std::string_view name) {
return;
}
++it;
skip_sapces_and_newline(it, end);
}
throw std::runtime_error("unclosed tag: " + std::string(name));
}

// return true means reach the close tag
template <size_t cdata_idx, refletable T, typename It>
IGUANA_INLINE auto skip_till_key(T &value, It &&it, It &&end) {
skip_sapces_and_newline(it, end);
while (true) {
match<'<'>(it, end);
if (*it == '/') [[unlikely]] {
Expand Down Expand Up @@ -246,7 +248,6 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
constexpr auto cdata_idx = get_type_index<is_cdata_t, std::decay_t<T>>();
skip_till<'>'>(it, end);
++it;
skip_sapces_and_newline(it, end);
if (skip_till_key<cdata_idx>(value, it, end)) {
match_close_tag(it, end, name);
return;
Expand Down Expand Up @@ -276,7 +277,6 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
if constexpr (!cdata_t<item_type>) {
parse_item(value.*member_ptr, it, end, key);
}
skip_sapces_and_newline(it, end);
if (skip_till_key<cdata_idx>(value, it, end)) {
match_close_tag(it, end, name);
parse_done = true;
Expand All @@ -294,7 +294,7 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
return;
}
// map parse
while (it != end) {
while (true) {
static constexpr auto frozen_map = get_iguana_struct_map<T>();
const auto &member_it = frozen_map.find(key);
if (member_it != frozen_map.end()) [[likely]] {
Expand All @@ -316,7 +316,6 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
skip_object_value(it, end, key);
#endif
}
skip_sapces_and_newline(it, end);
if (skip_till_key<cdata_idx>(value, it, end)) {
match_close_tag(it, end, name);
return;
Expand All @@ -326,7 +325,6 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
key = std::string_view{&*start,
static_cast<size_t>(std::distance(start, it))};
}
throw std::runtime_error("unclosed tag: " + std::string(name));
}

} // namespace detail
Expand Down
20 changes: 17 additions & 3 deletions test/test_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ struct some_type_t {
};
REFLECTION(some_type_t, price, description, child, hasdescription, c, d_v, name,
addr, status);

TEST_CASE("test parse_done") {
std::string str = R"(
<child>
<key1>10</key1>
</child>
)";
child_t c;
iguana::from_xml(c, str);
CHECK(c.key1 == 10);
}

TEST_CASE("test some type") {
auto validator_some_type = [](const some_type_t &s) {
CHECK(s.price[0] == 1.23f);
Expand Down Expand Up @@ -187,9 +199,6 @@ TEST_CASE("test some type") {
std::string ss;
iguana::to_xml(st, ss);

std::string ss2;
iguana::render_value(ss2, enum_status::stop);
std::cout << ss2 << std::endl;
some_type_t st1;
iguana::from_xml(st1, ss);
validator_some_type(st1);
Expand Down Expand Up @@ -418,6 +427,11 @@ TEST_CASE("test province example") {
validator(p1);
}

TEST_CASE("test get_number") {
std::string str = "3.14";
CHECK(iguana::get_number<float>(str) == 3.14f);
}

// doctest comments
// 'function' : must be 'attribute' - see issue #182
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
Expand Down
12 changes: 12 additions & 0 deletions test/test_xml_nothrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ TEST_CASE("test unkonwn key") {
validator(od1);
}

TEST_CASE("test exception") {
std::string str = R"(
<order_t>
<orderID>12345</orderID>
<c>a</c>
<e>
</order_t>
)";
order_t od;
iguana::from_xml(od, str);
}

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

0 comments on commit 51045b9

Please sign in to comment.