rippled
Loading...
Searching...
No Matches
Writer_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/json/TestOutputSuite.h>
21
22#include <xrpl/beast/unit_test.h>
23#include <xrpl/json/Writer.h>
24
25namespace Json {
26
28{
29public:
30 void
32 {
33 setup("trivial");
34 BEAST_EXPECT(output_.empty());
35 expectResult("");
36 }
37
38 void
40 {
41 setup("near trivial");
42 BEAST_EXPECT(output_.empty());
43 writer_->output(0);
44 expectResult("0");
45 }
46
47 void
49 {
50 setup("true");
51 writer_->output(true);
52 expectResult("true");
53
54 setup("false");
55 writer_->output(false);
56 expectResult("false");
57
58 setup("23");
59 writer_->output(23);
60 expectResult("23");
61
62 setup("23.0");
63 writer_->output(23.0);
64 expectResult("23.0");
65
66 setup("23.5");
67 writer_->output(23.5);
68 expectResult("23.5");
69
70 setup("a string");
71 writer_->output("a string");
72 expectResult("\"a string\"");
73
74 setup("nullptr");
75 writer_->output(nullptr);
76 expectResult("null");
77 }
78
79 void
81 {
82 setup("empty array");
84 writer_->finish();
85 expectResult("[]");
86
87 setup("empty object");
89 writer_->finish();
90 expectResult("{}");
91 }
92
93 void
95 {
96 setup("backslash");
97 writer_->output("\\");
98 expectResult("\"\\\\\"");
99
100 setup("quote");
101 writer_->output("\"");
102 expectResult("\"\\\"\"");
103
104 setup("backslash and quote");
105 writer_->output("\\\"");
106 expectResult("\"\\\\\\\"\"");
107
108 setup("escape embedded");
109 writer_->output("this contains a \\ in the middle of it.");
110 expectResult("\"this contains a \\\\ in the middle of it.\"");
111
112 setup("remaining escapes");
113 writer_->output("\b\f\n\r\t");
114 expectResult("\"\\b\\f\\n\\r\\t\"");
115 }
116
117 void
119 {
120 setup("empty array");
122 writer_->append(12);
123 writer_->finish();
124 expectResult("[12]");
125 }
126
127 void
129 {
130 setup("long array");
132 writer_->append(12);
133 writer_->append(true);
134 writer_->append("hello");
135 writer_->finish();
136 expectResult("[12,true,\"hello\"]");
137 }
138
139 void
141 {
142 setup("embedded array simple");
145 writer_->finish();
146 writer_->finish();
147 expectResult("[[]]");
148 }
149
150 void
152 {
153 setup("object");
155 writer_->set("hello", "world");
156 writer_->finish();
157
158 expectResult("{\"hello\":\"world\"}");
159 }
160
161 void
163 {
164 setup("complex object");
166
167 writer_->set("hello", "world");
168 writer_->startSet(Writer::array, "array");
169
170 writer_->append(true);
171 writer_->append(12);
174 writer_->set("goodbye", "cruel world.");
175 writer_->startSet(Writer::array, "subarray");
176 writer_->append(23.5);
178
180 "{\"hello\":\"world\",\"array\":[true,12,"
181 "[{\"goodbye\":\"cruel world.\","
182 "\"subarray\":[23.5]}]]}");
183 }
184
185 void
187 {
188 setup("object");
190 value["foo"] = 23;
192 writer_->set("hello", value);
193 writer_->finish();
194
195 expectResult("{\"hello\":{\"foo\":23}}");
196 }
197
198 void
199 run() override
200 {
201 testTrivial();
204 testEmpty();
205 testEscaping();
206 testArray();
209 testObject();
211 testJson();
212 }
213};
214
215BEAST_DEFINE_TESTSUITE(JsonWriter, json, ripple);
216
217} // namespace Json
void run() override
Runs the suite.
Represents a JSON value.
Definition json_value.h:149
void finish()
Finish the collection most recently started.
Definition Writer.cpp:358
void output(std::string const &)
Definition Writer.cpp:270
void startRoot(CollectionType)
Start a new collection at the root level.
Definition Writer.cpp:337
void append(Scalar t)
Append a value to an array.
void finishAll()
Finish all objects and arrays.
Definition Writer.cpp:315
void startAppend(CollectionType)
Start a new collection inside an array.
Definition Writer.cpp:343
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition Writer.cpp:350
void expectResult(std::string const &expected, std::string const &message="")
std::unique_ptr< Json::Writer > writer_
void setup(std::string const &testName)
T empty(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:25
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25