Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Apr 12, 2024
1 parent e0794ff commit 06e9623
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 15 deletions.
83 changes: 83 additions & 0 deletions src/Plugins/SimplnxCore/test/RenameDataObjectTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(true));
args.insert(RenameDataObject::k_NewName_Key, std::make_any<std::string>(k_NewName));
args.insert(RenameDataObject::k_DataObject_Key, std::make_any<DataPath>(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<bool>(true));
args.insert(RenameDataObject::k_NewName_Key, std::make_any<std::string>(k_NewName));
args.insert(RenameDataObject::k_DataObject_Key, std::make_any<DataPath>(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);
}
25 changes: 11 additions & 14 deletions src/simplnx/Common/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,6 @@ inline bool ExtractResult(Result<> result, std::vector<Error>& errors, std::vect
template <typename T = void>
inline Result<T> MergeResults(Result<T> first, Result<T> second)
{
usize warningsSize = first.warnings().size() + second.warnings().size();
std::vector<Warning> 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())
{
Expand Down Expand Up @@ -300,7 +287,17 @@ inline Result<T> MergeResults(Result<T> first, Result<T> second)
}

Result<T> result = errors.empty() ? Result<T>{} : Result<T>{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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/simplnx/Filter/Actions/RenameDataAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Result<> RenameDataAction::apply(DataStructure& dataStructure, Mode mode) const
{
std::vector<std::string> 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)
{
Expand Down

0 comments on commit 06e9623

Please sign in to comment.