1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(BOOST_SPIRIT_X3_TEST_UTILITIES)
8 #define BOOST_SPIRIT_X3_TEST_UTILITIES
10 #include <boost/regex.hpp>
11 #include <boost/filesystem.hpp>
12 #include <boost/filesystem/fstream.hpp>
14 namespace boost { namespace spirit { namespace x3 { namespace testing
16 namespace fs = boost::filesystem;
18 ////////////////////////////////////////////////////////////////////////////
21 // Compares the contents of in with the template tem. The template
22 // may include embedded regular expressions marked up within re_prefix
23 // and re_suffix tags. For example, given the default RE markup, this
24 // template <%[0-9]+%> will match any integer in in. The function
25 // will return the first non-matching position. The flag full_match
26 // indicates a full match. It is possible for returned pos to be
27 // at the end of in (in.end()) while still returning full_match ==
28 // false. In that case, we have a partial match.
29 ////////////////////////////////////////////////////////////////////////////
31 template <typename Iterator>
37 ) : pos(pos), full_match(full_match) {}
43 template <typename Range>
44 compare_result<typename Range::const_iterator>
48 , char const* re_prefix = "<%"
49 , char const* re_suffix = "%>"
52 ////////////////////////////////////////////////////////////////////////////
55 // 1) Call f, given the contents of input_path loaded in a string.
56 // The result of calling f is the output string.
57 // 2) Compare the result of calling f with expected template
58 // file (expect_path) using the low-level compare utility
60 ////////////////////////////////////////////////////////////////////////////
63 fs::path input_path, fs::path expect_path
65 , char const* re_prefix = "<%"
66 , char const* re_suffix = "%>"
69 ////////////////////////////////////////////////////////////////////////////
72 // For each *.input and *.expect file in a given directory,
73 // call the function f, passing in the *.input and *.expect paths.
74 ////////////////////////////////////////////////////////////////////////////
76 int for_each_file(fs::path p, F f);
78 ////////////////////////////////////////////////////////////////////////////
81 // Load file into a string.
82 ////////////////////////////////////////////////////////////////////////////
83 std::string load(fs::path p);
85 ////////////////////////////////////////////////////////////////////////////
87 ////////////////////////////////////////////////////////////////////////////
89 template <typename Iterator>
94 , char const* re_prefix
95 , char const* re_suffix
98 boost::regex e(re_prefix + std::string("(.*?)") + re_suffix);
99 boost::match_results<Iterator> what;
100 if (boost::regex_search(
102 , boost::match_default | boost::match_continuous))
105 first = what[0].second;
111 template <typename Range>
112 inline compare_result<typename Range::const_iterator>
116 , char const* re_prefix
117 , char const* re_suffix
120 typedef typename Range::const_iterator iter_t;
121 typedef compare_result<iter_t> compare_result_t;
123 iter_t in_first = in.begin();
124 iter_t in_last = in.end();
125 iter_t tem_first = tem.begin();
126 iter_t tem_last = tem.end();
129 while (in_first != in_last && tem_first != tem_last)
131 if (is_regex(tem_first, tem_last, re, re_prefix, re_suffix))
133 boost::match_results<iter_t> what;
135 if (!boost::regex_search(
136 in_first, in_last, what, e
137 , boost::match_default | boost::match_continuous))
139 // RE mismatch: exit now.
140 return compare_result_t(in_first, false);
144 // RE match: gobble the matching string.
145 in_first = what[0].second;
150 // Char by char comparison. Exit if we have a mismatch.
151 if (*in_first++ != *tem_first++)
152 return compare_result_t(in_first, false);
156 // Ignore trailing spaces in template
157 bool has_trailing_nonspaces = false;
158 while (tem_first != tem_last)
160 if (!std::isspace(*tem_first++))
162 has_trailing_nonspaces = true;
166 while (in_first != in_last)
168 if (!std::isspace(*in_first++))
170 has_trailing_nonspaces = true;
174 // return a full match only if the template is fully matched and if there
175 // are no more characters to match in the source
176 return compare_result_t(in_first, !has_trailing_nonspaces);
179 template <typename F>
180 inline int for_each_file(fs::path p, F f)
184 if (fs::exists(p) && fs::is_directory(p))
186 for (auto i = fs::directory_iterator(p); i != fs::directory_iterator(); ++i)
188 auto ext = fs::extension(i->path());
191 auto input_path = i->path();
192 auto expect_path = input_path;
193 expect_path.replace_extension(".expect");
194 f(input_path, expect_path);
200 std::cerr << "Directory: " << fs::absolute(p) << " does not exist." << std::endl;
205 catch (const fs::filesystem_error& ex)
207 std::cerr << ex.what() << '\n';
213 inline std::string load(fs::path p)
215 boost::filesystem::ifstream file(p);
218 std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
222 template <typename F>
224 fs::path input_path, fs::path expect_path
226 , char const* re_prefix
227 , char const* re_suffix
230 std::string output = f(load(input_path), input_path);
231 std::string expected = load(expect_path);
233 auto result = compare(output, expected);
234 if (!result.full_match)
236 std::cout << "=============================================" << std::endl;
237 std::cout << "==== Mismatch Found:" << std::endl;
240 for (auto i = output.begin(); i != result.pos; ++i)
251 << "==== File: " << expect_path
252 << ", Line: " << line
253 << ", Column: " << col
255 std::cerr << "=============================================" << std::endl;
259 std::cerr << "=============================================" << std::endl;
260 std::cerr << "==== End" << std::endl;
261 std::cerr << "=============================================" << std::endl;