From 4d519cc59657b5b09f9bb19fe69c9d82d0052ea5 Mon Sep 17 00:00:00 2001 From: Ian Roddis Date: Wed, 11 Aug 2021 10:16:38 -0300 Subject: [PATCH] - Adding default pretty-printer for Task - Adding equivalency check for Task - Adding test to ensure serialization / deserialization is reproducible. --- daggy/include/daggy/Serialization.hpp | 3 +++ daggy/include/daggy/Task.hpp | 8 ++++++++ daggy/src/Serialization.cpp | 9 +++++++-- tests/unit_serialization.cpp | 16 ++++++++++++++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/daggy/include/daggy/Serialization.hpp b/daggy/include/daggy/Serialization.hpp index f75f6f9..5324979 100644 --- a/daggy/include/daggy/Serialization.hpp +++ b/daggy/include/daggy/Serialization.hpp @@ -26,4 +26,7 @@ namespace daggy { std::string taskToJSON(const Task &task); std::string tasksToJSON(const std::vector &tasks); + + // default serialization + std::ostream &operator<<(std::ostream &os, const Task &task); } diff --git a/daggy/include/daggy/Task.hpp b/daggy/include/daggy/Task.hpp index cdf15e0..7bdb8ce 100644 --- a/daggy/include/daggy/Task.hpp +++ b/daggy/include/daggy/Task.hpp @@ -12,5 +12,13 @@ namespace daggy { uint32_t maxRetries; uint32_t retryIntervalSeconds; // Time to wait between retries std::unordered_set children; + + bool operator==(const Task &other) const { + return (name == other.name) + and (maxRetries == other.maxRetries) + and (retryIntervalSeconds == other.retryIntervalSeconds) + and (command == other.command) + and (children == other.children); + } }; } diff --git a/daggy/src/Serialization.cpp b/daggy/src/Serialization.cpp index e9c31b6..e82dd77 100644 --- a/daggy/src/Serialization.cpp +++ b/daggy/src/Serialization.cpp @@ -111,10 +111,10 @@ namespace daggy { // Create the tasks auto &taskNames = childrenMap[name]; for (size_t tid = 0; tid < commands.size(); ++tid) { - std::string taskName = name + "_" + std::to_string(tid); + std::string taskName = (commands.size() == 1 ? name : name + "_" + std::to_string(tid)); taskNames.push_back(taskName); tasks.emplace_back(Task{ - .name = name + "_" + std::to_string(tid), + .name = taskName, .command = commands[tid], .maxRetries = maxRetries, .retryIntervalSeconds = retryIntervalSeconds, @@ -195,4 +195,9 @@ namespace daggy { return ss.str(); } + + std::ostream &operator<<(std::ostream &os, const Task &task) { + os << taskToJSON(task); + return os; + } } \ No newline at end of file diff --git a/tests/unit_serialization.cpp b/tests/unit_serialization.cpp index c9bf47a..4590095 100644 --- a/tests/unit_serialization.cpp +++ b/tests/unit_serialization.cpp @@ -55,8 +55,20 @@ TEST_CASE("Task Serialization", "[serialize_tasks]") { SECTION("Build with no expansion") { std::string testTasks = R"([{"name": "A", "command": ["/bin/echo", "A"], "children": ["C"]}, {"name": "B", "command": ["/bin/echo", "B"], "children": ["C"]},{"name": "C", "command": ["/bin/echo", "C"]}])"; auto tasks = daggy::tasksFromJSON(testTasks); + + std::unordered_map taskMap; + for (size_t i = 0; i < tasks.size(); ++i) { + taskMap[tasks[i].name] = i; + } + auto genJSON = daggy::tasksToJSON(tasks); - std::cout << genJSON << std::endl; - REQUIRE_NOTHROW(daggy::tasksFromJSON(genJSON)); + auto regenTasks = daggy::tasksFromJSON(genJSON); + + REQUIRE(regenTasks.size() == tasks.size()); + + for (const auto &task : regenTasks) { + const auto &other = tasks[taskMap[task.name]]; + REQUIRE(task == other); + } } } \ No newline at end of file