rippled
Output_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/json_reader.h>
21 #include <ripple/json/json_writer.h>
22 #include <test/json/TestOutputSuite.h>
23 
24 namespace Json {
25 
27 {
28  void
29  runTest(std::string const& name, std::string const& valueDesc)
30  {
31  setup(name);
32  Json::Value value;
33  BEAST_EXPECT(Json::Reader().parse(valueDesc, value));
34  auto out = stringOutput(output_);
35  outputJson(value, out);
36 
37  // Compare with the original version.
38  auto expected = Json::FastWriter().write(value);
39  expectResult(expected);
40  expectResult(valueDesc);
41  expectResult(jsonAsString(value));
42  }
43 
44  void
45  runTest(std::string const& name)
46  {
47  runTest(name, name);
48  }
49 
50  void
51  run() override
52  {
53  runTest("empty dict", "{}");
54  runTest("empty array", "[]");
55  runTest("array", "[23,4.25,true,null,\"string\"]");
56  runTest("dict", "{\"hello\":\"world\"}");
57  runTest("array dict", "[{}]");
58  runTest("array array", "[[]]");
59  runTest("more complex", "{\"array\":[{\"12\":23},{},null,false,0.5]}");
60  }
61 };
62 
63 BEAST_DEFINE_TESTSUITE(Output, ripple_basics, ripple);
64 
65 } // namespace Json
ripple::test::TestOutputSuite::setup
void setup(std::string const &testName)
Definition: TestOutputSuite.h:37
std::string
STL class.
Json::Output_test::runTest
void runTest(std::string const &name, std::string const &valueDesc)
Definition: Output_test.cpp:29
Json::jsonAsString
std::string jsonAsString(Json::Value const &value)
Return the minimal string representation of a Json::Value in O(n) time.
Definition: Output.cpp:97
Json::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(JsonObject, ripple_basics, ripple)
Json::Output_test
Definition: Output_test.cpp:26
Json::Reader
Unserialize a JSON document into a Value.
Definition: json_reader.h:36
Json::outputJson
void outputJson(Json::Value const &value, Output const &out)
Writes a minimal representation of a Json value to an Output in O(n) time.
Definition: Output.cpp:90
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
ripple::test::TestOutputSuite
Definition: TestOutputSuite.h:30
ripple::test::TestOutputSuite::output_
std::string output_
Definition: TestOutputSuite.h:33
Json::Output_test::runTest
void runTest(std::string const &name)
Definition: Output_test.cpp:45
Json::FastWriter::write
std::string write(const Value &root) override
Definition: json_writer.cpp:193
Json::Output_test::run
void run() override
Definition: Output_test.cpp:51
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::TestOutputSuite::expectResult
void expectResult(std::string const &expected, std::string const &message="")
Definition: TestOutputSuite.h:46
Json::FastWriter
Outputs a Value in JSON format without formatting (not human friendly).
Definition: json_writer.h:52
Json::stringOutput
Output stringOutput(std::string &s)
Definition: json/Output.h:33
Json::Output
std::function< void(boost::beast::string_view const &)> Output
Definition: json/Output.h:30
Json::Value
Represents a JSON value.
Definition: json_value.h:145