Skip to content

Commit

Permalink
#6095: add a trivial test for the GameManager
Browse files Browse the repository at this point in the history
This may not turn out to be all that useful, since the RadiantTest sets
up a fake game by responding on the message bus, and never actually
loads any .game files. Currently it is testing only that the game
manager can return the expected config struct which reflects the fake
game info populated by RadiantTest.
  • Loading branch information
Matthew Mott committed Sep 13, 2022
1 parent db43447 commit 9b05e8b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
19 changes: 17 additions & 2 deletions libs/string/split.h
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <iterator>
#include <vector>

namespace string
{
Expand All @@ -11,8 +12,9 @@ namespace string
* the tokens at the end the given token container (which might be any container
* supporting insert iterators (std::vector, std::list, std::set, etc.)
*/
template<class ContainerType>
inline void split(ContainerType& tokens, const std::string& subject, const std::string& delimiters, bool trimEmpty = true)
template <typename ContainerType>
inline void split(ContainerType& tokens, const std::string& subject, const std::string& delimiters,
bool trimEmpty = true)
{
std::string::size_type lastPos = 0;
std::string::size_type length = subject.length();
Expand Down Expand Up @@ -46,4 +48,17 @@ inline void split(ContainerType& tokens, const std::string& subject, const std::
}
}

/**
* @brief Split the given string into parts, returning the result as a std::vector.
*
* Convenience wrapper for split() which does not require an existing container variable.
*/
inline std::vector<std::string> splitToVec(const std::string& subject,
const std::string& delimiters, bool trimEmpty = true)
{
std::vector<std::string> result;
split(result, subject, delimiters, trimEmpty);
return result;
}

}
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Expand Up @@ -15,6 +15,7 @@ add_executable(drtest
EntityClass.cpp
Favourites.cpp
FileTypes.cpp
Game.cpp
GeometryStore.cpp
Grid.cpp
HeadlessOpenGLContext.cpp
Expand Down
2 changes: 1 addition & 1 deletion test/DeclManager.cpp
Expand Up @@ -454,7 +454,7 @@ TEST_F(DeclManagerTest, DeclarationMetadata)
EXPECT_TRUE(decl);
EXPECT_EQ(decl->getDeclType(), decl::Type::TestDecl);
EXPECT_EQ(decl->getDeclFilePath(), "testdecls/exporttest.decl");
EXPECT_EQ(decl->getModName(), RadiantTest::DefaultGameType);
EXPECT_EQ(decl->getModName(), RadiantTest::DEFAULT_GAME_TYPE);
}

inline void expectDeclIsPresent(decl::Type type, const std::string& declName)
Expand Down
2 changes: 1 addition & 1 deletion test/EntityClass.cpp
Expand Up @@ -52,7 +52,7 @@ TEST_F(EntityClassTest, EntityClassDefFilename)
auto cls = GlobalEntityClassManager().findClass("dr:entity_using_modeldef");
EXPECT_TRUE(cls);

EXPECT_EQ(cls->getModName(), RadiantTest::DefaultGameType);
EXPECT_EQ(cls->getModName(), RadiantTest::DEFAULT_GAME_TYPE);
EXPECT_EQ(cls->getDeclFilePath(), "def/entity_with_model.def");
}

Expand Down
28 changes: 28 additions & 0 deletions test/Game.cpp
@@ -0,0 +1,28 @@
#include "RadiantTest.h"
#include "string/split.h"

namespace test
{

using GameTest = RadiantTest;

TEST_F(GameTest, GetCurrentGameConfig)
{
// Check that we can get the game manager and current game without crashing or anything
auto& mgr = GlobalGameManager();
auto game = mgr.currentGame();
ASSERT_TRUE(game);

// RadiantTest sets up a test game type which should be exposed via the GameManager
auto conf = mgr.getConfig();
EXPECT_EQ(conf.gameType, RadiantTest::DEFAULT_GAME_TYPE);

// The start of the engine path could be anywhere (depending on where the test binaries are
// being run from), but it should end with "resources/tdm"
auto pathComps = string::splitToVec(conf.enginePath, "/");
EXPECT_GE(pathComps.size(), 2);
EXPECT_EQ(pathComps.at(pathComps.size() - 1), "tdm");
EXPECT_EQ(pathComps.at(pathComps.size() - 2), "resources");
}

}
4 changes: 2 additions & 2 deletions test/Particles.cpp
Expand Up @@ -88,14 +88,14 @@ TEST_F(ParticlesTest, ParticleMetadata)

EXPECT_EQ(flamejet->getDeclName(), "flamejet");
EXPECT_EQ(flamejet->getDeclType(), decl::Type::Particle);
EXPECT_EQ(flamejet->getModName(), RadiantTest::DefaultGameType);
EXPECT_EQ(flamejet->getModName(), RadiantTest::DEFAULT_GAME_TYPE);
EXPECT_EQ(flamejet->getDeclFilePath(), "particles/testparticles.prt");

auto flamejetInPk4 = GlobalParticlesManager().getDefByName("flamejet_in_pk4");

EXPECT_EQ(flamejetInPk4->getDeclName(), "flamejet_in_pk4");
EXPECT_EQ(flamejetInPk4->getDeclType(), decl::Type::Particle);
EXPECT_EQ(flamejetInPk4->getModName(), RadiantTest::DefaultGameType);
EXPECT_EQ(flamejetInPk4->getModName(), RadiantTest::DEFAULT_GAME_TYPE);
EXPECT_EQ(flamejetInPk4->getDeclFilePath(), "particles/override_test.prt");
}

Expand Down
4 changes: 2 additions & 2 deletions test/RadiantTest.h
Expand Up @@ -172,13 +172,13 @@ class RadiantTest :
GlobalCommandSystem().executeCommand("OpenMap", mapsRelativePath);
}

static constexpr const char* DefaultGameType = "The Dark Mod 2.0 (Standalone)";
static constexpr const char* DEFAULT_GAME_TYPE = "The Dark Mod 2.0 (Standalone)";

virtual void handleGameConfigMessage(game::ConfigurationNeeded& message)
{
game::GameConfiguration config;

config.gameType = DefaultGameType;
config.gameType = DEFAULT_GAME_TYPE;
config.enginePath = _context.getTestProjectPath();

message.setConfig(config);
Expand Down

0 comments on commit 9b05e8b

Please sign in to comment.