The Battle for Wesnoth  1.19.0-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_positive_filter)
29 {
30  // These can be used both as the filter and as the input to be filtered,
31  // but for the unsigned_matches_if_present function only one of them is
32  // a valid filter.
33  config minus_3 {"x", -3};
34  config plus_3 {"x", 3};
35 
36  BOOST_ASSERT(!unsigned_matches_if_present(minus_3, minus_3, "x"));
37  BOOST_ASSERT(unsigned_matches_if_present(plus_3, plus_3, "x"));
38 
39  BOOST_ASSERT(!unsigned_matches_if_present(minus_3, plus_3, "x"));
40  BOOST_ASSERT(!unsigned_matches_if_present(plus_3, minus_3, "x"));
41 }
42 
43 BOOST_AUTO_TEST_CASE(test_int_signed_filter)
44 {
45  // These can be used both as the filter and as the input to be filtered.
46  config minus_3 {"x", -3};
47  config plus_3 {"x", 3};
48 
49  config any_negative {"x", "-infinity--1"};
50  config any_positive {"x", "1-infinity"};
51  config any_number {"x", "-infinity-infinity"};
52 
53  BOOST_ASSERT(int_matches_if_present(minus_3, minus_3, "x"));
54  BOOST_ASSERT(int_matches_if_present(plus_3, plus_3, "x"));
55 
56  BOOST_ASSERT(!int_matches_if_present(minus_3, plus_3, "x"));
57  BOOST_ASSERT(!int_matches_if_present(plus_3, minus_3, "x"));
58 
59  BOOST_ASSERT(int_matches_if_present(any_negative, minus_3, "x"));
60  BOOST_ASSERT(!int_matches_if_present(any_positive, minus_3, "x"));
61  BOOST_ASSERT(int_matches_if_present(any_number, minus_3, "x"));
62 
63  BOOST_ASSERT(!int_matches_if_present(any_negative, plus_3, "x"));
64  BOOST_ASSERT(int_matches_if_present(any_positive, plus_3, "x"));
65  BOOST_ASSERT(int_matches_if_present(any_number, plus_3, "x"));
66 }
67 
68 BOOST_AUTO_TEST_CASE(test_int_add_sub_filter)
69 {
70  config add_minus_3 {"add", -3};
71  config sub_3 {"sub", 3};
72 
73  BOOST_ASSERT(int_matches_if_present(add_minus_3, add_minus_3, "add"));
74  BOOST_ASSERT(int_matches_if_present_or_negative(add_minus_3, add_minus_3, "add", "sub"));
75  BOOST_ASSERT(int_matches_if_present(add_minus_3, add_minus_3, "some_other_key"));
76 
77  // A range, and one that only matches positive numbers
78  config add_2_to_4 {"add", "2-4"};
79  config sub_2_to_4 {"sub", "2-4"};
80 
81  BOOST_ASSERT(!int_matches_if_present(add_2_to_4, add_minus_3, "add"));
82  BOOST_ASSERT(int_matches_if_present(add_2_to_4, add_minus_3, "some_other_key"));
83  BOOST_ASSERT(!int_matches_if_present(sub_2_to_4, add_minus_3, "sub"));
84  BOOST_ASSERT(int_matches_if_present_or_negative(sub_2_to_4, add_minus_3, "sub", "add"));
85 }
86 
87 BOOST_AUTO_TEST_CASE(test_without_attribute_filter)
88 {
89  config add_3 {"add", 3};
90  config value_3 {"value", 3};
91 
92  BOOST_ASSERT(!int_matches_if_present(value_3, add_3, "value"));
93  BOOST_ASSERT(int_matches_if_present(value_3, add_3, "value", 3));
94  BOOST_ASSERT(!int_matches_if_present_or_negative(add_3, value_3, "add", "sub"));
95 }
96 
97 BOOST_AUTO_TEST_SUITE_END()
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.
bool unsigned_matches_if_present(const config &filter, const config &cfg, const std::string &attribute)
Restricts filters to only looking for values that are zero or more.
bool int_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, std::optional< int > def=std::nullopt)
bool int_matches_if_present_or_negative(const config &filter, const config &cfg, const std::string &attribute, const std::string &opposite, std::optional< int > def=std::nullopt)
Supports filters using "add" and "sub" attributes, for example a filter add=1 matching a cfg containi...
BOOST_AUTO_TEST_CASE(test_int_positive_filter)
BOOST_AUTO_TEST_SUITE(filesystem)