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