Commit f89c4b52 authored by miloyip's avatar miloyip

Use PrettyWriter to cover FileWriteStream::PutN()

parent 3675b33a
......@@ -22,10 +22,28 @@
#include "rapidjson/reader.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filewritestream.h"
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 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) {
StringBuffer buffer;
......@@ -33,24 +51,7 @@ TEST(PrettyWriter, Basic) {
Reader reader;
StringStream s(kJson);
reader.Parse(s, writer);
EXPECT_STREQ(
"{\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());
EXPECT_STREQ(kPrettyJson, buffer.GetString());
}
TEST(PrettyWriter, SetIndent) {
......@@ -137,22 +138,30 @@ TEST(PrettyWriter, OStreamWrapper) {
reader.Parse(s, writer);
std::string actual = ss.str();
EXPECT_STREQ(
"{\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"
"}",
actual.c_str());
EXPECT_STREQ(kPrettyJson, actual.c_str());
}
// For covering FileWriteStream::PutN()
TEST(PrettyWriter, FileWriteStream) {
char filename[L_tmpnam];
FILE* fp = TempFile(filename);
char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer));
PrettyWriter<FileWriteStream> writer(os);
Reader reader;
StringStream s(kJson);
reader.Parse(s, writer);
fclose(fp);
fp = fopen(filename, "rb");
fseek(fp, 0, SEEK_END);
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