json_binder_collection_test.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // json_binder_collection.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/27/17.
  6. //
  7. #include <gmock/gmock.h>
  8. #include "json/json_binder.hpp"
  9. using namespace json::binder;
  10. using namespace json::parser;
  11. TEST(JsonBinderArrayTest, ParsesSuccessfully) {
  12. char data[] = "[ 0, 1, 2 ]";
  13. using collect = std::vector<int>;
  14. collect out;
  15. value_binder<collect> binder{};
  16. EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
  17. EXPECT_THAT(out, collect({0, 1, 2}));
  18. }
  19. TEST(JsonBinderArrayTest, ThrowsOnMissingEndToken) {
  20. char data[] = "[ 0, 1";
  21. using collect = std::vector<int>;
  22. collect out;
  23. value_binder<collect> binder{};
  24. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  25. json::unterminated_json_exception);
  26. }
  27. TEST(JsonBinderArrayTest, ThrowsOnMissingStartToken) {
  28. char data[] = "0, 1";
  29. using collect = std::vector<int>;
  30. collect out;
  31. value_binder<collect> binder{};
  32. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  33. json::malformed_json_exception);
  34. }
  35. TEST(JsonBinderMapTest, ParsesSuccessfully) {
  36. char data[] = "{ \"a\" : 1 , \"b\" : 2 }";
  37. using collect = std::map<std::string, int>;
  38. collect out;
  39. value_binder<collect> binder{};
  40. EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
  41. EXPECT_THAT(out, collect({{"a", 1}, {"b", 2}}));
  42. }
  43. TEST(JsonBinderMapTest, ThrowsOnMissingEndToken) {
  44. char data[] = "{ \"a\" : 1 , \"b\" : 2";
  45. using collect = std::map<std::string, int>;
  46. collect out;
  47. value_binder<collect> binder{};
  48. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  49. json::unterminated_json_exception);
  50. }
  51. TEST(JsonBinderMapTest, ThrowsOnMissingValueForKey) {
  52. char data[] = "{ \"a\" : 1 , \"b\" }";
  53. using collect = std::map<std::string, int>;
  54. collect out;
  55. value_binder<collect> binder{};
  56. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  57. json::malformed_json_exception);
  58. }
  59. TEST(JsonBinderMapTest, ThrowsOnMissingStartToken) {
  60. char data[] = "\"a\" : 1 , \"b\" : 2";
  61. using collect = std::map<std::string, int>;
  62. collect out;
  63. value_binder<collect> binder{};
  64. EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
  65. json::malformed_json_exception);
  66. }
  67. TEST(JsonBinderArrayTest, WritesDataWithoutWhitespace) {
  68. std::string const expected = "[5,4,7]";
  69. std::stringstream ss;
  70. using collect = std::vector<int>;
  71. collect const in{ 5, 4, 7 };
  72. value_binder<collect> binder{};
  73. EXPECT_NO_THROW(write(json::binder::bind(in, binder), ss));
  74. EXPECT_THAT(ss.str(), expected);
  75. }
  76. TEST(JsonBinderMapTest, WritesDataWithoutWhitespace) {
  77. std::string const expected = "{\"a\":1,\"b\":2}";
  78. std::stringstream ss;
  79. using collect = std::map<std::string, int>;
  80. collect const in{ { "a", 1 }, { "b", 2 } };
  81. value_binder<collect> binder{};
  82. EXPECT_NO_THROW(write(json::binder::bind(in, binder), ss));
  83. EXPECT_THAT(ss.str(), expected);
  84. }