| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- //
- // json_binder_collection.t.h
- // json
- //
- // Created by Sam Jaffe on 2/27/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include "json/json_binder.hpp"
- using namespace json::binder;
- using namespace json::parser;
- class json_binder_collection_TestSuite : public CxxTest::TestSuite {
- public:
- void test_vector_parsing() {
- char data[] = "[ 0, 1, 2 ]";
- using collect = std::vector<int>;
- collect out;
- value_binder<collect> binder{};
- TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
- TS_ASSERT_EQUALS(out, collect({0, 1, 2}));
- }
-
- void test_vector_throws_if_array_unterminated() {
- char data[] = "[ 0, 1";
- using collect = std::vector<int>;
- collect out;
- value_binder<collect> binder{};
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- }
-
- void test_vector_throws_if_array_unstarted() {
- char data[] = "0, 1";
- using collect = std::vector<int>;
- collect out;
- value_binder<collect> binder{};
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
-
- void test_map_parsing() {
- char data[] = "{ \"a\" : 1 , \"b\" : 2 }";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
- TS_ASSERT_EQUALS(out, collect({{"a", 1}, {"b", 2}}));
- }
-
- void test_map_throws_if_object_unterminated() {
- char data[] = "{ \"a\" : 1 , \"b\" : 2";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- }
-
- void test_map_throws_if_object_missing_associative_token() {
- char data[] = "{ \"a\" : 1 , \"b\" }";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
- void test_map_throws_if_object_unstarted() {
- char data[] = "\"a\" : 1 , \"b\" : 2";
- using collect = std::map<std::string, int>;
- collect out;
- value_binder<collect> binder{};
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- }
-
- void test_write_vector() {
- 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{};
- TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
- TS_ASSERT_EQUALS(ss.str(), expected);
- }
-
- void test_write_map() {
- 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{};
- TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
- TS_ASSERT_EQUALS(ss.str(), expected);
- }
- };
|