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