1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*=============================================================================
Copyright (c) 2001-2015 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(BOOST_SPIRIT_X3_TEST_UTILITIES)
#define BOOST_SPIRIT_X3_TEST_UTILITIES
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace boost { namespace spirit { namespace x3 { namespace testing
{
namespace fs = boost::filesystem;
////////////////////////////////////////////////////////////////////////////
// compare
//
// Compares the contents of in with the template tem. The template
// may include embedded regular expressions marked up within re_prefix
// and re_suffix tags. For example, given the default RE markup, this
// template <%[0-9]+%> will match any integer in in. The function
// will return the first non-matching position. The flag full_match
// indicates a full match. It is possible for returned pos to be
// at the end of in (in.end()) while still returning full_match ==
// false. In that case, we have a partial match.
////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct compare_result
{
compare_result(
Iterator pos
, bool full_match
) : pos(pos), full_match(full_match) {}
Iterator pos;
bool full_match;
};
template <typename Range>
compare_result<typename Range::const_iterator>
compare(
Range const& in
, Range const& tem
, char const* re_prefix = "<%"
, char const* re_suffix = "%>"
);
////////////////////////////////////////////////////////////////////////////
// compare
//
// 1) Call f, given the contents of input_path loaded in a string.
// The result of calling f is the output string.
// 2) Compare the result of calling f with expected template
// file (expect_path) using the low-level compare utility
// abive
////////////////////////////////////////////////////////////////////////////
template <typename F>
bool compare(
fs::path input_path, fs::path expect_path
, F f
, char const* re_prefix = "<%"
, char const* re_suffix = "%>"
);
////////////////////////////////////////////////////////////////////////////
// for_each_file
//
// For each *.input and *.expect file in a given directory,
// call the function f, passing in the *.input and *.expect paths.
////////////////////////////////////////////////////////////////////////////
template <typename F>
int for_each_file(fs::path p, F f);
////////////////////////////////////////////////////////////////////////////
// load_file
//
// Load file into a string.
////////////////////////////////////////////////////////////////////////////
std::string load(fs::path p);
////////////////////////////////////////////////////////////////////////////
// Implementation
////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
inline bool is_regex(
Iterator& first
, Iterator last
, std::string& re
, char const* re_prefix
, char const* re_suffix
)
{
boost::regex e(re_prefix + std::string("(.*?)") + re_suffix);
boost::match_results<Iterator> what;
if (boost::regex_search(
first, last, what, e
, boost::match_default | boost::match_continuous))
{
re = what[1].str();
first = what[0].second;
return true;
}
return false;
}
template <typename Range>
inline compare_result<typename Range::const_iterator>
compare(
Range const& in
, Range const& tem
, char const* re_prefix
, char const* re_suffix
)
{
typedef typename Range::const_iterator iter_t;
typedef compare_result<iter_t> compare_result_t;
iter_t in_first = in.begin();
iter_t in_last = in.end();
iter_t tem_first = tem.begin();
iter_t tem_last = tem.end();
std::string re;
while (in_first != in_last && tem_first != tem_last)
{
if (is_regex(tem_first, tem_last, re, re_prefix, re_suffix))
{
boost::match_results<iter_t> what;
boost::regex e(re);
if (!boost::regex_search(
in_first, in_last, what, e
, boost::match_default | boost::match_continuous))
{
// RE mismatch: exit now.
return compare_result_t(in_first, false);
}
else
{
// RE match: gobble the matching string.
in_first = what[0].second;
}
}
else
{
// Char by char comparison. Exit if we have a mismatch.
if (*in_first++ != *tem_first++)
return compare_result_t(in_first, false);
}
}
// Ignore trailing spaces in template
bool has_trailing_nonspaces = false;
while (tem_first != tem_last)
{
if (!std::isspace(*tem_first++))
{
has_trailing_nonspaces = true;
break;
}
}
while (in_first != in_last)
{
if (!std::isspace(*in_first++))
{
has_trailing_nonspaces = true;
break;
}
}
// return a full match only if the template is fully matched and if there
// are no more characters to match in the source
return compare_result_t(in_first, !has_trailing_nonspaces);
}
template <typename F>
inline int for_each_file(fs::path p, F f)
{
try
{
if (fs::exists(p) && fs::is_directory(p))
{
for (auto i = fs::directory_iterator(p); i != fs::directory_iterator(); ++i)
{
auto ext = fs::extension(i->path());
if (ext == ".input")
{
auto input_path = i->path();
auto expect_path = input_path;
expect_path.replace_extension(".expect");
f(input_path, expect_path);
}
}
}
else
{
std::cerr << "Directory: " << fs::absolute(p) << " does not exist." << std::endl;
return 1;
}
}
catch (const fs::filesystem_error& ex)
{
std::cerr << ex.what() << '\n';
return 1;
}
return 0;
}
inline std::string load(fs::path p)
{
boost::filesystem::ifstream file(p);
if (!file)
return "";
std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return contents;
}
template <typename F>
inline bool compare(
fs::path input_path, fs::path expect_path
, F f
, char const* re_prefix
, char const* re_suffix
)
{
std::string output = f(load(input_path), input_path);
std::string expected = load(expect_path);
auto result = compare(output, expected, re_prefix, re_suffix);
if (!result.full_match)
{
std::cerr << "=============================================" << std::endl;
std::cerr << "==== Mismatch Found:" << std::endl;
int line = 1;
int col = 1;
for (auto i = output.begin(); i != result.pos; ++i)
{
if (*i == '\n')
{
line++;
col = 0;
}
++col;
}
std::cerr
<< "==== File: " << expect_path
<< ", Line: " << line
<< ", Column: " << col
<< std::endl;
std::cerr << "=============================================" << std::endl;
// Print output
std::cerr << output;
std::cerr << "=============================================" << std::endl;
std::cerr << "==== End" << std::endl;
std::cerr << "=============================================" << std::endl;
return false;
}
return true;
}
}}}}
#endif
|