// // json_binder_collection.t.h // json // // Created by Sam Jaffe on 2/27/17. // #pragma once #include #include "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; collect out; value_binder 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; collect out; value_binder 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; collect out; value_binder 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; collect out; value_binder 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; collect out; value_binder 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; collect out; value_binder 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; collect out; value_binder 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; collect const in{ 5, 4, 7 }; value_binder 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; collect const in{ { "a", 1 }, { "b", 2 } }; value_binder binder{}; TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss)); TS_ASSERT_EQUALS(ss.str(), expected); } };