The Battle for Wesnoth  1.19.0-dev
test_schema_validator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2023
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  config result;
47 
48  BOOST_CHECK_EXCEPTION(read(result, *stream, &validator), wml_exception, [](const wml_exception& e) {
49  return boost::algorithm::contains(e.dev_message, "Inheritance cycle from other/second to main/first found");
50  });
51 }
52 
53 BOOST_AUTO_TEST_CASE(test_super_cycle_only_if_used)
54 {
55  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle.cfg";
56  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle_only_if_used.cfg";
57 
58  auto validator = schema_validation::schema_validator(schema_path, false);
59 
60  validator.set_create_exceptions(true);
61 
62  preproc_map defines_map;
63  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
64  defines_map["SCHEMA_VALIDATION"] = preproc_define();
65 
66  auto stream = preprocess_file(config_path, &defines_map);
67 
68  config result;
69  BOOST_CHECK_NO_THROW(read(result, *stream, &validator));
70 }
71 
72 BOOST_AUTO_TEST_CASE(test_super_cycle_crashes_on_unknown_key)
73 {
74  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle.cfg";
75  constexpr auto config_path
76  = "src/tests/wml/schema/test_schema_validator/test_super_cycle_crashes_on_unknown_key.cfg";
77 
78  auto validator = schema_validation::schema_validator(schema_path, false);
79 
80  validator.set_create_exceptions(true);
81 
82  preproc_map defines_map;
83  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
84  defines_map["SCHEMA_VALIDATION"] = preproc_define();
85 
86  auto stream = preprocess_file(config_path, &defines_map);
87 
88  config result;
89 
90  BOOST_CHECK_EXCEPTION(read(result, *stream, &validator), wml_exception, [](const wml_exception& e) {
91  return boost::algorithm::contains(e.dev_message, "Invalid key 'unknown=' in tag [first]");
92  });
93 }
94 
95 BOOST_AUTO_TEST_CASE(test_super_missing)
96 {
97  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_missing.cfg";
98  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle.cfg";
99 
100  auto validator = schema_validation::schema_validator(schema_path, false);
101 
102  validator.set_create_exceptions(true);
103 
104  preproc_map defines_map;
105  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
106  defines_map["SCHEMA_VALIDATION"] = preproc_define();
107 
108  auto stream = preprocess_file(config_path, &defines_map);
109 
110  config result;
111 
112  BOOST_CHECK_EXCEPTION(read(result, *stream, &validator), wml_exception, [](const wml_exception& e) {
113  return boost::algorithm::contains(e.dev_message, "Super not/here not found. Needed by other/second");
114  });
115 }
116 
117 BOOST_AUTO_TEST_CASE(test_super_missing_only_if_used)
118 {
119  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_missing.cfg";
120  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle_only_if_used.cfg";
121 
122  auto validator = schema_validation::schema_validator(schema_path, false);
123 
124  validator.set_create_exceptions(true);
125 
126  preproc_map defines_map;
127  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
128  defines_map["SCHEMA_VALIDATION"] = preproc_define();
129 
130  auto stream = preprocess_file(config_path, &defines_map);
131 
132  config result;
133  BOOST_CHECK_NO_THROW(read(result, *stream, &validator));
134 }
135 
136 BOOST_AUTO_TEST_CASE(test_super_mandatory)
137 {
138  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_mandatory_missing.cfg";
139  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_mandatory.cfg";
140 
141  auto validator = schema_validation::schema_validator(schema_path, false);
142 
143  validator.set_create_exceptions(true);
144 
145  preproc_map defines_map;
146  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
147  defines_map["SCHEMA_VALIDATION"] = preproc_define();
148 
149  auto stream = preprocess_file(config_path, &defines_map);
150 
151  config result;
152  BOOST_CHECK_NO_THROW(read(result, *stream, &validator));
153 }
154 
155 BOOST_AUTO_TEST_CASE(test_super_mandatory_missing)
156 {
157  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_mandatory_missing.cfg";
158  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_mandatory_missing.cfg";
159 
160  auto validator = schema_validation::schema_validator(schema_path, false);
161 
162  validator.set_create_exceptions(true);
163 
164  preproc_map defines_map;
165  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
166  defines_map["SCHEMA_VALIDATION"] = preproc_define();
167 
168  auto stream = preprocess_file(config_path, &defines_map);
169 
170  config result;
171 
172  BOOST_CHECK_EXCEPTION(read(result, *stream, &validator), wml_exception, [](const wml_exception& e) {
173  return boost::algorithm::contains(e.dev_message, "Missing key 'id=' in tag [campaign]");
174  });
175 }
176 
177 BOOST_AUTO_TEST_CASE(test_super_cycle_mandatory)
178 {
179  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle_mandatory.cfg";
180  constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle_mandatory.cfg";
181 
182  auto validator = schema_validation::schema_validator(schema_path, false);
183 
184  validator.set_create_exceptions(true);
185 
186  preproc_map defines_map;
187  defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
188  defines_map["SCHEMA_VALIDATION"] = preproc_define();
189 
190  auto stream = preprocess_file(config_path, &defines_map);
191 
192  config result;
193  BOOST_CHECK_NO_THROW(read(result, *stream, &validator));
194 }
195 
196 BOOST_AUTO_TEST_CASE(test_schema_link_cycle)
197 {
198  constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_link_cycle.cfg";
199 
200  BOOST_CHECK_EXCEPTION(schema_validation::schema_validator(schema_path, false), abstract_validator::error, [](const abstract_validator::error& e) {
201  return boost::algorithm::contains(e.message, "Link cycle from");
202  });
203 }
204 
205 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
Realization of serialization/validator.hpp abstract validator.
Interfaces for manipulating version numbers of engine, add-ons, etc.
const version_info wesnoth_version(VERSION)
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:83
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.
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:627
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