The Battle for Wesnoth  1.19.10+dev
test_schema_validator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
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  COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-test"
17 
18 #include <boost/test/unit_test.hpp>
19 
20 #include <boost/algorithm/string/predicate.hpp>
21 
22 #include "game_version.hpp"
23 #include "serialization/parser.hpp"
26 #include "wml_exception.hpp"
27 
28 BOOST_AUTO_TEST_SUITE(test_schema_validator)
29 
30 BOOST_AUTO_TEST_CASE(test_super_cycle)
31 {
32  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle.cfg";
33  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle.cfg";
34 
35  auto validator = schema_validation::schema_validator(schema_path, false);
36 
37  validator.set_create_exceptions(true);
38 
39  // See: wesnoth.cpp > handle_validate_command
40  preproc_map defines_map;
41  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
42  defines_map["SCHEMA_VALIDATION"] = preproc_define();
43 
44  auto stream = preprocess_file(config_path, &defines_map);
45 
46  BOOST_CHECK_EXCEPTION(io::read(*stream, &validator), wml_exception, [](const wml_exception& e) {
47  return boost::algorithm::contains(e.dev_message, "Inheritance cycle from other/second to main/first found");
48  });
49 }
50 
51 BOOST_AUTO_TEST_CASE(test_super_cycle_only_if_used)
52 {
53  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle.cfg";
54  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle_only_if_used.cfg";
55 
56  auto validator = schema_validation::schema_validator(schema_path, false);
57 
58  validator.set_create_exceptions(true);
59 
60  preproc_map defines_map;
61  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
62  defines_map["SCHEMA_VALIDATION"] = preproc_define();
63 
64  auto stream = preprocess_file(config_path, &defines_map);
65  BOOST_CHECK_NO_THROW(io::read(*stream, &validator));
66 }
67 
68 BOOST_AUTO_TEST_CASE(test_super_cycle_crashes_on_unknown_key)
69 {
70  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle.cfg";
71  constexpr auto config_path
72  = "src/tests/wml/schema/test_schema_validator/test_super_cycle_crashes_on_unknown_key.cfg";
73 
74  auto validator = schema_validation::schema_validator(schema_path, false);
75 
76  validator.set_create_exceptions(true);
77 
78  preproc_map defines_map;
79  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
80  defines_map["SCHEMA_VALIDATION"] = preproc_define();
81 
82  auto stream = preprocess_file(config_path, &defines_map);
83 
84  BOOST_CHECK_EXCEPTION(io::read(*stream, &validator), wml_exception, [](const wml_exception& e) {
85  return boost::algorithm::contains(e.dev_message, "Invalid key 'unknown=' in tag [first]");
86  });
87 }
88 
89 BOOST_AUTO_TEST_CASE(test_super_missing)
90 {
91  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_missing.cfg";
92  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle.cfg";
93 
94  auto validator = schema_validation::schema_validator(schema_path, false);
95 
96  validator.set_create_exceptions(true);
97 
98  preproc_map defines_map;
99  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
100  defines_map["SCHEMA_VALIDATION"] = preproc_define();
101 
102  auto stream = preprocess_file(config_path, &defines_map);
103 
104  BOOST_CHECK_EXCEPTION(io::read(*stream, &validator), wml_exception, [](const wml_exception& e) {
105  return boost::algorithm::contains(e.dev_message, "Super not/here not found. Needed by other/second");
106  });
107 }
108 
109 BOOST_AUTO_TEST_CASE(test_super_missing_only_if_used)
110 {
111  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_missing.cfg";
112  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle_only_if_used.cfg";
113 
114  auto validator = schema_validation::schema_validator(schema_path, false);
115 
116  validator.set_create_exceptions(true);
117 
118  preproc_map defines_map;
119  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
120  defines_map["SCHEMA_VALIDATION"] = preproc_define();
121 
122  auto stream = preprocess_file(config_path, &defines_map);
123  BOOST_CHECK_NO_THROW(io::read(*stream, &validator));
124 }
125 
126 BOOST_AUTO_TEST_CASE(test_super_mandatory)
127 {
128  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_mandatory_missing.cfg";
129  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_mandatory.cfg";
130 
131  auto validator = schema_validation::schema_validator(schema_path, false);
132 
133  validator.set_create_exceptions(true);
134 
135  preproc_map defines_map;
136  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
137  defines_map["SCHEMA_VALIDATION"] = preproc_define();
138 
139  auto stream = preprocess_file(config_path, &defines_map);
140  BOOST_CHECK_NO_THROW(io::read(*stream, &validator));
141 }
142 
143 BOOST_AUTO_TEST_CASE(test_super_mandatory_missing)
144 {
145  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_mandatory_missing.cfg";
146  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_mandatory_missing.cfg";
147 
148  auto validator = schema_validation::schema_validator(schema_path, false);
149 
150  validator.set_create_exceptions(true);
151 
152  preproc_map defines_map;
153  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
154  defines_map["SCHEMA_VALIDATION"] = preproc_define();
155 
156  auto stream = preprocess_file(config_path, &defines_map);
157  BOOST_CHECK_EXCEPTION(io::read(*stream, &validator), wml_exception, [](const wml_exception& e) {
158  return boost::algorithm::contains(e.dev_message, "Missing key 'id=' in tag [campaign]");
159  });
160 }
161 
162 BOOST_AUTO_TEST_CASE(test_super_cycle_mandatory)
163 {
164  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle_mandatory.cfg";
165  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle_mandatory.cfg";
166 
167  auto validator = schema_validation::schema_validator(schema_path, false);
168 
169  validator.set_create_exceptions(true);
170 
171  preproc_map defines_map;
172  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
173  defines_map["SCHEMA_VALIDATION"] = preproc_define();
174 
175  auto stream = preprocess_file(config_path, &defines_map);
176  BOOST_CHECK_NO_THROW(io::read(*stream, &validator));
177 }
178 
179 BOOST_AUTO_TEST_CASE(test_schema_link_cycle)
180 {
181  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_link_cycle.cfg";
182 
183  BOOST_CHECK_EXCEPTION(schema_validation::schema_validator(schema_path, false), abstract_validator::error, [](const abstract_validator::error& e) {
184  return boost::algorithm::contains(e.message, "Link cycle from");
185  });
186 }
187 
188 BOOST_AUTO_TEST_SUITE_END()
Realization of serialization/validator.hpp abstract validator.
Interfaces for manipulating version numbers of engine, add-ons, etc.
const version_info wesnoth_version(VERSION)
config read(std::istream &in, abstract_validator *validator)
Definition: parser.cpp:627
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:86
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines)
Function to use the WML preprocessor on a file.
std::map< std::string, struct preproc_define > preproc_map
One of the realizations of serialization/validator.hpp abstract validator.
Used to manage with not initialized validators Supposed to be thrown from the constructor.
Definition: validator.hpp:97
Helper class, don't construct this directly.
BOOST_AUTO_TEST_SUITE(filesystem)
BOOST_AUTO_TEST_CASE(test_super_cycle)
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define e