Skip to content

Commit

Permalink
#5977: Add unit test checking that loading a map with an entity refer…
Browse files Browse the repository at this point in the history
…encing a missing entityDef doesn't discard its child primitives, even after saving
  • Loading branch information
codereader committed Jul 3, 2022
1 parent 8acdb35 commit 824123c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/EntityClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "string/join.h"

#include "algorithm/Entity.h"
#include "algorithm/FileUtils.h"
#include "algorithm/Scene.h"

namespace test
{
Expand Down Expand Up @@ -487,4 +489,27 @@ TEST_F(EntityClassTest, AnimInheritance)
EXPECT_EQ(a_cooler_model->getAnim("new_anim"), some_other_model->getAnim("new_anim"));
}

// Loading an entity of unknown class shouldn't dismiss any primitives
TEST_F(EntityClassTest, MissingEntityClassPreservesPrimitives)
{
std::string mapName = "missing_entitydef.map";
GlobalCommandSystem().executeCommand("OpenMap", mapName);

auto mapTextBeforeSaving = algorithm::loadTextFromVfsFile("maps/" + mapName);

// Find the entity whose class is unknown
auto entity = algorithm::getEntityByName(GlobalMapModule().getRoot(), "missing_entityclass_1");
EXPECT_TRUE(entity) << "Couldn't find the entity 'missing_entityclass_1' in this map";

EXPECT_EQ(algorithm::getChildCount(entity, algorithm::brushHasMaterial("textures/common/caulk")), 2)
<< "Expected 2 brushes to carry the caulk material";
EXPECT_EQ(algorithm::getChildCount(entity, algorithm::patchHasMaterial("textures/common/caulk")), 1)
<< "Expected 1 patch to carry the caulk material";

GlobalCommandSystem().executeCommand("SaveMap");

EXPECT_EQ(algorithm::loadTextFromVfsFile("maps/" + mapName), mapTextBeforeSaving)
<< "Map file contents are different after saving the map with the missing entityDef reference";
}

}
27 changes: 27 additions & 0 deletions test/algorithm/FileUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <string>
#include "ifilesystem.h"

namespace test
{

namespace algorithm
{

// Loads the entire text from the given vfs file into a string
inline std::string loadTextFromVfsFile(const std::string& vfsPath)
{
auto file = GlobalFileSystem().openTextFile(vfsPath);

std::stringstream textStream;
std::istream mapStream(&file->getInputStream());
textStream << mapStream.rdbuf();
textStream.flush();

return textStream.str();
}

}

}
5 changes: 5 additions & 0 deletions test/algorithm/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ inline std::function<bool(const scene::INodePtr&)> brushHasMaterial(const std::s
return [material](const scene::INodePtr& node) { return Node_isBrush(node) && Node_getIBrush(node)->hasShader(material); };
}

inline std::function<bool(const scene::INodePtr&)> patchHasMaterial(const std::string& material)
{
return [material](const scene::INodePtr& node) { return Node_isPatch(node) && Node_getIPatch(node)->getShader() == material; };
}

// Finds the first matching child brush of the given parent node, with any of the brush's faces matching the given material
inline scene::INodePtr findFirstBrushWithMaterial(const scene::INodePtr& parent, const std::string& material)
{
Expand Down
49 changes: 49 additions & 0 deletions test/resources/tdm/maps/missing_entitydef.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Version 2
// entity 0
{
"classname" "worldspawn"
}
// entity 1
{
"classname" "missing_entityclass"
"name" "missing_entityclass_1"
"model" "missing_entityclass_1"
"origin" "144 32 0"
// primitive 0
{
brushDef3
{
( 0 0 1 -64 ) ( ( 0.015625 0 -1.110223024625157e-16 ) ( 0 0.015625 -1 ) ) "textures/common/caulk" 0 0 0
( 0 1 0 -80 ) ( ( 0.015625 0 1 ) ( 0 0.015625 0 ) ) "textures/common/caulk" 0 0 0
( 1 0 0 -80 ) ( ( 0.015625 0 -1.110223024625157e-16 ) ( 0 0.015625 0 ) ) "textures/common/caulk" 0 0 0
( 0 0 -1 -64 ) ( ( 0.015625 0 -1.110223024625157e-16 ) ( 0 0.015625 1 ) ) "textures/common/caulk" 0 0 0
( 0 -1 0 0 ) ( ( 0.015625 0 -1 ) ( 0 0.015625 0 ) ) "textures/common/caulk" 0 0 0
( -1 0 0 0 ) ( ( 0.015625 0 -1.110223024625157e-16 ) ( 0 0.015625 -2.220446049250313e-16 ) ) "textures/common/caulk" 0 0 0
}
}
// primitive 1
{
brushDef3
{
( 0 0 1 -64 ) ( ( 0.015625 0 4.440892098500626e-16 ) ( 0 0.015625 0.25 ) ) "textures/common/caulk" 0 0 0
( 0 1 0 -80 ) ( ( 0.015625 0 -0.25 ) ( 0 0.015625 0 ) ) "textures/common/caulk" 0 0 0
( 1 0 0 0 ) ( ( 0.015625 0 -1.110223024625157e-16 ) ( 0 0.015625 0 ) ) "textures/common/caulk" 0 0 0
( 0 0 -1 -64 ) ( ( 0.015625 0 3.33066907387547e-16 ) ( 0 0.015625 -0.25 ) ) "textures/common/caulk" 0 0 0
( 0 -1 0 0 ) ( ( 0.015625 0 0.25 ) ( 0 0.015625 0 ) ) "textures/common/caulk" 0 0 0
( -1 0 0 -80 ) ( ( 0.015625 0 -1.110223024625157e-16 ) ( 0 0.015625 -4.440892098500626e-16 ) ) "textures/common/caulk" 0 0 0
}
}
// primitive 2
{
patchDef2
{
"textures/common/caulk"
( 3 3 0 0 0 )
(
( ( 112 -48 0 0 0 ) ( 112 -8 0 0 -0.625 ) ( 112 32 0 0 -1.25 ) )
( ( 152 -48 0 0.625 0 ) ( 152 -8 0 0.625 -0.625 ) ( 152 32 0 0.625 -1.25 ) )
( ( 192 -48 0 1.25 0 ) ( 192 -8 0 1.25 -0.625 ) ( 192 32 0 1.25 -1.25 ) )
)
}
}
}
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
<ItemGroup>
<ClInclude Include="..\..\..\test\algorithm\Entity.h" />
<ClInclude Include="..\..\..\test\algorithm\FileUtils.h" />
<ClInclude Include="..\..\..\test\algorithm\Primitives.h" />
<ClInclude Include="..\..\..\test\algorithm\Scene.h" />
<ClInclude Include="..\..\..\test\algorithm\View.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
<ClInclude Include="..\..\..\test\testutil\TemporaryFile.h">
<Filter>testutil</Filter>
</ClInclude>
<ClInclude Include="..\..\..\test\algorithm\FileUtils.h">
<Filter>algorithm</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="algorithm">
Expand Down

0 comments on commit 824123c

Please sign in to comment.