Commit f89c4b52 authored by miloyip's avatar miloyip

Use PrettyWriter to cover FileWriteStream::PutN()

parent 3675b33a
...@@ -22,10 +22,28 @@ ...@@ -22,10 +22,28 @@
#include "rapidjson/reader.h" #include "rapidjson/reader.h"
#include "rapidjson/prettywriter.h" #include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
#include "rapidjson/filewritestream.h"
using namespace rapidjson; using namespace rapidjson;
static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}"; static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}";
static const char kPrettyJson[] =
"{\n"
" \"hello\": \"world\",\n"
" \"t\": true,\n"
" \"f\": false,\n"
" \"n\": null,\n"
" \"i\": 123,\n"
" \"pi\": 3.1416,\n"
" \"a\": [\n"
" 1,\n"
" 2,\n"
" 3,\n"
" -1\n"
" ],\n"
" \"u64\": 1234567890123456789,\n"
" \"i64\": -1234567890123456789\n"
"}";
TEST(PrettyWriter, Basic) { TEST(PrettyWriter, Basic) {
StringBuffer buffer; StringBuffer buffer;
...@@ -33,24 +51,7 @@ TEST(PrettyWriter, Basic) { ...@@ -33,24 +51,7 @@ TEST(PrettyWriter, Basic) {
Reader reader; Reader reader;
StringStream s(kJson); StringStream s(kJson);
reader.Parse(s, writer); reader.Parse(s, writer);
EXPECT_STREQ( EXPECT_STREQ(kPrettyJson, buffer.GetString());
"{\n"
" \"hello\": \"world\",\n"
" \"t\": true,\n"
" \"f\": false,\n"
" \"n\": null,\n"
" \"i\": 123,\n"
" \"pi\": 3.1416,\n"
" \"a\": [\n"
" 1,\n"
" 2,\n"
" 3,\n"
" -1\n"
" ],\n"
" \"u64\": 1234567890123456789,\n"
" \"i64\": -1234567890123456789\n"
"}",
buffer.GetString());
} }
TEST(PrettyWriter, SetIndent) { TEST(PrettyWriter, SetIndent) {
...@@ -137,22 +138,30 @@ TEST(PrettyWriter, OStreamWrapper) { ...@@ -137,22 +138,30 @@ TEST(PrettyWriter, OStreamWrapper) {
reader.Parse(s, writer); reader.Parse(s, writer);
std::string actual = ss.str(); std::string actual = ss.str();
EXPECT_STREQ( EXPECT_STREQ(kPrettyJson, actual.c_str());
"{\n" }
" \"hello\": \"world\",\n"
" \"t\": true,\n" // For covering FileWriteStream::PutN()
" \"f\": false,\n" TEST(PrettyWriter, FileWriteStream) {
" \"n\": null,\n" char filename[L_tmpnam];
" \"i\": 123,\n" FILE* fp = TempFile(filename);
" \"pi\": 3.1416,\n" char buffer[16];
" \"a\": [\n" FileWriteStream os(fp, buffer, sizeof(buffer));
" 1,\n" PrettyWriter<FileWriteStream> writer(os);
" 2,\n" Reader reader;
" 3,\n" StringStream s(kJson);
" -1\n" reader.Parse(s, writer);
" ],\n" fclose(fp);
" \"u64\": 1234567890123456789,\n"
" \"i64\": -1234567890123456789\n" fp = fopen(filename, "rb");
"}", fseek(fp, 0, SEEK_END);
actual.c_str()); size_t size = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET);
char* json = (char*)malloc(size + 1);
size_t readLength = fread(json, 1, size, fp);
json[readLength] = '\0';
fclose(fp);
remove(filename);
EXPECT_STREQ(kPrettyJson, json);
free(json);
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment