Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
prettywritertest.cpp
Go to the documentation of this file.
1// Tencent is pleased to support the open source community by making RapidJSON available.
2//
3// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4//
5// Licensed under the MIT License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// http://opensource.org/licenses/MIT
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "unittest.h"
16#include "rapidjson/reader.h"
20
21#ifdef __clang__
22RAPIDJSON_DIAG_PUSH
23RAPIDJSON_DIAG_OFF(c++98-compat)
24#endif
25
26using namespace rapidjson;
27
28static 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}";
29static const char kPrettyJson[] =
30"{\n"
31" \"hello\": \"world\",\n"
32" \"t\": true,\n"
33" \"f\": false,\n"
34" \"n\": null,\n"
35" \"i\": 123,\n"
36" \"pi\": 3.1416,\n"
37" \"a\": [\n"
38" 1,\n"
39" 2,\n"
40" 3,\n"
41" -1\n"
42" ],\n"
43" \"u64\": 1234567890123456789,\n"
44" \"i64\": -1234567890123456789\n"
45"}";
46
47static const char kPrettyJson_FormatOptions_SLA[] =
48"{\n"
49" \"hello\": \"world\",\n"
50" \"t\": true,\n"
51" \"f\": false,\n"
52" \"n\": null,\n"
53" \"i\": 123,\n"
54" \"pi\": 3.1416,\n"
55" \"a\": [1, 2, 3, -1],\n"
56" \"u64\": 1234567890123456789,\n"
57" \"i64\": -1234567890123456789\n"
58"}";
59
61 StringBuffer buffer;
62 PrettyWriter<StringBuffer> writer(buffer);
63 Reader reader;
64 StringStream s(kJson);
65 reader.Parse(s, writer);
66 EXPECT_STREQ(kPrettyJson, buffer.GetString());
67}
68
69TEST(PrettyWriter, FormatOptions) {
70 StringBuffer buffer;
71 PrettyWriter<StringBuffer> writer(buffer);
73 Reader reader;
74 StringStream s(kJson);
75 reader.Parse(s, writer);
76 EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString());
77}
78
79TEST(PrettyWriter, SetIndent) {
80 StringBuffer buffer;
81 PrettyWriter<StringBuffer> writer(buffer);
82 writer.SetIndent('\t', 1);
83 Reader reader;
84 StringStream s(kJson);
85 reader.Parse(s, writer);
87 "{\n"
88 "\t\"hello\": \"world\",\n"
89 "\t\"t\": true,\n"
90 "\t\"f\": false,\n"
91 "\t\"n\": null,\n"
92 "\t\"i\": 123,\n"
93 "\t\"pi\": 3.1416,\n"
94 "\t\"a\": [\n"
95 "\t\t1,\n"
96 "\t\t2,\n"
97 "\t\t3,\n"
98 "\t\t-1\n"
99 "\t],\n"
100 "\t\"u64\": 1234567890123456789,\n"
101 "\t\"i64\": -1234567890123456789\n"
102 "}",
103 buffer.GetString());
104}
105
107 StringBuffer buffer;
108 PrettyWriter<StringBuffer> writer(buffer);
109 EXPECT_TRUE(writer.StartArray());
110 EXPECT_TRUE(writer.String("Hello\n"));
111 EXPECT_TRUE(writer.EndArray());
112 EXPECT_STREQ("[\n \"Hello\\n\"\n]", buffer.GetString());
113}
114
115#if RAPIDJSON_HAS_STDSTRING
116TEST(PrettyWriter, String_STDSTRING) {
117 StringBuffer buffer;
118 PrettyWriter<StringBuffer> writer(buffer);
119 EXPECT_TRUE(writer.StartArray());
120 EXPECT_TRUE(writer.String(std::string("Hello\n")));
121 EXPECT_TRUE(writer.EndArray());
122 EXPECT_STREQ("[\n \"Hello\\n\"\n]", buffer.GetString());
123}
124#endif
125
126#include <sstream>
127
129public:
130 typedef char Ch;
131
132 OStreamWrapper(std::ostream& os) : os_(os) {}
133
134 Ch Peek() const { assert(false); return '\0'; }
135 Ch Take() { assert(false); return '\0'; }
136 size_t Tell() const { return 0; }
137
138 Ch* PutBegin() { assert(false); return 0; }
139 void Put(Ch c) { os_.put(c); }
140 void Flush() { os_.flush(); }
141 size_t PutEnd(Ch*) { assert(false); return 0; }
142
143private:
145 OStreamWrapper& operator=(const OStreamWrapper&);
146
147 std::ostream& os_;
148};
149
150// For covering PutN() generic version
152 StringStream s(kJson);
153
154 std::stringstream ss;
155 OStreamWrapper os(ss);
156
158
159 Reader reader;
160 reader.Parse(s, writer);
161
162 std::string actual = ss.str();
163 EXPECT_STREQ(kPrettyJson, actual.c_str());
164}
165
166// For covering FileWriteStream::PutN()
168 char filename[L_tmpnam];
169 FILE* fp = TempFile(filename);
170 ASSERT_TRUE(fp!=NULL);
171 char buffer[16];
172 FileWriteStream os(fp, buffer, sizeof(buffer));
174 Reader reader;
175 StringStream s(kJson);
176 reader.Parse(s, writer);
177 fclose(fp);
178
179 fp = fopen(filename, "rb");
180 fseek(fp, 0, SEEK_END);
181 size_t size = static_cast<size_t>(ftell(fp));
182 fseek(fp, 0, SEEK_SET);
183 char* json = static_cast<char*>(malloc(size + 1));
184 size_t readLength = fread(json, 1, size, fp);
185 json[readLength] = '\0';
186 fclose(fp);
187 remove(filename);
188 EXPECT_STREQ(kPrettyJson, json);
189 free(json);
190}
191
192TEST(PrettyWriter, RawValue) {
193 StringBuffer buffer;
194 PrettyWriter<StringBuffer> writer(buffer);
195 writer.StartObject();
196 writer.Key("a");
197 writer.Int(1);
198 writer.Key("raw");
199 const char json[] = "[\"Hello\\nWorld\", 123.456]";
200 writer.RawValue(json, strlen(json), kArrayType);
201 writer.EndObject();
202 EXPECT_TRUE(writer.IsComplete());
204 "{\n"
205 " \"a\": 1,\n"
206 " \"raw\": [\"Hello\\nWorld\", 123.456]\n" // no indentation within raw value
207 "}",
208 buffer.GetString());
209}
210
211TEST(PrettyWriter, InvalidEventSequence) {
212 // {]
213 {
214 StringBuffer buffer;
215 PrettyWriter<StringBuffer> writer(buffer);
216 writer.StartObject();
218 EXPECT_FALSE(writer.IsComplete());
219 }
220
221 // [}
222 {
223 StringBuffer buffer;
224 PrettyWriter<StringBuffer> writer(buffer);
225 writer.StartArray();
227 EXPECT_FALSE(writer.IsComplete());
228 }
229
230 // { 1:
231 {
232 StringBuffer buffer;
233 PrettyWriter<StringBuffer> writer(buffer);
234 writer.StartObject();
235 EXPECT_THROW(writer.Int(1), AssertException);
236 EXPECT_FALSE(writer.IsComplete());
237 }
238
239 // { 'a' }
240 {
241 StringBuffer buffer;
242 PrettyWriter<StringBuffer> writer(buffer);
243 writer.StartObject();
244 writer.Key("a");
246 EXPECT_FALSE(writer.IsComplete());
247 }
248
249 // { 'a':'b','c' }
250 {
251 StringBuffer buffer;
252 PrettyWriter<StringBuffer> writer(buffer);
253 writer.StartObject();
254 writer.Key("a");
255 writer.String("b");
256 writer.Key("c");
258 EXPECT_FALSE(writer.IsComplete());
259 }
260}
261
263 double nan = std::numeric_limits<double>::quiet_NaN();
264
265 EXPECT_TRUE(internal::Double(nan).IsNan());
266 StringBuffer buffer;
267 {
268 PrettyWriter<StringBuffer> writer(buffer);
269 EXPECT_FALSE(writer.Double(nan));
270 }
271 {
273 EXPECT_TRUE(writer.Double(nan));
274 EXPECT_STREQ("NaN", buffer.GetString());
275 }
277 PrettyWriter<GenericStringBuffer<UTF16<> > > writer2(buffer2);
278 EXPECT_FALSE(writer2.Double(nan));
279}
280
282 double inf = std::numeric_limits<double>::infinity();
283
284 EXPECT_TRUE(internal::Double(inf).IsInf());
285 StringBuffer buffer;
286 {
287 PrettyWriter<StringBuffer> writer(buffer);
288 EXPECT_FALSE(writer.Double(inf));
289 }
290 {
291 PrettyWriter<StringBuffer> writer(buffer);
292 EXPECT_FALSE(writer.Double(-inf));
293 }
294 {
296 EXPECT_TRUE(writer.Double(inf));
297 }
298 {
300 EXPECT_TRUE(writer.Double(-inf));
301 }
302 EXPECT_STREQ("Infinity-Infinity", buffer.GetString());
303}
304
305TEST(PrettyWriter, Issue_889) {
306 char buf[100] = "Hello";
307
308 StringBuffer buffer;
309 PrettyWriter<StringBuffer> writer(buffer);
310 writer.StartArray();
311 writer.String(buf);
312 writer.EndArray();
313
314 EXPECT_STREQ("[\n \"Hello\"\n]", buffer.GetString());
315 EXPECT_TRUE(writer.IsComplete()); \
316}
317
318
319#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
320
321static PrettyWriter<StringBuffer> WriterGen(StringBuffer &target) {
322 PrettyWriter<StringBuffer> writer(target);
323 writer.StartObject();
324 writer.Key("a");
325 writer.Int(1);
326 return writer;
327}
328
329TEST(PrettyWriter, MoveCtor) {
330 StringBuffer buffer;
331 PrettyWriter<StringBuffer> writer(WriterGen(buffer));
332 writer.EndObject();
333 EXPECT_TRUE(writer.IsComplete());
335 "{\n"
336 " \"a\": 1\n"
337 "}",
338 buffer.GetString());
339}
340#endif
341
342TEST(PrettyWriter, Issue_1336) {
343#define T(meth, val, expected) \
344 { \
345 StringBuffer buffer; \
346 PrettyWriter<StringBuffer> writer(buffer); \
347 writer.meth(val); \
348 \
349 EXPECT_STREQ(expected, buffer.GetString()); \
350 EXPECT_TRUE(writer.IsComplete()); \
351 }
352
353 T(Bool, false, "false");
354 T(Bool, true, "true");
355 T(Int, 0, "0");
356 T(Uint, 0, "0");
357 T(Int64, 0, "0");
358 T(Uint64, 0, "0");
359 T(Double, 0, "0.0");
360 T(String, "Hello", "\"Hello\"");
361#undef T
362
363 StringBuffer buffer;
364 PrettyWriter<StringBuffer> writer(buffer);
365 writer.Null();
366
367 EXPECT_STREQ("null", buffer.GetString());
368 EXPECT_TRUE(writer.IsComplete());
369}
370
371#ifdef __clang__
372RAPIDJSON_DIAG_POP
373#endif
C-runtime library allocator.
Definition allocators.h:75
Wrapper of C file stream for output using fwrite().
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition reader.h:557
const Ch * GetString() const
size_t Tell() const
OStreamWrapper(std::ostream &os)
Writer with indentation and spacing.
bool EndArray(SizeType memberCount=0)
bool Key(const Ch *str, SizeType length, bool copy=false)
bool StartObject()
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
bool EndObject(SizeType memberCount=0)
bool Int(int i)
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
bool Double(double d)
bool String(const Ch *str, SizeType length, bool copy=false)
bool StartArray()
bool IsComplete() const
Checks whether the output is a complete JSON.
Definition writer.h:143
os_t os
#define EXPECT_THROW(statement, expected_exception)
Definition gtest.h:1879
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define EXPECT_STREQ(s1, s2)
Definition gtest.h:2027
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define ASSERT_TRUE(condition)
Definition gtest.h:1901
#define EXPECT_FALSE(condition)
Definition gtest.h:1898
main RapidJSON namespace
BasicOStreamWrapper< std::ostream > OStreamWrapper
@ kFormatSingleLineArray
Format arrays on a single line.
#define T(meth, val, expected)
@ kArrayType
array
Definition rapidjson.h:649
Read-only string stream.
Definition stream.h:154
UTF-8 encoding.
Definition encodings.h:96
FILE * TempFile(char *filename)
Definition unittest.h:80
@ kWriteNanAndInfFlag
Allow writing of Infinity, -Infinity and NaN.
Definition writer.h:68
char * s
uint8_t buf[2048]