json_binder_value_string.t.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // json_binder_value_string.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/25/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "json_binder.hpp"
  10. using namespace json::binder;
  11. using namespace json::parser;
  12. class json_binder_value_string_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_bind_to_string_type() {
  15. char data[] = "\"This is a string\"";
  16. std::string out = "";
  17. value_binder<std::string> binder{};
  18. parse(json::binder::bind(out, binder), data, allow_all);
  19. TS_ASSERT_EQUALS(out, "This is a string");
  20. }
  21. void test_bind_to_string_with_quote_recieves_whole_string() {
  22. char data[] = "\"This is a \\\"string\\\"\"";
  23. std::string out = "";
  24. value_binder<std::string> binder{};
  25. parse(json::binder::bind(out, binder), data, allow_all);
  26. TS_ASSERT_EQUALS(out, "This is a \"string\"");
  27. }
  28. void test_cannot_bind_raw_string() {
  29. char data[] = "This is a string";
  30. std::string out = "";
  31. value_binder<std::string> binder{};
  32. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  33. json::malformed_json_exception);
  34. TS_ASSERT_EQUALS(out, "");
  35. }
  36. void test_throws_error_binding_unterminated_string() {
  37. char data[] = "\"This is a string";
  38. std::string out = "";
  39. value_binder<std::string> binder{};
  40. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
  41. json::unterminated_json_exception);
  42. TS_ASSERT_EQUALS(out, "");
  43. }
  44. void test_does_not_crash_if_terminates_early() {
  45. char data[] = "\"This is a \"string";
  46. std::string out = "";
  47. value_binder<std::string> binder{};
  48. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  49. }
  50. void test_will_crash_if_terminates_with_remaining_with_option() {
  51. char data[] = "\"This is a \"string";
  52. std::string out = "";
  53. value_binder<std::string> binder{};
  54. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data,
  55. disable_concatenated_json_bodies),
  56. json::malformed_json_exception);
  57. }
  58. void test_will_not_crash_if_terminates_with_remaining_only_whitespace() {
  59. char data[] = "\"This is a \" ";
  60. std::string out = "";
  61. value_binder<std::string> binder{};
  62. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data,
  63. disable_concatenated_json_bodies));
  64. }
  65. void test_will_crash_if_terminates_with_remaining_buffered_by_whitespace() {
  66. char data[] = "\"This is a \" string";
  67. std::string out = "";
  68. value_binder<std::string> binder{};
  69. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data,
  70. disable_concatenated_json_bodies),
  71. json::malformed_json_exception);
  72. }
  73. };