The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
test_simple_wml.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2007 - 2025
3 by Karol Nowak <grywacz@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 <limits>
19#include <string>
20#include <vector>
21
22#include <boost/test/unit_test.hpp>
23
25
27
28BOOST_AUTO_TEST_CASE( simple_wml_first_test )
29{
30 const char* doctext = R"([test]
31a="blah"
32b="blah"
33c="\\"
34d="\"""
35[/test])";
36 simple_wml::document doc(doctext, INIT_STATE::INIT_COMPRESSED);
37
38 simple_wml::node& node = doc.root();
39 simple_wml::node* test_node = node.child("test");
40 node.set_attr("blah", "blah");
41 test_node->set_attr("e", "f");
42
43 BOOST_CHECK(test_node);
44 BOOST_CHECK((*test_node)["a"] == "blah");
45 BOOST_CHECK((*test_node)["b"] == "blah");
46 BOOST_CHECK((*test_node)["c"] == "\\\\");
47 BOOST_CHECK((*test_node)["d"] == "\\\"\"");
48 BOOST_CHECK(node["blah"] == "blah");
49 BOOST_CHECK((*test_node)["e"] == "f");
50}
51
52BOOST_AUTO_TEST_CASE( simple_wml_child_limits )
53{
54 constexpr std::size_t max_children = std::numeric_limits<unsigned short>::max() + 1;
55
56 // 1. Test programmatic add_child limit for identical tag names (child_list_index)
57 {
59 simple_wml::node& root = doc.root();
60
61 for(std::size_t i = 0; i < max_children; ++i) {
62 root.add_child("a");
63 }
64
65 BOOST_CHECK_EQUAL(root.children("a").size(), max_children);
66 BOOST_CHECK_THROW(root.add_child("a"), simple_wml::error);
67 BOOST_CHECK_THROW(root.add_child_at("a", 0), simple_wml::error);
68 }
69
70 // 2. Test programmatic add_child limit for distinct tag names (child_map_index)
71 {
73 simple_wml::node& root = doc.root();
74
75 std::vector<std::string> names;
76 names.reserve(max_children + 1);
77 for(std::size_t i = 0; i <= max_children; ++i) {
78 names.push_back("t" + std::to_string(i));
79 }
80
81 for(std::size_t i = 0; i < max_children; ++i) {
82 root.add_child(names[i].c_str());
83 }
84
85 BOOST_CHECK_THROW(root.add_child(names[max_children].c_str()), simple_wml::error);
86 }
87
88 // 3. Test parser limit when reading WML text
89 {
90 std::string wml;
91 wml.reserve((max_children + 1) * 8);
92 for(std::size_t i = 0; i <= max_children; ++i) {
93 wml += "[a]\n[/a]\n";
94 }
95
96 BOOST_CHECK_THROW(simple_wml::document(wml.c_str(), INIT_STATE::INIT_STATIC), simple_wml::error);
97 }
98}
99
100/* vim: set ts=4 sw=4: */
101BOOST_AUTO_TEST_SUITE_END()
std::vector< std::string > names
const child_list & children(const char *name) const
node * child(const char *name)
node & add_child(const char *name)
node & set_attr(const char *key, const char *value)
node & add_child_at(const char *name, std::size_t index)
BOOST_AUTO_TEST_SUITE(filesystem)
BOOST_AUTO_TEST_CASE(simple_wml_first_test)
#define i