From 3550a0a93940525bc32230fd851975631ed3f196 Mon Sep 17 00:00:00 2001 From: dota17 <50514813+dota17@users.noreply.github.com> Date: Tue, 17 Sep 2019 01:32:46 +0800 Subject: [PATCH] add some testcases: WriteTest, StreamWriterTest (#1015) --- src/test_lib_json/main.cpp | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 7de8491..cc5ebf2 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -1864,6 +1864,27 @@ JSONTEST_FIXTURE(WriterTest, dropNullPlaceholders) { JSONTEST_ASSERT(writer.write(nullValue) == "\n"); } +JSONTEST_FIXTURE(WriterTest, enableYAMLCompatibility) { + Json::FastWriter writer; + Json::Value root; + root["hello"] = "world"; + + JSONTEST_ASSERT(writer.write(root) == "{\"hello\":\"world\"}\n"); + + writer.enableYAMLCompatibility(); + JSONTEST_ASSERT(writer.write(root) == "{\"hello\": \"world\"}\n"); +} + +JSONTEST_FIXTURE(WriterTest, omitEndingLineFeed) { + Json::FastWriter writer; + Json::Value nullValue; + + JSONTEST_ASSERT(writer.write(nullValue) == "null\n"); + + writer.omitEndingLineFeed(); + JSONTEST_ASSERT(writer.write(nullValue) == "null"); +} + struct StreamWriterTest : JsonTest::TestCase {}; JSONTEST_FIXTURE(StreamWriterTest, dropNullPlaceholders) { @@ -1875,6 +1896,33 @@ JSONTEST_FIXTURE(StreamWriterTest, dropNullPlaceholders) { JSONTEST_ASSERT(Json::writeString(b, nullValue).empty()); } +JSONTEST_FIXTURE(StreamWriterTest, enableYAMLCompatibility) { + Json::StreamWriterBuilder b; + Json::Value root; + root["hello"] = "world"; + + b.settings_["indentation"] = ""; + JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}"); + + b.settings_["enableYAMLCompatibility"] = true; + JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\": \"world\"}"); + + b.settings_["enableYAMLCompatibility"] = false; + JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}"); +} + +JSONTEST_FIXTURE(StreamWriterTest, indentation) { + Json::StreamWriterBuilder b; + Json::Value root; + root["hello"] = "world"; + + b.settings_["indentation"] = ""; + JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}"); + + b.settings_["indentation"] = "\t"; + JSONTEST_ASSERT(Json::writeString(b, root) == "{\n\t\"hello\" : \"world\"\n}"); +} + JSONTEST_FIXTURE(StreamWriterTest, writeZeroes) { Json::String binary("hi", 3); // include trailing 0 JSONTEST_ASSERT_EQUAL(3, binary.length()); @@ -2622,7 +2670,11 @@ int main(int argc, const char* argv[]) { JSONTEST_REGISTER_FIXTURE(runner, ValueTest, precision); JSONTEST_REGISTER_FIXTURE(runner, WriterTest, dropNullPlaceholders); + JSONTEST_REGISTER_FIXTURE(runner, WriterTest, enableYAMLCompatibility); + JSONTEST_REGISTER_FIXTURE(runner, WriterTest, omitEndingLineFeed); JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, dropNullPlaceholders); + JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, enableYAMLCompatibility); + JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, indentation); JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, writeZeroes); JSONTEST_REGISTER_FIXTURE(runner, ReaderTest, parseWithNoErrors);