Skip to content

Commit

Permalink
Add some failing unit tests to check the material file name validatio…
Browse files Browse the repository at this point in the history
…n requirements
  • Loading branch information
codereader committed Apr 17, 2021
1 parent e129733 commit e14b83d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/MaterialExport.cpp
@@ -1,4 +1,5 @@
#include "RadiantTest.h"
#include "TdmMissionSetup.h"

#include <regex>
#include "ishaders.h"
Expand All @@ -11,6 +12,7 @@ namespace test
{

using MaterialExportTest = RadiantTest;
using MaterialExportTest_TdmMissionSetup = TdmMissionSetup;

inline void expectDefinitionContains(const MaterialPtr& material, const std::string& expectedContainedString)
{
Expand Down Expand Up @@ -1227,4 +1229,61 @@ TEST_F(MaterialExportTest, WritingMaterialFiles)
<< "New definition not found in file";
}

// Not all shader file paths are valid, they must be within the current mod's VFS structure, and in the materials/ folder

TEST_F(MaterialExportTest, ShaderFilePathValidation)
{
auto newMaterial = GlobalMaterialManager().createEmptyMaterial("textures/exporttest/somePath");
newMaterial->setDescription("--");

auto projectPath = _context.getTestProjectPath();

EXPECT_NO_THROW(newMaterial->setShaderFileName(projectPath + "materials/exporttest.mtr"));
EXPECT_NO_THROW(newMaterial->setShaderFileName(projectPath + "materials/_test.mtr"));

// materials2 is not a valid folder
EXPECT_THROW(newMaterial->setShaderFileName(projectPath + "materials2/exporttest.mtr"), std::invalid_argument);
EXPECT_THROW(newMaterial->setShaderFileName(projectPath + "exporttest.mtr"), std::invalid_argument);

// Wrong file extension
EXPECT_THROW(newMaterial->setShaderFileName(projectPath + "materials/exporttest.mtr2"), std::invalid_argument);

// No FM setup present
EXPECT_THROW(newMaterial->setShaderFileName(projectPath + "fms/testfm/materials/exporttest.mtr"), std::invalid_argument);
}

TEST_F(MaterialExportTest_TdmMissionSetup, ShaderFilePathValidation)
{
auto newMaterial = GlobalMaterialManager().createEmptyMaterial("textures/exporttest/somePath");
newMaterial->setDescription("--");

auto tdmPath = _context.getTestProjectPath();

// The base project path is OK to be used
EXPECT_NO_THROW(newMaterial->setShaderFileName(tdmPath + "materials/exporttest.mtr"));
EXPECT_NO_THROW(newMaterial->setShaderFileName(tdmPath + "materials/_test.mtr"));

// materials2 is not a valid folder
EXPECT_THROW(newMaterial->setShaderFileName(tdmPath + "materials2/exporttest.mtr"), std::invalid_argument);
EXPECT_THROW(newMaterial->setShaderFileName(tdmPath + "exporttest.mtr"), std::invalid_argument);

// Wrong file extension
EXPECT_THROW(newMaterial->setShaderFileName(tdmPath + "materials/exporttest.mtr2"), std::invalid_argument);

// FM setup says this is OK
auto missionPath = tdmPath + MaterialExportTest_TdmMissionSetup::MissionBasePath + "/" + MaterialExportTest_TdmMissionSetup::TestMissionName + "/";
auto wrongMissionPath = tdmPath + MaterialExportTest_TdmMissionSetup::MissionBasePath + "/tork/";

EXPECT_NO_THROW(newMaterial->setShaderFileName(missionPath + "materials/exporttest.mtr"));

EXPECT_THROW(newMaterial->setShaderFileName(missionPath + "materials2/exporttest.mtr"), std::invalid_argument);
EXPECT_THROW(newMaterial->setShaderFileName(missionPath + "exporttest.mtr"), std::invalid_argument);

// Wrong file extension
EXPECT_THROW(newMaterial->setShaderFileName(missionPath + "materials/exporttest.mtr2"), std::invalid_argument);

// Wrong mission name
EXPECT_THROW(newMaterial->setShaderFileName(wrongMissionPath + "materials/exporttest.mtr"), std::invalid_argument);
}

}
86 changes: 86 additions & 0 deletions test/TdmMissionSetup.h
@@ -0,0 +1,86 @@
#pragma once

#include "RadiantTest.h"

namespace test
{

// Test context setting up a temporary FM folder
class TdmMissionContext :
public radiant::TestContext
{
private:
std::string _projectFolder;

public:
TdmMissionContext()
{
// Set up the temporary settings folder
_projectFolder = (os::getTemporaryPath() / "tdm_fm/").string();

os::removeDirectory(_projectFolder);
os::makeDirectory(_projectFolder);
}

~TdmMissionContext()
{
if (!_projectFolder.empty())
{
os::removeDirectory(_projectFolder);
}
}

virtual std::string getTestProjectPath() const override
{
return os::standardPathWithSlash(_projectFolder);
}
};

// Test setup using an empty FM to be used as TDM project config
class TdmMissionSetup :
public RadiantTest
{
protected:
TdmMissionContext _context;

constexpr static const char* TestMissionName = "testfm";
constexpr static const char* MissionBasePath = "fms";

private:
std::string _testFmPath;
std::string _fmBasePath;

protected:
const std::string& getTestMissionPath()
{
return _testFmPath;
}

virtual void setupGameFolder() override
{
_fmBasePath = _context.getTestProjectPath() + MissionBasePath + "/";
_testFmPath = os::standardPathWithSlash(_fmBasePath + TestMissionName);

os::makeDirectory(_fmBasePath);
os::makeDirectory(_testFmPath);
}

virtual void TearDown() override
{
os::removeDirectory(_fmBasePath);
}

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

config.gameType = "The Dark Mod 2.0 (Standalone)";
config.enginePath = _context.getTestProjectPath();
config.modPath = std::string(MissionBasePath) + "/" + TestMissionName;

message.setConfig(config);
message.setHandled(true);
}
};

}
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Expand Up @@ -65,6 +65,7 @@
<ClInclude Include="..\..\..\test\HeadlessOpenGLContext.h" />
<ClInclude Include="..\..\..\test\math\MatrixUtils.h" />
<ClInclude Include="..\..\..\test\RadiantTest.h" />
<ClInclude Include="..\..\..\test\TdmMissionSetup.h" />
<ClInclude Include="..\..\..\test\TestContext.h" />
<ClInclude Include="..\..\..\test\TestLogFile.h" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Expand Up @@ -59,6 +59,7 @@
<ClInclude Include="..\..\..\test\math\MatrixUtils.h">
<Filter>math</Filter>
</ClInclude>
<ClInclude Include="..\..\..\test\TdmMissionSetup.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit e14b83d

Please sign in to comment.