OpenTTD Source  20240917-master-g9ab0a47812
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 
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(std::begin(window_desc->nwid_parts), std::end(window_desc->nwid_parts), [](const NWidgetPart &part) { return part.type == WWT_DEFSIZEBOX || part.type == WWT_STICKYBOX; });
52 
53  INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
54  CAPTURE(has_inikey);
55  CAPTURE(has_widget);
56 
57  CHECK((has_widget == has_inikey));
58 }
59 
66 static bool IsNWidgetTreeClosed(std::span<const NWidgetPart> nwid_parts)
67 {
68  int depth = 0;
69  for (const auto nwid : nwid_parts) {
70  if (IsContainerWidgetType(nwid.type)) ++depth;
71  if (nwid.type == WPT_ENDCONTAINER) --depth;
72  }
73  return depth == 0;
74 }
75 
76 TEST_CASE("WindowDesc - NWidgetParts properly closed")
77 {
78  const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
79 
80  INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
81 
82  CHECK(IsNWidgetTreeClosed(window_desc->nwid_parts));
83 }
84 
85 TEST_CASE_METHOD(WindowDescTestsFixture, "WindowDesc - NWidgetPart validity")
86 {
87  const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
88 
89  INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
90 
91  NWidgetStacked *shade_select = nullptr;
92  std::unique_ptr<NWidgetBase> root = nullptr;
93 
94  REQUIRE_NOTHROW(root = MakeWindowNWidgetTree(window_desc->nwid_parts, &shade_select));
95  CHECK((root != nullptr));
96 }
WindowDescTestsFixture
Definition: test_window_desc.cpp:25
mock_environment.h
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:66
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:1077
WindowDesc
High level window description.
Definition: window_gui.h:162
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:175
WindowDesc::source_location
const std::source_location source_location
Source location of this definition.
Definition: window_gui.h:171
_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:3153
NWidgetStacked
Stacked widgets, widgets all occupying the same space in the window.
Definition: widget_type.h:500
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:3229
WindowDesc::nwid_parts
const std::span< const NWidgetPart > nwid_parts
Span of nested widget parts describing the window.
Definition: window_gui.h:177
WPT_ENDCONTAINER
@ WPT_ENDCONTAINER
Widget part to denote end of a container.
Definition: widget_type.h:106