// // json_binder_value_string.t.h // json // // Created by Sam Jaffe on 2/25/17. // #include #include "json/json_binder.hpp" using namespace json::binder; using namespace json::parser; using namespace ::testing; TEST(JsonBinderStringTest, ParsesSuccessfully) { char data[] = "\"This is a string\""; std::string out = ""; value_binder binder{}; parse(json::binder::bind(out, binder), data, allow_all); EXPECT_THAT(out, "This is a string"); } TEST(JsonBinderStringTest, CanParseInternalQuotes) { char data[] = "\"This is a \\\"string\\\"\""; std::string out = ""; value_binder binder{}; parse(json::binder::bind(out, binder), data, allow_all); EXPECT_THAT(out, "This is a \"string\""); } TEST(JsonBinderStringTest, CannotParseRawString) { char data[] = "This is a string"; std::string out = ""; value_binder binder{}; EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all), json::malformed_json_exception); EXPECT_THAT(out, ""); } TEST(JsonBinderStringTest, ThrowsOnNoEndQuote) { char data[] = "\"This is a string"; std::string out = ""; value_binder binder{}; EXPECT_THROW(parse(json::binder::bind(out, binder), data, allow_all), json::unterminated_json_exception); EXPECT_THAT(out, ""); } TEST(JsonBinderStringTest, CanWrite) { std::string const expected = "\"This is a string\""; std::stringstream ss; std::string const in = "This is a string"; value_binder binder{}; write(json::binder::bind(in, binder), ss); EXPECT_THAT(ss.str(), expected); } TEST(JsonBinderStringTest, EscapesQuotesWhenWriting) { std::string const expected = "\"This is a \\\"string\\\"\""; std::stringstream ss; std::string const in = "This is a \"string\""; value_binder binder{}; write(json::binder::bind(in, binder), ss); EXPECT_THAT(ss.str(), expected); }