| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- // json_binder_tuple.t.h
- // json
- //
- // Created by Sam Jaffe on 2/26/17.
- //
- #include <gmock/gmock.h>
- #include "json/json_binder.hpp"
- using namespace json::binder;
- using namespace json::parser;
- struct point { int x; int y; };
- bool operator==(point const & lhs, point const & rhs) {
- return lhs.x == rhs.x && lhs.y == rhs.y;
- }
- TEST(JsonBinderTupleTest, ParsesTuple) {
- char data[] = "[ 1, 2 ]";
- using tuple = std::tuple<int, int>;
- tuple out = std::make_tuple(0, 0);
- auto binder = make_default_tuple_binder<int, int>();
- parse(json::binder::bind(out, binder), data, allow_all);
- EXPECT_THAT(out, std::make_tuple(1, 2));
- }
- TEST(JsonBinderTupleTest, ThrowsIfMissingData) {
- char data[] = "[ 1 ]";
- using tuple = std::tuple<int, int>;
- tuple out = std::make_tuple(0, 0);
- auto binder = make_default_tuple_binder<int, int>();
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- TEST(JsonBinderTupleTest, ThrowsIfDataRemaining) {
- char data[] = "[ 1, 2, 3 ]";
- using tuple = std::tuple<int, int>;
- tuple out = std::make_tuple(0, 0);
- auto binder = make_default_tuple_binder<int, int>();
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- TEST(JsonBinderTupleTest, ThrowsIfEOB) {
- char data[] = "[ 1, 2 ";
- using tuple = std::tuple<int, int>;
- tuple out = std::make_tuple(0, 0);
- auto binder = make_default_tuple_binder<int, int>();
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- }
- TEST(JsonBinderTupleTest, ThrowsIfMissingStartToken) {
- char data[] = "1, 2 ]";
- using tuple = std::tuple<int, int>;
- tuple out = std::make_tuple(0, 0);
- auto binder = make_default_tuple_binder<int, int>();
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- TEST(JsonBinderTupleTest, InnerTypeIsIndependant) {
- char data[] = "[ 1, 0.5, \"hello\" ]";
- using tuple = std::tuple<int, double, std::string>;
- tuple out = std::make_tuple(0, 0.0, "");
- auto binder = make_default_tuple_binder<int, double, std::string>();
- EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
- EXPECT_THAT(out, std::make_tuple(1, 0.5, "hello"));
- }
- TEST(JsonBinderTupleTest, WritesTupleWithoutWhitespace) {
- std::string expected = "[1,\"word\"]";
- std::stringstream ss;
- using tuple = std::tuple<int, std::string>;
- tuple const in = std::make_tuple(1, "word");
- auto binder = make_default_tuple_binder<int, std::string>();
- write(json::binder::bind(in, binder), ss);
- EXPECT_THAT(ss.str(), expected);
- }
- TEST(JsonBinderTupleTest, CanParseTupleAsStruct) {
- char data[] = "[ 10, 5 ]";
- point out{0, 0};
- auto binder = tuple_binder<point>()(&point::x)(&point::y);
- EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
- EXPECT_THAT(out, (point{10, 5}));
- }
- TEST(JsonBinderTupleTest, WritesTupleAsStructWithoutWhitespace) {
- std::string const expected = "[10,5]";
- std::stringstream ss;
- point const in{10, 5};
- auto binder = tuple_binder<point>()(&point::x)(&point::y);
- EXPECT_NO_THROW(write(json::binder::bind(in, binder), ss));
- EXPECT_THAT(ss.str(), expected);
- }
|