From a74260ee50a8c40e13d6dea16ef3d308a9164fea Mon Sep 17 00:00:00 2001 From: codereader Date: Mon, 24 May 2021 16:03:14 +0200 Subject: [PATCH] #5623: Add unit test cases covering all remaining merge actions --- libs/scene/merge/MergeAction.h | 2 +- libs/scene/merge/MergeOperation.cpp | 5 +- test/MapMerging.cpp | 132 +++++++++++++++++++++++++++- 3 files changed, 130 insertions(+), 9 deletions(-) diff --git a/libs/scene/merge/MergeAction.h b/libs/scene/merge/MergeAction.h index 234a7051e7..eb61f6bcd7 100644 --- a/libs/scene/merge/MergeAction.h +++ b/libs/scene/merge/MergeAction.h @@ -64,12 +64,12 @@ class RemoveNodeFromParentAction : assert(_nodeToRemove); } +public: const scene::INodePtr& getNodeToRemove() const { return _nodeToRemove; } -public: void applyChanges() override { removeNodeFromParent(_nodeToRemove); diff --git a/libs/scene/merge/MergeOperation.cpp b/libs/scene/merge/MergeOperation.cpp index f7bbe2e24f..61daac1b7d 100644 --- a/libs/scene/merge/MergeOperation.cpp +++ b/libs/scene/merge/MergeOperation.cpp @@ -1,8 +1,5 @@ #include "MergeOperation.h" - -#include "ientity.h" -#include "../Clone.h" -#include "../scenelib.h" +#include "MergeAction.h" namespace scene { diff --git a/test/MapMerging.cpp b/test/MapMerging.cpp index ecf4e93504..703c7c3a3d 100644 --- a/test/MapMerging.cpp +++ b/test/MapMerging.cpp @@ -394,26 +394,150 @@ std::shared_ptr findAction(const MergeOperation::Ptr& operation, const std::f return foundAction; } +template +std::size_t countActions(const MergeOperation::Ptr& operation, const std::function&)>& predicate) +{ + std::size_t count = 0; + + operation->foreachAction([&](const MergeAction::Ptr& action) + { + auto derivedAction = std::dynamic_pointer_cast(action); + + if (!derivedAction || !predicate(derivedAction)) return; + + ++count; + }); + + return count; +} + TEST_F(MapMergeTest, MergeActionsForMissingEntities) { auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx"); auto operation = MergeOperation::CreateFromComparisonResult(*result); - auto addEntityAction = findAction(operation, [](const std::shared_ptr& action) + auto action = findAction(operation, [](const std::shared_ptr& action) { auto sourceEntity = Node_getEntity(action->getSourceNodeToAdd()); return sourceEntity->getKeyValue("name") == "light_3"; }); - EXPECT_TRUE(addEntityAction) << "No merge action found for missing entity"; + EXPECT_TRUE(action) << "No merge action found for missing entity"; - addEntityAction = findAction(operation, [](const std::shared_ptr& action) + action = findAction(operation, [](const std::shared_ptr& action) { auto sourceEntity = Node_getEntity(action->getSourceNodeToAdd()); return sourceEntity->getKeyValue("name") == "func_static_2"; }); - EXPECT_TRUE(addEntityAction) << "No merge action found for missing entity"; + EXPECT_TRUE(action) << "No merge action found for missing entity"; +} + +TEST_F(MapMergeTest, MergeActionsForRemovedEntities) +{ + auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx"); + auto operation = MergeOperation::CreateFromComparisonResult(*result); + + auto action = findAction(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getNodeToRemove()); + return entity->getKeyValue("name") == "info_player_start_1"; + }); + + EXPECT_TRUE(action) << "No merge action found for removed entity"; +} + +TEST_F(MapMergeTest, MergeActionsForAddedKeyValues) +{ + auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx"); + auto operation = MergeOperation::CreateFromComparisonResult(*result); + + auto action = findAction(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getEntityNode()); + return entity->getKeyValue("name") == "light_1" && action->getKey() == "dist_check_period" && action->getValue() == "40"; + }); + + EXPECT_TRUE(action) << "No merge action found for added key value"; +} + +TEST_F(MapMergeTest, MergeActionsForRemovedKeyValues) +{ + auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx"); + auto operation = MergeOperation::CreateFromComparisonResult(*result); + + auto action = findAction(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getEntityNode()); + return entity->getKeyValue("name") == "light_1" && action->getKey() == "break" && action->getValue().empty(); + }); + + EXPECT_TRUE(action) << "No merge action found for removed key value"; +} + +TEST_F(MapMergeTest, MergeActionsForChangedKeyValues) +{ + auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx"); + auto operation = MergeOperation::CreateFromComparisonResult(*result); + + auto action = findAction(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getEntityNode()); + return entity->getKeyValue("name") == "light_1" && action->getKey() == "ai_see" && action->getValue() == "1"; + }); + + EXPECT_TRUE(action) << "No merge action found for changed key value"; + + action = findAction(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getEntityNode()); + return entity->getKeyValue("name") == "light_2" && action->getKey() == "origin" && action->getValue() == "280 160 0"; + }); + + EXPECT_TRUE(action) << "No merge action found for changed key value"; +} + +TEST_F(MapMergeTest, MergeActionsForChangedPrimitives) +{ + auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx"); + auto operation = MergeOperation::CreateFromComparisonResult(*result); + + auto addAction = findAction(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getParent()); + return entity->getKeyValue("name") == "func_static_30" && action->getSourceNodeToAdd()->getNodeType() == scene::INode::Type::Brush; + }); + + EXPECT_TRUE(addAction) << "No merge action found for added child node"; + + // func_static_1 has 2 additions and 1 removal (== 1 addition, 1 replacement) + auto addActionCount = countActions(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getParent()); + return entity->getKeyValue("name") == "func_static_1"; + }); + auto removeActionCount = countActions(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getNodeToRemove()->getParent()); + return entity->getKeyValue("name") == "func_static_1"; + }); + + EXPECT_EQ(addActionCount, 2) << "No merge action found for added child node"; + EXPECT_EQ(removeActionCount, 1) << "No merge action found for removed child node"; + + addActionCount = countActions(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getParent()); + return entity->isWorldspawn(); + }); + removeActionCount = countActions(operation, [](const std::shared_ptr& action) + { + auto entity = Node_getEntity(action->getNodeToRemove()->getParent()); + return entity->isWorldspawn(); + }); + + EXPECT_EQ(addActionCount, 3) << "No merge action found for added child node"; + EXPECT_EQ(removeActionCount, 3) << "No merge action found for removed child node"; } }