|
|
@@ -0,0 +1,52 @@
|
|
|
+//
|
|
|
+// json_binder_tuple.t.h
|
|
|
+// json
|
|
|
+//
|
|
|
+// Created by Sam Jaffe on 2/26/17.
|
|
|
+//
|
|
|
+
|
|
|
+#pragma once
|
|
|
+
|
|
|
+#include <cxxtest/TestSuite.h>
|
|
|
+#include "json_binder.hpp"
|
|
|
+
|
|
|
+using namespace json::binder;
|
|
|
+using namespace json::parser;
|
|
|
+
|
|
|
+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 = tuple_binder<std::tuple<int, int>>()
|
|
|
+ (get_binder<tuple, 0>())
|
|
|
+ (get_binder<tuple, 1>());
|
|
|
+ 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 = tuple_binder<std::tuple<int, int>>()
|
|
|
+ (get_binder<tuple, 0>())
|
|
|
+ (get_binder<tuple, 1>());
|
|
|
+ TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
|
|
|
+ json::malformed_json_exception);
|
|
|
+ TS_ASSERT_EQUALS(out, std::make_tuple(1, 0));
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 = tuple_binder<std::tuple<int, int>>()
|
|
|
+ (get_binder<tuple, 0>())
|
|
|
+ (get_binder<tuple, 1>());
|
|
|
+ TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
|
|
|
+ json::malformed_json_exception);
|
|
|
+ TS_ASSERT_EQUALS(out, std::make_tuple(1, 2));
|
|
|
+ }
|
|
|
+};
|