The Battle for Wesnoth  1.15.1+dev
visitor.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_cache.hpp"
21 #include "gui/widgets/label.hpp"
22 #include "gui/widgets/grid.hpp"
23 
24 #include <iostream>
25 #include <typeinfo>
26 
28  , gui2::widget* widget
29  , const std::string& id
30  , const unsigned row
31  , const unsigned column)
32 {
33  BOOST_REQUIRE_NE(widget, static_cast<gui2::widget*>(nullptr));
34 
35  widget->set_id(id);
36  grid.set_child(widget
37  , row
38  , column
41  , 0);
42 }
43 
44 template<class T>
45 static void test_control(T&& control)
46 {
47  //std::cerr << __func__ << ": " << typeid(T).name() << ".\n";
48 
49  const std::unique_ptr<gui2::iteration::walker_base> visitor(control.create_walker());
50 
51  BOOST_REQUIRE_NE(visitor.get(), static_cast<void*>(nullptr));
52 
53  /***** INITIAL STATE *****/
54 
55  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::self), false);
56  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::internal), true);
57  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
58 
59  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::self), &control);
60  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::internal), static_cast<void*>(nullptr));
61  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
62 
63  /***** VISITING WIDGET *****/
64 
68 
69  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::self), true);
70  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::internal), true);
71  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
72 
73  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::self), static_cast<void*>(nullptr));
74  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::internal), static_cast<void*>(nullptr));
75  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
76 
77  /***** POST END *****/
78 
80 }
81 
82 static void test_control()
83 {
84  /* Could add more widgets to the list. */
86 }
87 
88 static void test_grid()
89 {
90  /* An empty grid behaves the same as a control so test here. */
92 
93  //std::cerr << __func__ << ": Detailed test.\n";
94 
95  /* Test the child part here. */
96  gui2::grid grid(2 ,2);
101 
102  const std::unique_ptr<gui2::iteration::walker_base> visitor(grid.create_walker());
103 
104  /***** LABEL 1,1 *****/
105 
106  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
107 
108  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
109  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(1,1)");
110 
111  /***** LABEL 2,1 *****/
112 
114 
115  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
116 
117  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
118  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(2,1)");
119 
120  /***** LABEL 1,2 *****/
121 
123 
124  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
125 
126  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
127  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(1,2)");
128 
129  /***** LABEL 2,2 *****/
130 
132 
133  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
134 
135  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
136  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(2,2)");
137 
138  /***** END *****/
139 
141 
142  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
143 
144  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
145 
146  /***** POST END *****/
147 
149 }
150 
151 BOOST_AUTO_TEST_CASE(test_gui2_visitor)
152 {
153 
154  /**** Initialize the environment. *****/
156 
157  cache.clear_defines();
158  cache.add_define("EDITOR");
159  cache.add_define("MULTIPLAYER");
160 
161  test_control();
162  test_grid();
163 }
static config_cache & instance()
Get reference to the singleton object.
When calling next the following it has the following results.
Definition: walker.hpp:68
When calling next the following it has the following results.
Definition: walker.hpp:57
Visit its nested grid.
Definition: walker.hpp:38
Base class for all widgets.
Definition: widget.hpp:47
Label showing a text.
Definition: label.hpp:32
static void add_widget(gui2::grid &grid, gui2::widget *widget, const std::string &id, const unsigned row, const unsigned column)
Definition: visitor.cpp:27
Base container class.
Definition: grid.hpp:30
static void test_control(T &&control)
Definition: visitor.cpp:45
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
Visit the widget itself.
Definition: walker.hpp:36
void clear_defines()
Clear stored defines map to default values.
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: grid.cpp:699
static void test_grid()
Definition: visitor.cpp:88
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 grid()
Definition: general.cpp:505
void set_id(const std::string &id)
Definition: widget.cpp:95
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
Visit the children of its nested grid.
Definition: walker.hpp:40
BOOST_AUTO_TEST_CASE(test_gui2_visitor)
Definition: visitor.cpp:151
void add_define(const std::string &define)
Add a entry to preproc defines map.
Singleton class to manage game config file caching.