mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-04 19:25:51 +00:00
refactor: Migrate json unit tests to use doctest (#5533)
Co-authored-by: Bart Thomee <11445373+bthomee@users.noreply.github.com>
This commit is contained in:
@@ -138,6 +138,7 @@ test.toplevel > test.csf
|
||||
test.toplevel > xrpl.json
|
||||
test.unit_test > xrpl.basics
|
||||
tests.libxrpl > xrpl.basics
|
||||
tests.libxrpl > xrpl.json
|
||||
tests.libxrpl > xrpl.net
|
||||
xrpl.json > xrpl.basics
|
||||
xrpl.ledger > xrpl.basics
|
||||
|
||||
@@ -1,217 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <test/json/TestOutputSuite.h>
|
||||
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
#include <xrpl/json/Writer.h>
|
||||
|
||||
namespace Json {
|
||||
|
||||
class JsonWriter_test : public ripple::test::TestOutputSuite
|
||||
{
|
||||
public:
|
||||
void
|
||||
testTrivial()
|
||||
{
|
||||
setup("trivial");
|
||||
BEAST_EXPECT(output_.empty());
|
||||
expectResult("");
|
||||
}
|
||||
|
||||
void
|
||||
testNearTrivial()
|
||||
{
|
||||
setup("near trivial");
|
||||
BEAST_EXPECT(output_.empty());
|
||||
writer_->output(0);
|
||||
expectResult("0");
|
||||
}
|
||||
|
||||
void
|
||||
testPrimitives()
|
||||
{
|
||||
setup("true");
|
||||
writer_->output(true);
|
||||
expectResult("true");
|
||||
|
||||
setup("false");
|
||||
writer_->output(false);
|
||||
expectResult("false");
|
||||
|
||||
setup("23");
|
||||
writer_->output(23);
|
||||
expectResult("23");
|
||||
|
||||
setup("23.0");
|
||||
writer_->output(23.0);
|
||||
expectResult("23.0");
|
||||
|
||||
setup("23.5");
|
||||
writer_->output(23.5);
|
||||
expectResult("23.5");
|
||||
|
||||
setup("a string");
|
||||
writer_->output("a string");
|
||||
expectResult("\"a string\"");
|
||||
|
||||
setup("nullptr");
|
||||
writer_->output(nullptr);
|
||||
expectResult("null");
|
||||
}
|
||||
|
||||
void
|
||||
testEmpty()
|
||||
{
|
||||
setup("empty array");
|
||||
writer_->startRoot(Writer::array);
|
||||
writer_->finish();
|
||||
expectResult("[]");
|
||||
|
||||
setup("empty object");
|
||||
writer_->startRoot(Writer::object);
|
||||
writer_->finish();
|
||||
expectResult("{}");
|
||||
}
|
||||
|
||||
void
|
||||
testEscaping()
|
||||
{
|
||||
setup("backslash");
|
||||
writer_->output("\\");
|
||||
expectResult("\"\\\\\"");
|
||||
|
||||
setup("quote");
|
||||
writer_->output("\"");
|
||||
expectResult("\"\\\"\"");
|
||||
|
||||
setup("backslash and quote");
|
||||
writer_->output("\\\"");
|
||||
expectResult("\"\\\\\\\"\"");
|
||||
|
||||
setup("escape embedded");
|
||||
writer_->output("this contains a \\ in the middle of it.");
|
||||
expectResult("\"this contains a \\\\ in the middle of it.\"");
|
||||
|
||||
setup("remaining escapes");
|
||||
writer_->output("\b\f\n\r\t");
|
||||
expectResult("\"\\b\\f\\n\\r\\t\"");
|
||||
}
|
||||
|
||||
void
|
||||
testArray()
|
||||
{
|
||||
setup("empty array");
|
||||
writer_->startRoot(Writer::array);
|
||||
writer_->append(12);
|
||||
writer_->finish();
|
||||
expectResult("[12]");
|
||||
}
|
||||
|
||||
void
|
||||
testLongArray()
|
||||
{
|
||||
setup("long array");
|
||||
writer_->startRoot(Writer::array);
|
||||
writer_->append(12);
|
||||
writer_->append(true);
|
||||
writer_->append("hello");
|
||||
writer_->finish();
|
||||
expectResult("[12,true,\"hello\"]");
|
||||
}
|
||||
|
||||
void
|
||||
testEmbeddedArraySimple()
|
||||
{
|
||||
setup("embedded array simple");
|
||||
writer_->startRoot(Writer::array);
|
||||
writer_->startAppend(Writer::array);
|
||||
writer_->finish();
|
||||
writer_->finish();
|
||||
expectResult("[[]]");
|
||||
}
|
||||
|
||||
void
|
||||
testObject()
|
||||
{
|
||||
setup("object");
|
||||
writer_->startRoot(Writer::object);
|
||||
writer_->set("hello", "world");
|
||||
writer_->finish();
|
||||
|
||||
expectResult("{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
void
|
||||
testComplexObject()
|
||||
{
|
||||
setup("complex object");
|
||||
writer_->startRoot(Writer::object);
|
||||
|
||||
writer_->set("hello", "world");
|
||||
writer_->startSet(Writer::array, "array");
|
||||
|
||||
writer_->append(true);
|
||||
writer_->append(12);
|
||||
writer_->startAppend(Writer::array);
|
||||
writer_->startAppend(Writer::object);
|
||||
writer_->set("goodbye", "cruel world.");
|
||||
writer_->startSet(Writer::array, "subarray");
|
||||
writer_->append(23.5);
|
||||
writer_->finishAll();
|
||||
|
||||
expectResult(
|
||||
"{\"hello\":\"world\",\"array\":[true,12,"
|
||||
"[{\"goodbye\":\"cruel world.\","
|
||||
"\"subarray\":[23.5]}]]}");
|
||||
}
|
||||
|
||||
void
|
||||
testJson()
|
||||
{
|
||||
setup("object");
|
||||
Json::Value value(Json::objectValue);
|
||||
value["foo"] = 23;
|
||||
writer_->startRoot(Writer::object);
|
||||
writer_->set("hello", value);
|
||||
writer_->finish();
|
||||
|
||||
expectResult("{\"hello\":{\"foo\":23}}");
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testTrivial();
|
||||
testNearTrivial();
|
||||
testPrimitives();
|
||||
testEmpty();
|
||||
testEscaping();
|
||||
testArray();
|
||||
testLongArray();
|
||||
testEmbeddedArraySimple();
|
||||
testObject();
|
||||
testComplexObject();
|
||||
testJson();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(JsonWriter, json, ripple);
|
||||
|
||||
} // namespace Json
|
||||
File diff suppressed because it is too large
Load Diff
@@ -12,5 +12,7 @@ xrpl_add_test(basics)
|
||||
target_link_libraries(xrpl.test.basics PRIVATE xrpl.imports.test)
|
||||
xrpl_add_test(crypto)
|
||||
target_link_libraries(xrpl.test.crypto PRIVATE xrpl.imports.test)
|
||||
xrpl_add_test(json)
|
||||
target_link_libraries(xrpl.test.json PRIVATE xrpl.imports.test)
|
||||
xrpl_add_test(net)
|
||||
target_link_libraries(xrpl.test.net PRIVATE xrpl.imports.test)
|
||||
|
||||
@@ -17,50 +17,43 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <test/json/TestOutputSuite.h>
|
||||
|
||||
#include <xrpl/json/Output.h>
|
||||
#include <xrpl/json/json_reader.h>
|
||||
#include <xrpl/json/json_writer.h>
|
||||
|
||||
namespace Json {
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
struct Output_test : ripple::test::TestOutputSuite
|
||||
#include <string>
|
||||
|
||||
using namespace ripple;
|
||||
using namespace Json;
|
||||
|
||||
TEST_SUITE_BEGIN("JsonOutput");
|
||||
|
||||
static void
|
||||
checkOutput(std::string const& valueDesc)
|
||||
{
|
||||
void
|
||||
runTest(std::string const& name, std::string const& valueDesc)
|
||||
{
|
||||
setup(name);
|
||||
Json::Value value;
|
||||
BEAST_EXPECT(Json::Reader().parse(valueDesc, value));
|
||||
auto out = stringOutput(output_);
|
||||
outputJson(value, out);
|
||||
std::string output;
|
||||
Json::Value value;
|
||||
REQUIRE(Json::Reader().parse(valueDesc, value));
|
||||
auto out = stringOutput(output);
|
||||
outputJson(value, out);
|
||||
|
||||
// Compare with the original version.
|
||||
auto expected = Json::FastWriter().write(value);
|
||||
expectResult(expected);
|
||||
expectResult(valueDesc);
|
||||
expectResult(jsonAsString(value));
|
||||
}
|
||||
auto expected = Json::FastWriter().write(value);
|
||||
CHECK(output == expected);
|
||||
CHECK(output == valueDesc);
|
||||
CHECK(output == jsonAsString(value));
|
||||
}
|
||||
|
||||
void
|
||||
runTest(std::string const& name)
|
||||
{
|
||||
runTest(name, name);
|
||||
}
|
||||
TEST_CASE("output cases")
|
||||
{
|
||||
checkOutput("{}");
|
||||
checkOutput("[]");
|
||||
checkOutput(R"([23,4.25,true,null,"string"])");
|
||||
checkOutput(R"({"hello":"world"})");
|
||||
checkOutput("[{}]");
|
||||
checkOutput("[[]]");
|
||||
checkOutput(R"({"array":[{"12":23},{},null,false,0.5]})");
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
runTest("empty dict", "{}");
|
||||
runTest("empty array", "[]");
|
||||
runTest("array", "[23,4.25,true,null,\"string\"]");
|
||||
runTest("dict", "{\"hello\":\"world\"}");
|
||||
runTest("array dict", "[{}]");
|
||||
runTest("array array", "[[]]");
|
||||
runTest("more complex", "{\"array\":[{\"12\":23},{},null,false,0.5]}");
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(Output, json, ripple);
|
||||
|
||||
} // namespace Json
|
||||
TEST_SUITE_END();
|
||||
1294
src/tests/libxrpl/json/Value.cpp
Normal file
1294
src/tests/libxrpl/json/Value.cpp
Normal file
File diff suppressed because it is too large
Load Diff
192
src/tests/libxrpl/json/Writer.cpp
Normal file
192
src/tests/libxrpl/json/Writer.cpp
Normal file
@@ -0,0 +1,192 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <xrpl/json/Writer.h>
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
#include <google/protobuf/stubs/port.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
using namespace ripple;
|
||||
using namespace Json;
|
||||
|
||||
TEST_SUITE_BEGIN("JsonWriter");
|
||||
|
||||
struct WriterFixture
|
||||
{
|
||||
std::string output;
|
||||
std::unique_ptr<Writer> writer;
|
||||
|
||||
WriterFixture()
|
||||
{
|
||||
writer = std::make_unique<Writer>(stringOutput(output));
|
||||
}
|
||||
|
||||
void
|
||||
reset()
|
||||
{
|
||||
output.clear();
|
||||
writer = std::make_unique<Writer>(stringOutput(output));
|
||||
}
|
||||
|
||||
void
|
||||
expectOutput(std::string const& expected) const
|
||||
{
|
||||
CHECK(output == expected);
|
||||
}
|
||||
|
||||
void
|
||||
checkOutputAndReset(std::string const& expected)
|
||||
{
|
||||
expectOutput(expected);
|
||||
reset();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "trivial")
|
||||
{
|
||||
CHECK(output.empty());
|
||||
checkOutputAndReset("");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "near trivial")
|
||||
{
|
||||
CHECK(output.empty());
|
||||
writer->output(0);
|
||||
checkOutputAndReset("0");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "primitives")
|
||||
{
|
||||
writer->output(true);
|
||||
checkOutputAndReset("true");
|
||||
|
||||
writer->output(false);
|
||||
checkOutputAndReset("false");
|
||||
|
||||
writer->output(23);
|
||||
checkOutputAndReset("23");
|
||||
|
||||
writer->output(23.0);
|
||||
checkOutputAndReset("23.0");
|
||||
|
||||
writer->output(23.5);
|
||||
checkOutputAndReset("23.5");
|
||||
|
||||
writer->output("a string");
|
||||
checkOutputAndReset(R"("a string")");
|
||||
|
||||
writer->output(nullptr);
|
||||
checkOutputAndReset("null");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "empty")
|
||||
{
|
||||
writer->startRoot(Writer::array);
|
||||
writer->finish();
|
||||
checkOutputAndReset("[]");
|
||||
|
||||
writer->startRoot(Writer::object);
|
||||
writer->finish();
|
||||
checkOutputAndReset("{}");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "escaping")
|
||||
{
|
||||
writer->output("\\");
|
||||
checkOutputAndReset(R"("\\")");
|
||||
|
||||
writer->output("\"");
|
||||
checkOutputAndReset(R"("\"")");
|
||||
|
||||
writer->output("\\\"");
|
||||
checkOutputAndReset(R"("\\\"")");
|
||||
|
||||
writer->output("this contains a \\ in the middle of it.");
|
||||
checkOutputAndReset(R"("this contains a \\ in the middle of it.")");
|
||||
|
||||
writer->output("\b\f\n\r\t");
|
||||
checkOutputAndReset(R"("\b\f\n\r\t")");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "array")
|
||||
{
|
||||
writer->startRoot(Writer::array);
|
||||
writer->append(12);
|
||||
writer->finish();
|
||||
checkOutputAndReset("[12]");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "long array")
|
||||
{
|
||||
writer->startRoot(Writer::array);
|
||||
writer->append(12);
|
||||
writer->append(true);
|
||||
writer->append("hello");
|
||||
writer->finish();
|
||||
checkOutputAndReset(R"([12,true,"hello"])");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "embedded array simple")
|
||||
{
|
||||
writer->startRoot(Writer::array);
|
||||
writer->startAppend(Writer::array);
|
||||
writer->finish();
|
||||
writer->finish();
|
||||
checkOutputAndReset("[[]]");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "object")
|
||||
{
|
||||
writer->startRoot(Writer::object);
|
||||
writer->set("hello", "world");
|
||||
writer->finish();
|
||||
checkOutputAndReset(R"({"hello":"world"})");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "complex object")
|
||||
{
|
||||
writer->startRoot(Writer::object);
|
||||
writer->set("hello", "world");
|
||||
writer->startSet(Writer::array, "array");
|
||||
writer->append(true);
|
||||
writer->append(12);
|
||||
writer->startAppend(Writer::array);
|
||||
writer->startAppend(Writer::object);
|
||||
writer->set("goodbye", "cruel world.");
|
||||
writer->startSet(Writer::array, "subarray");
|
||||
writer->append(23.5);
|
||||
writer->finishAll();
|
||||
checkOutputAndReset(
|
||||
R"({"hello":"world","array":[true,12,[{"goodbye":"cruel world.","subarray":[23.5]}]]})");
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(WriterFixture, "json value")
|
||||
{
|
||||
Json::Value value(Json::objectValue);
|
||||
value["foo"] = 23;
|
||||
writer->startRoot(Writer::object);
|
||||
writer->set("hello", value);
|
||||
writer->finish();
|
||||
checkOutputAndReset(R"({"hello":{"foo":23}})");
|
||||
}
|
||||
|
||||
TEST_SUITE_END();
|
||||
2
src/tests/libxrpl/json/main.cpp
Normal file
2
src/tests/libxrpl/json/main.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include <doctest/doctest.h>
|
||||
Reference in New Issue
Block a user