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