The Battle for Wesnoth  1.17.0-dev
visitor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include <boost/test/unit_test.hpp>
19 
20 #include "config_cache.hpp"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/grid.hpp"
24 
25 #include <iostream>
26 #include <typeinfo>
27 
29  , gui2::widget* widget
30  , const std::string& id
31  , const unsigned row
32  , const unsigned column)
33 {
34  BOOST_REQUIRE_NE(widget, static_cast<gui2::widget*>(nullptr));
35 
36  widget->set_id(id);
37  grid.set_child(widget
38  , row
39  , column
42  , 0);
43 }
44 
45 template<class T>
46 static void test_control(T&& control)
47 {
48  //std::cerr << __func__ << ": " << typeid(T).name() << ".\n";
49 
50  const std::unique_ptr<gui2::iteration::walker_base> visitor(control.create_walker());
51 
52  BOOST_REQUIRE_NE(visitor.get(), static_cast<void*>(nullptr));
53 
54  /***** INITIAL STATE *****/
55 
56  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::self), false);
57  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::internal), true);
58  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
59 
60  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::self), &control);
61  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::internal), static_cast<void*>(nullptr));
62  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
63 
64  /***** VISITING WIDGET *****/
65 
69 
70  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::self), true);
71  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::internal), true);
72  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
73 
74  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::self), static_cast<void*>(nullptr));
75  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::internal), static_cast<void*>(nullptr));
76  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
77 
78  /***** POST END *****/
79 
81 }
82 
83 static void test_control()
84 {
85  /* Could add more widgets to the list. */
87 }
88 
89 static void test_grid()
90 {
91  /* An empty grid behaves the same as a control so test here. */
93 
94  //std::cerr << __func__ << ": Detailed test.\n";
95 
96  /* Test the child part here. */
97  gui2::grid grid(2 ,2);
102 
103  const std::unique_ptr<gui2::iteration::walker_base> visitor(grid.create_walker());
104 
105  /***** LABEL 1,1 *****/
106 
107  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
108 
109  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
110  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(1,1)");
111 
112  /***** LABEL 2,1 *****/
113 
115 
116  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
117 
118  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
119  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(2,1)");
120 
121  /***** LABEL 1,2 *****/
122 
124 
125  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
126 
127  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
128  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(1,2)");
129 
130  /***** LABEL 2,2 *****/
131 
133 
134  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
135 
136  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
137  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(2,2)");
138 
139  /***** END *****/
140 
142 
143  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
144 
145  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
146 
147  /***** POST END *****/
148 
150 }
151 
152 BOOST_AUTO_TEST_CASE(test_gui2_visitor)
153 {
154 
155  /**** Initialize the environment. *****/
157 
158  cache.clear_defines();
159  cache.add_define("EDITOR");
160  cache.add_define("MULTIPLAYER");
161 
162  test_control();
163  test_grid();
164 }
static config_cache & instance()
Get reference to the singleton object.
When calling next the following it has the following results.
Definition: walker.hpp:69
When calling next the following it has the following results.
Definition: walker.hpp:58
Visit its nested grid.
Definition: walker.hpp:39
Base class for all widgets.
Definition: widget.hpp:49
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:57
static void add_widget(gui2::grid &grid, gui2::widget *widget, const std::string &id, const unsigned row, const unsigned column)
Definition: visitor.cpp:28
Base container class.
Definition: grid.hpp:31
static void test_control(T &&control)
Definition: visitor.cpp:46
void set_child(widget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:70
Visit the widget itself.
Definition: walker.hpp:37
void clear_defines()
Clear stored defines map to default values.
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: grid.cpp:700
static void test_grid()
Definition: visitor.cpp:89
static tcache cache
Definition: minimap.cpp:124
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:56
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:49
bool grid()
Definition: general.cpp:535
void set_id(const std::string &id)
Definition: widget.cpp:98
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
Visit the children of its nested grid.
Definition: walker.hpp:41
BOOST_AUTO_TEST_CASE(test_gui2_visitor)
Definition: visitor.cpp:152
void add_define(const std::string &define)
Add a entry to preproc defines map.
Singleton class to manage game config file caching.