json_binder_tuple.t.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. }
  30. void test_bind_to_tuple_throws_if_too_many_entries() {
  31. char data[] = "[ 1, 2, 3 ]";
  32. using tuple = std::tuple<int, int>;
  33. tuple out = std::make_tuple(0, 0);
  34. auto binder = make_default_tuple_binder<int, int>();
  35. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  36. json::malformed_json_exception);
  37. }
  38. void test_bind_to_tuple_throws_if_unterminated() {
  39. char data[] = "[ 1, 2 ";
  40. using tuple = std::tuple<int, int>;
  41. tuple out = std::make_tuple(0, 0);
  42. auto binder = make_default_tuple_binder<int, int>();
  43. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  44. json::unterminated_json_exception);
  45. }
  46. void test_bind_to_tuple_throws_if_unstarted() {
  47. char data[] = "1, 2 ]";
  48. using tuple = std::tuple<int, int>;
  49. tuple out = std::make_tuple(0, 0);
  50. auto binder = make_default_tuple_binder<int, int>();
  51. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  52. json::malformed_json_exception);
  53. }
  54. void test_bind_to_tuple_with_multiple_types() {
  55. char data[] = "[ 1, 0.5, \"hello\" ]";
  56. using tuple = std::tuple<int, double, std::string>;
  57. tuple out = std::make_tuple(0, 0.0, "");
  58. auto binder = make_default_tuple_binder<int, double, std::string>();
  59. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  60. TS_ASSERT_EQUALS(out, std::make_tuple(1, 0.5, "hello"));
  61. }
  62. };