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

OpenTTD Source 14.0-beta3

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 <http://www.gnu.org/licenses/>.
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
22 extern std::vector<WindowDesc*> *_window_descs;
23
24
25 class WindowDescTestsFixture {
26 private:
27 MockEnvironment &mock = MockEnvironment::Instance();
28 };
29
30
31 TEST_CASE("WindowDesc - ini_key uniqueness")
32 {
33 std::set<std::string> seen;
34
35 for (const WindowDesc *window_desc : *_window_descs) {
36
37 if (window_desc->ini_key == nullptr) continue;
38
39 CAPTURE(window_desc->ini_key);
40 CHECK((seen.find(window_desc->ini_key) == std::end(seen)));
41
42 seen.insert(window_desc->ini_key);
43 }
44 }
45
46 TEST_CASE("WindowDesc - ini_key validity")
47 {
48 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
49
50 bool has_inikey = window_desc->ini_key != nullptr;
51 bool has_widget = std::any_of(window_desc->nwid_begin, window_desc->nwid_end, [](const NWidgetPart &part) { return part.type == WWT_DEFSIZEBOX || part.type == WWT_STICKYBOX; });
52
53 INFO(fmt::format("{}:{}", window_desc->file, window_desc->line));
54 CAPTURE(has_inikey);
55 CAPTURE(has_widget);
56
57 CHECK((has_widget == has_inikey));
58 }
59
67 static bool IsNWidgetTreeClosed(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end)
68 {
69 int depth = 0;
70 for (; nwid_begin < nwid_end; ++nwid_begin) {
71 if (IsContainerWidgetType(nwid_begin->type)) ++depth;
72 if (nwid_begin->type == WPT_ENDCONTAINER) --depth;
73 }
74 return depth == 0;
75 }
76
77 TEST_CASE("WindowDesc - NWidgetParts properly closed")
78 {
79 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
80
81 INFO(fmt::format("{}:{}", window_desc->file, window_desc->line));
82
83 CHECK(IsNWidgetTreeClosed(window_desc->nwid_begin, window_desc->nwid_end));
84 }
85
86 TEST_CASE_METHOD(WindowDescTestsFixture, "WindowDesc - NWidgetPart validity")
87 {
88 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
89
90 INFO(fmt::format("{}:{}", window_desc->file, window_desc->line));
91
92 NWidgetStacked *shade_select = nullptr;
93 std::unique_ptr<NWidgetBase> root = nullptr;
94
95 REQUIRE_NOTHROW(root = MakeWindowNWidgetTree(window_desc->nwid_begin, window_desc->nwid_end, &shade_select));
96 CHECK((root != nullptr));
97 }
WindowDescTestsFixture
Definition: test_window_desc.cpp:25
mock_environment.h
WindowDesc::line
const int line
Source line of this definition.
Definition: window_gui.h:162
WindowDesc::nwid_end
const NWidgetPart * nwid_end
Ending of nested widget parts describing the window.
Definition: window_gui.h:169
MakeWindowNWidgetTree
std::unique_ptr< NWidgetBase > MakeWindowNWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetStacked **shade_select)
Make a nested widget tree for a window from a parts array.
Definition: widget.cpp:3157
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:1038
WindowDesc
High level window description.
Definition: window_gui.h:153
WindowDesc::ini_key
const char * ini_key
Key to store window defaults in openttd.cfg. nullptr if nothing shall be stored.
Definition: window_gui.h:166
IsNWidgetTreeClosed
static bool IsNWidgetTreeClosed(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end)
Test if a NWidgetTree is properly closed, meaning the number of container-type parts matches the numb...
Definition: test_window_desc.cpp:67
_window_descs
std::vector< WindowDesc * > * _window_descs
List of WindowDescs.
Definition: window.cpp:99
MockEnvironment
Singleton class to set up the mock environemnt once.
Definition: mock_environment.h:17
IsContainerWidgetType
bool IsContainerWidgetType(WidgetType tp)
Test if WidgetType is a container widget.
Definition: widget.cpp:3079
NWidgetStacked
Stacked widgets, widgets all occupying the same space in the window.
Definition: widget_type.h:484
NWidgetPart::type
WidgetType type
Type of the part.
Definition: widget_type.h:1039
WindowDesc::file
const char *const file
Source file of this definition.
Definition: window_gui.h:161
WindowDesc::nwid_begin
const NWidgetPart * nwid_begin
Beginning of nested widget parts describing the window.
Definition: window_gui.h:168
WPT_ENDCONTAINER
@ WPT_ENDCONTAINER
Widget part to denote end of a container.
Definition: widget_type.h:100