rippled
Loading...
Searching...
No Matches
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 <test/json/TestOutputSuite.h>
21#include <xrpl/json/json_reader.h>
22#include <xrpl/json/json_writer.h>
23
24namespace 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);
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
63BEAST_DEFINE_TESTSUITE(Output, ripple_basics, ripple);
64
65} // namespace Json
Outputs a Value in JSON format without formatting (not human friendly).
Definition: json_writer.h:54
std::string write(const Value &root) override
Unserialize a JSON document into a Value.
Definition: json_reader.h:39
Represents a JSON value.
Definition: json_value.h:148
void expectResult(std::string const &expected, std::string const &message="")
void setup(std::string const &testName)
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
Output stringOutput(std::string &s)
std::string jsonAsString(Json::Value const &)
Return the minimal string representation of a Json::Value in O(n) time.
Definition: Output.cpp:100
void outputJson(Json::Value const &, Output const &)
Writes a minimal representation of a Json value to an Output in O(n) time.
Definition: Output.cpp:93
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
void runTest(std::string const &name)
Definition: Output_test.cpp:45
void runTest(std::string const &name, std::string const &valueDesc)
Definition: Output_test.cpp:29
void run() override
Runs the suite.
Definition: Output_test.cpp:51