Fixed end_map_value() calling point.

Renamed names that related to a visitor.
Renamed test name from json to json_like because I omit escape.
Added end_map_value() and end_map() implementation.
This commit is contained in:
Takatoshi Kondo
2016-05-05 10:53:14 +09:00
parent d5b515899c
commit 6593cba284
5 changed files with 113 additions and 105 deletions

View File

@@ -30,9 +30,9 @@ LIST (APPEND check_PROGRAMS
reference.cpp
streaming.cpp
streaming_c.cpp
unpack_visitor.cpp
user_class.cpp
version.cpp
visitor.cpp
zone.cpp
)

View File

@@ -25,9 +25,9 @@ check_PROGRAMS = \
reference \
streaming \
streaming_c \
unpack_visitor \
user_class \
version \
visitor \
zone
check_PROGRAMS += \
@@ -71,9 +71,9 @@ raw_SOURCES = raw.cpp
reference_SOURCES = reference.cpp
streaming_SOURCES = streaming.cpp
streaming_c_SOURCES = streaming_c.cpp
unpack_visitor_SOURCES = unpack_visitor.cpp
user_class_SOURCES = user_class.cpp
version_SOURCES = version.cpp
visitor_SOURCES = visitor.cpp
zone_SOURCES = zone.cpp
iterator_cpp11_SOURCES = iterator_cpp11.cpp

View File

@@ -3,14 +3,14 @@
#include <sstream>
// To avoid link error
TEST(unpack_visitor, dummy)
TEST(visitor, dummy)
{
}
#if MSGPACK_DEFAULT_API_VERSION >= 2
struct json_visitor : msgpack::v2::null_visitor {
json_visitor(std::string& s):m_s(s) {}
struct json_like_visitor : msgpack::v2::null_visitor {
json_like_visitor(std::string& s):m_s(s) {}
bool visit_nil() {
m_s += "null";
@@ -34,6 +34,7 @@ struct json_visitor : msgpack::v2::null_visitor {
return true;
}
bool visit_str(const char* v, uint32_t size) {
// I omit escape process.
m_s += '"' + std::string(v, size) + '"';
return true;
}
@@ -58,7 +59,12 @@ struct json_visitor : msgpack::v2::null_visitor {
m_s += ":";
return true;
}
bool end_map_value() {
m_s += ",";
return true;
}
bool end_map() {
m_s.erase(m_s.size() - 1, 1); // remove the last ','
m_s += "}";
return true;
}
@@ -71,7 +77,7 @@ struct json_visitor : msgpack::v2::null_visitor {
std::string& m_s;
};
TEST(unpack_visitor, json)
TEST(visitor, json_like)
{
std::stringstream ss;
msgpack::packer<std::stringstream> p(ss);
@@ -82,12 +88,12 @@ TEST(unpack_visitor, json)
p.pack_nil();
p.pack(true);
std::string json;
json_visitor v(json);
std::string json_like;
json_like_visitor v(json_like);
std::size_t off = 0;
bool ret = msgpack::v2::unpack_visit(ss.str().data(), ss.str().size(), off, v);
bool ret = msgpack::v2::parse(ss.str().data(), ss.str().size(), off, v);
EXPECT_TRUE(ret);
EXPECT_EQ("{\"key\":[42,null,true]}", json);
EXPECT_EQ("{\"key\":[42,null,true]}", json_like);
}
struct parse_error_check_visitor : msgpack::v2::null_visitor {
@@ -100,13 +106,13 @@ struct parse_error_check_visitor : msgpack::v2::null_visitor {
bool& m_called;
};
TEST(unpack_visitor, parse_error)
TEST(visitor, parse_error)
{
bool called = false;
parse_error_check_visitor v(called);
std::size_t off = 0;
char const data[] = { static_cast<char>(0x93u), 0x01u, static_cast<char>(0xc1u), 0x03u };
bool ret = msgpack::v2::unpack_visit(data, sizeof(data), off, v);
bool ret = msgpack::v2::parse(data, sizeof(data), off, v);
EXPECT_FALSE(ret);
EXPECT_TRUE(called);
}
@@ -121,13 +127,13 @@ struct insuf_bytes_check_visitor : msgpack::v2::null_visitor {
bool& m_called;
};
TEST(unpack_visitor, insuf_bytes)
TEST(visitor, insuf_bytes)
{
bool called = false;
insuf_bytes_check_visitor v(called);
std::size_t off = 0;
char const data[] = { static_cast<char>(0x93u), 0x01u, 0x01u };
bool ret = msgpack::v2::unpack_visit(data, sizeof(data), off, v);
bool ret = msgpack::v2::parse(data, sizeof(data), off, v);
EXPECT_FALSE(ret);
EXPECT_TRUE(called);
}