The Battle for Wesnoth  1.19.5+dev
wml_equivalence.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 2024
3  by CrawlCycle <73139676+CrawlCycle@users.noreply.github.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 #include "wml_equivalence.hpp"
17 #include "serialization/parser.hpp"
18 #include <boost/filesystem.hpp>
19 #include <boost/filesystem/fstream.hpp>
20 #include <boost/test/unit_test.hpp>
21 
22 namespace bfs = boost::filesystem;
23 /**
24  * Self destructive temporary file at the current directory.
25  * @pre An other user does not have a file with the same name.
26  */
27 class tmp_file final
28 {
29 public:
30  /** Set file to @p content and close the file */
31  void set(const std::string& content)
32  {
33  // If any step raises an exception, tmp_file will go out of scope
34  // and trigger the destructor.
35  const auto mode = std::ios_base::out;
36  stream.open(path, std::ios_base::trunc | mode);
37  auto permission = (bfs::perms::owner_read | bfs::perms::owner_write);
38  bfs::permissions(path, permission);
39  stream << content;
40  stream.close();
41  }
42 
44  {
45  if(bfs::exists(path)) {
47  }
48  }
49 
50  /** Path to the temporary file */
51  const bfs::path path = bfs::unique_path("%%%%-%%%%-%%%%-%%%%-%%%%-%%%%.tmp.out");
52 
53 private:
54  bfs::ofstream stream;
55 };
56 
57 config preprocess_and_parse(const std::string& wml_str, preproc_map* macro_map)
58 {
59  tmp_file tmp_f;
60  tmp_f.set(wml_str);
61  auto b = preprocess_file(tmp_f.path.string(), macro_map);
62  config actual;
63  read(actual, *b);
64  return actual;
65 }
66 
67 void check_wml_equivalence(const std::string& a, const std::string& b)
68 {
69  config config_a = preprocess_and_parse(a);
70  config config_b = preprocess_and_parse(b);
71  BOOST_CHECK_EQUAL(config_a, config_b);
72 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Self destructive temporary file at the current directory.
void set(const std::string &content)
Set file to content and close the file.
const bfs::path path
Path to the temporary file.
bfs::ofstream stream
std::string path
Definition: filesystem.cpp:90
void remove()
Removes a tip.
Definition: tooltip.cpp:94
bool exists(const image::locator &i_locator)
Returns true if the given image actually exists, without loading it.
Definition: picture.cpp:818
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
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:622
void check_wml_equivalence(const std::string &a, const std::string &b)
Assert two WML strings are equivalent.
config preprocess_and_parse(const std::string &wml_str, preproc_map *macro_map)
Make a syntax tree by preprocessing and parsing a WML string.
Tool to check if two WML string are equivalent.
#define b