| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- //
- // json_binder_tuple.t.h
- // json
- //
- // Created by Sam Jaffe on 2/26/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.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;
- }
- class json_binder_tuple_TestSuite : public CxxTest::TestSuite {
- public:
- void test_bind_to_tuple() {
- 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);
- TS_ASSERT_EQUALS(out, std::make_tuple(1, 2));
- }
-
- void test_bind_to_tuple_throws_if_missing_entry() {
- char data[] = "[ 1 ]";
- using tuple = std::tuple<int, int>;
- tuple out = std::make_tuple(0, 0);
- auto binder = make_default_tuple_binder<int, int>();
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- void test_bind_to_tuple_throws_if_too_many_entries() {
- 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>();
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
-
- void test_bind_to_tuple_throws_if_unterminated() {
- 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>();
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- }
-
- void test_bind_to_tuple_throws_if_unstarted() {
- 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>();
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
-
- void test_bind_to_tuple_with_multiple_types() {
- 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>();
- TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
- TS_ASSERT_EQUALS(out, std::make_tuple(1, 0.5, "hello"));
- }
-
- void test_write_tuple() {
- 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);
- TS_ASSERT_EQUALS(ss.str(), expected);
- }
-
- void test_parse_struct_tuple_binding() {
- char data[] = "[ 10, 5 ]";
- point out{0, 0};
- auto binder = tuple_binder<point>()(&point::x)(&point::y);
- TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
- TS_ASSERT_EQUALS(out, (point{10, 5}));
- }
-
- void test_write_struct_tuple_binding() {
- std::string const expected = "[10,5]";
- std::stringstream ss;
- point const in{10, 5};
- auto binder = tuple_binder<point>()(&point::x)(&point::y);
- TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
- TS_ASSERT_EQUALS(ss.str(), expected);
- }
- };
|