json_binder_tuple_test.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // json_binder_tuple.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/26/17.
  6. //
  7. #include "json/json_binder.hpp"
  8. #include <gmock/gmock.h>
  9. using namespace json::binder;
  10. using namespace json::parser;
  11. struct point {
  12. int x;
  13. int y;
  14. };
  15. bool operator==(point const & lhs, point const & rhs) {
  16. return lhs.x == rhs.x && lhs.y == rhs.y;
  17. }
  18. TEST(JsonBinderTupleTest, ParsesTuple) {
  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. EXPECT_THAT(out, std::make_tuple(1, 2));
  25. }
  26. TEST(JsonBinderTupleTest, ThrowsIfMissingData) {
  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. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  32. json::malformed_json_exception);
  33. }
  34. TEST(JsonBinderTupleTest, ThrowsIfDataRemaining) {
  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. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  40. json::malformed_json_exception);
  41. }
  42. TEST(JsonBinderTupleTest, ThrowsIfEOB) {
  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. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  48. json::unterminated_json_exception);
  49. }
  50. TEST(JsonBinderTupleTest, ThrowsIfMissingStartToken) {
  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. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  56. json::malformed_json_exception);
  57. }
  58. TEST(JsonBinderTupleTest, InnerTypeIsIndependant) {
  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. EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
  64. EXPECT_THAT(out, std::make_tuple(1, 0.5, "hello"));
  65. }
  66. TEST(JsonBinderTupleTest, WritesTupleWithoutWhitespace) {
  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. EXPECT_THAT(ss.str(), expected);
  74. }
  75. TEST(JsonBinderTupleTest, CanParseTupleAsStruct) {
  76. char data[] = "[ 10, 5 ]";
  77. point out{0, 0};
  78. auto binder = tuple_binder<point>()(&point::x)(&point::y);
  79. EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
  80. EXPECT_THAT(out, (point{10, 5}));
  81. }
  82. TEST(JsonBinderTupleTest, WritesTupleAsStructWithoutWhitespace) {
  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. EXPECT_NO_THROW(write(json::binder::bind(in, binder), ss));
  88. EXPECT_THAT(ss.str(), expected);
  89. }