json_binder_value_int_test.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // json_binder_value_int.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/24/17.
  6. //
  7. #include "json/json_binder.hpp"
  8. #include <gmock/gmock.h>
  9. using namespace json::binder;
  10. using namespace json::parser;
  11. using namespace ::testing;
  12. TEST(JsonBinderIntTest, ParsesSuccessfully) {
  13. char data[] = "100";
  14. int out = 0;
  15. value_binder<int> binder{};
  16. EXPECT_NO_THROW(parse(bind(out, binder), data, allow_all));
  17. EXPECT_THAT(out, 100);
  18. }
  19. TEST(JsonBinderIntTest, ThrowsWhenDomainMismatch) {
  20. char data[] = "-1";
  21. unsigned int out = 0;
  22. value_binder<unsigned int> binder{};
  23. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  24. json::json_numeric_exception);
  25. }
  26. TEST(JsonBinderIntTest, ThrowsOnEOB) {
  27. char data[] = "";
  28. int out = 0;
  29. value_binder<int> binder{};
  30. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  31. json::unterminated_json_exception);
  32. }
  33. TEST(JsonBinderIntTest, ThrowsOnNaN) {
  34. char data[] = "one";
  35. int out = 0;
  36. value_binder<int> binder{};
  37. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  38. json::json_numeric_exception);
  39. }
  40. TEST(JsonBinderIntTest, ThrowsOnOOB) {
  41. char data[] = "1000000000000";
  42. int out = 0;
  43. value_binder<int> binder{};
  44. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  45. json::json_numeric_exception);
  46. EXPECT_THAT(out, 0);
  47. }
  48. TEST(JsonBinderIntTest, ThrowsWhenNarrowing) {
  49. char data[] = "2147483648";
  50. int out = 0;
  51. value_binder<int> binder{};
  52. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  53. json::json_numeric_width_exception);
  54. EXPECT_THAT(out, 0);
  55. }
  56. TEST(JsonBinderIntTest, ThrowsWhenNarrowingNegative) {
  57. char data[] = "-2147483649";
  58. int out = 0;
  59. value_binder<int> binder{};
  60. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  61. json::json_numeric_width_exception);
  62. }
  63. TEST(JsonBinderShortTest, ThrowsWhenNarrowing) {
  64. char data[] = "32768";
  65. short out = 0;
  66. value_binder<short> binder{};
  67. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  68. json::json_numeric_width_exception);
  69. EXPECT_THAT(out, 0);
  70. }
  71. TEST(JsonBinderShortTest, ThrowsWhenNarrowingNegative) {
  72. char data[] = "-32769";
  73. short out = 0;
  74. value_binder<short> binder{};
  75. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  76. json::json_numeric_width_exception);
  77. }
  78. TEST(JsonBinderIntTest, CanParseMaxInt) {
  79. char data[] = "2147483647";
  80. int out = 0;
  81. value_binder<int> binder{};
  82. EXPECT_NO_THROW(parse(bind(out, binder), data, allow_all));
  83. }
  84. TEST(JsonBinderIntTest, CanParseMinInt) {
  85. char data[] = "-2147483648";
  86. int out = 0;
  87. value_binder<int> binder{};
  88. EXPECT_NO_THROW(parse(bind(out, binder), data, allow_all));
  89. }
  90. TEST(JsonBinderIntTest, ThrowsWhenGivenFloatingPoint) {
  91. char data[] = "2.0";
  92. int out = 0;
  93. value_binder<int> binder{};
  94. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  95. json::json_numeric_exception);
  96. EXPECT_THAT(out, 0);
  97. }
  98. TEST(JsonBinderIntTest, CanParseHex) {
  99. char data[] = "0xF";
  100. int out = 0;
  101. value_binder<int> binder{};
  102. parse(bind(out, binder), data, allow_all);
  103. EXPECT_THAT(out, 15);
  104. }
  105. TEST(JsonBinderIntTest, ThrowsWhenNarrowingHex) {
  106. char data[] = "0x100000000";
  107. int out = 0;
  108. value_binder<int> binder{};
  109. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  110. json::json_numeric_exception);
  111. }
  112. TEST(JsonBinderIntTest, CanParseOctal) {
  113. char data[] = "010";
  114. int out = 0;
  115. value_binder<int> binder{};
  116. parse(bind(out, binder), data, allow_all);
  117. EXPECT_THAT(out, 8);
  118. }
  119. TEST(JsonBinderIntTest, ThrowsWhenNarrowingOctal) {
  120. char data[] = "040000000000";
  121. int out = 0;
  122. value_binder<int> binder{};
  123. EXPECT_THROW(parse(bind(out, binder), data, allow_all),
  124. json::json_numeric_exception);
  125. }
  126. // TODO Parametrize this
  127. TEST(JsonBinderIntTest, WritesSuccessfully) {
  128. std::string const expected = "100";
  129. std::stringstream ss;
  130. int const in = 100;
  131. value_binder<int> binder{};
  132. write(bind(in, binder), ss);
  133. EXPECT_THAT(ss.str(), expected);
  134. }
  135. TEST(JsonBinderIntTest, CanWriteUnsigned) {
  136. std::string const expected = "2147483648";
  137. std::stringstream ss;
  138. unsigned int const in = 2147483648;
  139. value_binder<unsigned int> binder{};
  140. write(bind(in, binder), ss);
  141. EXPECT_THAT(ss.str(), expected);
  142. }
  143. TEST(JsonBinderIntTest, CanWriteMinInt) {
  144. std::string const expected = "-2147483648";
  145. std::stringstream ss;
  146. int const in = -2147483648;
  147. value_binder<int> binder{};
  148. write(bind(in, binder), ss);
  149. EXPECT_THAT(ss.str(), expected);
  150. }