json_binder_value_int.t.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // json_binder_value_int.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/24/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_int_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_bind_to_integer_type() {
  15. char data[] = "100";
  16. int out = 0;
  17. value_binder<int> binder{};
  18. parse(bind(out, binder), data, allow_all);
  19. TS_ASSERT_EQUALS(out, 100);
  20. }
  21. void test_output_from_integer_type() {
  22. std::string expected = "100";
  23. std::stringstream ss;
  24. int in = 100;
  25. value_binder<int> binder{};
  26. write(bind(in, binder), ss);
  27. TS_ASSERT_EQUALS(ss.str(), expected);
  28. }
  29. void test_empty_buffer_to_int_throws() {
  30. char data[] = "";
  31. int out = 0;
  32. value_binder<int> binder{};
  33. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  34. json::unterminated_json_exception);
  35. TS_ASSERT_EQUALS(out, 0);
  36. }
  37. void test_non_integral_to_int_throws() {
  38. char data[] = "one";
  39. int out = 0;
  40. value_binder<int> binder{};
  41. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  42. json::json_numeric_exception);
  43. }
  44. void test_oversized_int_throws() {
  45. char data[] = "1000000000000";
  46. int out = 0;
  47. value_binder<int> binder{};
  48. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  49. json::json_numeric_exception);
  50. TS_ASSERT_EQUALS(out, 0);
  51. }
  52. void test_unsigned_int_downcast_to_int_throws() {
  53. char data[] = "2147483648";
  54. int out = 0;
  55. value_binder<int> binder{};
  56. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  57. json::json_numeric_width_exception);
  58. TS_ASSERT_EQUALS(out, 0);
  59. }
  60. void test_signed_int_max_parses() {
  61. char data[] = "2147483647";
  62. int out = 0;
  63. value_binder<int> binder{};
  64. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  65. }
  66. void test_signed_int_min_parses() {
  67. char data[] = "-2147483648";
  68. int out = 0;
  69. value_binder<int> binder{};
  70. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  71. }
  72. void test_parsing_short_will_error_over_narrowing() {
  73. char data[] = "100000";
  74. short out = 0;
  75. value_binder<short> binder{};
  76. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  77. json::json_numeric_width_exception);
  78. TS_ASSERT_EQUALS(out, 0);
  79. }
  80. void test_double_to_int_throws() {
  81. char data[] = "2.0";
  82. int out = 0;
  83. value_binder<int> binder{};
  84. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  85. json::json_numeric_exception);
  86. TS_ASSERT_EQUALS(out, 0);
  87. }
  88. void test_whitespace_breaks_parsing_numeric_token() {
  89. char data[] = "10 0";
  90. int out = 0;
  91. value_binder<int> binder{};
  92. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  93. TS_ASSERT_EQUALS(out, 10);
  94. }
  95. void test_unterminated_input_causes_exception_when_flagged() {
  96. char data[] = "10 0";
  97. int out = 0;
  98. value_binder<int> binder{};
  99. TS_ASSERT_THROWS(parse(bind(out, binder), data,
  100. disable_concatenated_json_bodies),
  101. json::malformed_json_exception);
  102. }
  103. };