json_binder_value_string.t.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/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_write_string() {
  45. std::string const expected = "\"This is a string\"";
  46. std::stringstream ss;
  47. std::string const in = "This is a string";
  48. value_binder<std::string> binder{};
  49. write(json::binder::bind(in, binder), ss);
  50. TS_ASSERT_EQUALS(ss.str(), expected);
  51. }
  52. void test_write_string_with_quotes() {
  53. std::string const expected = "\"This is a \\\"string\\\"\"";
  54. std::stringstream ss;
  55. std::string const in = "This is a \"string\"";
  56. value_binder<std::string> binder{};
  57. write(json::binder::bind(in, binder), ss);
  58. TS_ASSERT_EQUALS(ss.str(), expected);
  59. }
  60. };