-
Notifications
You must be signed in to change notification settings - Fork 43
/
lest_decompose.hpp
381 lines (315 loc) · 10.7 KB
/
lest_decompose.hpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2013, 2014 by Martin Moene
//
// lest is based on ideas by Kevlin Henney, see video at
// http://skillsmatter.com/podcast/agile-testing/kevlin-henney-rethinking-unit-testing-in-c-plus-plus
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef LEST_LEST_HPP_INCLUDED
#define LEST_LEST_HPP_INCLUDED
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <cstddef>
#ifdef __clang__
# pragma clang diagnostic ignored "-Waggregate-return"
# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-comparison"
#elif defined __GNUC__
# pragma GCC diagnostic ignored "-Waggregate-return"
# pragma GCC diagnostic push
#endif
// Suppress shadow and unused-value warning for sections:
#if defined __clang__
# define lest_SUPPRESS_WSHADOW _Pragma( "clang diagnostic push" ) \
_Pragma( "clang diagnostic ignored \"-Wshadow\"" )
# define lest_SUPPRESS_WUNUSED _Pragma( "clang diagnostic push" ) \
_Pragma( "clang diagnostic ignored \"-Wunused-value\"" )
# define lest_RESTORE_WARNINGS _Pragma( "clang diagnostic pop" )
#elif defined __GNUC__
# define lest_SUPPRESS_WSHADOW _Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wshadow\"" )
# define lest_SUPPRESS_WUNUSED _Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wunused-value\"" )
# define lest_RESTORE_WARNINGS _Pragma( "GCC diagnostic pop" )
#else
# define lest_SUPPRESS_WSHADOW /*empty*/
# define lest_SUPPRESS_WUNUSED /*empty*/
# define lest_RESTORE_WARNINGS /*empty*/
#endif
#ifndef lest_NO_SHORT_ASSERTION_NAMES
# define TEST lest_TEST
# define CASE lest_CASE
# define CASE_ON lest_CASE_ON
# define EXPECT lest_EXPECT
# define EXPECT_NOT lest_EXPECT_NOT
# define EXPECT_NO_THROW lest_EXPECT_NO_THROW
# define EXPECT_THROWS lest_EXPECT_THROWS
# define EXPECT_THROWS_AS lest_EXPECT_THROWS_AS
#endif
#define lest_TEST \
lest_CASE
#define lest_CASE( name ) \
name, []
#define lest_CASE_ON( name, ... ) \
name, [__VA_ARGS__]
#define lest_EXPECT( expr ) \
do { \
try \
{ \
if ( lest::result failed = lest_DECOMPOSE( expr ) ) \
throw lest::failure{ lest_LOCATION, #expr, failed.decomposition }; \
} \
catch(...) \
{ \
lest::inform( lest_LOCATION, #expr ); \
} \
} while ( lest::is_false() )
#define lest_EXPECT_NOT( expr ) \
lest_EXPECT( !(expr) )
#define lest_EXPECT_NO_THROW( expr ) \
do \
{ \
lest_SUPPRESS_WUNUSED \
try { expr; } \
catch (...) { lest::inform( lest_LOCATION, #expr ); } \
lest_RESTORE_WARNINGS \
} while ( lest::is_false() )
#define lest_EXPECT_THROWS( expr ) \
do \
{ \
lest_SUPPRESS_WUNUSED \
try { expr; } catch (...) { break; } \
throw lest::expected{ lest_LOCATION, #expr }; \
lest_RESTORE_WARNINGS \
} while ( lest::is_false() )
#define lest_EXPECT_THROWS_AS( expr, excpt ) \
do \
{ \
lest_SUPPRESS_WUNUSED \
try { expr; } catch ( excpt & ) { break; } catch (...) {} \
throw lest::expected{ lest_LOCATION, #expr, lest::of_type( #excpt ) }; \
lest_RESTORE_WARNINGS \
} while ( lest::is_false() )
#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr )
#define lest_LOCATION lest::location{__FILE__, __LINE__}
namespace lest {
using text = std::string;
struct test
{
const text name;
const std::function<void()> behaviour;
};
struct result
{
const bool passed;
const text decomposition;
explicit operator bool() { return ! passed; }
};
struct location
{
const text file;
const int line;
location( text file_, int line_)
: file( file_), line( line_) {}
};
struct comment
{
const text info;
comment( text info_) : info( info_) {}
explicit operator bool() { return ! info.empty(); }
};
struct message : std::runtime_error
{
const text kind;
const location where;
const comment note;
~message() throw() {} // GCC 4.6
message( text kind_, location where_, text expr_, text note_ = "" )
: std::runtime_error( expr_), kind( kind_), where( where_), note( note_) {}
};
struct failure : message
{
failure( location where_, std::string expr_, std::string decomposition_)
: message{ "failed", where_, expr_ + " for " + decomposition_} {}
};
struct expected : message
{
expected( location where_, std::string expr_, std::string excpt_ = "" )
: message{ "failed: didn't get exception", where_, expr_, excpt_} {}
};
struct unexpected : message
{
unexpected( location where_, std::string expr_, std::string note_ = "" )
: message{ "failed: got unexpected exception", where_, expr_, note_} {}
};
inline bool is_false( ) { return false; }
inline bool is_true ( bool flag ) { return flag; }
inline text with_message( text message )
{
return "with message \"" + message + "\"";
}
inline text of_type( text type )
{
return "of type " + type;
}
inline void inform( location where, char const * expr )
{
try
{
throw;
}
catch( lest::failure const & )
{
throw;
}
catch( std::exception const & e )
{
throw lest::unexpected{ where, expr, lest::with_message( e.what() ) }; \
}
catch(...)
{
throw lest::unexpected{ where, expr, "of unknown type" }; \
}
}
inline text pluralise( text word, int n )
{
return n == 1 ? word : word + "s";
}
inline std::ostream & operator<<( std::ostream & os, comment note )
{
return os << (note ? " " + note.info : "" );
}
inline std::ostream & operator<<( std::ostream & os, location where )
{
#ifdef __GNUG__
return os << where.file << ":" << where.line;
#else
return os << where.file << "(" << where.line << ")";
#endif
}
inline void report( std::ostream & os, message const & e, std::string test )
{
os << e.where << ": " << e.kind << e.note << ": " << test << ": " << e.what() << std::endl;
}
template <std::size_t N>
int run( test const (&specification)[N], std::ostream & os = std::cout )
{
int failures = 0;
for ( auto & testing : specification )
{
try
{
testing.behaviour();
}
catch( message const & e )
{
++failures;
report( os, e, testing.name );
}
}
if ( failures > 0 )
{
os << failures << " out of " << N << " " << pluralise("test", N) << " failed." << std::endl;
}
return failures;
}
// Expression decomposition:
inline std::string to_string( std::nullptr_t const & ) { return "nullptr"; }
inline std::string to_string( std::string const & txt ) { return "\"" + txt + "\"" ; }
inline std::string to_string( char const * const & txt ) { return "\"" + std::string( txt ) + "\"" ; }
inline std::string to_string( char const & txt ) { return "\'" + std::string( 1, txt ) + "\'" ; }
template<typename T>
struct is_container
{
template<typename U>
static auto test( int ) -> decltype( std::declval<U>().begin() == std::declval<U>().end(), std::true_type() );
template<typename>
static auto test( ... ) -> std::false_type;
#ifdef _MSC_VER
enum { value = std::is_same< decltype( test<T>(0) ), std::true_type >::value };
#else
static constexpr bool value = std::is_same< decltype( test<T>(0) ), std::true_type >::value;
#endif
};
template< typename T, typename R >
using ForContainer = typename std::enable_if< is_container<T>::value, R>::type;
template< typename T, typename R >
using ForNonContainerNonPointer = typename std::enable_if< ! (is_container<T>::value || std::is_pointer<T>::value), R>::type;
template< typename T >
inline std::string make_string( T const * ptr )
{
// Note showbase affects the behavior of /integer/ output;
std::ostringstream os;
os << std::internal << std::hex << std::showbase << std::setw( 2 + 2 * sizeof(T*) ) << std::setfill('0') << reinterpret_cast<std::ptrdiff_t>( ptr );
return os.str();
}
template< typename C, typename R >
inline std::string make_string( R C::* ptr )
{
std::ostringstream os;
os << std::internal << std::hex << std::showbase << std::setw( 2 + 2 * sizeof(R C::* ) ) << std::setfill('0') << ptr;
return os.str();
}
template< typename T >
auto to_string( T const * ptr ) -> std::string
{
return ! ptr ? "nullptr" : make_string( ptr );
}
template< typename C, typename R >
auto to_string( R C::* ptr ) -> std::string
{
return ! ptr ? "nullptr" : make_string( ptr );
}
template< typename T >
inline auto to_string( T const & value ) -> ForNonContainerNonPointer<T, std::string>
{
std::ostringstream os; os << std::boolalpha << value; return os.str();
}
template< typename C >
inline auto to_string( C const & cont ) -> ForContainer<C, std::string>
{
std::stringstream os;
os << "{ "; std::copy( cont.begin(), cont.end(), std::ostream_iterator<typename C::value_type>( os, ", " ) ); os << "}";
return os.str();
}
template< typename L, typename R >
inline std::string to_string( L const & lhs, std::string op, R const & rhs )
{
std::ostringstream os; os << to_string( lhs ) << " " << op << " " << to_string( rhs ); return os.str();
}
template< typename L >
struct expression_lhs
{
const L lhs;
expression_lhs( L lhs_) : lhs( lhs_) {}
operator result() { return result{ !!lhs, to_string( lhs ) }; }
template< typename R > result operator==( R const & rhs ) { return result{ lhs == rhs, to_string( lhs, "==", rhs ) }; }
template< typename R > result operator!=( R const & rhs ) { return result{ lhs != rhs, to_string( lhs, "!=", rhs ) }; }
template< typename R > result operator< ( R const & rhs ) { return result{ lhs < rhs, to_string( lhs, "<" , rhs ) }; }
template< typename R > result operator<=( R const & rhs ) { return result{ lhs <= rhs, to_string( lhs, "<=", rhs ) }; }
template< typename R > result operator> ( R const & rhs ) { return result{ lhs > rhs, to_string( lhs, ">" , rhs ) }; }
template< typename R > result operator>=( R const & rhs ) { return result{ lhs >= rhs, to_string( lhs, ">=", rhs ) }; }
};
struct expression_decomposer
{
template< typename L >
expression_lhs<L const &> operator<< ( L const & operand )
{
return expression_lhs<L const &>( operand );
}
};
} // namespace lest
#ifdef __clang__
# pragma clang diagnostic pop
#elif defined __GNUC__
# pragma GCC diagnostic pop
#endif
#endif // LEST_LEST_HPP_INCLUDED