Skip to content

Commit

Permalink
#5622: Couple of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed May 23, 2021
1 parent 17a3345 commit b0db81e
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 18 deletions.
2 changes: 1 addition & 1 deletion libs/scene/SceneGraphComparer.h
Expand Up @@ -39,7 +39,7 @@ struct ComparisonResult

struct EntityDifference
{
std::string fingerPrint;
std::string fingerprint;
scene::INodePtr node;
std::string entityName;

Expand Down
110 changes: 110 additions & 0 deletions test/MapMerging.cpp
Expand Up @@ -3,11 +3,13 @@
#include "icommandsystem.h"
#include "itransformable.h"
#include "ibrush.h"
#include "imapresource.h"
#include "ipatch.h"
#include "icomparablenode.h"
#include "algorithm/Scene.h"
#include "registry/registry.h"
#include "scenelib.h"
#include "scene/SceneGraphComparer.h"

namespace test
{
Expand Down Expand Up @@ -223,4 +225,112 @@ TEST_F(MapMergeTest, EntityFingerprintInsensitiveToChildOrder)
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
}

inline scene::ComparisonResult::Ptr performComparison(const std::string& targetMap, const std::string& sourceMapPath)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));

auto resource = GlobalMapResourceManager().createFromPath(sourceMapPath);
EXPECT_TRUE(resource->load()) << "Test map not found in path " << sourceMapPath;

scene::SceneGraphComparer comparer(resource->getRootNode(), GlobalMapModule().getRoot());
comparer.compare();

return comparer.getResult();
}

inline bool resultHasEntityDifference(const scene::ComparisonResult::Ptr& result, const std::string& name,
const scene::ComparisonResult::EntityDifference::Type type)
{
for (const auto& difference : result->differingEntities)
{
if (difference.entityName == name && difference.type == type)
{
return true;
}
}

return false;
}

inline bool hasKeyValueDifference(const scene::ComparisonResult::EntityDifference& diff,
const std::string& key, const std::string& value, scene::ComparisonResult::KeyValueDifference::Type type)
{
for (const auto& difference : diff.differingKeyValues)
{
if (difference.type == type && difference.key == key && difference.value == value)
{
return true;
}
}

return false;
}

inline scene::ComparisonResult::EntityDifference getEntityDifference(const scene::ComparisonResult::Ptr& result, const std::string& name)
{
for (const auto& difference : result->differingEntities)
{
if (difference.entityName == name)
{
return difference;
}
}

return scene::ComparisonResult::EntityDifference();
}

TEST_F(MapMergeTest, DetectMissingEntities)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");

// The player start has been removed in the changed map
EXPECT_TRUE(resultHasEntityDifference(result, "info_player_start_1", scene::ComparisonResult::EntityDifference::Type::EntityMissingInSource));
}

TEST_F(MapMergeTest, DetectAddedEntities)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");

// light_3 start has been added to the changed map
EXPECT_TRUE(resultHasEntityDifference(result, "light_3", scene::ComparisonResult::EntityDifference::Type::EntityMissingInBase));
}

TEST_F(MapMergeTest, DetectAddedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");

// light_1 has different key values, dist_check_period = 40 has been added
auto diff = getEntityDifference(result, "light_1");

EXPECT_EQ(diff.type, scene::ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "dist_check_period", "40", scene::ComparisonResult::KeyValueDifference::Type::KeyValueAdded));
}

TEST_F(MapMergeTest, DetectRemovedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");

// light_1 has different key values, break = 1 has been removed
auto diff = getEntityDifference(result, "light_1");

EXPECT_EQ(diff.type, scene::ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "break", "1", scene::ComparisonResult::KeyValueDifference::Type::KeyValueRemoved));
}

TEST_F(MapMergeTest, DetectChangedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");

// light_1 has different key values, "ai_see" has been changed to "1"
auto diff = getEntityDifference(result, "light_1");

EXPECT_EQ(diff.type, scene::ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "ai_see", "1", scene::ComparisonResult::KeyValueDifference::Type::KeyValueChanged));

