json_binder_collection.t.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // json_binder_collection.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/27/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. class json_binder_collection_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_vector_parsing() {
  15. char data[] = "[ 0, 1, 2 ]";
  16. using collect = std::vector<int>;
  17. collect out;
  18. value_binder<collect> binder{};
  19. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  20. TS_ASSERT_EQUALS(out, collect({0, 1, 2}));
  21. }
  22. void test_vector_throws_if_array_unterminated() {
  23. char data[] = "[ 0, 1";
  24. using collect = std::vector<int>;
  25. collect out;
  26. value_binder<collect> binder{};
  27. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  28. json::unterminated_json_exception);
  29. }
  30. void test_vector_throws_if_array_unstarted() {
  31. char data[] = "0, 1";
  32. using collect = std::vector<int>;
  33. collect out;
  34. value_binder<collect> binder{};
  35. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  36. json::malformed_json_exception);
  37. }
  38. void test_map_parsing() {
  39. char data[] = "{ \"a\" : 1 , \"b\" : 2 }";
  40. using collect = std::map<std::string, int>;
  41. collect out;
  42. value_binder<collect> binder{};
  43. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  44. TS_ASSERT_EQUALS(out, collect({{"a", 1}, {"b", 2}}));
  45. }
  46. void test_map_throws_if_object_unterminated() {
  47. char data[] = "{ \"a\" : 1 , \"b\" : 2";
  48. using collect = std::map<std::string, int>;
  49. collect out;
  50. value_binder<collect> binder{};
  51. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  52. json::unterminated_json_exception);
  53. }
  54. void test_map_throws_if_object_missing_associative_token() {
  55. char data[] = "{ \"a\" : 1 , \"b\" }";
  56. using collect = std::map<std::string, int>;
  57. collect out;
  58. value_binder<collect> binder{};
  59. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  60. json::malformed_json_exception);
  61. }
  62. void test_map_throws_if_object_unstarted() {
  63. char data[] = "\"a\" : 1 , \"b\" : 2";
  64. using collect = std::map<std::string, int>;
  65. collect out;
  66. value_binder<collect> binder{};
  67. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  68. json::malformed_json_exception);
  69. }
  70. void test_write_vector() {
  71. std::string const expected = "[5,4,7]";
  72. std::stringstream ss;
  73. using collect = std::vector<int>;
  74. collect const in{ 5, 4, 7 };
  75. value_binder<collect> binder{};
  76. TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
  77. TS_ASSERT_EQUALS(ss.str(), expected);
  78. }
  79. void test_write_map() {
  80. std::string const expected = "{\"a\":1,\"b\":2}";
  81. std::stringstream ss;
  82. using collect = std::map<std::string, int>;
  83. collect const in{ { "a", 1 }, { "b", 2 } };
  84. value_binder<collect> binder{};
  85. TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
  86. TS_ASSERT_EQUALS(ss.str(), expected);
  87. }
  88. };