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#include <xrpl/beast/unit_test.h>
22#include <xrpl/json/Writer.h>
23
24namespace Json {
25
27{
28public:
29 void
31 {
32 setup("trivial");
33 BEAST_EXPECT(output_.empty());
34 expectResult("");
35 }
36
37 void
39 {
40 setup("near trivial");
41 BEAST_EXPECT(output_.empty());
42 writer_->output(0);
43 expectResult("0");
44 }
45
46 void
48 {
49 setup("true");
50 writer_->output(true);
51 expectResult("true");
52
53 setup("false");
54 writer_->output(false);
55 expectResult("false");
56
57 setup("23");
58 writer_->output(23);
59 expectResult("23");
60
61 setup("23.0");
62 writer_->output(23.0);
63 expectResult("23.0");
64
65 setup("23.5");
66 writer_->output(23.5);
67 expectResult("23.5");
68
69 setup("a string");
70 writer_->output("a string");
71 expectResult("\"a string\"");
72
73 setup("nullptr");
74 writer_->output(nullptr);
75 expectResult("null");
76 }
77
78 void
80 {
81 setup("empty array");
83 writer_->finish();
84 expectResult("[]");
85
86 setup("empty object");
88 writer_->finish();
89 expectResult("{}");
90 }
91
92 void
94 {
95 setup("backslash");
96 writer_->output("\\");
97 expectResult("\"\\\\\"");
98
99 setup("quote");
100 writer_->output("\"");
101 expectResult("\"\\\"\"");
102
103 setup("backslash and quote");
104 writer_->output("\\\"");
105 expectResult("\"\\\\\\\"\"");
106
107 setup("escape embedded");
108 writer_->output("this contains a \\ in the middle of it.");
109 expectResult("\"this contains a \\\\ in the middle of it.\"");
110
111 setup("remaining escapes");
112 writer_->output("\b\f\n\r\t");
113 expectResult("\"\\b\\f\\n\\r\\t\"");
114 }
115
116 void
118 {
119 setup("empty array");
121 writer_->append(12);
122 writer_->finish();
123 expectResult("[12]");
124 }
125
126 void
128 {
129 setup("long array");
131 writer_->append(12);
132 writer_->append(true);
133 writer_->append("hello");
134 writer_->finish();
135 expectResult("[12,true,\"hello\"]");
136 }
137
138 void
140 {
141 setup("embedded array simple");
144 writer_->finish();
145 writer_->finish();
146 expectResult("[[]]");
147 }
148
149 void
151 {
152 setup("object");
154 writer_->set("hello", "world");
155 writer_->finish();
156
157 expectResult("{\"hello\":\"world\"}");
158 }
159
160 void
162 {
163 setup("complex object");
165
166 writer_->set("hello", "world");
167 writer_->startSet(Writer::array, "array");
168
169 writer_->append(true);
170 writer_->append(12);
173 writer_->set("goodbye", "cruel world.");
174 writer_->startSet(Writer::array, "subarray");
175 writer_->append(23.5);
177
179 "{\"hello\":\"world\",\"array\":[true,12,"
180 "[{\"goodbye\":\"cruel world.\","
181 "\"subarray\":[23.5]}]]}");
182 }
183
184 void
186 {
187 setup("object");
189 value["foo"] = 23;
191 writer_->set("hello", value);
192 writer_->finish();
193
194 expectResult("{\"hello\":{\"foo\":23}}");
195 }
196
197 void
198 run() override
199 {
200 testTrivial();
203 testEmpty();
204 testEscaping();
205 testArray();
208 testObject();
210 testJson();
211 }
212};
213
214BEAST_DEFINE_TESTSUITE(JsonWriter, ripple_basics, ripple);
215
216} // namespace Json
void run() override
Runs the suite.
Represents a JSON value.
Definition: json_value.h:148
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.
Definition: json/Writer.h:165
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.
Definition: json/Writer.h:189
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:44
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26