OpenTTD Source: src/tests/test_window_desc.cpp Source File

OpenTTD Source 15.3

test_window_desc.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11
12#include "../3rdparty/catch2/catch.hpp"
13
14#include "mock_environment.h"
15
16#include "../window_gui.h"
17
18#include "../safeguards.h"
19
24extern std::vector<WindowDesc*> *_window_descs;
25
26
27 class WindowDescTestsFixture {
28private:
29 MockEnvironment &mock = MockEnvironment::Instance();
30};
31
32
33TEST_CASE("WindowDesc - ini_key uniqueness")
34{
35 std::set<std::string_view> seen;
36
37 for (const WindowDesc *window_desc : *_window_descs) {
38
39 if (window_desc->ini_key.empty()) continue;
40
41 CAPTURE(window_desc->ini_key);
42 CHECK((seen.find(window_desc->ini_key) == std::end(seen)));
43
44 seen.insert(window_desc->ini_key);
45 }
46}
47
48TEST_CASE("WindowDesc - ini_key validity")
49{
50 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
51
52 bool has_inikey = !window_desc->ini_key.empty();
53 bool has_widget = std::any_of(std::begin(window_desc->nwid_parts), std::end(window_desc->nwid_parts), [](const NWidgetPart &part) { return part.type == WWT_DEFSIZEBOX || part.type == WWT_STICKYBOX; });
54
55 INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
56 CAPTURE(has_inikey);
57 CAPTURE(has_widget);
58
59 CHECK((has_widget == has_inikey));
60}
61
68 static bool IsNWidgetTreeClosed(std::span<const NWidgetPart> nwid_parts)
69{
70 int depth = 0;
71 for (const auto nwid : nwid_parts) {
72 if (IsContainerWidgetType(nwid.type)) ++depth;
73 if (nwid.type == WPT_ENDCONTAINER) --depth;
74 }
75 return depth == 0;
76}
77
78TEST_CASE("WindowDesc - NWidgetParts properly closed")
79{
80 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
81
82 INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
83
84 CHECK(IsNWidgetTreeClosed(window_desc->nwid_parts));
85}
86
87TEST_CASE_METHOD(WindowDescTestsFixture, "WindowDesc - NWidgetPart validity")
88{
89 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
90
91 INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
92
93 NWidgetStacked *shade_select = nullptr;
94 std::unique_ptr<NWidgetBase> root = nullptr;
95
96 REQUIRE_NOTHROW(root = MakeWindowNWidgetTree(window_desc->nwid_parts, &shade_select));
97 CHECK((root != nullptr));
98}
MockEnvironment
Singleton class to set up the mock environment once.
Definition mock_environment.h:17
NWidgetStacked
Stacked widgets, widgets all occupying the same space in the window.
Definition widget_type.h:524
WindowDescTestsFixture
Definition test_window_desc.cpp:27
MakeWindowNWidgetTree
std::unique_ptr< NWidgetBase > MakeWindowNWidgetTree(std::span< const NWidgetPart > nwid_parts, NWidgetStacked **shade_select)
Make a nested widget tree for a window from a parts array.
Definition widget.cpp:3429
mock_environment.h
Singleton instance to create a mock FontCache/SpriteCache environment.
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition widget_type.h:1097
WindowDesc
High level window description.
Definition window_gui.h:168
WindowDesc::ini_key
const std::string_view ini_key
Key to store window defaults in openttd.cfg. An empty string if nothing shall be stored.
Definition window_gui.h:181
WindowDesc::source_location
const std::source_location source_location
Source location of this definition.
Definition window_gui.h:177
WindowDesc::nwid_parts
const std::span< const NWidgetPart > nwid_parts
Span of nested widget parts describing the window.
Definition window_gui.h:183
IsNWidgetTreeClosed
static bool IsNWidgetTreeClosed(std::span< const NWidgetPart > nwid_parts)
Test if a NWidgetTree is properly closed, meaning the number of container-type parts matches the numb...
Definition test_window_desc.cpp:68
_window_descs
std::vector< WindowDesc * > * _window_descs
List of WindowDescs.
Definition window.cpp:102
IsContainerWidgetType
bool IsContainerWidgetType(WidgetType tp)
Test if WidgetType is a container widget.
Definition widget.cpp:3353
WPT_ENDCONTAINER
@ WPT_ENDCONTAINER
Widget part to denote end of a container.
Definition widget_type.h:97