Skip to content

Commit

Permalink
#5430: Start writing some unit tests checking the colour schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 23, 2020
1 parent 91de737 commit 3212c01
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
101 changes: 101 additions & 0 deletions test/ColourSchemes.cpp
@@ -0,0 +1,101 @@
#include "RadiantTest.h"

#include "icolourscheme.h"
#include "math/Vector3.h"
#include "os/path.h"
#include "string/convert.h"
#include "xmlutil/Document.h"

namespace test
{

class ColourSchemeTest :
public RadiantTest
{};

class ColourSchemeTestWithEmptySettings :
public ColourSchemeTest
{
public:
void SetUp() override
{
// Kill any colours.xml file present in the settings folder before regular SetUp
fs::path coloursFile = _context.getSettingsPath();
coloursFile /= "colours.xml";

fs::remove(coloursFile);

RadiantTest::SetUp();
}
};

namespace
{

std::map<std::string, Vector3> loadSchemeFromXml(const std::string& name, const std::string& xmlPath)
{
xml::Document doc(xmlPath);

auto schemes = doc.findXPath("//colourscheme[@name='" + name + "']");
EXPECT_EQ(schemes.size(), 1) << "Multiple schemes named " << name << " in file " << xmlPath;

auto schemeColours = doc.findXPath("//colourscheme[@name='" + name + "']//colour");
EXPECT_NE(schemeColours.size(), 0) << "Scheme " << name << " is empty";

std::map<std::string, Vector3> result;

for (auto colourNode : schemeColours)
{
result[colourNode.getAttributeValue("name")] = string::convert<Vector3>(colourNode.getAttributeValue("value"));
}

return result;
}

std::size_t getItemCountInScheme(const colours::IColourScheme& scheme)
{
std::size_t result = 0;

scheme.foreachColour([&](const std::string&, const colours::IColourItem& item) { ++result; });

return result;
}

}

// With an empty settings folder, the default schemes should be loaded
TEST_F(ColourSchemeTestWithEmptySettings, LoadDefaultSchemes)
{
auto defaultSchemeNames = {
"DarkRadiant Default",
"QE3Radiant Original",
"Black & Green",
"Maya/Max/Lightwave Emulation",
"Super Mal"
};

// Check the default colours.xml file
std::string defaultColoursFile = _context.getRuntimeDataPath() + "colours.xml";
EXPECT_TRUE(fs::exists(defaultColoursFile)) << "Could not find factory colours file: " << defaultColoursFile;

for (auto name : defaultSchemeNames)
{
EXPECT_TRUE(GlobalColourSchemeManager().schemeExists(name)) << "Scheme " << name << " not found";

auto defaultScheme = loadSchemeFromXml(name, defaultColoursFile);
auto& loadedScheme = GlobalColourSchemeManager().getColourScheme(name);

// Number of items must match
EXPECT_EQ(defaultScheme.size(), getItemCountInScheme(loadedScheme));

// Each item must match
for (const auto& defaultColour : defaultScheme)
{
EXPECT_EQ(loadedScheme.getColour(defaultColour.first).getColour(), defaultColour.second)
<< "Scheme " << name << ": colour " << defaultColour.first
<< " doesn't match the default value " << defaultColour.second;
}
}
}

}
1 change: 1 addition & 0 deletions test/Makefile.am
Expand Up @@ -29,6 +29,7 @@ drtest_SOURCES = math/Matrix4.cpp \
math/Plane3.cpp \
math/Quaternion.cpp \
Camera.cpp \
ColourSchemes.cpp \
CSG.cpp \
HeadlessOpenGLContext.cpp \
FacePlane.cpp \
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Expand Up @@ -66,6 +66,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\test\Camera.cpp" />
<ClCompile Include="..\..\..\test\ColourSchemes.cpp" />
<ClCompile Include="..\..\..\test\CSG.cpp" />
<ClCompile Include="..\..\..\test\Face.cpp" />
<ClCompile Include="..\..\..\test\FacePlane.cpp" />
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Expand Up @@ -27,6 +27,7 @@
<ClCompile Include="..\..\..\test\Face.cpp" />
<ClCompile Include="..\..\..\test\Selection.cpp" />
<ClCompile Include="..\..\..\test\MessageBus.cpp" />
<ClCompile Include="..\..\..\test\ColourSchemes.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\HeadlessOpenGLContext.h" />
Expand Down

0 comments on commit 3212c01

Please sign in to comment.