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
22#include <xrpl/json/json_reader.h>
23#include <xrpl/json/json_writer.h>
24
25namespace Json {
26
28{
29 void
30 runTest(std::string const& name, std::string const& valueDesc)
31 {
32 setup(name);
33 Json::Value value;
34 BEAST_EXPECT(Json::Reader().parse(valueDesc, value));
35 auto out = stringOutput(output_);
36 outputJson(value, out);
37
38 // Compare with the original version.
39 auto expected = Json::FastWriter().write(value);
40 expectResult(expected);
41 expectResult(valueDesc);
43 }
44
45 void
46 runTest(std::string const& name)
47 {
48 runTest(name, name);
49 }
50
51 void
52 run() override
53 {
54 runTest("empty dict", "{}");
55 runTest("empty array", "[]");
56 runTest("array", "[23,4.25,true,null,\"string\"]");
57 runTest("dict", "{\"hello\":\"world\"}");
58 runTest("array dict", "[{}]");
59 runTest("array array", "[[]]");
60 runTest("more complex", "{\"array\":[{\"12\":23},{},null,false,0.5]}");
61 }
62};
63
64BEAST_DEFINE_TESTSUITE(Output, ripple_basics, ripple);
65
66} // namespace Json
Outputs a Value in JSON format without formatting (not human friendly).
Definition: json_writer.h:54
std::string write(Value const &root) override
Unserialize a JSON document into a Value.
Definition: json_reader.h:39
Represents a JSON value.
Definition: json_value.h:149
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:25
void runTest(std::string const &name)
Definition: Output_test.cpp:46
void runTest(std::string const &name, std::string const &valueDesc)
Definition: Output_test.cpp:30
void run() override
Runs the suite.
Definition: Output_test.cpp:52