| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // json_binder_collection.t.h
- // json
- //
- // Created by Sam Jaffe on 2/27/17.
- //
- #include "json/json_binder.hpp"
- #include <gmock/gmock.h>
- using namespace json::binder;
- using namespace json::parser;
- TEST(JsonBinderArrayTest, ParsesSuccessfully) {
- char data[] = "[ 0, 1, 2 ]";
- using collect = std::vector<int>;
- collect out;
- value_binder<collect> binder{};
- EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
- EXPECT_THAT(out, collect({0, 1, 2}));
- }
- TEST(JsonBinderArrayTest, ThrowsOnMissingEndToken) {
- char data[] = "[ 0, 1";
- using collect = std::vector<int>;
- collect out;
- value_binder<collect> binder{};
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- }
- TEST(JsonBinderArrayTest, ThrowsOnMissingStartToken) {
- char data[] = "0, 1";
- using collect = std::vector<int>;
- collect out;
- value_binder<collect> binder{};
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- TEST(JsonBinderMapTest, ParsesSuccessfully) {
- char data[] = "{ \"a\" : 1 , \"b\" : 2 }";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- EXPECT_NO_THROW(parse(json::binder::bind(out, binder), data, allow_all));
- EXPECT_THAT(out, collect({{"a", 1}, {"b", 2}}));
- }
- TEST(JsonBinderMapTest, ThrowsOnMissingEndToken) {
- char data[] = "{ \"a\" : 1 , \"b\" : 2";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- }
- TEST(JsonBinderMapTest, ThrowsOnMissingValueForKey) {
- char data[] = "{ \"a\" : 1 , \"b\" }";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- TEST(JsonBinderMapTest, ThrowsOnMissingStartToken) {
- char data[] = "\"a\" : 1 , \"b\" : 2";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- TEST(JsonBinderArrayTest, WritesDataWithoutWhitespace) {
- std::string const expected = "[5,4,7]";
- std::stringstream ss;
- using collect = std::vector<int>;
- collect const in{5, 4, 7};
- value_binder<collect> binder{};
- EXPECT_NO_THROW(write(json::binder::bind(in, binder), ss));
- EXPECT_THAT(ss.str(), expected);
- }
- TEST(JsonBinderMapTest, WritesDataWithoutWhitespace) {
- std::string const expected = "{\"a\":1,\"b\":2}";
- std::stringstream ss;
- using collect = std::map<std::string, int>;
- collect const in{{"a", 1}, {"b", 2}};
- value_binder<collect> binder{};
- EXPECT_NO_THROW(write(json::binder::bind(in, binder), ss));
- EXPECT_THAT(ss.str(), expected);
- }
|