json_binder_tuple_test.cxx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // json_binder_tuple.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/26/17.
  6. //
  7. #include <gmock/gmock.h>
  8. #include "json/json_binder.hpp"
  9. using namespace json::binder;
  10. using namespace json::parser;
  11. struct point { int x; int y; };
  12. bool operator==(point const & lhs, point const & rhs) {
  13. return lhs.x == rhs.x && lhs.y == rhs.y;
  14. }
  15. TEST(JsonBinderTupleTest, ParsesTuple) {
  16. char data[] = "[ 1, 2 ]";
  17. using tuple = std::tuple<int, int>;
  18. tuple out = std::make_tuple(0, 0);
  19. auto binder = make_default_tuple_binder<int, int>();
  20. parse(json::binder::bind(out, binder), data, allow_all);
  21. EXPECT_THAT(out, std::make_tuple(1, 2));
  22. }
  23. TEST(JsonBinderTupleTest, ThrowsIfMissingData) {
  24. char data[] = "[ 1 ]";
  25. using tuple = std::tuple<int, int>;
  26. tuple out = std::make_tuple(0, 0);
  27. auto binder = make_default_tuple_binder<int, int>();
  28. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  29. json::malformed_json_exception);
  30. }
  31. TEST(JsonBinderTupleTest, ThrowsIfDataRemaining) {
  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. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  37. json::malformed_json_exception);
  38. }
  39. TEST(JsonBinderTupleTest, ThrowsIfEOB) {
  40. char data[] = "[ 1, 2 ";
  41. using tuple = std::tuple<int, int>;
  42. tuple out = std::make_tuple(0, 0);
  43. auto binder = make_default_tuple_binder<int, int>();
  44. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  45. json::unterminated_json_exception);
  46. }
  47. TEST(JsonBinderTupleTest, ThrowsIfMissingStartToken) {
  48. char data[] = "1, 2 ]";
  49. using tuple = std::tuple<int, int>;
  50. tuple out = std::make_tuple(0, 0);
  51. auto binder = make_default_tuple_binder<int, int>();
  52. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  53. json::malformed_json_exception);
  54. }
  55. TEST(JsonBinderTupleTest, InnerTypeIsIndependant) {
  56. char data[] = "[ 1, 0.5, \"hello\" ]";
  57. using tuple = std::tuple<int, double, std::string>;
  58. tuple out = std::make_tuple(0, 0.0, "");
  59. auto binder = make_default_tuple_binder<int, double, std::string>();
  60. EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
  61. EXPECT_THAT(out, std::make_tuple(1, 0.5, "hello"));
  62. }
  63. TEST(JsonBinderTupleTest, WritesTupleWithoutWhitespace) {
  64. std::string expected = "[1,\"word\"]";
  65. std::stringstream ss;
  66. using tuple = std::tuple<int, std::string>;
  67. tuple const in = std::make_tuple(1, "word");
  68. auto binder = make_default_tuple_binder<int, std::string>();
  69. write(json::binder::bind(in, binder), ss);
  70. EXPECT_THAT(ss.str(), expected);
  71. }
  72. TEST(JsonBinderTupleTest, CanParseTupleAsStruct) {
  73. char data[] = "[ 10, 5 ]";
  74. point out{0, 0};
  75. auto binder = tuple_binder<point>()(&point::x)(&point::y);
  76. EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
  77. EXPECT_THAT(out, (point{10, 5}));
  78. }
  79. TEST(JsonBinderTupleTest, WritesTupleAsStructWithoutWhitespace) {
  80. std::string const expected = "[10,5]";
  81. std::stringstream ss;
  82. point const in{10, 5};
  83. auto binder = tuple_binder<point>()(&point::x)(&point::y);
  84. EXPECT_NO_THROW(write(json::binder::bind(in, binder), ss));
  85. EXPECT_THAT(ss.str(), expected);
  86. }