Skip to content

Commit

Permalink
#5127: Add a few unit test cases covering the add/remove functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Dec 27, 2020
1 parent 8be6c5e commit 6796334
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions radiantcore/decl/FavouritesManager.cpp
Expand Up @@ -15,6 +15,8 @@ namespace

void FavouritesManager::addFavourite(decl::Type type, const std::string& path)
{
if (path.empty()) return;

auto set = _favouritesByType.find(type);

if (set == _favouritesByType.end())
Expand All @@ -27,6 +29,8 @@ void FavouritesManager::addFavourite(decl::Type type, const std::string& path)

void FavouritesManager::removeFavourite(decl::Type type, const std::string& path)
{
if (path.empty()) return;

auto set = _favouritesByType.find(type);

if (set == _favouritesByType.end())
Expand All @@ -39,6 +43,8 @@ void FavouritesManager::removeFavourite(decl::Type type, const std::string& path

bool FavouritesManager::isFavourite(decl::Type type, const std::string& path)
{
if (path.empty()) return false;

auto set = _favouritesByType.find(type);

return set != _favouritesByType.end() ? set->second.get().count(path) > 0 : false;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@ add_executable(drtest
CSG.cpp
Face.cpp
FacePlane.cpp
Favourites.cpp
FileTypes.cpp
HeadlessOpenGLContext.cpp
ImageLoading.cpp
Expand Down
78 changes: 78 additions & 0 deletions test/Favourites.cpp
@@ -0,0 +1,78 @@
#include "RadiantTest.h"

#include "ifavourites.h"

namespace test
{

using FavouritesTest = RadiantTest;

TEST_F(FavouritesTest, AddingAndRemovingFavourites)
{
EXPECT_TRUE(GlobalFavouritesManager().getFavourites(decl::Type::Material).empty());

// Add caulk
GlobalFavouritesManager().addFavourite(decl::Type::Material, "textures/common/caulk");

EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).size(), 1);
EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).count("textures/common/caulk"), 1);
EXPECT_TRUE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/caulk"));
EXPECT_FALSE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/clip"));

// Add clip
GlobalFavouritesManager().addFavourite(decl::Type::Material, "textures/common/clip");

EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).size(), 2);
EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).count("textures/common/caulk"), 1);
EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).count("textures/common/clip"), 1);
EXPECT_TRUE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/caulk"));
EXPECT_TRUE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/clip"));

// Remove caulk
GlobalFavouritesManager().removeFavourite(decl::Type::Material, "textures/common/caulk");

EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).size(), 1);
EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).count("textures/common/clip"), 1);
EXPECT_FALSE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/caulk"));
EXPECT_TRUE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/clip"));

// Remove clip
GlobalFavouritesManager().removeFavourite(decl::Type::Material, "textures/common/clip");

EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).size(), 0);
EXPECT_FALSE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/caulk"));
EXPECT_FALSE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/clip"));
}

TEST_F(FavouritesTest, RemovingNonExistent)
{
EXPECT_TRUE(GlobalFavouritesManager().getFavourites(decl::Type::Material).empty());

// Add caulk
GlobalFavouritesManager().addFavourite(decl::Type::Material, "textures/common/caulk");

EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).size(), 1);
EXPECT_TRUE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/caulk"));

// Remove non-existent => shouldn't change anything
GlobalFavouritesManager().removeFavourite(decl::Type::Material, "textures/doesntexist");

EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).size(), 1);
EXPECT_TRUE(GlobalFavouritesManager().isFavourite(decl::Type::Material, "textures/common/caulk"));
}

TEST_F(FavouritesTest, AddingEmptyPaths)
{
EXPECT_TRUE(GlobalFavouritesManager().getFavourites(decl::Type::Material).empty());

// Add an empty string
GlobalFavouritesManager().addFavourite(decl::Type::Material, "");

// Should still be empty
EXPECT_TRUE(GlobalFavouritesManager().getFavourites(decl::Type::Material).empty());

// Empty strings always evaluate to false
EXPECT_FALSE(GlobalFavouritesManager().isFavourite(decl::Type::Material, ""));
}

}
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Expand Up @@ -73,6 +73,7 @@
<ClCompile Include="..\..\..\test\CSG.cpp" />
<ClCompile Include="..\..\..\test\Face.cpp" />
<ClCompile Include="..\..\..\test\FacePlane.cpp" />
<ClCompile Include="..\..\..\test\Favourites.cpp" />
<ClCompile Include="..\..\..\test\FileTypes.cpp" />
<ClCompile Include="..\..\..\test\HeadlessOpenGLContext.cpp" />
<ClCompile Include="..\..\..\test\ImageLoading.cpp" />
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Expand Up @@ -35,6 +35,7 @@
<ClCompile Include="..\..\..\test\PatchIterators.cpp" />
<ClCompile Include="..\..\..\test\ImageLoading.cpp" />
<ClCompile Include="..\..\..\test\LayerManipulation.cpp" />
<ClCompile Include="..\..\..\test\Favourites.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\HeadlessOpenGLContext.h" />
Expand Down

0 comments on commit 6796334

Please sign in to comment.