json_binder_tuple.t.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/json_binder.hpp"
  10. using namespace json::binder;
  11. using namespace json::parser;
  12. struct point { int x; int y; };
  13. bool operator==(point const & lhs, point const & rhs) {
  14. return lhs.x == rhs.x && lhs.y == rhs.y;
  15. }
  16. class json_binder_tuple_TestSuite : public CxxTest::TestSuite {
  17. public:
  18. void test_bind_to_tuple() {
  19. char data[] = "[ 1, 2 ]";
  20. using tuple = std::tuple<int, int>;
  21. tuple out = std::make_tuple(0, 0);
  22. auto binder = make_default_tuple_binder<int, int>();
  23. parse(json::binder::bind(out, binder), data, allow_all);
  24. TS_ASSERT_EQUALS(out, std::make_tuple(1, 2));
  25. }
  26. void test_bind_to_tuple_throws_if_missing_entry() {
  27. char data[] = "[ 1 ]";
  28. using tuple = std::tuple<int, int>;
  29. tuple out = std::make_tuple(0, 0);
  30. auto binder = make_default_tuple_binder<int, int>();
  31. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  32. json::malformed_json_exception);
  33. }
  34. void test_bind_to_tuple_throws_if_too_many_entries() {
  35. char data[] = "[ 1, 2, 3 ]";
  36. using tuple = std::tuple<int, int>;
  37. tuple out = std::make_tuple(0, 0);
  38. auto binder = make_default_tuple_binder<int, int>();
  39. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  40. json::malformed_json_exception);
  41. }
  42. void test_bind_to_tuple_throws_if_unterminated() {
  43. char data[] = "[ 1, 2 ";
  44. using tuple = std::tuple<int, int>;
  45. tuple out = std::make_tuple(0, 0);
  46. auto binder = make_default_tuple_binder<int, int>();
  47. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  48. json::unterminated_json_exception);
  49. }
  50. void test_bind_to_tuple_throws_if_unstarted() {
  51. char data[] = "1, 2 ]";
  52. using tuple = std::tuple<int, int>;
  53. tuple out = std::make_tuple(0, 0);
  54. auto binder = make_default_tuple_binder<int, int>();
  55. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  56. json::malformed_json_exception);
  57. }
  58. void test_bind_to_tuple_with_multiple_types() {
  59. char data[] = "[ 1, 0.5, \"hello\" ]";
  60. using tuple = std::tuple<int, double, std::string>;
  61. tuple out = std::make_tuple(0, 0.0, "");
  62. auto binder = make_default_tuple_binder<int, double, std::string>();
  63. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  64. TS_ASSERT_EQUALS(out, std::make_tuple(1, 0.5, "hello"));
  65. }
  66. void test_write_tuple() {
  67. std::string expected = "[1,\"word\"]";
  68. std::stringstream ss;
  69. using tuple = std::tuple<int, std::string>;
  70. tuple const in = std::make_tuple(1, "word");
  71. auto binder = make_default_tuple_binder<int, std::string>();
  72. write(json::binder::bind(in, binder), ss);
  73. TS_ASSERT_EQUALS(ss.str(), expected);
  74. }
  75. void test_parse_struct_tuple_binding() {
  76. char data[] = "[ 10, 5 ]";
  77. point out{0, 0};
  78. auto binder = tuple_binder<point>()(&point::x)(&point::y);
  79. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  80. TS_ASSERT_EQUALS(out, (point{10, 5}));
  81. }
  82. void test_write_struct_tuple_binding() {
  83. std::string const expected = "[10,5]";
  84. std::stringstream ss;
  85. point const in{10, 5};
  86. auto binder = tuple_binder<point>()(&point::x)(&point::y);
  87. TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
  88. TS_ASSERT_EQUALS(ss.str(), expected);
  89. }
  90. };