| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- //
- // json_binder_value_int.t.h
- // json
- //
- // Created by Sam Jaffe on 2/24/17.
- //
- #include "json/json_binder.hpp"
- #include <gmock/gmock.h>
- using namespace json::binder;
- using namespace json::parser;
- using namespace ::testing;
- TEST(JsonBinderIntTest, ParsesSuccessfully) {
- char data[] = "100";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_NO_THROW(parse(bind(out, binder), data, allow_all));
- EXPECT_THAT(out, 100);
- }
- TEST(JsonBinderIntTest, ThrowsWhenDomainMismatch) {
- char data[] = "-1";
- unsigned int out = 0;
- value_binder<unsigned int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- }
- TEST(JsonBinderIntTest, ThrowsOnEOB) {
- char data[] = "";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- }
- TEST(JsonBinderIntTest, ThrowsOnNaN) {
- char data[] = "one";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- }
- TEST(JsonBinderIntTest, ThrowsOnOOB) {
- char data[] = "1000000000000";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- EXPECT_THAT(out, 0);
- }
- TEST(JsonBinderIntTest, ThrowsWhenNarrowing) {
- char data[] = "2147483648";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_width_exception);
- EXPECT_THAT(out, 0);
- }
- TEST(JsonBinderIntTest, ThrowsWhenNarrowingNegative) {
- char data[] = "-2147483649";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_width_exception);
- }
- TEST(JsonBinderShortTest, ThrowsWhenNarrowing) {
- char data[] = "32768";
- short out = 0;
- value_binder<short> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_width_exception);
- EXPECT_THAT(out, 0);
- }
- TEST(JsonBinderShortTest, ThrowsWhenNarrowingNegative) {
- char data[] = "-32769";
- short out = 0;
- value_binder<short> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_width_exception);
- }
- TEST(JsonBinderIntTest, CanParseMaxInt) {
- char data[] = "2147483647";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_NO_THROW(parse(bind(out, binder), data, allow_all));
- }
- TEST(JsonBinderIntTest, CanParseMinInt) {
- char data[] = "-2147483648";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_NO_THROW(parse(bind(out, binder), data, allow_all));
- }
- TEST(JsonBinderIntTest, ThrowsWhenGivenFloatingPoint) {
- char data[] = "2.0";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- EXPECT_THAT(out, 0);
- }
- TEST(JsonBinderIntTest, CanParseHex) {
- char data[] = "0xF";
- int out = 0;
- value_binder<int> binder{};
- parse(bind(out, binder), data, allow_all);
- EXPECT_THAT(out, 15);
- }
- TEST(JsonBinderIntTest, ThrowsWhenNarrowingHex) {
- char data[] = "0x100000000";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- }
- TEST(JsonBinderIntTest, CanParseOctal) {
- char data[] = "010";
- int out = 0;
- value_binder<int> binder{};
- parse(bind(out, binder), data, allow_all);
- EXPECT_THAT(out, 8);
- }
- TEST(JsonBinderIntTest, ThrowsWhenNarrowingOctal) {
- char data[] = "040000000000";
- int out = 0;
- value_binder<int> binder{};
- EXPECT_THROW(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- }
- // TODO Parametrize this
- TEST(JsonBinderIntTest, WritesSuccessfully) {
- std::string const expected = "100";
- std::stringstream ss;
- int const in = 100;
- value_binder<int> binder{};
- write(bind(in, binder), ss);
- EXPECT_THAT(ss.str(), expected);
- }
- TEST(JsonBinderIntTest, CanWriteUnsigned) {
- std::string const expected = "2147483648";
- std::stringstream ss;
- unsigned int const in = 2147483648;
- value_binder<unsigned int> binder{};
- write(bind(in, binder), ss);
- EXPECT_THAT(ss.str(), expected);
- }
- TEST(JsonBinderIntTest, CanWriteMinInt) {
- std::string const expected = "-2147483648";
- std::stringstream ss;
- int const in = -2147483648;
- value_binder<int> binder{};
- write(bind(in, binder), ss);
- EXPECT_THAT(ss.str(), expected);
- }
|