From 06e962316d0b79bac01cf5fd1ee24736b2f73ce7 Mon Sep 17 00:00:00 2001 From: nyoungbq Date: Fri, 12 Apr 2024 17:21:59 -0400 Subject: [PATCH] Add tests --- .../SimplnxCore/test/RenameDataObjectTest.cpp | 83 +++++++++++++++++++ src/simplnx/Common/Result.hpp | 25 +++--- .../Filter/Actions/RenameDataAction.cpp | 2 +- 3 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/Plugins/SimplnxCore/test/RenameDataObjectTest.cpp b/src/Plugins/SimplnxCore/test/RenameDataObjectTest.cpp index 688dd7e7fa..60819357d7 100644 --- a/src/Plugins/SimplnxCore/test/RenameDataObjectTest.cpp +++ b/src/Plugins/SimplnxCore/test/RenameDataObjectTest.cpp @@ -66,3 +66,86 @@ TEST_CASE("SimplnxCore::RenameDataAction(Valid Parameters)", "[SimplnxCore][Rena REQUIRE(dataObject->getName() == k_NewName); } + +TEST_CASE("SimplnxCore::RenameDataAction(Valid Overwrite)", "[SimplnxCore][RenameDataAction]") +{ + static constexpr StringLiteral k_NewName = Constants::k_GroupHName; + static const DataPath k_DataPath({Constants::k_GroupAName, Constants::k_GroupCName, Constants::k_GroupDName, Constants::k_ArrayIName}); + + RenameDataObject filter; + DataStructure dataStructure = UnitTest::CreateComplexMultiLevelDataGraph(); + Arguments args; + + args.insert(RenameDataObject::k_AllowOverwrite_Key, std::make_any(true)); + args.insert(RenameDataObject::k_NewName_Key, std::make_any(k_NewName)); + args.insert(RenameDataObject::k_DataObject_Key, std::make_any(k_DataPath)); + + auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + + bool warningFound = false; + for(const auto& warning : preflightResult.outputActions.warnings()) + { + if(warning.code == -6602) + { + warningFound = true; + } + } + REQUIRE(warningFound); + + auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + + // Verify rename was successful + { + DataPath newPath({Constants::k_GroupAName, Constants::k_GroupCName, Constants::k_GroupDName, k_NewName}); + auto* dataObject = dataStructure.getData(newPath); + REQUIRE(dataObject != nullptr); + + REQUIRE(dataObject->getName() == k_NewName); + } + + // Verify old DataGroup (`H`) was removed + { + DataPath oldHPath({Constants::k_GroupAName, Constants::k_GroupHName}); + auto* dataObject = dataStructure.getData(oldHPath); + REQUIRE(dataObject == nullptr); + } + + // Verify old DataGroup (`H`) sub-array (`N`) was removed + { + DataPath oldHChildPath({Constants::k_GroupAName, Constants::k_GroupHName, Constants::k_ArrayNName}); + auto* dataObject = dataStructure.getData(oldHChildPath); + REQUIRE(dataObject == nullptr); + } +} + +TEST_CASE("SimplnxCore::RenameDataAction(InValid Overwrite)", "[SimplnxCore][RenameDataAction]") +{ + static constexpr StringLiteral k_NewName = Constants::k_GroupDName; + static const DataPath k_DataPath({Constants::k_GroupAName, Constants::k_GroupCName, Constants::k_GroupDName, Constants::k_ArrayIName}); + + RenameDataObject filter; + DataStructure dataStructure = UnitTest::CreateComplexMultiLevelDataGraph(); + Arguments args; + + args.insert(RenameDataObject::k_AllowOverwrite_Key, std::make_any(true)); + args.insert(RenameDataObject::k_NewName_Key, std::make_any(k_NewName)); + args.insert(RenameDataObject::k_DataObject_Key, std::make_any(k_DataPath)); + + auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(preflightResult.outputActions); + + bool errorFound = false; + for(const auto& error : preflightResult.outputActions.errors()) + { + if(error.code == -6601) + { + errorFound = true; + } + } + REQUIRE(errorFound); + + auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); +} diff --git a/src/simplnx/Common/Result.hpp b/src/simplnx/Common/Result.hpp index a2b15a7deb..5bb9b52f75 100644 --- a/src/simplnx/Common/Result.hpp +++ b/src/simplnx/Common/Result.hpp @@ -259,19 +259,6 @@ inline bool ExtractResult(Result<> result, std::vector& errors, std::vect template inline Result MergeResults(Result first, Result second) { - usize warningsSize = first.warnings().size() + second.warnings().size(); - std::vector warnings; - warnings.reserve(warningsSize); - - for(auto&& warning : first.warnings()) - { - warnings.push_back(std::move(warning)); - } - for(auto&& warning : second.warnings()) - { - warnings.push_back(std::move(warning)); - } - usize errorsSize = 0; if(first.invalid()) { @@ -300,7 +287,17 @@ inline Result MergeResults(Result first, Result second) } Result result = errors.empty() ? Result{} : Result{nonstd::make_unexpected(std::move(errors))}; - result.warnings() = std::move(warnings); + + result.m_Warnings.reserve(first.warnings().size() + second.warnings().size()); + for(auto&& warning : first.warnings()) + { + result.m_Warnings.push_back(std::move(warning)); + } + for(auto&& warning : second.warnings()) + { + result.m_Warnings.push_back(std::move(warning)); + } + return result; } diff --git a/src/simplnx/Filter/Actions/RenameDataAction.cpp b/src/simplnx/Filter/Actions/RenameDataAction.cpp index b7b293fb54..2c08321902 100644 --- a/src/simplnx/Filter/Actions/RenameDataAction.cpp +++ b/src/simplnx/Filter/Actions/RenameDataAction.cpp @@ -71,7 +71,7 @@ Result<> RenameDataAction::apply(DataStructure& dataStructure, Mode mode) const { std::vector pathVec = m_Path.getPathVector(); - // The canRename() function returns 1 if the base object already has the objects new name + // The canRename() function returns true if the base object already has the objects new name // so in that case we will never make it here for(const auto& name : pathVec) {