// light_2 has a different origin
diff = getEntityDifference(result, "light_2");
EXPECT_EQ(diff.type, scene::ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "origin", "280 160 0", scene::ComparisonResult::KeyValueDifference::Type::KeyValueChanged));
}

}
80 changes: 63 additions & 17 deletions test/resources/tdm/maps/fingerprinting.mapx
Expand Up @@ -8,7 +8,7 @@
<selectionGroups/>
<selectionSets/>
<properties>
<property key="EditTimeInSeconds" value="287"/>
<property key="EditTimeInSeconds" value="367"/>
<property key="LastCameraAngle" value="-36.9 45.6 0"/>
<property key="LastCameraPosition" value="-81.4144 -139.899 261.902"/>
<property key="LastShaderClipboardMaterial" value="textures/numbers/2"/>
Expand Down Expand Up @@ -174,14 +174,14 @@
<patch number="4" width="3" height="3" fixedSubdivisions="false">
<material name="textures/numbers/1"/>
<controlVertices>
<controlVertex row="0" column="0" x="-64.000043" y="63.999957" z="0" u="0" v="0"/>
<controlVertex row="1" column="0" x="-64.000043" y="95.999978" z="0" u="0" v="-1.000001"/>
<controlVertex row="2" column="0" x="-64.000043" y="128.000000" z="0" u="0" v="-2.000001"/>
<controlVertex row="0" column="1" x="-32.000022" y="63.999957" z="0" u="1.000001" v="0"/>
<controlVertex row="1" column="1" x="-32.000022" y="95.999978" z="0" u="1.000001" v="-1.000001"/>
<controlVertex row="2" column="1" x="-32.000022" y="128.000000" z="0" u="1.000001" v="-2.000001"/>
<controlVertex row="0" column="2" x="0" y="63.999957" z="0" u="2.000001" v="0"/>
<controlVertex row="1" column="2" x="0" y="95.999978" z="0" u="2.000001" v="-1.000001"/>
<controlVertex row="0" column="0" x="-64.000046" y="63.999958" z="0" u="0" v="0"/>
<controlVertex row="1" column="0" x="-64.000046" y="95.999977" z="0" u="0" v="-1.000001"/>
<controlVertex row="2" column="0" x="-64.000046" y="128.000000" z="0" u="0" v="-2.000001"/>
<controlVertex row="0" column="1" x="-32.000023" y="63.999958" z="0" u="1.000001" v="0"/>
<controlVertex row="1" column="1" x="-32.000023" y="95.999977" z="0" u="1.000001" v="-1.000001"/>
<controlVertex row="2" column="1" x="-32.000023" y="128.000000" z="0" u="1.000001" v="-2.000001"/>
<controlVertex row="0" column="2" x="0" y="63.999958" z="0" u="2.000001" v="0"/>
<controlVertex row="1" column="2" x="0" y="95.999977" z="0" u="2.000001" v="-1.000001"/>
<controlVertex row="2" column="2" x="0" y="128.000000" z="0" u="2.000001" v="-2.000001"/>
</controlVertices>
<layers>
Expand All @@ -193,14 +193,14 @@
<patch number="5" width="3" height="3" fixedSubdivisions="false">
<material name="textures/numbers/2"/>
<controlVertices>
<controlVertex row="0" column="0" x="-0.000043" y="63.999957" z="0" u="1.999999" v="-0.000001"/>
<controlVertex row="1" column="0" x="-0.000043" y="95.999978" z="0" u="2.999999" v="-0.000001"/>
<controlVertex row="0" column="0" x="-0.000043" y="63.999958" z="0" u="1.999999" v="-0.000001"/>
<controlVertex row="1" column="0" x="-0.000043" y="95.999977" z="0" u="2.999999" v="-0.000001"/>
<controlVertex row="2" column="0" x="-0.000043" y="128.000000" z="0" u="4.000000" v="-0.000001"/>
<controlVertex row="0" column="1" x="31.999978" y="63.999957" z="0" u="1.999999" v="0.999999"/>
<controlVertex row="1" column="1" x="31.999978" y="95.999978" z="0" u="2.999999" v="0.999999"/>
<controlVertex row="2" column="1" x="31.999978" y="128.000000" z="0" u="4.000000" v="0.999999"/>
<controlVertex row="0" column="2" x="64.000000" y="63.999957" z="0" u="1.999999" v="2.000000"/>
<controlVertex row="1" column="2" x="64.000000" y="95.999978" z="0" u="2.999999" v="2.000000"/>
<controlVertex row="0" column="1" x="31.999977" y="63.999958" z="0" u="1.999999" v="0.999999"/>
<controlVertex row="1" column="1" x="31.999977" y="95.999977" z="0" u="2.999999" v="0.999999"/>
<controlVertex row="2" column="1" x="31.999977" y="128.000000" z="0" u="4.000000" v="0.999999"/>
<controlVertex row="0" column="2" x="64.000000" y="63.999958" z="0" u="1.999999" v="2.000000"/>
<controlVertex row="1" column="2" x="64.000000" y="95.999977" z="0" u="2.999999" v="2.000000"/>
<controlVertex row="2" column="2" x="64.000000" y="128.000000" z="0" u="4.000000" v="2.000000"/>
</controlVertices>
<layers>
Expand Down Expand Up @@ -304,9 +304,55 @@
<keyValues>
<keyValue key="classname" value="func_static"/>
<keyValue key="name" value="func_static_1"/>
<keyValue key="dummyspawnarg" value="dummyvalue"/>
<keyValue key="model" value="func_static_1"/>
<keyValue key="origin" value="96 -32 0"/>
<keyValue key="dummyspawnarg" value="dummyvalue"/>
</keyValues>
<layers>
<layer id="0"/>
</layers>
<selectionGroups/>
<selectionSets/>
</entity>
<entity number="2">
<primitives/>
<keyValues>
<keyValue key="classname" value="light"/>
<keyValue key="name" value="light_2"/>
<keyValue key="light_center" value="0 0 0"/>
<keyValue key="light_radius" value="208 208 320"/>
<keyValue key="origin" value="256 96 0"/>
</keyValues>
<layers>
<layer id="0"/>
</layers>
<selectionGroups/>
<selectionSets/>
</entity>
<entity number="3">
<primitives/>
<keyValues>
<keyValue key="classname" value="light"/>
<keyValue key="name" value="light_1"/>
<keyValue key="ai_see" value="0"/>
<keyValue key="light_center" value="0 0 0"/>
<keyValue key="light_radius" value="208 208 320"/>
<keyValue key="origin" value="144 96 0"/>
<keyValue key="break" value="1"/>
</keyValues>
<layers>
<layer id="0"/>
</layers>
<selectionGroups/>
<selectionSets/>
</entity>
<entity number="4">
<primitives/>
<keyValues>
<keyValue key="classname" value="info_player_start"/>
<keyValue key="name" value="info_player_start_1"/>
<keyValue key="origin" value="64 160 0"/>
<keyValue key="angle" value="90"/>
</keyValues>
<layers>
<layer id="0"/>
Expand Down

0 comments on commit b0db81e

Please sign in to comment.