The Battle for Wesnoth  1.15.9+dev
wml_equivalence.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 by CrawlCycle <73139676+CrawlCycle@users.noreply.github.com>
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 */
14 #include "wml_equivalence.hpp"
15 #include "serialization/parser.hpp"
16 #include <boost/filesystem.hpp>
17 #include <boost/test/unit_test.hpp>
18 
19 namespace bfs = boost::filesystem;
20 /**
21  * Self destructive temporary file at the current directory.
22  * @pre An other user does not have a file with the same name.
23  */
24 class tmp_file final
25 {
26 public:
27  /** Set file to @p content and close the file */
28  void set(const std::string& content)
29  {
30  // If any step raises an exception, tmp_file will go out of scope
31  // and trigger the destructor.
32  const auto mode = std::ios_base::out;
33  stream.open(path, std::ios_base::trunc | mode);
34  auto permission = (bfs::perms::owner_read | bfs::perms::owner_write);
35  bfs::permissions(path, permission);
36  stream << content;
37  stream.close();
38  }
39 
41  {
42  if(bfs::exists(path)) {
44  }
45  }
46 
47  /** Path to the temporary file */
48  const bfs::path path = bfs::unique_path("%%%%-%%%%-%%%%-%%%%-%%%%-%%%%.tmp.out");
49 
50 private:
51  bfs::ofstream stream;
52 };
53 
54 config preprocess_and_parse(const std::string& wml_str, preproc_map* macro_map)
55 {
56  tmp_file tmp_f;
57  tmp_f.set(wml_str);
58  auto b = preprocess_file(tmp_f.path.string(), macro_map);
59  config actual;
60  read(actual, *b);
61  return actual;
62 }
63 
64 void check_wml_equivalence(const std::string& a, const std::string& b)
65 {
66  config config_a = preprocess_and_parse(a);
67  config config_b = preprocess_and_parse(b);
68  BOOST_CHECK_EQUAL(config_a, config_b);
69 }
void remove()
Removes a tip.
Definition: tooltip.cpp:176
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:1010
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:626
void set(const std::string &content)
Set file to content and close the file.
std::string path
Definition: game_config.cpp:38
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:59
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