json_binder_terminate.t.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // json_binder_test_terminate.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/26/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_terminate_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_whitespace_breaks_parsing_numeric_token() {
  15. char data[] = "10 0";
  16. int out = 0;
  17. value_binder<int> binder{};
  18. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  19. TS_ASSERT_EQUALS(out, 10);
  20. }
  21. void test_unterminated_input_causes_exception_when_flagged() {
  22. char data[] = "10 0";
  23. int out = 0;
  24. value_binder<int> binder{};
  25. TS_ASSERT_THROWS(parse(bind(out, binder), data,
  26. disable_concatenated_json_bodies),
  27. json::malformed_json_exception);
  28. }
  29. void test_does_not_crash_if_terminates_early() {
  30. char data[] = "\"This is a \"string";
  31. std::string out = "";
  32. value_binder<std::string> binder{};
  33. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
  34. TS_ASSERT_EQUALS(out, "This is a ");
  35. }
  36. void test_will_crash_if_terminates_with_remaining_with_option() {
  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,
  41. disable_concatenated_json_bodies),
  42. json::malformed_json_exception);
  43. }
  44. void test_will_not_crash_if_terminates_with_remaining_only_whitespace() {
  45. char data[] = "\"This is a \" ";
  46. std::string out = "";
  47. value_binder<std::string> binder{};
  48. TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data,
  49. disable_concatenated_json_bodies));
  50. }
  51. void test_will_crash_if_terminates_with_remaining_buffered_by_whitespace() {
  52. char data[] = "\"This is a \" string";
  53. std::string out = "";
  54. value_binder<std::string> binder{};
  55. TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data,
  56. disable_concatenated_json_bodies),
  57. json::malformed_json_exception);
  58. }
  59. };