| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- //
- // json_binder_value_string.t.h
- // json
- //
- // Created by Sam Jaffe on 2/25/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include "json/json_binder.hpp"
- using namespace json::binder;
- using namespace json::parser;
- class json_binder_value_string_TestSuite : public CxxTest::TestSuite {
- public:
- void test_bind_to_string_type() {
- char data[] = "\"This is a string\"";
- std::string out = "";
- value_binder<std::string> binder{};
- parse(json::binder::bind(out, binder), data, allow_all);
- TS_ASSERT_EQUALS(out, "This is a string");
- }
-
- void test_bind_to_string_with_quote_recieves_whole_string() {
- char data[] = "\"This is a \\\"string\\\"\"";
- std::string out = "";
- value_binder<std::string> binder{};
- parse(json::binder::bind(out, binder), data, allow_all);
- TS_ASSERT_EQUALS(out, "This is a \"string\"");
- }
-
- void test_cannot_bind_raw_string() {
- char data[] = "This is a string";
- std::string out = "";
- value_binder<std::string> binder{};
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::malformed_json_exception);
- TS_ASSERT_EQUALS(out, "");
- }
-
- void test_throws_error_binding_unterminated_string() {
- char data[] = "\"This is a string";
- std::string out = "";
- value_binder<std::string> binder{};
- TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
- json::unterminated_json_exception);
- TS_ASSERT_EQUALS(out, "");
- }
-
- void test_write_string() {
- std::string const expected = "\"This is a string\"";
- std::stringstream ss;
- std::string const in = "This is a string";
- value_binder<std::string> binder{};
- write(json::binder::bind(in, binder), ss);
- TS_ASSERT_EQUALS(ss.str(), expected);
- }
-
- void test_write_string_with_quotes() {
- std::string const expected = "\"This is a \\\"string\\\"\"";
- std::stringstream ss;
- std::string const in = "This is a \"string\"";
- value_binder<std::string> binder{};
- write(json::binder::bind(in, binder), ss);
- TS_ASSERT_EQUALS(ss.str(), expected);
- }
- };
|