The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
test_config.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2014 - 2025
3 by Chris Beck <render787@gmail.com>
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-test"
17
18#include <boost/test/unit_test.hpp>
19// Work around a Boost<1.67 bug fixed in 1.67: Include test_case.hpp after
20// unit_tests.hpp. https://svn.boost.org/trac10/ticket/13387
21#include <boost/test/data/test_case.hpp> // for parametrized test
22#include <cmath>
23#include <limits>
24
25#include "config.hpp"
26#include "variable_info.hpp"
27
28BOOST_AUTO_TEST_SUITE(test_config)
29
30BOOST_AUTO_TEST_CASE(test_config_attribute_value)
31{
32 config c;
33 config c1;
34 config c2;
35 const config& cc = c;
36 int x_int;
37 std::string x_str;
38 long long x_sll;
39 double x_dbl;
40
41// compare identical assigned int vs string
42 c1["x"] = 6;
43 c2["x"] = "6";
44 BOOST_CHECK_EQUAL(c1["x"], c2["x"]);
45
46// compare identical assigned int vs floating point
47 c1["x"] = 6;
48 c2["x"] = 6.0;
49 BOOST_CHECK_EQUAL(c1["x"], c2["x"]);
50
51// compare identical assigned int-string vs floating point
52 c1["x"] = "6";
53 c2["x"] = 6.0;
54 BOOST_CHECK_EQUAL(c1["x"], c2["x"]);
55
56// compare identical assigned floating point-string vs int
57 c1["x"] = 6;
58 c2["x"] = "6.0";
59 BOOST_CHECK_NE(c1["x"], c2["x"]);
60
61// compare identical assigned floating point vs string
62 c1["x"] = 6.0;
63 c2["x"] = "6.0";
64 BOOST_CHECK_NE(c1["x"], c2["x"]);
65
66// check what happens when trying to get a numeric result from a non-numeric value
67 c["x"] = "1aaaa";
68 x_str = c["x"].str();
69 BOOST_CHECK_EQUAL(x_str, "1aaaa");
70 x_int = c["x"].to_int();
71 BOOST_CHECK_EQUAL(x_int, 1);
72 x_sll = c["x"].to_long_long();
73 BOOST_CHECK_EQUAL(x_sll, 1ll);
74 x_dbl = c["x"].to_double();
75 BOOST_CHECK_EQUAL(x_dbl, 1.0);
76
77 c["x"] = "1.7aaaa";
78 x_str = c["x"].str();
79 BOOST_CHECK_EQUAL(x_str, "1.7aaaa");
80 x_int = c["x"].to_int();
81 BOOST_CHECK_EQUAL(x_int, 1);
82 x_sll = c["x"].to_long_long();
83 BOOST_CHECK_EQUAL(x_sll, 1ll);
84 x_dbl = c["x"].to_double();
85 BOOST_CHECK_EQUAL(x_dbl, 1.7);
86
87 c["x"] = "aaaa1";
88 x_str = c["x"].str();
89 BOOST_CHECK_EQUAL(x_str, "aaaa1");
90 x_int = c["x"].to_int();
91 BOOST_CHECK_EQUAL(x_int, 0);
92 x_sll = c["x"].to_long_long();
93 BOOST_CHECK_EQUAL(x_sll, 0ll);
94 x_dbl = c["x"].to_double();
95 BOOST_CHECK_EQUAL(x_dbl, 0.0);
96
97// check type conversion when assigned as int
98 c["x"] = 1;
99 x_str = c["x"].str();
100 BOOST_CHECK_EQUAL(x_str, "1");
101 x_int = c["x"].to_int();
102 BOOST_CHECK_EQUAL(x_int, 1);
103 x_sll = c["x"].to_long_long();
104 BOOST_CHECK_EQUAL(x_sll, 1ll);
105 x_dbl = c["x"].to_double();
106 BOOST_CHECK_EQUAL(x_dbl, 1.0);
107
108// check type conversion when assigned as int (again)
109 c["x"] = 10000000;
110 x_int = c["x"].to_int();
111 BOOST_CHECK_EQUAL(x_int, 10000000);
112 x_str = c["x"].str();
113 BOOST_CHECK_EQUAL(x_str, "10000000");
114 x_sll = c["x"].to_long_long();
115 BOOST_CHECK_EQUAL(x_sll, 10000000ll);
116 x_dbl = c["x"].to_double();
117 BOOST_CHECK_EQUAL(x_dbl, 1e7);
118
119// check type conversion when assigned aan empty string
120 c["x"] = "";
121 x_sll = c["x"].to_long_long();
122 BOOST_CHECK_EQUAL(x_sll, 0ll);
123 x_str = c["x"].str();
124 BOOST_CHECK_EQUAL(x_str, "");
125 x_int = c["x"].to_int();
126 BOOST_CHECK_EQUAL(x_int, 0);
127 x_dbl = c["x"].to_double();
128 BOOST_CHECK_EQUAL(x_dbl, 0.0);
129
130// check type conversion when assigned as a hex string
131 c["x"] = "0x11";
132 x_int = c["x"].to_int();
133 BOOST_CHECK_EQUAL(x_int, 0);
134 x_str = c["x"].str();
135 BOOST_CHECK_EQUAL(x_str, "0x11");
136 x_sll = c["x"].to_long_long();
137 BOOST_CHECK_EQUAL(x_sll, 0ll);
138 x_dbl = c["x"].to_double();
139 BOOST_CHECK_EQUAL(x_dbl, 0.0);
140
141// check type conversion when assigned as a hex string (again)
142 c["x"] = "0xab";
143 x_int = c["x"].to_int();
144 BOOST_CHECK_EQUAL(x_int, 0);
145 x_str = c["x"].str();
146 BOOST_CHECK_EQUAL(x_str, "0xab");
147 x_sll = c["x"].to_long_long();
148 BOOST_CHECK_EQUAL(x_sll, 0ll);
149 x_dbl = c["x"].to_double();
150 BOOST_CHECK_EQUAL(x_dbl, 0.0);
151
152// check type conversion when assigned as a string with leading zeroes
153 c["x"] = "00001111";
154 x_int = c["x"].to_int();
155 BOOST_CHECK_EQUAL(x_int, 1111);
156 x_str = c["x"].str();
157 BOOST_CHECK_EQUAL(x_str, "00001111");
158 x_sll = c["x"].to_long_long();
159 BOOST_CHECK_EQUAL(x_sll, 1111ll);
160 x_dbl = c["x"].to_double();
161 BOOST_CHECK_EQUAL(x_dbl, 1.111e3);
162
163// check type conversion when assigned as a string with only zeroes
164 c["x"] = "000000";
165 x_int = c["x"].to_int();
166 BOOST_CHECK_EQUAL(x_int, 0);
167 x_str = c["x"].str();
168 BOOST_CHECK_EQUAL(x_str, "000000");
169 x_sll = c["x"].to_long_long();
170 BOOST_CHECK_EQUAL(x_sll, 0ll);
171 x_dbl = c["x"].to_double();
172 BOOST_CHECK_EQUAL(x_dbl, 0.0);
173
174// check type conversion when assigned as a string with leading zeroes and is too large to fit in an int
175 c["x"] = "01234567890123456789";
176 x_sll = c["x"].to_long_long();
177 BOOST_CHECK_EQUAL(x_sll, 1234567890123456789ll);
178 x_str = c["x"].str();
179 BOOST_CHECK_EQUAL(x_str, "01234567890123456789");
180 x_int = c["x"].to_int();
181 BOOST_CHECK_EQUAL(x_int, 0);
182 x_dbl = c["x"].to_double();
183 BOOST_CHECK_EQUAL(x_dbl, 1.23456789012345678e18);
184
185// check type conversion when assigned as a string with no leading zeroes and is too large to fit in an int
186 c["x"] = "99999999999999999999";
187 x_sll = c["x"].to_long_long();
188 BOOST_CHECK_EQUAL(x_sll, 0ll);
189 x_str = c["x"].str();
190 BOOST_CHECK_EQUAL(x_str, "99999999999999999999");
191 x_int = c["x"].to_int();
192 BOOST_CHECK_EQUAL(x_int, 0);
193 x_dbl = c["x"].to_double();
194 BOOST_CHECK_EQUAL(x_dbl, 1e20);
195
196// check type conversion when assigned as a floating point
197 c["x"] = 1.499;
198 x_sll = c["x"].to_long_long();
199 BOOST_CHECK_EQUAL(x_sll, 1ll);
200 x_str = c["x"].str();
201 BOOST_CHECK_EQUAL(x_str, "1.499");
202 x_int = c["x"].to_int();
203 BOOST_CHECK_EQUAL(x_int, 1);
204 x_dbl = c["x"].to_double();
205 BOOST_CHECK(std::abs(x_dbl - 1.499) < 1e-6);
206
207// check type conversion when assigned as a long long (int overflows and saturates)
208 c["x"] = 123456789123ll;
209 x_int = c["x"].to_int();
210 BOOST_CHECK_EQUAL(x_int, std::numeric_limits<int>::max());
211 x_dbl = c["x"].to_double();
212 BOOST_CHECK_EQUAL(x_dbl, 1.23456789123e11);
213 x_sll = c["x"].to_long_long();
214 BOOST_CHECK_EQUAL(x_sll, 123456789123ll);
215 x_str = c["x"].str();
216 BOOST_CHECK_EQUAL(x_str, "123456789123");
217
218// check heterogeneous comparison
219 using namespace std::literals;
220
221 c["x"] = "1";
222 BOOST_CHECK_EQUAL(c["x"], "1");
223 c["x"] = 222;
224 BOOST_CHECK_EQUAL(c["x"], "222");
225 BOOST_CHECK_EQUAL(c["x"], "222"s);
226 c["x"] = "test";
227 BOOST_CHECK_EQUAL(c["x"], "test"sv);
228 c["x"] = false;
229 BOOST_CHECK_EQUAL(c["x"], "no");
230 BOOST_CHECK_EQUAL(c["x"], "false");
231 c["x"] = 9.87654321;
232 BOOST_CHECK_EQUAL(c["x"], "9.87654321");
233 BOOST_CHECK_EQUAL(c["x"], t_string{"9.87654321"});
234 BOOST_CHECK_EQUAL(c["x"], config_attribute_value::create("9.87654321"));
235 c["x"] = "sfvsdgdsfg";
236 BOOST_CHECK_EQUAL(c["x"], "sfvsdgdsfg");
237 BOOST_CHECK_EQUAL(c["x"], "sfvsdgdsfg"s);
238 BOOST_CHECK_EQUAL(c["x"], "sfvsdgdsfg"sv);
239 BOOST_CHECK_EQUAL(c["x"], t_string{"sfvsdgdsfg"});
240 BOOST_CHECK_NE(c["x"], "a random string");
241
242 // reversed order heterogenous comparison
243 c["x"] = 9.87654321;
244 BOOST_CHECK_EQUAL("9.87654321", c["x"]);
245 BOOST_CHECK_EQUAL(t_string{"9.87654321"}, c["x"]);
246 BOOST_CHECK_EQUAL(config_attribute_value::create("9.87654321"), c["x"]);
247 BOOST_CHECK_NE("1.23456789", c["x"]);
248 BOOST_CHECK_NE("1.23456789"s, c["x"]);
249 BOOST_CHECK_NE("1.23456789"sv, c["x"]);
250
251 // blank != "" test.
252 c.clear();
253 BOOST_CHECK(cc["x"] != "");
254 BOOST_CHECK(cc["x"].empty());
255 BOOST_CHECK(cc["x"].blank());
256
257 BOOST_CHECK(c["x"] != "");
258 BOOST_CHECK(c["x"].empty());
259 BOOST_CHECK(c["x"].blank());
260
261 BOOST_CHECK_EQUAL(cc["x"], c["x"]);
262
263 c["x"] = "";
264 BOOST_CHECK(cc["x"].empty());
265 BOOST_CHECK(cc["x"].empty());
266 BOOST_CHECK(!cc["x"].blank());
267
268 BOOST_CHECK(c["x"].empty());
269 BOOST_CHECK(c["x"].empty());
270 BOOST_CHECK(!c["x"].blank());
271
272 BOOST_CHECK_EQUAL(cc["x"], c["x"]);
273}
274
275BOOST_AUTO_TEST_CASE(test_variable_info)
276{
277 // Returns the integer value of the given variable in the config
278 const auto scalar_value = [](const std::string& var, const config& cfg) {
279 return variable_access_const{var, cfg}.as_scalar().to_int();
280 };
281
282 config c;
283 {
284 variable_access_const access("", c);
285 // We dotn allow empty keys
286 BOOST_CHECK_THROW(access.as_scalar(), invalid_variablename_exception);
287 }
288 {
289 variable_access_const access("some_non_existent.", c);
290 // We dotn allow empty keys
291 BOOST_CHECK_THROW(access.as_scalar(), invalid_variablename_exception);
292 }
293 {
294 variable_access_const access("some_non_existent[0]value", c);
295 // We expect '.' after ']'
296 BOOST_CHECK_THROW(access.as_scalar(), invalid_variablename_exception);
297 }
298 {
299 variable_access_const access("some_non_existent", c);
300 // we return empty be default
301 BOOST_CHECK(!access.exists_as_container());
302 BOOST_CHECK_EQUAL(access.as_container(), config());
303 BOOST_CHECK(!access.exists_as_attribute());
304 BOOST_CHECK_EQUAL(access.as_scalar(), config::attribute_value());
305 }
306 {
307 variable_access_const access("a.b[0].c[1].d.e.f[2]", c);
308 // we return empty be default
309 BOOST_CHECK(!access.exists_as_container());
310 BOOST_CHECK_EQUAL(access.as_container(), config());
311 // Explicit indexes can never be an attribute
312 BOOST_CHECK_THROW(access.as_scalar(), invalid_variablename_exception);
313 }
314 BOOST_CHECK(c.empty());
315 {
316 config c2;
317 variable_access_create access("a.b[0].c[1].d.e.f[2].g", c2);
318 access.as_scalar() = 84;
319 BOOST_CHECK_EQUAL(scalar_value("a.length", c2), 1);
320 BOOST_CHECK_EQUAL(scalar_value("a.b.length", c2), 1);
321 BOOST_CHECK_EQUAL(scalar_value("a.b.c.length", c2), 2);
322 BOOST_CHECK_EQUAL(scalar_value("a.b.c[1].d.e.f.length", c2), 3);
323 // we set g as a scalar
324 BOOST_CHECK_EQUAL(scalar_value("a.b.c[1].d.e.f[2].g.length", c2), 0);
325 BOOST_CHECK_EQUAL(scalar_value("a.b.c[1].d.e.f[2].g", c2), 84);
326 }
327 {
328 config c2;
329 variable_access_throw access("a.b[9].c", c2);
330 BOOST_CHECK_THROW(access.as_scalar(), invalid_variablename_exception);
331 }
332 {
333 /* clang-format off */
334 const config nonempty{
335 "tag1", config(),
336 "tag1", config{
337 "tag2", config(),
338 "tag2", config(),
339 "tag2", config{
340 "atribute1", 88,
341 "atribute2", "value",
342 },
343 },
344 "tag1", config(),
345 };
346 /* clang-format on */
347 /** This is the config:
348 [tag1]
349 [/tag1]
350 [tag1]
351 [tag2]
352 [/tag2]
353 [tag2]
354 [/tag2]
355 [tag2]
356 atribute1 = 88
357 atribute2 = "value"
358 [/tag2]
359 [/tag1]
360 [tag1]
361 [/tag1]
362 */
363 BOOST_CHECK_EQUAL(scalar_value("tag1.length", nonempty), 3);
364 BOOST_CHECK_EQUAL(scalar_value("tag1.tag2.length", nonempty), 0);
365 BOOST_CHECK_EQUAL(scalar_value("tag1[1].tag2.length", nonempty), 3);
366 BOOST_CHECK_EQUAL(scalar_value("tag1[1].tag2[2].atribute1", nonempty), 88);
367 int count = 0;
368 for([[maybe_unused]] const config& child : variable_access_const("tag1", nonempty).as_array()) {
369 ++count;
370 }
371 BOOST_CHECK_EQUAL(count, 3);
372 count = 0;
373 for([[maybe_unused]] const config& child : variable_access_const("tag1.tag2", nonempty).as_array()) {
374 ++count;
375 }
376 BOOST_CHECK_EQUAL(count, 0);
377 count = 0;
378 // explicit indexes as range always return a one element range, whether they exist or not.
379 for([[maybe_unused]] const config& child : variable_access_const("tag1.tag2[5]", nonempty).as_array()) {
380 ++count;
381 }
382 BOOST_CHECK_EQUAL(count, 1);
383 }
384}
385
386BOOST_AUTO_TEST_CASE(add_child_EmptyThis_newKey_AppendAndReturnNewEmptyChild)
387{
388 config actual;
389 const config new_child = actual.add_child("A");
390 const config expected("A");
391 BOOST_CHECK_EQUAL(actual, expected);
392 BOOST_CHECK_EQUAL(new_child, config());
393}
394
395namespace bdata = boost::unit_test::data;
396BOOST_DATA_TEST_CASE(add_child_NonEmptyThis_newOrExistingKey_lOrRValue_AppendAndReturnNewChild,
397 bdata::make({"A", "B", "C"}) * bdata::make<std::string>({"lvalue_ref", "rvalue_ref"}),
398 key,
399 update_ref_kind) // 3 * 2 = 6 cases
400{
401 // Data for testing base.add_child(key, update)
402 const config base{"A", config(), "a", 1}; // [A][/A] a = 1
403 const config update{"B", config(), "b", 2}; // [B][/B] b = 2
404 // Expected config: [A][/A] a = 1 [key] [B][/B] b = 2 [/key]
405 /* clang-format off */
406 const config expected{
407 // base
408 "A", config(), "a", 1,
409 // [key] copy of update [/key]
410 key, config(update)};
411 /* clang-format on */
412 // Make actual
413 config actual(base);
414 config new_child;
415 if(update_ref_kind == std::string("lvalue_ref"))
416 new_child = actual.add_child(key, update);
417 else
418 new_child = actual.add_child(key, config(update)); // rvalue ref.
419
420 BOOST_CHECK_EQUAL(actual, expected);
421 BOOST_CHECK_EQUAL(new_child, update);
422 // Assert the new child is a copy of update
423 BOOST_CHECK_NE(&new_child, &update);
424}
425
426BOOST_AUTO_TEST_SUITE_END()
Variant for storing WML attributes.
static config_attribute_value create(const T val)
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
config & add_child(std::string_view key)
Definition config.cpp:428
Additional functionality for a non-const variable_info.
Information on a WML variable.
maybe_const_t< config::attribute_value, V > & as_scalar() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
bool exists_as_container() const
maybe_const_t< config, V > & as_container() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
bool exists_as_attribute() const
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
BOOST_AUTO_TEST_CASE(test_config_attribute_value)
BOOST_DATA_TEST_CASE(add_child_NonEmptyThis_newOrExistingKey_lOrRValue_AppendAndReturnNewChild, bdata::make({"A", "B", "C"}) *bdata::make< std::string >({"lvalue_ref", "rvalue_ref"}), key, update_ref_kind)
BOOST_AUTO_TEST_SUITE(filesystem)
static map_location::direction s
variable_info< const variable_info_implementation::vi_policy_const > variable_access_const
Read-only access.
#define e
#define c