json_binder_tuple.t.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 = make_default_tuple_binder<int, int>();
  19. parse(json::binder::bind(out, binder), data, allow_all);
  20. TS_ASSERT_EQUALS(out, std::make_tuple(1, 2));
  21. }
  22. void test_bind_to_tuple_throws_if_missing_entry() {
  23. char data[] = "[ 1 ]";
  24. using tuple = std::tuple<int, int>;
  25. tuple out = std::make_tuple(0, 0);
  26. auto binder = make_default_tuple_binder<int, int>();
  27. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  28. json::malformed_json_exception);
  29. TS_ASSERT_EQUALS(out, std::make_tuple(1, 0));
  30. }
  31. void test_bind_to_tuple_throws_if_too_many_entries() {
  32. char data[] = "[ 1, 2, 3 ]";
  33. using tuple = std::tuple<int, int>;
  34. tuple out = std::make_tuple(0, 0);
  35. auto binder = make_default_tuple_binder<int, int>();
  36. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  37. json::malformed_json_exception);
  38. TS_ASSERT_EQUALS(out, std::make_tuple(1, 2));
  39. }
  40. void test_bind_to_tuple_with_multiple_types() {
  41. char data[] = "[ 1, 0.5, \"hello\" ]";
  42. using tuple = std::tuple<int, double, std::string>;
  43. tuple out = std::make_tuple(0, 0.0, "");
  44. auto binder = make_default_tuple_binder<int, double, std::string>();
  45. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  46. TS_ASSERT_EQUALS(out, std::make_tuple(1, 0.5, "hello"));
  47. }
  48. };