Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
prettywritertest.cpp File Reference
#include "unittest.h"
#include "rapidjson/reader.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filewritestream.h"
#include <sstream>
Include dependency graph for prettywritertest.cpp:

Go to the source code of this file.

Classes

class  OStreamWrapper
 

Macros

#define T(meth, val, expected)
 

Functions

 TEST (PrettyWriter, Basic)
 
 TEST (PrettyWriter, FormatOptions)
 
 TEST (PrettyWriter, SetIndent)
 
 TEST (PrettyWriter, String)
 
 TEST (PrettyWriter, OStreamWrapper)
 
 TEST (PrettyWriter, FileWriteStream)
 
 TEST (PrettyWriter, RawValue)
 
 TEST (PrettyWriter, InvalidEventSequence)
 
 TEST (PrettyWriter, NaN)
 
 TEST (PrettyWriter, Inf)
 
 TEST (PrettyWriter, Issue_889)
 
 TEST (PrettyWriter, Issue_1336)
 

Macro Definition Documentation

◆ T

#define T ( meth,
val,
expected )
Value:
{ \
StringBuffer buffer; \
PrettyWriter<StringBuffer> writer(buffer); \
writer.meth(val); \
\
EXPECT_STREQ(expected, buffer.GetString()); \
EXPECT_TRUE(writer.IsComplete()); \
}
const Ch * GetString() const
Writer with indentation and spacing.

Function Documentation

◆ TEST() [1/12]

TEST ( PrettyWriter ,
Basic  )

Definition at line 60 of file prettywritertest.cpp.

60 {
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}
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition reader.h:557
#define EXPECT_STREQ(s1, s2)
Definition gtest.h:2027
Read-only string stream.
Definition stream.h:154
char * s
Here is the call graph for this function:

◆ TEST() [2/12]

Definition at line 167 of file prettywritertest.cpp.

167 {
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}
Wrapper of C file stream for output using fwrite().
os_t os
#define ASSERT_TRUE(condition)
Definition gtest.h:1901
bool remove(const path &p)
FILE * TempFile(char *filename)
Definition unittest.h:80
Here is the call graph for this function:

◆ TEST() [3/12]

TEST ( PrettyWriter ,
FormatOptions  )

Definition at line 69 of file prettywritertest.cpp.

69 {
70 StringBuffer buffer;
71 PrettyWriter<StringBuffer> writer(buffer);
72 writer.SetFormatOptions(kFormatSingleLineArray);
73 Reader reader;
74 StringStream s(kJson);
75 reader.Parse(s, writer);
76 EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString());
77}
@ kFormatSingleLineArray
Format arrays on a single line.
Here is the call graph for this function:

◆ TEST() [4/12]

TEST ( PrettyWriter ,
Inf  )

Definition at line 281 of file prettywritertest.cpp.

281 {
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}
C-runtime library allocator.
Definition allocators.h:75
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define EXPECT_FALSE(condition)
Definition gtest.h:1898
UTF-8 encoding.
Definition encodings.h:96
@ kWriteNanAndInfFlag
Allow writing of Infinity, -Infinity and NaN.
Definition writer.h:68
Here is the call graph for this function:

◆ TEST() [5/12]

TEST ( PrettyWriter ,
InvalidEventSequence  )

Definition at line 211 of file prettywritertest.cpp.

211 {
212 // {]
213 {
214 StringBuffer buffer;
215 PrettyWriter<StringBuffer> writer(buffer);
216 writer.StartObject();
217 EXPECT_THROW(writer.EndArray(), AssertException);
218 EXPECT_FALSE(writer.IsComplete());
219 }
220
221 // [}
222 {
223 StringBuffer buffer;
224 PrettyWriter<StringBuffer> writer(buffer);
225 writer.StartArray();
226 EXPECT_THROW(writer.EndObject(), AssertException);
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");
245 EXPECT_THROW(writer.EndObject(), AssertException);
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");
257 EXPECT_THROW(writer.EndObject(), AssertException);
258 EXPECT_FALSE(writer.IsComplete());
259 }
260}
#define EXPECT_THROW(statement, expected_exception)
Definition gtest.h:1879
Here is the call graph for this function:

◆ TEST() [6/12]

TEST ( PrettyWriter ,
Issue_1336  )

Definition at line 342 of file prettywritertest.cpp.

342 {
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}
#define T(meth, val, expected)
Here is the call graph for this function:

◆ TEST() [7/12]

TEST ( PrettyWriter ,
Issue_889  )

Definition at line 305 of file prettywritertest.cpp.

305 {
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}
uint8_t buf[2048]
Here is the call graph for this function:

◆ TEST() [8/12]

TEST ( PrettyWriter ,
NaN  )

Definition at line 262 of file prettywritertest.cpp.

262 {
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}
Here is the call graph for this function:

◆ TEST() [9/12]

Definition at line 151 of file prettywritertest.cpp.

151 {
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}
static const Segment ss(Segment::ss)
Here is the call graph for this function:

◆ TEST() [10/12]

TEST ( PrettyWriter ,
RawValue  )

Definition at line 192 of file prettywritertest.cpp.

192 {
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}
@ kArrayType
array
Definition rapidjson.h:649
Here is the call graph for this function:

◆ TEST() [11/12]

TEST ( PrettyWriter ,
SetIndent  )

Definition at line 79 of file prettywritertest.cpp.

79 {
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}
Here is the call graph for this function:

◆ TEST() [12/12]

TEST ( PrettyWriter ,
String  )

Definition at line 106 of file prettywritertest.cpp.

106 {
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}
Here is the call graph for this function: