rippled
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 <ripple/beast/unit_test.h>
21 #include <ripple/json/Object.h>
22 #include <test/json/TestOutputSuite.h>
23 
24 namespace 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 
52 public:
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  }
164  expectResult(
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"); };
202  expectException(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(); };
210  expectException(fail);
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(); };
218  expectException(fail);
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();
247  testSubsShort();
248 
251  testKeyFailure();
252  }
253 };
254 
255 BEAST_DEFINE_TESTSUITE(JsonObject, ripple_basics, ripple);
256 
257 } // namespace Json
Json::JsonObject_test::testSimple
void testSimple()
Definition: Object_test.cpp:66
std::string
STL class.
Json::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(JsonObject, ripple_basics, ripple)
Json::JsonObject_test::expectResult
void expectResult(std::string const &expected)
Definition: Object_test.cpp:46
Json::JsonObject_test::testSubs
void testSubs()
Definition: Object_test.cpp:96
Json::stringWriterObject
WriterObject stringWriterObject(std::string &s)
Definition: Object.cpp:241
std::string::clear
T clear(T... args)
Json::JsonObject_test::setup
void setup(std::string const &testName)
Definition: Object_test.cpp:29
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
ripple::test::TestOutputSuite
Definition: TestOutputSuite.h:30
Json::JsonObject_test::testKeyFailure
void testKeyFailure()
Definition: Object_test.cpp:223
ripple::test::TestOutputSuite::output_
std::string output_
Definition: TestOutputSuite.h:33
Json::JsonObject_test::testTrivial
void testTrivial()
Definition: Object_test.cpp:54
Json::JsonObject_test::testOneSub
void testOneSub()
Definition: Object_test.cpp:85
Json::JsonObject_test::testFailureObject
void testFailureObject()
Definition: Object_test.cpp:171
Json::JsonObject_test::run
void run() override
Definition: Object_test.cpp:240
Json::JsonObject_test::testSubsShort
void testSubsShort()
Definition: Object_test.cpp:139
Json::Object
Represents a JSON object being written to a Writer.
Definition: Object.h:178
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
Json::JsonObject_test::testFailureArray
void testFailureArray()
Definition: Object_test.cpp:194
Json::JsonObject_test
Definition: Object_test.cpp:26
Json::JsonObject_test::writerObject_
std::unique_ptr< WriterObject > writerObject_
Definition: Object_test.cpp:35
ripple::TestSuite::expectException
bool expectException(Functor f, std::string const &message="")
Definition: TestSuite.h:99
std::unique_ptr
STL class.
Json::JsonObject_test::makeRoot
Object & makeRoot()
Definition: Object_test.cpp:38
Json::Value
Represents a JSON value.
Definition: json_value.h:145