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#include <xrpl/beast/unit_test.h>
22#include <xrpl/json/Object.h>
23
24namespace Json {
25
27{
28 void
29 setup(std::string const& testName)
30 {
31 testcase(testName);
32 output_.clear();
33 }
34
36
37 Object&
39 {
41 std::make_unique<WriterObject>(stringWriterObject(output_));
42 return **writerObject_;
43 }
44
45 void
46 expectResult(std::string const& expected)
47 {
48 writerObject_.reset();
49 TestOutputSuite::expectResult(expected);
50 }
51
52public:
53 void
55 {
56 setup("trivial");
57
58 {
59 auto& root = makeRoot();
60 (void)root;
61 }
62 expectResult("{}");
63 }
64
65 void
67 {
68 setup("simple");
69 {
70 auto& root = makeRoot();
71 root["hello"] = "world";
72 root["skidoo"] = 23;
73 root["awake"] = false;
74 root["temperature"] = 98.6;
75 }
76
78 "{\"hello\":\"world\","
79 "\"skidoo\":23,"
80 "\"awake\":false,"
81 "\"temperature\":98.6}");
82 }
83
84 void
86 {
87 setup("oneSub");
88 {
89 auto& root = makeRoot();
90 root.setArray("ar");
91 }
92 expectResult("{\"ar\":[]}");
93 }
94
95 void
97 {
98 setup("subs");
99 {
100 auto& root = makeRoot();
101
102 {
103 // Add an array with three entries.
104 auto array = root.setArray("ar");
105 array.append(23);
106 array.append(false);
107 array.append(23.5);
108 }
109
110 {
111 // Add an object with one entry.
112 auto obj = root.setObject("obj");
113 obj["hello"] = "world";
114 }
115
116 {
117 // Add another object with two entries.
118 Json::Value value;
119 value["h"] = "w";
120 value["f"] = false;
121 root["obj2"] = value;
122 }
123 }
124
125 // Json::Value has an unstable order...
126 auto case1 =
127 "{\"ar\":[23,false,23.5],"
128 "\"obj\":{\"hello\":\"world\"},"
129 "\"obj2\":{\"h\":\"w\",\"f\":false}}";
130 auto case2 =
131 "{\"ar\":[23,false,23.5],"
132 "\"obj\":{\"hello\":\"world\"},"
133 "\"obj2\":{\"f\":false,\"h\":\"w\"}}";
134 writerObject_.reset();
135 BEAST_EXPECT(output_ == case1 || output_ == case2);
136 }
137
138 void
140 {
141 setup("subsShort");
142
143 {
144 auto& root = makeRoot();
145
146 {
147 // Add an array with three entries.
148 auto array = root.setArray("ar");
149 array.append(23);
150 array.append(false);
151 array.append(23.5);
152 }
153
154 // Add an object with one entry.
155 root.setObject("obj")["hello"] = "world";
156
157 {
158 // Add another object with two entries.
159 auto object = root.setObject("obj2");
160 object.set("h", "w");
161 object.set("f", false);
162 }
163 }
165 "{\"ar\":[23,false,23.5],"
166 "\"obj\":{\"hello\":\"world\"},"
167 "\"obj2\":{\"h\":\"w\",\"f\":false}}");
168 }
169
170 void
172 {
173 {
174 setup("object failure assign");
175 auto& root = makeRoot();
176 auto obj = root.setObject("o1");
177 expectException([&]() { root["fail"] = "complete"; });
178 }
179 {
180 setup("object failure object");
181 auto& root = makeRoot();
182 auto obj = root.setObject("o1");
183 expectException([&]() { root.setObject("o2"); });
184 }
185 {
186 setup("object failure Array");
187 auto& root = makeRoot();
188 auto obj = root.setArray("o1");
189 expectException([&]() { root.setArray("o2"); });
190 }
191 }
192
193 void
195 {
196 {
197 setup("array failure append");
198 auto& root = makeRoot();
199 auto array = root.setArray("array");
200 auto subarray = array.appendArray();
201 auto fail = [&]() { array.append("fail"); };
203 }
204 {
205 setup("array failure appendArray");
206 auto& root = makeRoot();
207 auto array = root.setArray("array");
208 auto subarray = array.appendArray();
209 auto fail = [&]() { array.appendArray(); };
211 }
212 {
213 setup("array failure appendObject");
214 auto& root = makeRoot();
215 auto array = root.setArray("array");
216 auto subarray = array.appendArray();
217 auto fail = [&]() { array.appendObject(); };
219 }
220 }
221
222 void
224 {
225 setup("repeating keys");
226 auto& root = makeRoot();
227 root.set("foo", "bar");
228 root.set("baz", 0);
229 // setting key again throws in !NDEBUG builds
230 auto set_again = [&]() { root.set("foo", "bar"); };
231#ifdef NDEBUG
232 set_again();
233 pass();
234#else
235 expectException(set_again);
236#endif
237 }
238
239 void
240 run() override
241 {
242 testTrivial();
243 testSimple();
244
245 testOneSub();
246 testSubs();
248
252 }
253};
254
255BEAST_DEFINE_TESTSUITE(JsonObject, ripple_basics, ripple);
256
257} // namespace Json
void setup(std::string const &testName)
Definition: Object_test.cpp:29
void expectResult(std::string const &expected)
Definition: Object_test.cpp:46
std::unique_ptr< WriterObject > writerObject_
Definition: Object_test.cpp:35
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:148
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)
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:26