Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Common/Cpp/Json/JsonTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ nlohmann::json to_nlohmann(const JsonValue& json){
return ret;
}
if (json.is_object()){
nlohmann::json ret;
nlohmann::json::object_t ret;
for (const auto& item : *json.to_object()){
ret[item.first] = to_nlohmann(item.second);
}
Expand Down
77 changes: 77 additions & 0 deletions Common/Cpp/Json/JsonTools_Tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* JSON Tools Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <string>
#include <string_view>
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "JsonValue.h"
#include "JsonTools_Tests.h"

namespace PokemonAutomation{
namespace{



struct DumpInput{
std::string_view name;
std::string_view text;
};
struct DumpExpectation{
std::string_view dumped;
};
struct DumpCase{
DumpInput input;
DumpExpectation expectation;
};

// Empty containers must keep their type through dump() and parse_json().
const DumpCase CASES[] = {
{.input = {.name = "Empty object", .text = "{}"}, .expectation = {.dumped = "{}"}},
{.input = {.name = "Nested empty object", .text = R"({"object":{}})"}, .expectation = {.dumped = R"({"object":{}})"}},
{.input = {.name = "Nested empty array", .text = R"({"array":[]})"}, .expectation = {.dumped = R"({"array":[]})"}},
};



class Test_JsonTools_DumpRoundTrip : public UnitTest{
public:
explicit Test_JsonTools_DumpRoundTrip(DumpCase test)
: UnitTest(std::format("Common::JsonTools - {} round trip", test.input.name))
, m_test(test)
{}

virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{
const std::string dumped = parse_json(std::string(m_test.input.text)).dump(-1);
if (dumped != m_test.expectation.dumped){
return UnitTestResult(std::format("dump: expected {}, got {}", m_test.expectation.dumped, dumped));
}
const std::string again = parse_json(dumped).dump(-1);
if (again != dumped){
return UnitTestResult(std::format("dump after reparse: expected {}, got {}", dumped, again));
}
return true;
}

private:
DumpCase m_test;
};



} // namespace



void add_tests_JsonTools(UnitTestDatabase& database){
for (const DumpCase& test : CASES){
database.add<Test_JsonTools_DumpRoundTrip>(test);
}
}



} // namespace PokemonAutomation
22 changes: 22 additions & 0 deletions Common/Cpp/Json/JsonTools_Tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* JSON Tools Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_JsonTools_Tests_H
#define PokemonAutomation_JsonTools_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"

namespace PokemonAutomation{



// Register the JSON dump/parse round-trip tests.
void add_tests_JsonTools(UnitTestDatabase& database);



} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

#include "Common/Cpp/Json/JsonTools_Tests.h"
#include "Common/Cpp/ListenerSet.h"
#include "Common/Cpp/MoveOnlyFunction_Tests.h"
#include "Common/Cpp/ScopeExit.h"
Expand Down Expand Up @@ -51,13 +52,14 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 55 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=35 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
add_tests_Sockets(ret, GlobalThreadPools::unlimited_normal());
add_tests_BlackBorderDetector(ret);
add_tests_ListenerSet(ret);
add_tests_JsonTools(ret);
add_tests_MoveOnlyFunction(ret);
SysbotBase::add_tests_SysbotBasePing(ret);
SysbotBase::add_tests_SysbotBaseReceiveLine(ret);
Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ file(GLOB CORE_LIBRARY_SOURCES
../Common/Cpp/Json/JsonObject.h
../Common/Cpp/Json/JsonTools.cpp
../Common/Cpp/Json/JsonTools.h
../Common/Cpp/Json/JsonTools_Tests.cpp
../Common/Cpp/Json/JsonTools_Tests.h
../Common/Cpp/Json/JsonValue.cpp
../Common/Cpp/Json/JsonValue.h
../Common/Cpp/LifetimeSanitizer.cpp
Expand Down
Loading