Changeset - r28099:b14facf80e04
[Not reviewed]
master
0 2 1
Peter Nelson - 13 months ago 2023-11-02 11:16:37
peter1138@openttd.org
Add: WindowDesc unit test to validate NWidgetPart lists.
3 files changed with 63 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/tests/CMakeLists.txt
Show inline comments
 
add_test_files(
 
    landscape_partial_pixel_z.cpp
 
    math_func.cpp
 
    mock_environment.h
 
    mock_fontcache.h
 
    mock_spritecache.cpp
 
    mock_spritecache.h
src/tests/mock_environment.h
Show inline comments
 
new file 100644
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file mock_environment.h Singleton instance to create a mock FontCache/SpriteCache environment. */
 

	
 
#ifndef MOCK_ENVIRONMENT_H
 
#define MOCK_ENVIRONMENT_H
 

	
 
#include "mock_fontcache.h"
 
#include "mock_spritecache.h"
 

	
 
/** Singleton class to set up the mock environemnt once. */
 
class MockEnvironment {
 
public:
 
	static MockEnvironment &Instance()
 
	{
 
		static MockEnvironment instance;
 
		return instance;
 
	}
 

	
 
	MockEnvironment(MockEnvironment const &) = delete;
 
	void operator=(MockEnvironment const &) = delete;
 

	
 
private:
 
	MockEnvironment()
 
	{
 
		/* Mock SpriteCache initialization is needed for some widget generators. */
 
		MockGfxLoadSprites();
 

	
 
		/* Mock FontCache initialization is needed for some NWidgetParts. */
 
		MockFontCache::InitializeFontCaches();
 
	}
 
};
 

	
 
#endif /* MOCK_ENVIRONMENT_H */
src/tests/test_window_desc.cpp
Show inline comments
 
@@ -11,6 +11,8 @@
 

	
 
#include "../3rdparty/catch2/catch.hpp"
 

	
 
#include "mock_environment.h"
 

	
 
#include "../window_gui.h"
 

	
 
/**
 
@@ -19,6 +21,13 @@
 
 */
 
extern std::vector<WindowDesc*> *_window_descs;
 

	
 

	
 
class WindowDescTestsFixture {
 
private:
 
	MockEnvironment &mock = MockEnvironment::Instance();
 
};
 

	
 

	
 
TEST_CASE("WindowDesc - ini_key uniqueness")
 
{
 
	std::set<std::string> seen;
 
@@ -73,3 +82,17 @@ TEST_CASE("WindowDesc - NWidgetParts pro
 

	
 
	CHECK(IsNWidgetTreeClosed(window_desc->nwid_begin, window_desc->nwid_end));
 
}
 

	
 
TEST_CASE_METHOD(WindowDescTestsFixture, "WindowDesc - NWidgetPart validity")
 
{
 
	const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
 

	
 
	INFO(fmt::format("{}:{}", window_desc->file, window_desc->line));
 

	
 
	int biggest_index = -1;
 
	NWidgetStacked *shade_select = nullptr;
 
	NWidgetBase *root = nullptr;
 

	
 
	REQUIRE_NOTHROW(root = MakeWindowNWidgetTree(window_desc->nwid_begin, window_desc->nwid_end, &biggest_index, &shade_select));
 
	CHECK((root != nullptr));
 
}
0 comments (0 inline, 0 general)