json_binder_tuple.t.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // json_binder_tuple.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/26/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "json_binder.hpp"
  10. using namespace json::binder;
  11. using namespace json::parser;
  12. class json_binder_tuple_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_bind_to_tuple() {
  15. char data[] = "[ 1, 2 ]";
  16. using tuple = std::tuple<int, int>;
  17. tuple out = std::make_tuple(0, 0);
  18. auto binder = tuple_binder<std::tuple<int, int>>()
  19. (get_binder<tuple, 0>())
  20. (get_binder<tuple, 1>());
  21. parse(json::binder::bind(out, binder), data, allow_all);
  22. TS_ASSERT_EQUALS(out, std::make_tuple(1, 2));
  23. }
  24. void test_bind_to_tuple_throws_if_missing_entry() {
  25. char data[] = "[ 1 ]";
  26. using tuple = std::tuple<int, int>;
  27. tuple out = std::make_tuple(0, 0);
  28. auto binder = tuple_binder<std::tuple<int, int>>()
  29. (get_binder<tuple, 0>())
  30. (get_binder<tuple, 1>());
  31. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  32. json::malformed_json_exception);
  33. TS_ASSERT_EQUALS(out, std::make_tuple(1, 0));
  34. }
  35. void test_bind_to_tuple_throws_if_too_many_entries() {
  36. char data[] = "[ 1, 2, 3 ]";
  37. using tuple = std::tuple<int, int>;
  38. tuple out = std::make_tuple(0, 0);
  39. auto binder = tuple_binder<std::tuple<int, int>>()
  40. (get_binder<tuple, 0>())
  41. (get_binder<tuple, 1>());
  42. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  43. json::malformed_json_exception);
  44. TS_ASSERT_EQUALS(out, std::make_tuple(1, 2));
  45. }
  46. };