The Battle for Wesnoth  1.15.2+dev
iterator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 #include "config.hpp"
20 #include "config_cache.hpp"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/grid.hpp"
24 
25 #include <iostream>
26 #include <sstream>
27 #include <typeinfo>
28 
29 /*
30  * In the unit tests we use a widget tree that looks like:
31  *
32  * [0]
33  * \
34  * [1|2|3|4]
35  * \
36  * [5|6|7|8]
37  *
38  * Where widgets 0 and 1 are a grid and the rest of the widgets a label.
39  * The unit tests traverse the tree.
40  */
41 
42 static std::string top_down_t_t_t_result()
43 {
44  static const std::string result =
45  "At '0'. Iterate widget: reached the end. Iterate grid: failed. "
46  "Iterate child: proceed. Down and visit '1'.\n"
47  "At '1'. Iterate widget: reached the end. Iterate grid: failed. "
48  "Iterate child: proceed. Down and visit '5'.\n"
49  "At '5'. Iterate widget: reached the end. Iterate grid: failed. "
50  "Iterate child: reached the end. Up widget '5'. "
51  "Iterate: reached '6'. Down and visit '6'.\n"
52  "At '6'. Iterate widget: reached the end. Iterate grid: failed. "
53  "Iterate child: reached the end. Up widget '6'. "
54  "Iterate: reached '7'. Down and visit '7'.\n"
55  "At '7'. Iterate widget: reached the end. Iterate grid: failed. "
56  "Iterate child: reached the end. Up widget '7'. "
57  "Iterate: reached '8'. Down and visit '8'.\n"
58  "At '8'. Iterate widget: reached the end. Iterate grid: failed. "
59  "Iterate child: reached the end. Up widget '8'. "
60  "Iterate: failed. Up widget '1'. "
61  "Iterate: reached '2'. Down and visit '2'.\n"
62  "At '2'. Iterate widget: reached the end. Iterate grid: failed. "
63  "Iterate child: reached the end. Up widget '2'. "
64  "Iterate: reached '3'. Down and visit '3'.\n"
65  "At '3'. Iterate widget: reached the end. Iterate grid: failed. "
66  "Iterate child: reached the end. Up widget '3'. "
67  "Iterate: reached '4'. Down and visit '4'.\n"
68  "At '4'. Iterate widget: reached the end. Iterate grid: failed. "
69  "Iterate child: reached the end. Up widget '4'. "
70  "Iterate: failed. Finished iteration.\n";
71 
72  return result;
73 }
74 
75 static std::string bottom_up_t_t_t_result()
76 {
77  static const std::string result =
78  "Constructor: Down widget '1'. Down widget '5'. Finished at '5'.\n"
79  "At '5'. Iterate widget: reached the end. Iterate grid: failed. "
80  "Iterate child: Up '1'. Iterate child: visit '1'. "
81  "Down widget '6'. Visit '6'.\n"
82  "At '6'. Iterate widget: reached the end. Iterate grid: failed. "
83  "Iterate child: Up '1'. Iterate child: visit '1'. "
84  "Down widget '7'. Visit '7'.\n"
85  "At '7'. Iterate widget: reached the end. Iterate grid: failed. "
86  "Iterate child: Up '1'. Iterate child: visit '1'. "
87  "Down widget '8'. Visit '8'.\n"
88  "At '8'. Iterate widget: reached the end. Iterate grid: failed. "
89  "Iterate child: Up '1'. Iterate child: reached the end. Visit '1'.\n"
90  "At '1'. Iterate widget: reached the end. Iterate grid: failed. "
91  "Iterate child: Up '0'. Iterate child: visit '0'. "
92  "Down widget '2'. Visit '2'.\n"
93  "At '2'. Iterate widget: reached the end. Iterate grid: failed. "
94  "Iterate child: Up '0'. Iterate child: visit '0'. "
95  "Down widget '3'. Visit '3'.\n"
96  "At '3'. Iterate widget: reached the end. Iterate grid: failed. "
97  "Iterate child: Up '0'. Iterate child: visit '0'. "
98  "Down widget '4'. Visit '4'.\n"
99  "At '4'. Iterate widget: reached the end. Iterate grid: failed. "
100  "Iterate child: Up '0'. Iterate child: reached the end. Visit '0'.\n"
101  "At '0'. Iterate widget: reached the end. Iterate grid: failed. "
102  "Iterate child: Finished iteration.\n";
103 
104  return result;
105 }
106 
108  , gui2::widget* widget
109  , const std::string& id
110  , const unsigned row
111  , const unsigned column)
112 {
113  BOOST_REQUIRE_NE(widget, static_cast<gui2::widget*>(nullptr));
114 
115  widget->set_id(id);
116  grid.set_child(widget
117  , row
118  , column
121  , 0);
122 }
123 
124 template<class T>
125 static void test_control(T&& control)
126 {
127  {
129  true
130  , true
131  , true>>
132  iterator(control);
133 
134  /***** INITIAL STATE *****/
135 
136  BOOST_CHECK_EQUAL(iterator.at_end(), false);
137 
138  BOOST_CHECK_EQUAL(&*iterator, &control);
139 
140  /***** POST END *****/
141 
142  BOOST_CHECK_EQUAL(iterator.next(), false);
143 
144  BOOST_CHECK_EQUAL(iterator.at_end(), true);
145 
146  }
147  {
149  false
150  , true
151  , true>>
152  iterator(control);
153 
154  /***** INITIAL STATE *****/
155 
156  BOOST_CHECK_EQUAL(iterator.at_end(), true);
157  }
158 
159  {
161  BOOST_CHECK_EQUAL(iterator.at_end(), false);
162  }
163  {
165  BOOST_CHECK_EQUAL(iterator.at_end(), true);
166  }
167 }
168 
169 static void test_control()
170 {
171  /* Could add more widgets to the list. */
173 
174 }
175 
176 static void test_grid()
177 {
178  /* An empty grid behaves the same as a control so test here. */
180 
181  /* Test the child part here. */
182  gui2::grid grid(2 ,2);
183  grid.set_id("0");
184 
185  gui2::grid* g = new gui2::grid(2, 2);
186  add_widget(grid, g, "1", 0, 0);
190 
195 
196  {
197  std::stringstream sstr;
198  lg::redirect_output_setter redirect_output(sstr);
199 
201  true
202  , true
203  , true>>
204  iterator(grid);
205 
206  while(iterator.next()) {
207  /* DO NOTHING */
208  }
209 
210  BOOST_CHECK_EQUAL(top_down_t_t_t_result(), sstr.str());
211  }
212  {
213  std::stringstream sstr;
214  lg::redirect_output_setter redirect_output(sstr);
215 
217  true
218  , true
219  , true>>
220  iterator(grid);
221 
222  for( ; !iterator.at_end(); ++iterator) {
223  /* DO NOTHING */
224  }
225 
226  BOOST_CHECK_EQUAL(top_down_t_t_t_result(), sstr.str());
227  }
228  {
229  std::stringstream sstr;
230  lg::redirect_output_setter redirect_output(sstr);
231 
233  true
234  , true
235  , true>>
236  iterator(grid);
237 
238  while(iterator.next()) {
239  /* DO NOTHING */
240  }
241 
242  BOOST_CHECK_EQUAL(bottom_up_t_t_t_result(), sstr.str());
243  }
244  {
245  std::stringstream sstr;
246  lg::redirect_output_setter redirect_output(sstr);
247 
249  true
250  , true
251  , true>>
252  iterator(grid);
253 
254  for( ; !iterator.at_end(); ++iterator) {
255  /* DO NOTHING */
256  }
257 
258  BOOST_CHECK_EQUAL(bottom_up_t_t_t_result(), sstr.str());
259  }
260 }
261 
262 BOOST_AUTO_TEST_CASE(test_gui2_iterator)
263 {
264  /**** Initialize the environment. *****/
266 
267  cache.clear_defines();
268  cache.add_define("EDITOR");
269  cache.add_define("MULTIPLAYER");
270 
271  lg::set_log_domain_severity("gui/iterator", lg::debug());
272  lg::timestamps(false);
273 
274  std::stringstream sstr;
275  lg::redirect_output_setter redirect_output(sstr);
276 
277  test_control();
278  test_grid();
279 }
static config_cache & instance()
Get reference to the singleton object.
static void test_control(T &&control)
Definition: iterator.cpp:125
void timestamps(bool t)
Definition: log.cpp:75
Base class for all widgets.
Definition: widget.hpp:47
Label showing a text.
Definition: label.hpp:32
Helper class to redirect the output of the logger in a certain scope.
Definition: log.hpp:72
Definitions for the interface to Wesnoth Markup Language (WML).
Base container class.
Definition: grid.hpp:30
bool at_end() const
Has the iterator reached the end?
Definition: iterator.hpp:60
static void add_widget(gui2::grid &grid, gui2::widget *widget, const std::string &id, const unsigned row, const unsigned column)
Definition: iterator.cpp:107
logger & debug()
Definition: log.cpp:96
static std::string top_down_t_t_t_result()
Definition: iterator.cpp:42
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:69
void clear_defines()
Clear stored defines map to default values.
double g
Definition: astarsearch.cpp:64
static tcache cache
Definition: minimap.cpp:134
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:55
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:48
bool set_log_domain_severity(const std::string &name, int severity)
Definition: log.cpp:118
bool grid()
Definition: general.cpp:505
The iterator class.
Definition: iterator.hpp:38
static std::string bottom_up_t_t_t_result()
Definition: iterator.cpp:75
void set_id(const std::string &id)
Definition: widget.cpp:95
BOOST_AUTO_TEST_CASE(test_gui2_iterator)
Definition: iterator.cpp:262
Contains the base iterator class for the gui2 widgets.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
static void test_grid()
Definition: iterator.cpp:176
void add_define(const std::string &define)
Add a entry to preproc defines map.
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
Singleton class to manage game config file caching.