rippled
Loading...
Searching...
No Matches
Object_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/Object.h>
24
25namespace Json {
26
28{
29 void
30 setup(std::string const& testName)
31 {
32 testcase(testName);
33 output_.clear();
34 }
35
37
38 Object&
45
46 void
47 expectResult(std::string const& expected)
48 {
49 writerObject_.reset();
50 TestOutputSuite::expectResult(expected);
51 }
52
53public:
54 void
56 {
57 setup("trivial");
58
59 {
60 auto& root = makeRoot();
61 (void)root;
62 }
63 expectResult("{}");
64 }
65
66 void
68 {
69 setup("simple");
70 {
71 auto& root = makeRoot();
72 root["hello"] = "world";
73 root["skidoo"] = 23;
74 root["awake"] = false;
75 root["temperature"] = 98.6;
76 }
77
79 "{\"hello\":\"world\","
80 "\"skidoo\":23,"
81 "\"awake\":false,"
82 "\"temperature\":98.6}");
83 }
84
85 void
87 {
88 setup("oneSub");
89 {
90 auto& root = makeRoot();
91 root.setArray("ar");
92 }
93 expectResult("{\"ar\":[]}");
94 }
95
96 void
98 {
99 setup("subs");
100 {
101 auto& root = makeRoot();
102
103 {
104 // Add an array with three entries.
105 auto array = root.setArray("ar");
106 array.append(23);
107 array.append(false);
108 array.append(23.5);
109 }
110
111 {
112 // Add an object with one entry.
113 auto obj = root.setObject("obj");
114 obj["hello"] = "world";
115 }
116
117 {
118 // Add another object with two entries.
119 Json::Value value;
120 value["h"] = "w";
121 value["f"] = false;
122 root["obj2"] = value;
123 }
124 }
125
126 // Json::Value has an unstable order...
127 auto case1 =
128 "{\"ar\":[23,false,23.5],"
129 "\"obj\":{\"hello\":\"world\"},"
130 "\"obj2\":{\"h\":\"w\",\"f\":false}}";
131 auto case2 =
132 "{\"ar\":[23,false,23.5],"
133 "\"obj\":{\"hello\":\"world\"},"
134 "\"obj2\":{\"f\":false,\"h\":\"w\"}}";
135 writerObject_.reset();
136 BEAST_EXPECT(output_ == case1 || output_ == case2);
137 }
138
139 void
141 {
142 setup("subsShort");
143
144 {
145 auto& root = makeRoot();
146
147 {
148 // Add an array with three entries.
149 auto array = root.setArray("ar");
150 array.append(23);
151 array.append(false);
152 array.append(23.5);
153 }
154
155 // Add an object with one entry.
156 root.setObject("obj")["hello"] = "world";
157
158 {
159 // Add another object with two entries.
160 auto object = root.setObject("obj2");
161 object.set("h", "w");
162 object.set("f", false);
163 }
164 }
166 "{\"ar\":[23,false,23.5],"
167 "\"obj\":{\"hello\":\"world\"},"
168 "\"obj2\":{\"h\":\"w\",\"f\":false}}");
169 }
170
171 void
173 {
174 {
175 setup("object failure assign");
176 auto& root = makeRoot();
177 auto obj = root.setObject("o1");
178 expectException([&]() { root["fail"] = "complete"; });
179 }
180 {
181 setup("object failure object");
182 auto& root = makeRoot();
183 auto obj = root.setObject("o1");
184 expectException([&]() { root.setObject("o2"); });
185 }
186 {
187 setup("object failure Array");
188 auto& root = makeRoot();
189 auto obj = root.setArray("o1");
190 expectException([&]() { root.setArray("o2"); });
191 }
192 }
193
194 void
196 {
197 {
198 setup("array failure append");
199 auto& root = makeRoot();
200 auto array = root.setArray("array");
201 auto subarray = array.appendArray();
202 auto fail = [&]() { array.append("fail"); };
204 }
205 {
206 setup("array failure appendArray");
207 auto& root = makeRoot();
208 auto array = root.setArray("array");
209 auto subarray = array.appendArray();
210 auto fail = [&]() { array.appendArray(); };
212 }
213 {
214 setup("array failure appendObject");
215 auto& root = makeRoot();
216 auto array = root.setArray("array");
217 auto subarray = array.appendArray();
218 auto fail = [&]() { array.appendObject(); };
220 }
221 }
222
223 void
225 {
226 setup("repeating keys");
227 auto& root = makeRoot();
228 root.set("foo", "bar");
229 root.set("baz", 0);
230 // setting key again throws in !NDEBUG builds
231 auto set_again = [&]() { root.set("foo", "bar"); };
232#ifdef NDEBUG
233 set_again();
234 pass();
235#else
236 expectException(set_again);
237#endif
238 }
239
240 void
241 run() override
242 {
243 testTrivial();
244 testSimple();
245
246 testOneSub();
247 testSubs();
249
253 }
254};
255
256BEAST_DEFINE_TESTSUITE(JsonObject, json, ripple);
257
258} // namespace Json
void setup(std::string const &testName)
void expectResult(std::string const &expected)
std::unique_ptr< WriterObject > writerObject_
void run() override
Runs the suite.
Represents a JSON object being written to a Writer.
Definition Object.h:180
Represents a JSON value.
Definition json_value.h:149
void pass()
Record a successful test condition.
Definition suite.h:511
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:533
bool expectException(Functor f, std::string const &message="")
Definition TestSuite.h:100
T clear(T... args)
T is_same_v
JSON (JavaScript Object Notation).
Definition json_errors.h:25
WriterObject stringWriterObject(std::string &)
Definition Object.cpp:247
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25