The Battle for Wesnoth  1.19.7+dev
test_config_filters.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2024
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-test"
16 
17 #include <boost/test/unit_test.hpp>
18 // Work around a Boost<1.67 bug fixed in 1.67: Include test_case.hpp after
19 // unit_tests.hpp. https://svn.boost.org/trac10/ticket/13387
20 #include <boost/test/data/test_case.hpp> // for parametrized test
21 
22 #include "utils/config_filters.hpp"
23 
24 using namespace utils::config_filters;
25 
26 BOOST_AUTO_TEST_SUITE(config_filters)
27 
28 BOOST_AUTO_TEST_CASE(test_int_signed_filter)
29 {
30  // These can be used both as the filter and as the input to be filtered.
31  config minus_3 {"x", -3};
32  config plus_3 {"x", 3};
33 
34  config any_negative {"x", "-infinity--1"};
35  config any_positive {"x", "1-infinity"};
36  config any_number {"x", "-infinity-infinity"};
37 
38  BOOST_ASSERT(int_matches_if_present(minus_3, minus_3, "x"));
39  BOOST_ASSERT(int_matches_if_present(plus_3, plus_3, "x"));
40 
41  BOOST_ASSERT(!int_matches_if_present(minus_3, plus_3, "x"));
42  BOOST_ASSERT(!int_matches_if_present(plus_3, minus_3, "x"));
43 
44  BOOST_ASSERT(int_matches_if_present(any_negative, minus_3, "x"));
45  BOOST_ASSERT(!int_matches_if_present(any_positive, minus_3, "x"));
46  BOOST_ASSERT(int_matches_if_present(any_number, minus_3, "x"));
47 
48  BOOST_ASSERT(!int_matches_if_present(any_negative, plus_3, "x"));
49  BOOST_ASSERT(int_matches_if_present(any_positive, plus_3, "x"));
50  BOOST_ASSERT(int_matches_if_present(any_number, plus_3, "x"));
51 }
52 
53 BOOST_AUTO_TEST_CASE(test_int_add_sub_filter)
54 {
55  config add_minus_3 {"add", -3};
56  config sub_3 {"sub", 3};
57 
58  BOOST_ASSERT(int_matches_if_present(add_minus_3, add_minus_3, "add"));
59  BOOST_ASSERT(int_matches_if_present_or_negative(add_minus_3, add_minus_3, "add", "sub"));
60  BOOST_ASSERT(int_matches_if_present(add_minus_3, add_minus_3, "some_other_key"));
61 
62  // A range, and one that only matches positive numbers
63  config add_2_to_4 {"add", "2-4"};
64  config sub_2_to_4 {"sub", "2-4"};
65 
66  BOOST_ASSERT(!int_matches_if_present(add_2_to_4, add_minus_3, "add"));
67  BOOST_ASSERT(int_matches_if_present(add_2_to_4, add_minus_3, "some_other_key"));
68  BOOST_ASSERT(!int_matches_if_present(sub_2_to_4, add_minus_3, "sub"));
69  BOOST_ASSERT(int_matches_if_present_or_negative(sub_2_to_4, add_minus_3, "sub", "add"));
70 }
71 
72 BOOST_AUTO_TEST_CASE(test_without_attribute_filter)
73 {
74  config add_3 {"add", 3};
75  config value_3 {"value", 3};
76 
77  BOOST_ASSERT(!int_matches_if_present(value_3, add_3, "value"));
78  BOOST_ASSERT(int_matches_if_present(value_3, add_3, "value", 3));
79  BOOST_ASSERT(!int_matches_if_present_or_negative(add_3, value_3, "add", "sub"));
80 }
81 
82 BOOST_AUTO_TEST_SUITE_END()
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.
bool int_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, utils::optional< int > def=utils::nullopt)
bool int_matches_if_present_or_negative(const config &filter, const config &cfg, const std::string &attribute, const std::string &opposite, utils::optional< int > def=utils::nullopt)
Supports filters using "add" and "sub" attributes, for example a filter add=1 matching a cfg containi...
BOOST_AUTO_TEST_CASE(test_int_signed_filter)
BOOST_AUTO_TEST_SUITE(filesystem)