Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed Jul 25, 2023
1 parent b92afa5 commit a723f8e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
6 changes: 4 additions & 2 deletions iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ IGUANA_INLINE void parse_escape(U &value, It &&it, It &&end) {
if (*it == 'u') {
++it;
if (std::distance(it, end) <= 4)
throw std::runtime_error(R"(Expected 4 hexadecimal digits)");
IGUANA_UNLIKELY {
throw std::runtime_error(R"(Expected 4 hexadecimal digits)");
}
auto code_point = parse_unicode_hex4(it);
encode_utf8(value, code_point);
} else if (*it == 'n') {
Expand Down Expand Up @@ -177,7 +179,7 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end) {
IGUANA_UNLIKELY case '\\' : ++it;
parse_escape(value, it, end);
break;
IGUANA_UNLIKELY case ']' : return;
// IGUANA_UNLIKELY case ']' : return;
IGUANA_UNLIKELY case '"' : ++it;
return;
IGUANA_LIKELY default : value.push_back(*it);
Expand Down
2 changes: 1 addition & 1 deletion iguana/json_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include "define.h"
#include "detail/charconv.h"
#include "reflection.hpp"
#include "enum_reflection.hpp"
#include "reflection.hpp"
#include "value.hpp"
#include <filesystem>
#include <forward_list>
Expand Down
35 changes: 35 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,41 @@ TEST_CASE("check some types") {
value_type{std::in_place_index_t<1>{}, &point_t::y});
}

TEST_CASE("test exception") {
{
std::string str = R"({"\u8001": "name"})";
std::unordered_map<std::string_view, std::string> mp;
iguana::from_json(mp, str);
CHECK(mp[""] == "name");
}
{
std::string str = R"("a": "\)";
std::unordered_map<std::string, std::string> mp;
CHECK_THROWS(iguana::from_json(mp, str));
}
{
std::string str = R"("a": "\u8")";
std::unordered_map<std::string, std::string> mp;
CHECK_THROWS(iguana::from_json(mp, str));
}
{
std::string str = R"([10, d5])";
std::deque<char> char_q(str.begin(), str.end());
std::vector<int> arr;
CHECK_THROWS(iguana::from_json(arr, char_q));
}
{
std::string str = R"("a": "\)";
std::unordered_map<std::string, char> mp;
CHECK_THROWS(iguana::from_json(mp, str));
}
{
std::string str = R"("a: "\")";
std::unordered_map<std::string, std::string_view> mp;
CHECK_THROWS(iguana::from_json(mp, str));
}
}

// doctest comments
// 'function' : must be 'attribute' - see issue #182
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
Expand Down
10 changes: 7 additions & 3 deletions test/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,21 +681,25 @@ TEST_CASE("test the string_view") {
}

struct st_char_t {
char a;
std::vector<char> a;
char b[5];
char c[5];
};
REFLECTION(st_char_t, a, b, c);
TEST_CASE("test char") {
std::string str = R"(
{
"a": "\n",
"a": ["\n", "\t", "\f", "\r", "\b"],
"b": ["1", "2", "3", "4", "5"],
"c": "1234\n"
}
)";
auto validator = [](st_char_t c) {
CHECK(c.a == '\n');
CHECK(c.a[0] == '\n');
CHECK(c.a[1] == '\t');
CHECK(c.a[2] == '\f');
CHECK(c.a[3] == '\r');
CHECK(c.a[4] == '\b');
CHECK(c.b[0] == '1');
CHECK(c.b[1] == '2');
CHECK(c.b[2] == '3');
Expand Down

0 comments on commit a723f8e

Please sign in to comment.