Matthias Andreas Benkard | 4c71268 | 2018-04-10 20:54:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Catch v2.2.2 |
| 3 | * Generated: 2018-04-06 12:05:03.186665 |
| 4 | * ---------------------------------------------------------- |
| 5 | * This file has been merged from multiple headers. Please don't edit it directly |
| 6 | * Copyright (c) 2018 Two Blue Cubes Ltd. All rights reserved. |
| 7 | * |
| 8 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 9 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 10 | */ |
| 11 | #ifndef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED |
| 12 | #define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED |
| 13 | // start catch.hpp |
| 14 | |
| 15 | |
| 16 | #define CATCH_VERSION_MAJOR 2 |
| 17 | #define CATCH_VERSION_MINOR 2 |
| 18 | #define CATCH_VERSION_PATCH 2 |
| 19 | |
| 20 | #ifdef __clang__ |
| 21 | # pragma clang system_header |
| 22 | #elif defined __GNUC__ |
| 23 | # pragma GCC system_header |
| 24 | #endif |
| 25 | |
| 26 | // start catch_suppress_warnings.h |
| 27 | |
| 28 | #ifdef __clang__ |
| 29 | # ifdef __ICC // icpc defines the __clang__ macro |
| 30 | # pragma warning(push) |
| 31 | # pragma warning(disable: 161 1682) |
| 32 | # else // __ICC |
| 33 | # pragma clang diagnostic ignored "-Wunused-variable" |
| 34 | # pragma clang diagnostic push |
| 35 | # pragma clang diagnostic ignored "-Wpadded" |
| 36 | # pragma clang diagnostic ignored "-Wswitch-enum" |
| 37 | # pragma clang diagnostic ignored "-Wcovered-switch-default" |
| 38 | # endif |
| 39 | #elif defined __GNUC__ |
| 40 | # pragma GCC diagnostic ignored "-Wparentheses" |
| 41 | # pragma GCC diagnostic push |
| 42 | # pragma GCC diagnostic ignored "-Wunused-variable" |
| 43 | # pragma GCC diagnostic ignored "-Wpadded" |
| 44 | #endif |
| 45 | // end catch_suppress_warnings.h |
| 46 | #if defined(CATCH_CONFIG_MAIN) || defined(CATCH_CONFIG_RUNNER) |
| 47 | # define CATCH_IMPL |
| 48 | # define CATCH_CONFIG_ALL_PARTS |
| 49 | #endif |
| 50 | |
| 51 | // In the impl file, we want to have access to all parts of the headers |
| 52 | // Can also be used to sanely support PCHs |
| 53 | #if defined(CATCH_CONFIG_ALL_PARTS) |
| 54 | # define CATCH_CONFIG_EXTERNAL_INTERFACES |
| 55 | # if defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 56 | # undef CATCH_CONFIG_DISABLE_MATCHERS |
| 57 | # endif |
| 58 | # define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER |
| 59 | #endif |
| 60 | |
| 61 | #if !defined(CATCH_CONFIG_IMPL_ONLY) |
| 62 | // start catch_platform.h |
| 63 | |
| 64 | #ifdef __APPLE__ |
| 65 | # include <TargetConditionals.h> |
| 66 | # if TARGET_OS_OSX == 1 |
| 67 | # define CATCH_PLATFORM_MAC |
| 68 | # elif TARGET_OS_IPHONE == 1 |
| 69 | # define CATCH_PLATFORM_IPHONE |
| 70 | # endif |
| 71 | |
| 72 | #elif defined(linux) || defined(__linux) || defined(__linux__) |
| 73 | # define CATCH_PLATFORM_LINUX |
| 74 | |
| 75 | #elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) |
| 76 | # define CATCH_PLATFORM_WINDOWS |
| 77 | #endif |
| 78 | |
| 79 | // end catch_platform.h |
| 80 | |
| 81 | #ifdef CATCH_IMPL |
| 82 | # ifndef CLARA_CONFIG_MAIN |
| 83 | # define CLARA_CONFIG_MAIN_NOT_DEFINED |
| 84 | # define CLARA_CONFIG_MAIN |
| 85 | # endif |
| 86 | #endif |
| 87 | |
| 88 | // start catch_user_interfaces.h |
| 89 | |
| 90 | namespace Catch { |
| 91 | unsigned int rngSeed(); |
| 92 | } |
| 93 | |
| 94 | // end catch_user_interfaces.h |
| 95 | // start catch_tag_alias_autoregistrar.h |
| 96 | |
| 97 | // start catch_common.h |
| 98 | |
| 99 | // start catch_compiler_capabilities.h |
| 100 | |
| 101 | // Detect a number of compiler features - by compiler |
| 102 | // The following features are defined: |
| 103 | // |
| 104 | // CATCH_CONFIG_COUNTER : is the __COUNTER__ macro supported? |
| 105 | // CATCH_CONFIG_WINDOWS_SEH : is Windows SEH supported? |
| 106 | // CATCH_CONFIG_POSIX_SIGNALS : are POSIX signals supported? |
| 107 | // **************** |
| 108 | // Note to maintainers: if new toggles are added please document them |
| 109 | // in configuration.md, too |
| 110 | // **************** |
| 111 | |
| 112 | // In general each macro has a _NO_<feature name> form |
| 113 | // (e.g. CATCH_CONFIG_NO_POSIX_SIGNALS) which disables the feature. |
| 114 | // Many features, at point of detection, define an _INTERNAL_ macro, so they |
| 115 | // can be combined, en-mass, with the _NO_ forms later. |
| 116 | |
| 117 | #ifdef __cplusplus |
| 118 | |
| 119 | # if __cplusplus >= 201402L |
| 120 | # define CATCH_CPP14_OR_GREATER |
| 121 | # endif |
| 122 | |
| 123 | # if __cplusplus >= 201703L |
| 124 | # define CATCH_CPP17_OR_GREATER |
| 125 | # endif |
| 126 | |
| 127 | #endif |
| 128 | |
| 129 | #if defined(CATCH_CPP17_OR_GREATER) |
| 130 | # define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS |
| 131 | #endif |
| 132 | |
| 133 | #ifdef __clang__ |
| 134 | |
| 135 | # define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 136 | _Pragma( "clang diagnostic push" ) \ |
| 137 | _Pragma( "clang diagnostic ignored \"-Wexit-time-destructors\"" ) \ |
| 138 | _Pragma( "clang diagnostic ignored \"-Wglobal-constructors\"") |
| 139 | # define CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ |
| 140 | _Pragma( "clang diagnostic pop" ) |
| 141 | |
| 142 | # define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ |
| 143 | _Pragma( "clang diagnostic push" ) \ |
| 144 | _Pragma( "clang diagnostic ignored \"-Wparentheses\"" ) |
| 145 | # define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ |
| 146 | _Pragma( "clang diagnostic pop" ) |
| 147 | |
| 148 | #endif // __clang__ |
| 149 | |
| 150 | //////////////////////////////////////////////////////////////////////////////// |
| 151 | // Assume that non-Windows platforms support posix signals by default |
| 152 | #if !defined(CATCH_PLATFORM_WINDOWS) |
| 153 | #define CATCH_INTERNAL_CONFIG_POSIX_SIGNALS |
| 154 | #endif |
| 155 | |
| 156 | //////////////////////////////////////////////////////////////////////////////// |
| 157 | // We know some environments not to support full POSIX signals |
| 158 | #if defined(__CYGWIN__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(__DJGPP__) |
| 159 | #define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS |
| 160 | #endif |
| 161 | |
| 162 | #ifdef __OS400__ |
| 163 | # define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS |
| 164 | # define CATCH_CONFIG_COLOUR_NONE |
| 165 | #endif |
| 166 | |
| 167 | //////////////////////////////////////////////////////////////////////////////// |
| 168 | // Cygwin |
| 169 | #ifdef __CYGWIN__ |
| 170 | |
| 171 | // Required for some versions of Cygwin to declare gettimeofday |
| 172 | // see: http://stackoverflow.com/questions/36901803/gettimeofday-not-declared-in-this-scope-cygwin |
| 173 | # define _BSD_SOURCE |
| 174 | |
| 175 | #endif // __CYGWIN__ |
| 176 | |
| 177 | //////////////////////////////////////////////////////////////////////////////// |
| 178 | // Visual C++ |
| 179 | #ifdef _MSC_VER |
| 180 | |
| 181 | # if _MSC_VER >= 1900 // Visual Studio 2015 or newer |
| 182 | # define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS |
| 183 | # endif |
| 184 | |
| 185 | // Universal Windows platform does not support SEH |
| 186 | // Or console colours (or console at all...) |
| 187 | # if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) |
| 188 | # define CATCH_CONFIG_COLOUR_NONE |
| 189 | # else |
| 190 | # define CATCH_INTERNAL_CONFIG_WINDOWS_SEH |
| 191 | # endif |
| 192 | |
| 193 | #endif // _MSC_VER |
| 194 | |
| 195 | //////////////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | // DJGPP |
| 198 | #ifdef __DJGPP__ |
| 199 | # define CATCH_INTERNAL_CONFIG_NO_WCHAR |
| 200 | #endif // __DJGPP__ |
| 201 | |
| 202 | //////////////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | // Use of __COUNTER__ is suppressed during code analysis in |
| 205 | // CLion/AppCode 2017.2.x and former, because __COUNTER__ is not properly |
| 206 | // handled by it. |
| 207 | // Otherwise all supported compilers support COUNTER macro, |
| 208 | // but user still might want to turn it off |
| 209 | #if ( !defined(__JETBRAINS_IDE__) || __JETBRAINS_IDE__ >= 20170300L ) |
| 210 | #define CATCH_INTERNAL_CONFIG_COUNTER |
| 211 | #endif |
| 212 | |
| 213 | #if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER) |
| 214 | # define CATCH_CONFIG_COUNTER |
| 215 | #endif |
| 216 | #if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && !defined(CATCH_CONFIG_WINDOWS_SEH) |
| 217 | # define CATCH_CONFIG_WINDOWS_SEH |
| 218 | #endif |
| 219 | // This is set by default, because we assume that unix compilers are posix-signal-compatible by default. |
| 220 | #if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) |
| 221 | # define CATCH_CONFIG_POSIX_SIGNALS |
| 222 | #endif |
| 223 | // This is set by default, because we assume that compilers with no wchar_t support are just rare exceptions. |
| 224 | #if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_WCHAR) |
| 225 | # define CATCH_CONFIG_WCHAR |
| 226 | #endif |
| 227 | |
| 228 | #if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) |
| 229 | # define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS |
| 230 | #endif |
| 231 | |
| 232 | #if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) |
| 233 | # define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS |
| 234 | # define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS |
| 235 | #endif |
| 236 | #if !defined(CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS) |
| 237 | # define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS |
| 238 | # define CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS |
| 239 | #endif |
| 240 | |
| 241 | // end catch_compiler_capabilities.h |
| 242 | #define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line |
| 243 | #define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) |
| 244 | #ifdef CATCH_CONFIG_COUNTER |
| 245 | # define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ ) |
| 246 | #else |
| 247 | # define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ ) |
| 248 | #endif |
| 249 | |
| 250 | #include <iosfwd> |
| 251 | #include <string> |
| 252 | #include <cstdint> |
| 253 | |
| 254 | namespace Catch { |
| 255 | |
| 256 | struct CaseSensitive { enum Choice { |
| 257 | Yes, |
| 258 | No |
| 259 | }; }; |
| 260 | |
| 261 | class NonCopyable { |
| 262 | NonCopyable( NonCopyable const& ) = delete; |
| 263 | NonCopyable( NonCopyable && ) = delete; |
| 264 | NonCopyable& operator = ( NonCopyable const& ) = delete; |
| 265 | NonCopyable& operator = ( NonCopyable && ) = delete; |
| 266 | |
| 267 | protected: |
| 268 | NonCopyable(); |
| 269 | virtual ~NonCopyable(); |
| 270 | }; |
| 271 | |
| 272 | struct SourceLineInfo { |
| 273 | |
| 274 | SourceLineInfo() = delete; |
| 275 | SourceLineInfo( char const* _file, std::size_t _line ) noexcept |
| 276 | : file( _file ), |
| 277 | line( _line ) |
| 278 | {} |
| 279 | |
| 280 | SourceLineInfo( SourceLineInfo const& other ) = default; |
| 281 | SourceLineInfo( SourceLineInfo && ) = default; |
| 282 | SourceLineInfo& operator = ( SourceLineInfo const& ) = default; |
| 283 | SourceLineInfo& operator = ( SourceLineInfo && ) = default; |
| 284 | |
| 285 | bool empty() const noexcept; |
| 286 | bool operator == ( SourceLineInfo const& other ) const noexcept; |
| 287 | bool operator < ( SourceLineInfo const& other ) const noexcept; |
| 288 | |
| 289 | char const* file; |
| 290 | std::size_t line; |
| 291 | }; |
| 292 | |
| 293 | std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ); |
| 294 | |
| 295 | // Use this in variadic streaming macros to allow |
| 296 | // >> +StreamEndStop |
| 297 | // as well as |
| 298 | // >> stuff +StreamEndStop |
| 299 | struct StreamEndStop { |
| 300 | std::string operator+() const; |
| 301 | }; |
| 302 | template<typename T> |
| 303 | T const& operator + ( T const& value, StreamEndStop ) { |
| 304 | return value; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | #define CATCH_INTERNAL_LINEINFO \ |
| 309 | ::Catch::SourceLineInfo( __FILE__, static_cast<std::size_t>( __LINE__ ) ) |
| 310 | |
| 311 | // end catch_common.h |
| 312 | namespace Catch { |
| 313 | |
| 314 | struct RegistrarForTagAliases { |
| 315 | RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); |
| 316 | }; |
| 317 | |
| 318 | } // end namespace Catch |
| 319 | |
| 320 | #define CATCH_REGISTER_TAG_ALIAS( alias, spec ) \ |
| 321 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 322 | namespace{ Catch::RegistrarForTagAliases INTERNAL_CATCH_UNIQUE_NAME( AutoRegisterTagAlias )( alias, spec, CATCH_INTERNAL_LINEINFO ); } \ |
| 323 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS |
| 324 | |
| 325 | // end catch_tag_alias_autoregistrar.h |
| 326 | // start catch_test_registry.h |
| 327 | |
| 328 | // start catch_interfaces_testcase.h |
| 329 | |
| 330 | #include <vector> |
| 331 | #include <memory> |
| 332 | |
| 333 | namespace Catch { |
| 334 | |
| 335 | class TestSpec; |
| 336 | |
| 337 | struct ITestInvoker { |
| 338 | virtual void invoke () const = 0; |
| 339 | virtual ~ITestInvoker(); |
| 340 | }; |
| 341 | |
| 342 | using ITestCasePtr = std::shared_ptr<ITestInvoker>; |
| 343 | |
| 344 | class TestCase; |
| 345 | struct IConfig; |
| 346 | |
| 347 | struct ITestCaseRegistry { |
| 348 | virtual ~ITestCaseRegistry(); |
| 349 | virtual std::vector<TestCase> const& getAllTests() const = 0; |
| 350 | virtual std::vector<TestCase> const& getAllTestsSorted( IConfig const& config ) const = 0; |
| 351 | }; |
| 352 | |
| 353 | bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); |
| 354 | std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ); |
| 355 | std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ); |
| 356 | |
| 357 | } |
| 358 | |
| 359 | // end catch_interfaces_testcase.h |
| 360 | // start catch_stringref.h |
| 361 | |
| 362 | #include <cstddef> |
| 363 | #include <string> |
| 364 | #include <iosfwd> |
| 365 | |
| 366 | namespace Catch { |
| 367 | |
| 368 | class StringData; |
| 369 | |
| 370 | /// A non-owning string class (similar to the forthcoming std::string_view) |
| 371 | /// Note that, because a StringRef may be a substring of another string, |
| 372 | /// it may not be null terminated. c_str() must return a null terminated |
| 373 | /// string, however, and so the StringRef will internally take ownership |
| 374 | /// (taking a copy), if necessary. In theory this ownership is not externally |
| 375 | /// visible - but it does mean (substring) StringRefs should not be shared between |
| 376 | /// threads. |
| 377 | class StringRef { |
| 378 | public: |
| 379 | using size_type = std::size_t; |
| 380 | |
| 381 | private: |
| 382 | friend struct StringRefTestAccess; |
| 383 | |
| 384 | char const* m_start; |
| 385 | size_type m_size; |
| 386 | |
| 387 | char* m_data = nullptr; |
| 388 | |
| 389 | void takeOwnership(); |
| 390 | |
| 391 | static constexpr char const* const s_empty = ""; |
| 392 | |
| 393 | public: // construction/ assignment |
| 394 | StringRef() noexcept |
| 395 | : StringRef( s_empty, 0 ) |
| 396 | {} |
| 397 | |
| 398 | StringRef( StringRef const& other ) noexcept |
| 399 | : m_start( other.m_start ), |
| 400 | m_size( other.m_size ) |
| 401 | {} |
| 402 | |
| 403 | StringRef( StringRef&& other ) noexcept |
| 404 | : m_start( other.m_start ), |
| 405 | m_size( other.m_size ), |
| 406 | m_data( other.m_data ) |
| 407 | { |
| 408 | other.m_data = nullptr; |
| 409 | } |
| 410 | |
| 411 | StringRef( char const* rawChars ) noexcept; |
| 412 | |
| 413 | StringRef( char const* rawChars, size_type size ) noexcept |
| 414 | : m_start( rawChars ), |
| 415 | m_size( size ) |
| 416 | {} |
| 417 | |
| 418 | StringRef( std::string const& stdString ) noexcept |
| 419 | : m_start( stdString.c_str() ), |
| 420 | m_size( stdString.size() ) |
| 421 | {} |
| 422 | |
| 423 | ~StringRef() noexcept { |
| 424 | delete[] m_data; |
| 425 | } |
| 426 | |
| 427 | auto operator = ( StringRef const &other ) noexcept -> StringRef& { |
| 428 | delete[] m_data; |
| 429 | m_data = nullptr; |
| 430 | m_start = other.m_start; |
| 431 | m_size = other.m_size; |
| 432 | return *this; |
| 433 | } |
| 434 | |
| 435 | operator std::string() const; |
| 436 | |
| 437 | void swap( StringRef& other ) noexcept; |
| 438 | |
| 439 | public: // operators |
| 440 | auto operator == ( StringRef const& other ) const noexcept -> bool; |
| 441 | auto operator != ( StringRef const& other ) const noexcept -> bool; |
| 442 | |
| 443 | auto operator[] ( size_type index ) const noexcept -> char; |
| 444 | |
| 445 | public: // named queries |
| 446 | auto empty() const noexcept -> bool { |
| 447 | return m_size == 0; |
| 448 | } |
| 449 | auto size() const noexcept -> size_type { |
| 450 | return m_size; |
| 451 | } |
| 452 | |
| 453 | auto numberOfCharacters() const noexcept -> size_type; |
| 454 | auto c_str() const -> char const*; |
| 455 | |
| 456 | public: // substrings and searches |
| 457 | auto substr( size_type start, size_type size ) const noexcept -> StringRef; |
| 458 | |
| 459 | // Returns the current start pointer. |
| 460 | // Note that the pointer can change when if the StringRef is a substring |
| 461 | auto currentData() const noexcept -> char const*; |
| 462 | |
| 463 | private: // ownership queries - may not be consistent between calls |
| 464 | auto isOwned() const noexcept -> bool; |
| 465 | auto isSubstring() const noexcept -> bool; |
| 466 | }; |
| 467 | |
| 468 | auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string; |
| 469 | auto operator + ( StringRef const& lhs, char const* rhs ) -> std::string; |
| 470 | auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string; |
| 471 | |
| 472 | auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; |
| 473 | auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; |
| 474 | |
| 475 | inline auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { |
| 476 | return StringRef( rawChars, size ); |
| 477 | } |
| 478 | |
| 479 | } // namespace Catch |
| 480 | |
| 481 | // end catch_stringref.h |
| 482 | namespace Catch { |
| 483 | |
| 484 | template<typename C> |
| 485 | class TestInvokerAsMethod : public ITestInvoker { |
| 486 | void (C::*m_testAsMethod)(); |
| 487 | public: |
| 488 | TestInvokerAsMethod( void (C::*testAsMethod)() ) noexcept : m_testAsMethod( testAsMethod ) {} |
| 489 | |
| 490 | void invoke() const override { |
| 491 | C obj; |
| 492 | (obj.*m_testAsMethod)(); |
| 493 | } |
| 494 | }; |
| 495 | |
| 496 | auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker*; |
| 497 | |
| 498 | template<typename C> |
| 499 | auto makeTestInvoker( void (C::*testAsMethod)() ) noexcept -> ITestInvoker* { |
| 500 | return new(std::nothrow) TestInvokerAsMethod<C>( testAsMethod ); |
| 501 | } |
| 502 | |
| 503 | struct NameAndTags { |
| 504 | NameAndTags( StringRef const& name_ = StringRef(), StringRef const& tags_ = StringRef() ) noexcept; |
| 505 | StringRef name; |
| 506 | StringRef tags; |
| 507 | }; |
| 508 | |
| 509 | struct AutoReg : NonCopyable { |
| 510 | AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef const& classOrMethod, NameAndTags const& nameAndTags ) noexcept; |
| 511 | ~AutoReg(); |
| 512 | }; |
| 513 | |
| 514 | } // end namespace Catch |
| 515 | |
| 516 | #if defined(CATCH_CONFIG_DISABLE) |
| 517 | #define INTERNAL_CATCH_TESTCASE_NO_REGISTRATION( TestName, ... ) \ |
| 518 | static void TestName() |
| 519 | #define INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION( TestName, ClassName, ... ) \ |
| 520 | namespace{ \ |
| 521 | struct TestName : ClassName { \ |
| 522 | void test(); \ |
| 523 | }; \ |
| 524 | } \ |
| 525 | void TestName::test() |
| 526 | |
| 527 | #endif |
| 528 | |
| 529 | /////////////////////////////////////////////////////////////////////////////// |
| 530 | #define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \ |
| 531 | static void TestName(); \ |
| 532 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 533 | namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &TestName ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); } /* NOLINT */ \ |
| 534 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ |
| 535 | static void TestName() |
| 536 | #define INTERNAL_CATCH_TESTCASE( ... ) \ |
| 537 | INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), __VA_ARGS__ ) |
| 538 | |
| 539 | /////////////////////////////////////////////////////////////////////////////// |
| 540 | #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \ |
| 541 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 542 | namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &QualifiedMethod ), CATCH_INTERNAL_LINEINFO, "&" #QualifiedMethod, Catch::NameAndTags{ __VA_ARGS__ } ); } /* NOLINT */ \ |
| 543 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS |
| 544 | |
| 545 | /////////////////////////////////////////////////////////////////////////////// |
| 546 | #define INTERNAL_CATCH_TEST_CASE_METHOD2( TestName, ClassName, ... )\ |
| 547 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 548 | namespace{ \ |
| 549 | struct TestName : ClassName{ \ |
| 550 | void test(); \ |
| 551 | }; \ |
| 552 | Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ __VA_ARGS__ } ); /* NOLINT */ \ |
| 553 | } \ |
| 554 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ |
| 555 | void TestName::test() |
| 556 | #define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, ... ) \ |
| 557 | INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), ClassName, __VA_ARGS__ ) |
| 558 | |
| 559 | /////////////////////////////////////////////////////////////////////////////// |
| 560 | #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \ |
| 561 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 562 | Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( Function ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); /* NOLINT */ \ |
| 563 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS |
| 564 | |
| 565 | // end catch_test_registry.h |
| 566 | // start catch_capture.hpp |
| 567 | |
| 568 | // start catch_assertionhandler.h |
| 569 | |
| 570 | // start catch_assertioninfo.h |
| 571 | |
| 572 | // start catch_result_type.h |
| 573 | |
| 574 | namespace Catch { |
| 575 | |
| 576 | // ResultWas::OfType enum |
| 577 | struct ResultWas { enum OfType { |
| 578 | Unknown = -1, |
| 579 | Ok = 0, |
| 580 | Info = 1, |
| 581 | Warning = 2, |
| 582 | |
| 583 | FailureBit = 0x10, |
| 584 | |
| 585 | ExpressionFailed = FailureBit | 1, |
| 586 | ExplicitFailure = FailureBit | 2, |
| 587 | |
| 588 | Exception = 0x100 | FailureBit, |
| 589 | |
| 590 | ThrewException = Exception | 1, |
| 591 | DidntThrowException = Exception | 2, |
| 592 | |
| 593 | FatalErrorCondition = 0x200 | FailureBit |
| 594 | |
| 595 | }; }; |
| 596 | |
| 597 | bool isOk( ResultWas::OfType resultType ); |
| 598 | bool isJustInfo( int flags ); |
| 599 | |
| 600 | // ResultDisposition::Flags enum |
| 601 | struct ResultDisposition { enum Flags { |
| 602 | Normal = 0x01, |
| 603 | |
| 604 | ContinueOnFailure = 0x02, // Failures fail test, but execution continues |
| 605 | FalseTest = 0x04, // Prefix expression with ! |
| 606 | SuppressFail = 0x08 // Failures are reported but do not fail the test |
| 607 | }; }; |
| 608 | |
| 609 | ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ); |
| 610 | |
| 611 | bool shouldContinueOnFailure( int flags ); |
| 612 | inline bool isFalseTest( int flags ) { return ( flags & ResultDisposition::FalseTest ) != 0; } |
| 613 | bool shouldSuppressFailure( int flags ); |
| 614 | |
| 615 | } // end namespace Catch |
| 616 | |
| 617 | // end catch_result_type.h |
| 618 | namespace Catch { |
| 619 | |
| 620 | struct AssertionInfo |
| 621 | { |
| 622 | StringRef macroName; |
| 623 | SourceLineInfo lineInfo; |
| 624 | StringRef capturedExpression; |
| 625 | ResultDisposition::Flags resultDisposition; |
| 626 | |
| 627 | // We want to delete this constructor but a compiler bug in 4.8 means |
| 628 | // the struct is then treated as non-aggregate |
| 629 | //AssertionInfo() = delete; |
| 630 | }; |
| 631 | |
| 632 | } // end namespace Catch |
| 633 | |
| 634 | // end catch_assertioninfo.h |
| 635 | // start catch_decomposer.h |
| 636 | |
| 637 | // start catch_tostring.h |
| 638 | |
| 639 | #include <vector> |
| 640 | #include <cstddef> |
| 641 | #include <type_traits> |
| 642 | #include <string> |
| 643 | // start catch_stream.h |
| 644 | |
| 645 | #include <iosfwd> |
| 646 | #include <cstddef> |
| 647 | #include <ostream> |
| 648 | |
| 649 | namespace Catch { |
| 650 | |
| 651 | std::ostream& cout(); |
| 652 | std::ostream& cerr(); |
| 653 | std::ostream& clog(); |
| 654 | |
| 655 | class StringRef; |
| 656 | |
| 657 | struct IStream { |
| 658 | virtual ~IStream(); |
| 659 | virtual std::ostream& stream() const = 0; |
| 660 | }; |
| 661 | |
| 662 | auto makeStream( StringRef const &filename ) -> IStream const*; |
| 663 | |
| 664 | class ReusableStringStream { |
| 665 | std::size_t m_index; |
| 666 | std::ostream* m_oss; |
| 667 | public: |
| 668 | ReusableStringStream(); |
| 669 | ~ReusableStringStream(); |
| 670 | |
| 671 | auto str() const -> std::string; |
| 672 | |
| 673 | template<typename T> |
| 674 | auto operator << ( T const& value ) -> ReusableStringStream& { |
| 675 | *m_oss << value; |
| 676 | return *this; |
| 677 | } |
| 678 | auto get() -> std::ostream& { return *m_oss; } |
| 679 | |
| 680 | static void cleanup(); |
| 681 | }; |
| 682 | } |
| 683 | |
| 684 | // end catch_stream.h |
| 685 | |
| 686 | #ifdef __OBJC__ |
| 687 | // start catch_objc_arc.hpp |
| 688 | |
| 689 | #import <Foundation/Foundation.h> |
| 690 | |
| 691 | #ifdef __has_feature |
| 692 | #define CATCH_ARC_ENABLED __has_feature(objc_arc) |
| 693 | #else |
| 694 | #define CATCH_ARC_ENABLED 0 |
| 695 | #endif |
| 696 | |
| 697 | void arcSafeRelease( NSObject* obj ); |
| 698 | id performOptionalSelector( id obj, SEL sel ); |
| 699 | |
| 700 | #if !CATCH_ARC_ENABLED |
| 701 | inline void arcSafeRelease( NSObject* obj ) { |
| 702 | [obj release]; |
| 703 | } |
| 704 | inline id performOptionalSelector( id obj, SEL sel ) { |
| 705 | if( [obj respondsToSelector: sel] ) |
| 706 | return [obj performSelector: sel]; |
| 707 | return nil; |
| 708 | } |
| 709 | #define CATCH_UNSAFE_UNRETAINED |
| 710 | #define CATCH_ARC_STRONG |
| 711 | #else |
| 712 | inline void arcSafeRelease( NSObject* ){} |
| 713 | inline id performOptionalSelector( id obj, SEL sel ) { |
| 714 | #ifdef __clang__ |
| 715 | #pragma clang diagnostic push |
| 716 | #pragma clang diagnostic ignored "-Warc-performSelector-leaks" |
| 717 | #endif |
| 718 | if( [obj respondsToSelector: sel] ) |
| 719 | return [obj performSelector: sel]; |
| 720 | #ifdef __clang__ |
| 721 | #pragma clang diagnostic pop |
| 722 | #endif |
| 723 | return nil; |
| 724 | } |
| 725 | #define CATCH_UNSAFE_UNRETAINED __unsafe_unretained |
| 726 | #define CATCH_ARC_STRONG __strong |
| 727 | #endif |
| 728 | |
| 729 | // end catch_objc_arc.hpp |
| 730 | #endif |
| 731 | |
| 732 | #ifdef _MSC_VER |
| 733 | #pragma warning(push) |
| 734 | #pragma warning(disable:4180) // We attempt to stream a function (address) by const&, which MSVC complains about but is harmless |
| 735 | #endif |
| 736 | |
| 737 | // We need a dummy global operator<< so we can bring it into Catch namespace later |
| 738 | struct Catch_global_namespace_dummy {}; |
| 739 | std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy); |
| 740 | |
| 741 | namespace Catch { |
| 742 | // Bring in operator<< from global namespace into Catch namespace |
| 743 | using ::operator<<; |
| 744 | |
| 745 | namespace Detail { |
| 746 | |
| 747 | extern const std::string unprintableString; |
| 748 | |
| 749 | std::string rawMemoryToString( const void *object, std::size_t size ); |
| 750 | |
| 751 | template<typename T> |
| 752 | std::string rawMemoryToString( const T& object ) { |
| 753 | return rawMemoryToString( &object, sizeof(object) ); |
| 754 | } |
| 755 | |
| 756 | template<typename T> |
| 757 | class IsStreamInsertable { |
| 758 | template<typename SS, typename TT> |
| 759 | static auto test(int) |
| 760 | -> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type()); |
| 761 | |
| 762 | template<typename, typename> |
| 763 | static auto test(...)->std::false_type; |
| 764 | |
| 765 | public: |
| 766 | static const bool value = decltype(test<std::ostream, const T&>(0))::value; |
| 767 | }; |
| 768 | |
| 769 | template<typename E> |
| 770 | std::string convertUnknownEnumToString( E e ); |
| 771 | |
| 772 | template<typename T> |
| 773 | typename std::enable_if<!std::is_enum<T>::value, std::string>::type convertUnstreamable( T const& value ) { |
| 774 | #if !defined(CATCH_CONFIG_FALLBACK_STRINGIFIER) |
| 775 | (void)value; |
| 776 | return Detail::unprintableString; |
| 777 | #else |
| 778 | return CATCH_CONFIG_FALLBACK_STRINGIFIER(value); |
| 779 | #endif |
| 780 | } |
| 781 | template<typename T> |
| 782 | typename std::enable_if<std::is_enum<T>::value, std::string>::type convertUnstreamable( T const& value ) { |
| 783 | return convertUnknownEnumToString( value ); |
| 784 | } |
| 785 | |
| 786 | #if defined(_MANAGED) |
| 787 | //! Convert a CLR string to a utf8 std::string |
| 788 | template<typename T> |
| 789 | std::string clrReferenceToString( T^ ref ) { |
| 790 | if (ref == nullptr) |
| 791 | return std::string("null"); |
| 792 | auto bytes = System::Text::Encoding::UTF8->GetBytes(ref->ToString()); |
| 793 | cli::pin_ptr<System::Byte> p = &bytes[0]; |
| 794 | return std::string(reinterpret_cast<char const *>(p), bytes->Length); |
| 795 | } |
| 796 | #endif |
| 797 | |
| 798 | } // namespace Detail |
| 799 | |
| 800 | // If we decide for C++14, change these to enable_if_ts |
| 801 | template <typename T, typename = void> |
| 802 | struct StringMaker { |
| 803 | template <typename Fake = T> |
| 804 | static |
| 805 | typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type |
| 806 | convert(const Fake& value) { |
| 807 | ReusableStringStream rss; |
| 808 | rss << value; |
| 809 | return rss.str(); |
| 810 | } |
| 811 | |
| 812 | template <typename Fake = T> |
| 813 | static |
| 814 | typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type |
| 815 | convert( const Fake& value ) { |
| 816 | return Detail::convertUnstreamable( value ); |
| 817 | } |
| 818 | }; |
| 819 | |
| 820 | namespace Detail { |
| 821 | |
| 822 | // This function dispatches all stringification requests inside of Catch. |
| 823 | // Should be preferably called fully qualified, like ::Catch::Detail::stringify |
| 824 | template <typename T> |
| 825 | std::string stringify(const T& e) { |
| 826 | return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::convert(e); |
| 827 | } |
| 828 | |
| 829 | template<typename E> |
| 830 | std::string convertUnknownEnumToString( E e ) { |
| 831 | return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<E>::type>(e)); |
| 832 | } |
| 833 | |
| 834 | #if defined(_MANAGED) |
| 835 | template <typename T> |
| 836 | std::string stringify( T^ e ) { |
| 837 | return ::Catch::StringMaker<T^>::convert(e); |
| 838 | } |
| 839 | #endif |
| 840 | |
| 841 | } // namespace Detail |
| 842 | |
| 843 | // Some predefined specializations |
| 844 | |
| 845 | template<> |
| 846 | struct StringMaker<std::string> { |
| 847 | static std::string convert(const std::string& str); |
| 848 | }; |
| 849 | #ifdef CATCH_CONFIG_WCHAR |
| 850 | template<> |
| 851 | struct StringMaker<std::wstring> { |
| 852 | static std::string convert(const std::wstring& wstr); |
| 853 | }; |
| 854 | #endif |
| 855 | |
| 856 | template<> |
| 857 | struct StringMaker<char const *> { |
| 858 | static std::string convert(char const * str); |
| 859 | }; |
| 860 | template<> |
| 861 | struct StringMaker<char *> { |
| 862 | static std::string convert(char * str); |
| 863 | }; |
| 864 | |
| 865 | #ifdef CATCH_CONFIG_WCHAR |
| 866 | template<> |
| 867 | struct StringMaker<wchar_t const *> { |
| 868 | static std::string convert(wchar_t const * str); |
| 869 | }; |
| 870 | template<> |
| 871 | struct StringMaker<wchar_t *> { |
| 872 | static std::string convert(wchar_t * str); |
| 873 | }; |
| 874 | #endif |
| 875 | |
| 876 | // TBD: Should we use `strnlen` to ensure that we don't go out of the buffer, |
| 877 | // while keeping string semantics? |
| 878 | template<int SZ> |
| 879 | struct StringMaker<char[SZ]> { |
| 880 | static std::string convert(char const* str) { |
| 881 | return ::Catch::Detail::stringify(std::string{ str }); |
| 882 | } |
| 883 | }; |
| 884 | template<int SZ> |
| 885 | struct StringMaker<signed char[SZ]> { |
| 886 | static std::string convert(signed char const* str) { |
| 887 | return ::Catch::Detail::stringify(std::string{ reinterpret_cast<char const *>(str) }); |
| 888 | } |
| 889 | }; |
| 890 | template<int SZ> |
| 891 | struct StringMaker<unsigned char[SZ]> { |
| 892 | static std::string convert(unsigned char const* str) { |
| 893 | return ::Catch::Detail::stringify(std::string{ reinterpret_cast<char const *>(str) }); |
| 894 | } |
| 895 | }; |
| 896 | |
| 897 | template<> |
| 898 | struct StringMaker<int> { |
| 899 | static std::string convert(int value); |
| 900 | }; |
| 901 | template<> |
| 902 | struct StringMaker<long> { |
| 903 | static std::string convert(long value); |
| 904 | }; |
| 905 | template<> |
| 906 | struct StringMaker<long long> { |
| 907 | static std::string convert(long long value); |
| 908 | }; |
| 909 | template<> |
| 910 | struct StringMaker<unsigned int> { |
| 911 | static std::string convert(unsigned int value); |
| 912 | }; |
| 913 | template<> |
| 914 | struct StringMaker<unsigned long> { |
| 915 | static std::string convert(unsigned long value); |
| 916 | }; |
| 917 | template<> |
| 918 | struct StringMaker<unsigned long long> { |
| 919 | static std::string convert(unsigned long long value); |
| 920 | }; |
| 921 | |
| 922 | template<> |
| 923 | struct StringMaker<bool> { |
| 924 | static std::string convert(bool b); |
| 925 | }; |
| 926 | |
| 927 | template<> |
| 928 | struct StringMaker<char> { |
| 929 | static std::string convert(char c); |
| 930 | }; |
| 931 | template<> |
| 932 | struct StringMaker<signed char> { |
| 933 | static std::string convert(signed char c); |
| 934 | }; |
| 935 | template<> |
| 936 | struct StringMaker<unsigned char> { |
| 937 | static std::string convert(unsigned char c); |
| 938 | }; |
| 939 | |
| 940 | template<> |
| 941 | struct StringMaker<std::nullptr_t> { |
| 942 | static std::string convert(std::nullptr_t); |
| 943 | }; |
| 944 | |
| 945 | template<> |
| 946 | struct StringMaker<float> { |
| 947 | static std::string convert(float value); |
| 948 | }; |
| 949 | template<> |
| 950 | struct StringMaker<double> { |
| 951 | static std::string convert(double value); |
| 952 | }; |
| 953 | |
| 954 | template <typename T> |
| 955 | struct StringMaker<T*> { |
| 956 | template <typename U> |
| 957 | static std::string convert(U* p) { |
| 958 | if (p) { |
| 959 | return ::Catch::Detail::rawMemoryToString(p); |
| 960 | } else { |
| 961 | return "nullptr"; |
| 962 | } |
| 963 | } |
| 964 | }; |
| 965 | |
| 966 | template <typename R, typename C> |
| 967 | struct StringMaker<R C::*> { |
| 968 | static std::string convert(R C::* p) { |
| 969 | if (p) { |
| 970 | return ::Catch::Detail::rawMemoryToString(p); |
| 971 | } else { |
| 972 | return "nullptr"; |
| 973 | } |
| 974 | } |
| 975 | }; |
| 976 | |
| 977 | #if defined(_MANAGED) |
| 978 | template <typename T> |
| 979 | struct StringMaker<T^> { |
| 980 | static std::string convert( T^ ref ) { |
| 981 | return ::Catch::Detail::clrReferenceToString(ref); |
| 982 | } |
| 983 | }; |
| 984 | #endif |
| 985 | |
| 986 | namespace Detail { |
| 987 | template<typename InputIterator> |
| 988 | std::string rangeToString(InputIterator first, InputIterator last) { |
| 989 | ReusableStringStream rss; |
| 990 | rss << "{ "; |
| 991 | if (first != last) { |
| 992 | rss << ::Catch::Detail::stringify(*first); |
| 993 | for (++first; first != last; ++first) |
| 994 | rss << ", " << ::Catch::Detail::stringify(*first); |
| 995 | } |
| 996 | rss << " }"; |
| 997 | return rss.str(); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | #ifdef __OBJC__ |
| 1002 | template<> |
| 1003 | struct StringMaker<NSString*> { |
| 1004 | static std::string convert(NSString * nsstring) { |
| 1005 | if (!nsstring) |
| 1006 | return "nil"; |
| 1007 | return std::string("@") + [nsstring UTF8String]; |
| 1008 | } |
| 1009 | }; |
| 1010 | template<> |
| 1011 | struct StringMaker<NSObject*> { |
| 1012 | static std::string convert(NSObject* nsObject) { |
| 1013 | return ::Catch::Detail::stringify([nsObject description]); |
| 1014 | } |
| 1015 | |
| 1016 | }; |
| 1017 | namespace Detail { |
| 1018 | inline std::string stringify( NSString* nsstring ) { |
| 1019 | return StringMaker<NSString*>::convert( nsstring ); |
| 1020 | } |
| 1021 | |
| 1022 | } // namespace Detail |
| 1023 | #endif // __OBJC__ |
| 1024 | |
| 1025 | } // namespace Catch |
| 1026 | |
| 1027 | ////////////////////////////////////////////////////// |
| 1028 | // Separate std-lib types stringification, so it can be selectively enabled |
| 1029 | // This means that we do not bring in |
| 1030 | |
| 1031 | #if defined(CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS) |
| 1032 | # define CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER |
| 1033 | # define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER |
| 1034 | # define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER |
| 1035 | #endif |
| 1036 | |
| 1037 | // Separate std::pair specialization |
| 1038 | #if defined(CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER) |
| 1039 | #include <utility> |
| 1040 | namespace Catch { |
| 1041 | template<typename T1, typename T2> |
| 1042 | struct StringMaker<std::pair<T1, T2> > { |
| 1043 | static std::string convert(const std::pair<T1, T2>& pair) { |
| 1044 | ReusableStringStream rss; |
| 1045 | rss << "{ " |
| 1046 | << ::Catch::Detail::stringify(pair.first) |
| 1047 | << ", " |
| 1048 | << ::Catch::Detail::stringify(pair.second) |
| 1049 | << " }"; |
| 1050 | return rss.str(); |
| 1051 | } |
| 1052 | }; |
| 1053 | } |
| 1054 | #endif // CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER |
| 1055 | |
| 1056 | // Separate std::tuple specialization |
| 1057 | #if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER) |
| 1058 | #include <tuple> |
| 1059 | namespace Catch { |
| 1060 | namespace Detail { |
| 1061 | template< |
| 1062 | typename Tuple, |
| 1063 | std::size_t N = 0, |
| 1064 | bool = (N < std::tuple_size<Tuple>::value) |
| 1065 | > |
| 1066 | struct TupleElementPrinter { |
| 1067 | static void print(const Tuple& tuple, std::ostream& os) { |
| 1068 | os << (N ? ", " : " ") |
| 1069 | << ::Catch::Detail::stringify(std::get<N>(tuple)); |
| 1070 | TupleElementPrinter<Tuple, N + 1>::print(tuple, os); |
| 1071 | } |
| 1072 | }; |
| 1073 | |
| 1074 | template< |
| 1075 | typename Tuple, |
| 1076 | std::size_t N |
| 1077 | > |
| 1078 | struct TupleElementPrinter<Tuple, N, false> { |
| 1079 | static void print(const Tuple&, std::ostream&) {} |
| 1080 | }; |
| 1081 | |
| 1082 | } |
| 1083 | |
| 1084 | template<typename ...Types> |
| 1085 | struct StringMaker<std::tuple<Types...>> { |
| 1086 | static std::string convert(const std::tuple<Types...>& tuple) { |
| 1087 | ReusableStringStream rss; |
| 1088 | rss << '{'; |
| 1089 | Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, rss.get()); |
| 1090 | rss << " }"; |
| 1091 | return rss.str(); |
| 1092 | } |
| 1093 | }; |
| 1094 | } |
| 1095 | #endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER |
| 1096 | |
| 1097 | namespace Catch { |
| 1098 | struct not_this_one {}; // Tag type for detecting which begin/ end are being selected |
| 1099 | |
| 1100 | // Import begin/ end from std here so they are considered alongside the fallback (...) overloads in this namespace |
| 1101 | using std::begin; |
| 1102 | using std::end; |
| 1103 | |
| 1104 | not_this_one begin( ... ); |
| 1105 | not_this_one end( ... ); |
| 1106 | |
| 1107 | template <typename T> |
| 1108 | struct is_range { |
| 1109 | static const bool value = |
| 1110 | !std::is_same<decltype(begin(std::declval<T>())), not_this_one>::value && |
| 1111 | !std::is_same<decltype(end(std::declval<T>())), not_this_one>::value; |
| 1112 | }; |
| 1113 | |
| 1114 | #if defined(_MANAGED) // Managed types are never ranges |
| 1115 | template <typename T> |
| 1116 | struct is_range<T^> { |
| 1117 | static const bool value = false; |
| 1118 | }; |
| 1119 | #endif |
| 1120 | |
| 1121 | template<typename Range> |
| 1122 | std::string rangeToString( Range const& range ) { |
| 1123 | return ::Catch::Detail::rangeToString( begin( range ), end( range ) ); |
| 1124 | } |
| 1125 | |
| 1126 | // Handle vector<bool> specially |
| 1127 | template<typename Allocator> |
| 1128 | std::string rangeToString( std::vector<bool, Allocator> const& v ) { |
| 1129 | ReusableStringStream rss; |
| 1130 | rss << "{ "; |
| 1131 | bool first = true; |
| 1132 | for( bool b : v ) { |
| 1133 | if( first ) |
| 1134 | first = false; |
| 1135 | else |
| 1136 | rss << ", "; |
| 1137 | rss << ::Catch::Detail::stringify( b ); |
| 1138 | } |
| 1139 | rss << " }"; |
| 1140 | return rss.str(); |
| 1141 | } |
| 1142 | |
| 1143 | template<typename R> |
| 1144 | struct StringMaker<R, typename std::enable_if<is_range<R>::value && !::Catch::Detail::IsStreamInsertable<R>::value>::type> { |
| 1145 | static std::string convert( R const& range ) { |
| 1146 | return rangeToString( range ); |
| 1147 | } |
| 1148 | }; |
| 1149 | |
| 1150 | template <typename T, int SZ> |
| 1151 | struct StringMaker<T[SZ]> { |
| 1152 | static std::string convert(T const(&arr)[SZ]) { |
| 1153 | return rangeToString(arr); |
| 1154 | } |
| 1155 | }; |
| 1156 | |
| 1157 | } // namespace Catch |
| 1158 | |
| 1159 | // Separate std::chrono::duration specialization |
| 1160 | #if defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) |
| 1161 | #include <ctime> |
| 1162 | #include <ratio> |
| 1163 | #include <chrono> |
| 1164 | |
| 1165 | namespace Catch { |
| 1166 | |
| 1167 | template <class Ratio> |
| 1168 | struct ratio_string { |
| 1169 | static std::string symbol(); |
| 1170 | }; |
| 1171 | |
| 1172 | template <class Ratio> |
| 1173 | std::string ratio_string<Ratio>::symbol() { |
| 1174 | Catch::ReusableStringStream rss; |
| 1175 | rss << '[' << Ratio::num << '/' |
| 1176 | << Ratio::den << ']'; |
| 1177 | return rss.str(); |
| 1178 | } |
| 1179 | template <> |
| 1180 | struct ratio_string<std::atto> { |
| 1181 | static std::string symbol(); |
| 1182 | }; |
| 1183 | template <> |
| 1184 | struct ratio_string<std::femto> { |
| 1185 | static std::string symbol(); |
| 1186 | }; |
| 1187 | template <> |
| 1188 | struct ratio_string<std::pico> { |
| 1189 | static std::string symbol(); |
| 1190 | }; |
| 1191 | template <> |
| 1192 | struct ratio_string<std::nano> { |
| 1193 | static std::string symbol(); |
| 1194 | }; |
| 1195 | template <> |
| 1196 | struct ratio_string<std::micro> { |
| 1197 | static std::string symbol(); |
| 1198 | }; |
| 1199 | template <> |
| 1200 | struct ratio_string<std::milli> { |
| 1201 | static std::string symbol(); |
| 1202 | }; |
| 1203 | |
| 1204 | //////////// |
| 1205 | // std::chrono::duration specializations |
| 1206 | template<typename Value, typename Ratio> |
| 1207 | struct StringMaker<std::chrono::duration<Value, Ratio>> { |
| 1208 | static std::string convert(std::chrono::duration<Value, Ratio> const& duration) { |
| 1209 | ReusableStringStream rss; |
| 1210 | rss << duration.count() << ' ' << ratio_string<Ratio>::symbol() << 's'; |
| 1211 | return rss.str(); |
| 1212 | } |
| 1213 | }; |
| 1214 | template<typename Value> |
| 1215 | struct StringMaker<std::chrono::duration<Value, std::ratio<1>>> { |
| 1216 | static std::string convert(std::chrono::duration<Value, std::ratio<1>> const& duration) { |
| 1217 | ReusableStringStream rss; |
| 1218 | rss << duration.count() << " s"; |
| 1219 | return rss.str(); |
| 1220 | } |
| 1221 | }; |
| 1222 | template<typename Value> |
| 1223 | struct StringMaker<std::chrono::duration<Value, std::ratio<60>>> { |
| 1224 | static std::string convert(std::chrono::duration<Value, std::ratio<60>> const& duration) { |
| 1225 | ReusableStringStream rss; |
| 1226 | rss << duration.count() << " m"; |
| 1227 | return rss.str(); |
| 1228 | } |
| 1229 | }; |
| 1230 | template<typename Value> |
| 1231 | struct StringMaker<std::chrono::duration<Value, std::ratio<3600>>> { |
| 1232 | static std::string convert(std::chrono::duration<Value, std::ratio<3600>> const& duration) { |
| 1233 | ReusableStringStream rss; |
| 1234 | rss << duration.count() << " h"; |
| 1235 | return rss.str(); |
| 1236 | } |
| 1237 | }; |
| 1238 | |
| 1239 | //////////// |
| 1240 | // std::chrono::time_point specialization |
| 1241 | // Generic time_point cannot be specialized, only std::chrono::time_point<system_clock> |
| 1242 | template<typename Clock, typename Duration> |
| 1243 | struct StringMaker<std::chrono::time_point<Clock, Duration>> { |
| 1244 | static std::string convert(std::chrono::time_point<Clock, Duration> const& time_point) { |
| 1245 | return ::Catch::Detail::stringify(time_point.time_since_epoch()) + " since epoch"; |
| 1246 | } |
| 1247 | }; |
| 1248 | // std::chrono::time_point<system_clock> specialization |
| 1249 | template<typename Duration> |
| 1250 | struct StringMaker<std::chrono::time_point<std::chrono::system_clock, Duration>> { |
| 1251 | static std::string convert(std::chrono::time_point<std::chrono::system_clock, Duration> const& time_point) { |
| 1252 | auto converted = std::chrono::system_clock::to_time_t(time_point); |
| 1253 | |
| 1254 | #ifdef _MSC_VER |
| 1255 | std::tm timeInfo = {}; |
| 1256 | gmtime_s(&timeInfo, &converted); |
| 1257 | #else |
| 1258 | std::tm* timeInfo = std::gmtime(&converted); |
| 1259 | #endif |
| 1260 | |
| 1261 | auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); |
| 1262 | char timeStamp[timeStampSize]; |
| 1263 | const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; |
| 1264 | |
| 1265 | #ifdef _MSC_VER |
| 1266 | std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); |
| 1267 | #else |
| 1268 | std::strftime(timeStamp, timeStampSize, fmt, timeInfo); |
| 1269 | #endif |
| 1270 | return std::string(timeStamp); |
| 1271 | } |
| 1272 | }; |
| 1273 | } |
| 1274 | #endif // CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER |
| 1275 | |
| 1276 | #ifdef _MSC_VER |
| 1277 | #pragma warning(pop) |
| 1278 | #endif |
| 1279 | |
| 1280 | // end catch_tostring.h |
| 1281 | #include <iosfwd> |
| 1282 | |
| 1283 | #ifdef _MSC_VER |
| 1284 | #pragma warning(push) |
| 1285 | #pragma warning(disable:4389) // '==' : signed/unsigned mismatch |
| 1286 | #pragma warning(disable:4018) // more "signed/unsigned mismatch" |
| 1287 | #pragma warning(disable:4312) // Converting int to T* using reinterpret_cast (issue on x64 platform) |
| 1288 | #pragma warning(disable:4180) // qualifier applied to function type has no meaning |
| 1289 | #endif |
| 1290 | |
| 1291 | namespace Catch { |
| 1292 | |
| 1293 | struct ITransientExpression { |
| 1294 | auto isBinaryExpression() const -> bool { return m_isBinaryExpression; } |
| 1295 | auto getResult() const -> bool { return m_result; } |
| 1296 | virtual void streamReconstructedExpression( std::ostream &os ) const = 0; |
| 1297 | |
| 1298 | ITransientExpression( bool isBinaryExpression, bool result ) |
| 1299 | : m_isBinaryExpression( isBinaryExpression ), |
| 1300 | m_result( result ) |
| 1301 | {} |
| 1302 | |
| 1303 | // We don't actually need a virtual destructor, but many static analysers |
| 1304 | // complain if it's not here :-( |
| 1305 | virtual ~ITransientExpression(); |
| 1306 | |
| 1307 | bool m_isBinaryExpression; |
| 1308 | bool m_result; |
| 1309 | |
| 1310 | }; |
| 1311 | |
| 1312 | void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ); |
| 1313 | |
| 1314 | template<typename LhsT, typename RhsT> |
| 1315 | class BinaryExpr : public ITransientExpression { |
| 1316 | LhsT m_lhs; |
| 1317 | StringRef m_op; |
| 1318 | RhsT m_rhs; |
| 1319 | |
| 1320 | void streamReconstructedExpression( std::ostream &os ) const override { |
| 1321 | formatReconstructedExpression |
| 1322 | ( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) ); |
| 1323 | } |
| 1324 | |
| 1325 | public: |
| 1326 | BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs ) |
| 1327 | : ITransientExpression{ true, comparisonResult }, |
| 1328 | m_lhs( lhs ), |
| 1329 | m_op( op ), |
| 1330 | m_rhs( rhs ) |
| 1331 | {} |
| 1332 | }; |
| 1333 | |
| 1334 | template<typename LhsT> |
| 1335 | class UnaryExpr : public ITransientExpression { |
| 1336 | LhsT m_lhs; |
| 1337 | |
| 1338 | void streamReconstructedExpression( std::ostream &os ) const override { |
| 1339 | os << Catch::Detail::stringify( m_lhs ); |
| 1340 | } |
| 1341 | |
| 1342 | public: |
| 1343 | explicit UnaryExpr( LhsT lhs ) |
| 1344 | : ITransientExpression{ false, lhs ? true : false }, |
| 1345 | m_lhs( lhs ) |
| 1346 | {} |
| 1347 | }; |
| 1348 | |
| 1349 | // Specialised comparison functions to handle equality comparisons between ints and pointers (NULL deduces as an int) |
| 1350 | template<typename LhsT, typename RhsT> |
| 1351 | auto compareEqual( LhsT const& lhs, RhsT const& rhs ) -> bool { return static_cast<bool>(lhs == rhs); } |
| 1352 | template<typename T> |
| 1353 | auto compareEqual( T* const& lhs, int rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); } |
| 1354 | template<typename T> |
| 1355 | auto compareEqual( T* const& lhs, long rhs ) -> bool { return lhs == reinterpret_cast<void const*>( rhs ); } |
| 1356 | template<typename T> |
| 1357 | auto compareEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; } |
| 1358 | template<typename T> |
| 1359 | auto compareEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) == rhs; } |
| 1360 | |
| 1361 | template<typename LhsT, typename RhsT> |
| 1362 | auto compareNotEqual( LhsT const& lhs, RhsT&& rhs ) -> bool { return static_cast<bool>(lhs != rhs); } |
| 1363 | template<typename T> |
| 1364 | auto compareNotEqual( T* const& lhs, int rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); } |
| 1365 | template<typename T> |
| 1366 | auto compareNotEqual( T* const& lhs, long rhs ) -> bool { return lhs != reinterpret_cast<void const*>( rhs ); } |
| 1367 | template<typename T> |
| 1368 | auto compareNotEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) != rhs; } |
| 1369 | template<typename T> |
| 1370 | auto compareNotEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast<void const*>( lhs ) != rhs; } |
| 1371 | |
| 1372 | template<typename LhsT> |
| 1373 | class ExprLhs { |
| 1374 | LhsT m_lhs; |
| 1375 | public: |
| 1376 | explicit ExprLhs( LhsT lhs ) : m_lhs( lhs ) {} |
| 1377 | |
| 1378 | template<typename RhsT> |
| 1379 | auto operator == ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { |
| 1380 | return { compareEqual( m_lhs, rhs ), m_lhs, "==", rhs }; |
| 1381 | } |
| 1382 | auto operator == ( bool rhs ) -> BinaryExpr<LhsT, bool> const { |
| 1383 | return { m_lhs == rhs, m_lhs, "==", rhs }; |
| 1384 | } |
| 1385 | |
| 1386 | template<typename RhsT> |
| 1387 | auto operator != ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { |
| 1388 | return { compareNotEqual( m_lhs, rhs ), m_lhs, "!=", rhs }; |
| 1389 | } |
| 1390 | auto operator != ( bool rhs ) -> BinaryExpr<LhsT, bool> const { |
| 1391 | return { m_lhs != rhs, m_lhs, "!=", rhs }; |
| 1392 | } |
| 1393 | |
| 1394 | template<typename RhsT> |
| 1395 | auto operator > ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { |
| 1396 | return { static_cast<bool>(m_lhs > rhs), m_lhs, ">", rhs }; |
| 1397 | } |
| 1398 | template<typename RhsT> |
| 1399 | auto operator < ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { |
| 1400 | return { static_cast<bool>(m_lhs < rhs), m_lhs, "<", rhs }; |
| 1401 | } |
| 1402 | template<typename RhsT> |
| 1403 | auto operator >= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { |
| 1404 | return { static_cast<bool>(m_lhs >= rhs), m_lhs, ">=", rhs }; |
| 1405 | } |
| 1406 | template<typename RhsT> |
| 1407 | auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { |
| 1408 | return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs }; |
| 1409 | } |
| 1410 | |
| 1411 | auto makeUnaryExpr() const -> UnaryExpr<LhsT> { |
| 1412 | return UnaryExpr<LhsT>{ m_lhs }; |
| 1413 | } |
| 1414 | }; |
| 1415 | |
| 1416 | void handleExpression( ITransientExpression const& expr ); |
| 1417 | |
| 1418 | template<typename T> |
| 1419 | void handleExpression( ExprLhs<T> const& expr ) { |
| 1420 | handleExpression( expr.makeUnaryExpr() ); |
| 1421 | } |
| 1422 | |
| 1423 | struct Decomposer { |
| 1424 | template<typename T> |
| 1425 | auto operator <= ( T const& lhs ) -> ExprLhs<T const&> { |
| 1426 | return ExprLhs<T const&>{ lhs }; |
| 1427 | } |
| 1428 | |
| 1429 | auto operator <=( bool value ) -> ExprLhs<bool> { |
| 1430 | return ExprLhs<bool>{ value }; |
| 1431 | } |
| 1432 | }; |
| 1433 | |
| 1434 | } // end namespace Catch |
| 1435 | |
| 1436 | #ifdef _MSC_VER |
| 1437 | #pragma warning(pop) |
| 1438 | #endif |
| 1439 | |
| 1440 | // end catch_decomposer.h |
| 1441 | // start catch_interfaces_capture.h |
| 1442 | |
| 1443 | #include <string> |
| 1444 | |
| 1445 | namespace Catch { |
| 1446 | |
| 1447 | class AssertionResult; |
| 1448 | struct AssertionInfo; |
| 1449 | struct SectionInfo; |
| 1450 | struct SectionEndInfo; |
| 1451 | struct MessageInfo; |
| 1452 | struct Counts; |
| 1453 | struct BenchmarkInfo; |
| 1454 | struct BenchmarkStats; |
| 1455 | struct AssertionReaction; |
| 1456 | |
| 1457 | struct ITransientExpression; |
| 1458 | |
| 1459 | struct IResultCapture { |
| 1460 | |
| 1461 | virtual ~IResultCapture(); |
| 1462 | |
| 1463 | virtual bool sectionStarted( SectionInfo const& sectionInfo, |
| 1464 | Counts& assertions ) = 0; |
| 1465 | virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0; |
| 1466 | virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0; |
| 1467 | |
| 1468 | virtual void benchmarkStarting( BenchmarkInfo const& info ) = 0; |
| 1469 | virtual void benchmarkEnded( BenchmarkStats const& stats ) = 0; |
| 1470 | |
| 1471 | virtual void pushScopedMessage( MessageInfo const& message ) = 0; |
| 1472 | virtual void popScopedMessage( MessageInfo const& message ) = 0; |
| 1473 | |
| 1474 | virtual void handleFatalErrorCondition( StringRef message ) = 0; |
| 1475 | |
| 1476 | virtual void handleExpr |
| 1477 | ( AssertionInfo const& info, |
| 1478 | ITransientExpression const& expr, |
| 1479 | AssertionReaction& reaction ) = 0; |
| 1480 | virtual void handleMessage |
| 1481 | ( AssertionInfo const& info, |
| 1482 | ResultWas::OfType resultType, |
| 1483 | StringRef const& message, |
| 1484 | AssertionReaction& reaction ) = 0; |
| 1485 | virtual void handleUnexpectedExceptionNotThrown |
| 1486 | ( AssertionInfo const& info, |
| 1487 | AssertionReaction& reaction ) = 0; |
| 1488 | virtual void handleUnexpectedInflightException |
| 1489 | ( AssertionInfo const& info, |
| 1490 | std::string const& message, |
| 1491 | AssertionReaction& reaction ) = 0; |
| 1492 | virtual void handleIncomplete |
| 1493 | ( AssertionInfo const& info ) = 0; |
| 1494 | virtual void handleNonExpr |
| 1495 | ( AssertionInfo const &info, |
| 1496 | ResultWas::OfType resultType, |
| 1497 | AssertionReaction &reaction ) = 0; |
| 1498 | |
| 1499 | virtual bool lastAssertionPassed() = 0; |
| 1500 | virtual void assertionPassed() = 0; |
| 1501 | |
| 1502 | // Deprecated, do not use: |
| 1503 | virtual std::string getCurrentTestName() const = 0; |
| 1504 | virtual const AssertionResult* getLastResult() const = 0; |
| 1505 | virtual void exceptionEarlyReported() = 0; |
| 1506 | }; |
| 1507 | |
| 1508 | IResultCapture& getResultCapture(); |
| 1509 | } |
| 1510 | |
| 1511 | // end catch_interfaces_capture.h |
| 1512 | namespace Catch { |
| 1513 | |
| 1514 | struct TestFailureException{}; |
| 1515 | struct AssertionResultData; |
| 1516 | struct IResultCapture; |
| 1517 | class RunContext; |
| 1518 | |
| 1519 | class LazyExpression { |
| 1520 | friend class AssertionHandler; |
| 1521 | friend struct AssertionStats; |
| 1522 | friend class RunContext; |
| 1523 | |
| 1524 | ITransientExpression const* m_transientExpression = nullptr; |
| 1525 | bool m_isNegated; |
| 1526 | public: |
| 1527 | LazyExpression( bool isNegated ); |
| 1528 | LazyExpression( LazyExpression const& other ); |
| 1529 | LazyExpression& operator = ( LazyExpression const& ) = delete; |
| 1530 | |
| 1531 | explicit operator bool() const; |
| 1532 | |
| 1533 | friend auto operator << ( std::ostream& os, LazyExpression const& lazyExpr ) -> std::ostream&; |
| 1534 | }; |
| 1535 | |
| 1536 | struct AssertionReaction { |
| 1537 | bool shouldDebugBreak = false; |
| 1538 | bool shouldThrow = false; |
| 1539 | }; |
| 1540 | |
| 1541 | class AssertionHandler { |
| 1542 | AssertionInfo m_assertionInfo; |
| 1543 | AssertionReaction m_reaction; |
| 1544 | bool m_completed = false; |
| 1545 | IResultCapture& m_resultCapture; |
| 1546 | |
| 1547 | public: |
| 1548 | AssertionHandler |
| 1549 | ( StringRef macroName, |
| 1550 | SourceLineInfo const& lineInfo, |
| 1551 | StringRef capturedExpression, |
| 1552 | ResultDisposition::Flags resultDisposition ); |
| 1553 | ~AssertionHandler() { |
| 1554 | if ( !m_completed ) { |
| 1555 | m_resultCapture.handleIncomplete( m_assertionInfo ); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | template<typename T> |
| 1560 | void handleExpr( ExprLhs<T> const& expr ) { |
| 1561 | handleExpr( expr.makeUnaryExpr() ); |
| 1562 | } |
| 1563 | void handleExpr( ITransientExpression const& expr ); |
| 1564 | |
| 1565 | void handleMessage(ResultWas::OfType resultType, StringRef const& message); |
| 1566 | |
| 1567 | void handleExceptionThrownAsExpected(); |
| 1568 | void handleUnexpectedExceptionNotThrown(); |
| 1569 | void handleExceptionNotThrownAsExpected(); |
| 1570 | void handleThrowingCallSkipped(); |
| 1571 | void handleUnexpectedInflightException(); |
| 1572 | |
| 1573 | void complete(); |
| 1574 | void setCompleted(); |
| 1575 | |
| 1576 | // query |
| 1577 | auto allowThrows() const -> bool; |
| 1578 | }; |
| 1579 | |
| 1580 | void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef matcherString ); |
| 1581 | |
| 1582 | } // namespace Catch |
| 1583 | |
| 1584 | // end catch_assertionhandler.h |
| 1585 | // start catch_message.h |
| 1586 | |
| 1587 | #include <string> |
| 1588 | |
| 1589 | namespace Catch { |
| 1590 | |
| 1591 | struct MessageInfo { |
| 1592 | MessageInfo( std::string const& _macroName, |
| 1593 | SourceLineInfo const& _lineInfo, |
| 1594 | ResultWas::OfType _type ); |
| 1595 | |
| 1596 | std::string macroName; |
| 1597 | std::string message; |
| 1598 | SourceLineInfo lineInfo; |
| 1599 | ResultWas::OfType type; |
| 1600 | unsigned int sequence; |
| 1601 | |
| 1602 | bool operator == ( MessageInfo const& other ) const; |
| 1603 | bool operator < ( MessageInfo const& other ) const; |
| 1604 | private: |
| 1605 | static unsigned int globalCount; |
| 1606 | }; |
| 1607 | |
| 1608 | struct MessageStream { |
| 1609 | |
| 1610 | template<typename T> |
| 1611 | MessageStream& operator << ( T const& value ) { |
| 1612 | m_stream << value; |
| 1613 | return *this; |
| 1614 | } |
| 1615 | |
| 1616 | ReusableStringStream m_stream; |
| 1617 | }; |
| 1618 | |
| 1619 | struct MessageBuilder : MessageStream { |
| 1620 | MessageBuilder( std::string const& macroName, |
| 1621 | SourceLineInfo const& lineInfo, |
| 1622 | ResultWas::OfType type ); |
| 1623 | |
| 1624 | template<typename T> |
| 1625 | MessageBuilder& operator << ( T const& value ) { |
| 1626 | m_stream << value; |
| 1627 | return *this; |
| 1628 | } |
| 1629 | |
| 1630 | MessageInfo m_info; |
| 1631 | }; |
| 1632 | |
| 1633 | class ScopedMessage { |
| 1634 | public: |
| 1635 | explicit ScopedMessage( MessageBuilder const& builder ); |
| 1636 | ~ScopedMessage(); |
| 1637 | |
| 1638 | MessageInfo m_info; |
| 1639 | }; |
| 1640 | |
| 1641 | } // end namespace Catch |
| 1642 | |
| 1643 | // end catch_message.h |
| 1644 | #if !defined(CATCH_CONFIG_DISABLE) |
| 1645 | |
| 1646 | #if !defined(CATCH_CONFIG_DISABLE_STRINGIFICATION) |
| 1647 | #define CATCH_INTERNAL_STRINGIFY(...) #__VA_ARGS__ |
| 1648 | #else |
| 1649 | #define CATCH_INTERNAL_STRINGIFY(...) "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION" |
| 1650 | #endif |
| 1651 | |
| 1652 | #if defined(CATCH_CONFIG_FAST_COMPILE) |
| 1653 | |
| 1654 | /////////////////////////////////////////////////////////////////////////////// |
| 1655 | // Another way to speed-up compilation is to omit local try-catch for REQUIRE* |
| 1656 | // macros. |
| 1657 | #define INTERNAL_CATCH_TRY |
| 1658 | #define INTERNAL_CATCH_CATCH( capturer ) |
| 1659 | |
| 1660 | #else // CATCH_CONFIG_FAST_COMPILE |
| 1661 | |
| 1662 | #define INTERNAL_CATCH_TRY try |
| 1663 | #define INTERNAL_CATCH_CATCH( handler ) catch(...) { handler.handleUnexpectedInflightException(); } |
| 1664 | |
| 1665 | #endif |
| 1666 | |
| 1667 | #define INTERNAL_CATCH_REACT( handler ) handler.complete(); |
| 1668 | |
| 1669 | /////////////////////////////////////////////////////////////////////////////// |
| 1670 | #define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \ |
| 1671 | do { \ |
| 1672 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ |
| 1673 | INTERNAL_CATCH_TRY { \ |
| 1674 | CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ |
| 1675 | catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \ |
| 1676 | CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ |
| 1677 | } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ |
| 1678 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 1679 | } while( (void)0, false && static_cast<bool>( !!(__VA_ARGS__) ) ) // the expression here is never evaluated at runtime but it forces the compiler to give it a look |
| 1680 | // The double negation silences MSVC's C4800 warning, the static_cast forces short-circuit evaluation if the type has overloaded &&. |
| 1681 | |
| 1682 | /////////////////////////////////////////////////////////////////////////////// |
| 1683 | #define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \ |
| 1684 | INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ |
| 1685 | if( Catch::getResultCapture().lastAssertionPassed() ) |
| 1686 | |
| 1687 | /////////////////////////////////////////////////////////////////////////////// |
| 1688 | #define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \ |
| 1689 | INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ |
| 1690 | if( !Catch::getResultCapture().lastAssertionPassed() ) |
| 1691 | |
| 1692 | /////////////////////////////////////////////////////////////////////////////// |
| 1693 | #define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \ |
| 1694 | do { \ |
| 1695 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ |
| 1696 | try { \ |
| 1697 | static_cast<void>(__VA_ARGS__); \ |
| 1698 | catchAssertionHandler.handleExceptionNotThrownAsExpected(); \ |
| 1699 | } \ |
| 1700 | catch( ... ) { \ |
| 1701 | catchAssertionHandler.handleUnexpectedInflightException(); \ |
| 1702 | } \ |
| 1703 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 1704 | } while( false ) |
| 1705 | |
| 1706 | /////////////////////////////////////////////////////////////////////////////// |
| 1707 | #define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \ |
| 1708 | do { \ |
| 1709 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \ |
| 1710 | if( catchAssertionHandler.allowThrows() ) \ |
| 1711 | try { \ |
| 1712 | static_cast<void>(__VA_ARGS__); \ |
| 1713 | catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ |
| 1714 | } \ |
| 1715 | catch( ... ) { \ |
| 1716 | catchAssertionHandler.handleExceptionThrownAsExpected(); \ |
| 1717 | } \ |
| 1718 | else \ |
| 1719 | catchAssertionHandler.handleThrowingCallSkipped(); \ |
| 1720 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 1721 | } while( false ) |
| 1722 | |
| 1723 | /////////////////////////////////////////////////////////////////////////////// |
| 1724 | #define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \ |
| 1725 | do { \ |
| 1726 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \ |
| 1727 | if( catchAssertionHandler.allowThrows() ) \ |
| 1728 | try { \ |
| 1729 | static_cast<void>(expr); \ |
| 1730 | catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ |
| 1731 | } \ |
| 1732 | catch( exceptionType const& ) { \ |
| 1733 | catchAssertionHandler.handleExceptionThrownAsExpected(); \ |
| 1734 | } \ |
| 1735 | catch( ... ) { \ |
| 1736 | catchAssertionHandler.handleUnexpectedInflightException(); \ |
| 1737 | } \ |
| 1738 | else \ |
| 1739 | catchAssertionHandler.handleThrowingCallSkipped(); \ |
| 1740 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 1741 | } while( false ) |
| 1742 | |
| 1743 | /////////////////////////////////////////////////////////////////////////////// |
| 1744 | #define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \ |
| 1745 | do { \ |
| 1746 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ |
| 1747 | catchAssertionHandler.handleMessage( messageType, ( Catch::MessageStream() << __VA_ARGS__ + ::Catch::StreamEndStop() ).m_stream.str() ); \ |
| 1748 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 1749 | } while( false ) |
| 1750 | |
| 1751 | /////////////////////////////////////////////////////////////////////////////// |
| 1752 | #define INTERNAL_CATCH_INFO( macroName, log ) \ |
| 1753 | Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log ); |
| 1754 | |
| 1755 | /////////////////////////////////////////////////////////////////////////////// |
| 1756 | // Although this is matcher-based, it can be used with just a string |
| 1757 | #define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \ |
| 1758 | do { \ |
| 1759 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ |
| 1760 | if( catchAssertionHandler.allowThrows() ) \ |
| 1761 | try { \ |
| 1762 | static_cast<void>(__VA_ARGS__); \ |
| 1763 | catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ |
| 1764 | } \ |
| 1765 | catch( ... ) { \ |
| 1766 | Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher ); \ |
| 1767 | } \ |
| 1768 | else \ |
| 1769 | catchAssertionHandler.handleThrowingCallSkipped(); \ |
| 1770 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 1771 | } while( false ) |
| 1772 | |
| 1773 | #endif // CATCH_CONFIG_DISABLE |
| 1774 | |
| 1775 | // end catch_capture.hpp |
| 1776 | // start catch_section.h |
| 1777 | |
| 1778 | // start catch_section_info.h |
| 1779 | |
| 1780 | // start catch_totals.h |
| 1781 | |
| 1782 | #include <cstddef> |
| 1783 | |
| 1784 | namespace Catch { |
| 1785 | |
| 1786 | struct Counts { |
| 1787 | Counts operator - ( Counts const& other ) const; |
| 1788 | Counts& operator += ( Counts const& other ); |
| 1789 | |
| 1790 | std::size_t total() const; |
| 1791 | bool allPassed() const; |
| 1792 | bool allOk() const; |
| 1793 | |
| 1794 | std::size_t passed = 0; |
| 1795 | std::size_t failed = 0; |
| 1796 | std::size_t failedButOk = 0; |
| 1797 | }; |
| 1798 | |
| 1799 | struct Totals { |
| 1800 | |
| 1801 | Totals operator - ( Totals const& other ) const; |
| 1802 | Totals& operator += ( Totals const& other ); |
| 1803 | |
| 1804 | Totals delta( Totals const& prevTotals ) const; |
| 1805 | |
| 1806 | int error = 0; |
| 1807 | Counts assertions; |
| 1808 | Counts testCases; |
| 1809 | }; |
| 1810 | } |
| 1811 | |
| 1812 | // end catch_totals.h |
| 1813 | #include <string> |
| 1814 | |
| 1815 | namespace Catch { |
| 1816 | |
| 1817 | struct SectionInfo { |
| 1818 | SectionInfo |
| 1819 | ( SourceLineInfo const& _lineInfo, |
| 1820 | std::string const& _name, |
| 1821 | std::string const& _description = std::string() ); |
| 1822 | |
| 1823 | std::string name; |
| 1824 | std::string description; |
| 1825 | SourceLineInfo lineInfo; |
| 1826 | }; |
| 1827 | |
| 1828 | struct SectionEndInfo { |
| 1829 | SectionEndInfo( SectionInfo const& _sectionInfo, Counts const& _prevAssertions, double _durationInSeconds ); |
| 1830 | |
| 1831 | SectionInfo sectionInfo; |
| 1832 | Counts prevAssertions; |
| 1833 | double durationInSeconds; |
| 1834 | }; |
| 1835 | |
| 1836 | } // end namespace Catch |
| 1837 | |
| 1838 | // end catch_section_info.h |
| 1839 | // start catch_timer.h |
| 1840 | |
| 1841 | #include <cstdint> |
| 1842 | |
| 1843 | namespace Catch { |
| 1844 | |
| 1845 | auto getCurrentNanosecondsSinceEpoch() -> uint64_t; |
| 1846 | auto getEstimatedClockResolution() -> uint64_t; |
| 1847 | |
| 1848 | class Timer { |
| 1849 | uint64_t m_nanoseconds = 0; |
| 1850 | public: |
| 1851 | void start(); |
| 1852 | auto getElapsedNanoseconds() const -> uint64_t; |
| 1853 | auto getElapsedMicroseconds() const -> uint64_t; |
| 1854 | auto getElapsedMilliseconds() const -> unsigned int; |
| 1855 | auto getElapsedSeconds() const -> double; |
| 1856 | }; |
| 1857 | |
| 1858 | } // namespace Catch |
| 1859 | |
| 1860 | // end catch_timer.h |
| 1861 | #include <string> |
| 1862 | |
| 1863 | namespace Catch { |
| 1864 | |
| 1865 | class Section : NonCopyable { |
| 1866 | public: |
| 1867 | Section( SectionInfo const& info ); |
| 1868 | ~Section(); |
| 1869 | |
| 1870 | // This indicates whether the section should be executed or not |
| 1871 | explicit operator bool() const; |
| 1872 | |
| 1873 | private: |
| 1874 | SectionInfo m_info; |
| 1875 | |
| 1876 | std::string m_name; |
| 1877 | Counts m_assertions; |
| 1878 | bool m_sectionIncluded; |
| 1879 | Timer m_timer; |
| 1880 | }; |
| 1881 | |
| 1882 | } // end namespace Catch |
| 1883 | |
| 1884 | #define INTERNAL_CATCH_SECTION( ... ) \ |
| 1885 | if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, __VA_ARGS__ ) ) |
| 1886 | |
| 1887 | // end catch_section.h |
| 1888 | // start catch_benchmark.h |
| 1889 | |
| 1890 | #include <cstdint> |
| 1891 | #include <string> |
| 1892 | |
| 1893 | namespace Catch { |
| 1894 | |
| 1895 | class BenchmarkLooper { |
| 1896 | |
| 1897 | std::string m_name; |
| 1898 | std::size_t m_count = 0; |
| 1899 | std::size_t m_iterationsToRun = 1; |
| 1900 | uint64_t m_resolution; |
| 1901 | Timer m_timer; |
| 1902 | |
| 1903 | static auto getResolution() -> uint64_t; |
| 1904 | public: |
| 1905 | // Keep most of this inline as it's on the code path that is being timed |
| 1906 | BenchmarkLooper( StringRef name ) |
| 1907 | : m_name( name ), |
| 1908 | m_resolution( getResolution() ) |
| 1909 | { |
| 1910 | reportStart(); |
| 1911 | m_timer.start(); |
| 1912 | } |
| 1913 | |
| 1914 | explicit operator bool() { |
| 1915 | if( m_count < m_iterationsToRun ) |
| 1916 | return true; |
| 1917 | return needsMoreIterations(); |
| 1918 | } |
| 1919 | |
| 1920 | void increment() { |
| 1921 | ++m_count; |
| 1922 | } |
| 1923 | |
| 1924 | void reportStart(); |
| 1925 | auto needsMoreIterations() -> bool; |
| 1926 | }; |
| 1927 | |
| 1928 | } // end namespace Catch |
| 1929 | |
| 1930 | #define BENCHMARK( name ) \ |
| 1931 | for( Catch::BenchmarkLooper looper( name ); looper; looper.increment() ) |
| 1932 | |
| 1933 | // end catch_benchmark.h |
| 1934 | // start catch_interfaces_exception.h |
| 1935 | |
| 1936 | // start catch_interfaces_registry_hub.h |
| 1937 | |
| 1938 | #include <string> |
| 1939 | #include <memory> |
| 1940 | |
| 1941 | namespace Catch { |
| 1942 | |
| 1943 | class TestCase; |
| 1944 | struct ITestCaseRegistry; |
| 1945 | struct IExceptionTranslatorRegistry; |
| 1946 | struct IExceptionTranslator; |
| 1947 | struct IReporterRegistry; |
| 1948 | struct IReporterFactory; |
| 1949 | struct ITagAliasRegistry; |
| 1950 | class StartupExceptionRegistry; |
| 1951 | |
| 1952 | using IReporterFactoryPtr = std::shared_ptr<IReporterFactory>; |
| 1953 | |
| 1954 | struct IRegistryHub { |
| 1955 | virtual ~IRegistryHub(); |
| 1956 | |
| 1957 | virtual IReporterRegistry const& getReporterRegistry() const = 0; |
| 1958 | virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0; |
| 1959 | virtual ITagAliasRegistry const& getTagAliasRegistry() const = 0; |
| 1960 | |
| 1961 | virtual IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() = 0; |
| 1962 | |
| 1963 | virtual StartupExceptionRegistry const& getStartupExceptionRegistry() const = 0; |
| 1964 | }; |
| 1965 | |
| 1966 | struct IMutableRegistryHub { |
| 1967 | virtual ~IMutableRegistryHub(); |
| 1968 | virtual void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) = 0; |
| 1969 | virtual void registerListener( IReporterFactoryPtr const& factory ) = 0; |
| 1970 | virtual void registerTest( TestCase const& testInfo ) = 0; |
| 1971 | virtual void registerTranslator( const IExceptionTranslator* translator ) = 0; |
| 1972 | virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; |
| 1973 | virtual void registerStartupException() noexcept = 0; |
| 1974 | }; |
| 1975 | |
| 1976 | IRegistryHub& getRegistryHub(); |
| 1977 | IMutableRegistryHub& getMutableRegistryHub(); |
| 1978 | void cleanUp(); |
| 1979 | std::string translateActiveException(); |
| 1980 | |
| 1981 | } |
| 1982 | |
| 1983 | // end catch_interfaces_registry_hub.h |
| 1984 | #if defined(CATCH_CONFIG_DISABLE) |
| 1985 | #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \ |
| 1986 | static std::string translatorName( signature ) |
| 1987 | #endif |
| 1988 | |
| 1989 | #include <exception> |
| 1990 | #include <string> |
| 1991 | #include <vector> |
| 1992 | |
| 1993 | namespace Catch { |
| 1994 | using exceptionTranslateFunction = std::string(*)(); |
| 1995 | |
| 1996 | struct IExceptionTranslator; |
| 1997 | using ExceptionTranslators = std::vector<std::unique_ptr<IExceptionTranslator const>>; |
| 1998 | |
| 1999 | struct IExceptionTranslator { |
| 2000 | virtual ~IExceptionTranslator(); |
| 2001 | virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0; |
| 2002 | }; |
| 2003 | |
| 2004 | struct IExceptionTranslatorRegistry { |
| 2005 | virtual ~IExceptionTranslatorRegistry(); |
| 2006 | |
| 2007 | virtual std::string translateActiveException() const = 0; |
| 2008 | }; |
| 2009 | |
| 2010 | class ExceptionTranslatorRegistrar { |
| 2011 | template<typename T> |
| 2012 | class ExceptionTranslator : public IExceptionTranslator { |
| 2013 | public: |
| 2014 | |
| 2015 | ExceptionTranslator( std::string(*translateFunction)( T& ) ) |
| 2016 | : m_translateFunction( translateFunction ) |
| 2017 | {} |
| 2018 | |
| 2019 | std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override { |
| 2020 | try { |
| 2021 | if( it == itEnd ) |
| 2022 | std::rethrow_exception(std::current_exception()); |
| 2023 | else |
| 2024 | return (*it)->translate( it+1, itEnd ); |
| 2025 | } |
| 2026 | catch( T& ex ) { |
| 2027 | return m_translateFunction( ex ); |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | protected: |
| 2032 | std::string(*m_translateFunction)( T& ); |
| 2033 | }; |
| 2034 | |
| 2035 | public: |
| 2036 | template<typename T> |
| 2037 | ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { |
| 2038 | getMutableRegistryHub().registerTranslator |
| 2039 | ( new ExceptionTranslator<T>( translateFunction ) ); |
| 2040 | } |
| 2041 | }; |
| 2042 | } |
| 2043 | |
| 2044 | /////////////////////////////////////////////////////////////////////////////// |
| 2045 | #define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ |
| 2046 | static std::string translatorName( signature ); \ |
| 2047 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 2048 | namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \ |
| 2049 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ |
| 2050 | static std::string translatorName( signature ) |
| 2051 | |
| 2052 | #define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) |
| 2053 | |
| 2054 | // end catch_interfaces_exception.h |
| 2055 | // start catch_approx.h |
| 2056 | |
| 2057 | #include <type_traits> |
| 2058 | #include <stdexcept> |
| 2059 | |
| 2060 | namespace Catch { |
| 2061 | namespace Detail { |
| 2062 | |
| 2063 | class Approx { |
| 2064 | private: |
| 2065 | bool equalityComparisonImpl(double other) const; |
| 2066 | |
| 2067 | public: |
| 2068 | explicit Approx ( double value ); |
| 2069 | |
| 2070 | static Approx custom(); |
| 2071 | |
| 2072 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2073 | Approx operator()( T const& value ) { |
| 2074 | Approx approx( static_cast<double>(value) ); |
| 2075 | approx.epsilon( m_epsilon ); |
| 2076 | approx.margin( m_margin ); |
| 2077 | approx.scale( m_scale ); |
| 2078 | return approx; |
| 2079 | } |
| 2080 | |
| 2081 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2082 | explicit Approx( T const& value ): Approx(static_cast<double>(value)) |
| 2083 | {} |
| 2084 | |
| 2085 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2086 | friend bool operator == ( const T& lhs, Approx const& rhs ) { |
| 2087 | auto lhs_v = static_cast<double>(lhs); |
| 2088 | return rhs.equalityComparisonImpl(lhs_v); |
| 2089 | } |
| 2090 | |
| 2091 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2092 | friend bool operator == ( Approx const& lhs, const T& rhs ) { |
| 2093 | return operator==( rhs, lhs ); |
| 2094 | } |
| 2095 | |
| 2096 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2097 | friend bool operator != ( T const& lhs, Approx const& rhs ) { |
| 2098 | return !operator==( lhs, rhs ); |
| 2099 | } |
| 2100 | |
| 2101 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2102 | friend bool operator != ( Approx const& lhs, T const& rhs ) { |
| 2103 | return !operator==( rhs, lhs ); |
| 2104 | } |
| 2105 | |
| 2106 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2107 | friend bool operator <= ( T const& lhs, Approx const& rhs ) { |
| 2108 | return static_cast<double>(lhs) < rhs.m_value || lhs == rhs; |
| 2109 | } |
| 2110 | |
| 2111 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2112 | friend bool operator <= ( Approx const& lhs, T const& rhs ) { |
| 2113 | return lhs.m_value < static_cast<double>(rhs) || lhs == rhs; |
| 2114 | } |
| 2115 | |
| 2116 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2117 | friend bool operator >= ( T const& lhs, Approx const& rhs ) { |
| 2118 | return static_cast<double>(lhs) > rhs.m_value || lhs == rhs; |
| 2119 | } |
| 2120 | |
| 2121 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2122 | friend bool operator >= ( Approx const& lhs, T const& rhs ) { |
| 2123 | return lhs.m_value > static_cast<double>(rhs) || lhs == rhs; |
| 2124 | } |
| 2125 | |
| 2126 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2127 | Approx& epsilon( T const& newEpsilon ) { |
| 2128 | double epsilonAsDouble = static_cast<double>(newEpsilon); |
| 2129 | if( epsilonAsDouble < 0 || epsilonAsDouble > 1.0 ) { |
| 2130 | throw std::domain_error |
| 2131 | ( "Invalid Approx::epsilon: " + |
| 2132 | Catch::Detail::stringify( epsilonAsDouble ) + |
| 2133 | ", Approx::epsilon has to be between 0 and 1" ); |
| 2134 | } |
| 2135 | m_epsilon = epsilonAsDouble; |
| 2136 | return *this; |
| 2137 | } |
| 2138 | |
| 2139 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2140 | Approx& margin( T const& newMargin ) { |
| 2141 | double marginAsDouble = static_cast<double>(newMargin); |
| 2142 | if( marginAsDouble < 0 ) { |
| 2143 | throw std::domain_error |
| 2144 | ( "Invalid Approx::margin: " + |
| 2145 | Catch::Detail::stringify( marginAsDouble ) + |
| 2146 | ", Approx::Margin has to be non-negative." ); |
| 2147 | |
| 2148 | } |
| 2149 | m_margin = marginAsDouble; |
| 2150 | return *this; |
| 2151 | } |
| 2152 | |
| 2153 | template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |
| 2154 | Approx& scale( T const& newScale ) { |
| 2155 | m_scale = static_cast<double>(newScale); |
| 2156 | return *this; |
| 2157 | } |
| 2158 | |
| 2159 | std::string toString() const; |
| 2160 | |
| 2161 | private: |
| 2162 | double m_epsilon; |
| 2163 | double m_margin; |
| 2164 | double m_scale; |
| 2165 | double m_value; |
| 2166 | }; |
| 2167 | } |
| 2168 | |
| 2169 | template<> |
| 2170 | struct StringMaker<Catch::Detail::Approx> { |
| 2171 | static std::string convert(Catch::Detail::Approx const& value); |
| 2172 | }; |
| 2173 | |
| 2174 | } // end namespace Catch |
| 2175 | |
| 2176 | // end catch_approx.h |
| 2177 | // start catch_string_manip.h |
| 2178 | |
| 2179 | #include <string> |
| 2180 | #include <iosfwd> |
| 2181 | |
| 2182 | namespace Catch { |
| 2183 | |
| 2184 | bool startsWith( std::string const& s, std::string const& prefix ); |
| 2185 | bool startsWith( std::string const& s, char prefix ); |
| 2186 | bool endsWith( std::string const& s, std::string const& suffix ); |
| 2187 | bool endsWith( std::string const& s, char suffix ); |
| 2188 | bool contains( std::string const& s, std::string const& infix ); |
| 2189 | void toLowerInPlace( std::string& s ); |
| 2190 | std::string toLower( std::string const& s ); |
| 2191 | std::string trim( std::string const& str ); |
| 2192 | bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ); |
| 2193 | |
| 2194 | struct pluralise { |
| 2195 | pluralise( std::size_t count, std::string const& label ); |
| 2196 | |
| 2197 | friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ); |
| 2198 | |
| 2199 | std::size_t m_count; |
| 2200 | std::string m_label; |
| 2201 | }; |
| 2202 | } |
| 2203 | |
| 2204 | // end catch_string_manip.h |
| 2205 | #ifndef CATCH_CONFIG_DISABLE_MATCHERS |
| 2206 | // start catch_capture_matchers.h |
| 2207 | |
| 2208 | // start catch_matchers.h |
| 2209 | |
| 2210 | #include <string> |
| 2211 | #include <vector> |
| 2212 | |
| 2213 | namespace Catch { |
| 2214 | namespace Matchers { |
| 2215 | namespace Impl { |
| 2216 | |
| 2217 | template<typename ArgT> struct MatchAllOf; |
| 2218 | template<typename ArgT> struct MatchAnyOf; |
| 2219 | template<typename ArgT> struct MatchNotOf; |
| 2220 | |
| 2221 | class MatcherUntypedBase { |
| 2222 | public: |
| 2223 | MatcherUntypedBase() = default; |
| 2224 | MatcherUntypedBase ( MatcherUntypedBase const& ) = default; |
| 2225 | MatcherUntypedBase& operator = ( MatcherUntypedBase const& ) = delete; |
| 2226 | std::string toString() const; |
| 2227 | |
| 2228 | protected: |
| 2229 | virtual ~MatcherUntypedBase(); |
| 2230 | virtual std::string describe() const = 0; |
| 2231 | mutable std::string m_cachedToString; |
| 2232 | }; |
| 2233 | |
| 2234 | template<typename ObjectT> |
| 2235 | struct MatcherMethod { |
| 2236 | virtual bool match( ObjectT const& arg ) const = 0; |
| 2237 | }; |
| 2238 | template<typename PtrT> |
| 2239 | struct MatcherMethod<PtrT*> { |
| 2240 | virtual bool match( PtrT* arg ) const = 0; |
| 2241 | }; |
| 2242 | |
| 2243 | template<typename T> |
| 2244 | struct MatcherBase : MatcherUntypedBase, MatcherMethod<T> { |
| 2245 | |
| 2246 | MatchAllOf<T> operator && ( MatcherBase const& other ) const; |
| 2247 | MatchAnyOf<T> operator || ( MatcherBase const& other ) const; |
| 2248 | MatchNotOf<T> operator ! () const; |
| 2249 | }; |
| 2250 | |
| 2251 | template<typename ArgT> |
| 2252 | struct MatchAllOf : MatcherBase<ArgT> { |
| 2253 | bool match( ArgT const& arg ) const override { |
| 2254 | for( auto matcher : m_matchers ) { |
| 2255 | if (!matcher->match(arg)) |
| 2256 | return false; |
| 2257 | } |
| 2258 | return true; |
| 2259 | } |
| 2260 | std::string describe() const override { |
| 2261 | std::string description; |
| 2262 | description.reserve( 4 + m_matchers.size()*32 ); |
| 2263 | description += "( "; |
| 2264 | bool first = true; |
| 2265 | for( auto matcher : m_matchers ) { |
| 2266 | if( first ) |
| 2267 | first = false; |
| 2268 | else |
| 2269 | description += " and "; |
| 2270 | description += matcher->toString(); |
| 2271 | } |
| 2272 | description += " )"; |
| 2273 | return description; |
| 2274 | } |
| 2275 | |
| 2276 | MatchAllOf<ArgT>& operator && ( MatcherBase<ArgT> const& other ) { |
| 2277 | m_matchers.push_back( &other ); |
| 2278 | return *this; |
| 2279 | } |
| 2280 | |
| 2281 | std::vector<MatcherBase<ArgT> const*> m_matchers; |
| 2282 | }; |
| 2283 | template<typename ArgT> |
| 2284 | struct MatchAnyOf : MatcherBase<ArgT> { |
| 2285 | |
| 2286 | bool match( ArgT const& arg ) const override { |
| 2287 | for( auto matcher : m_matchers ) { |
| 2288 | if (matcher->match(arg)) |
| 2289 | return true; |
| 2290 | } |
| 2291 | return false; |
| 2292 | } |
| 2293 | std::string describe() const override { |
| 2294 | std::string description; |
| 2295 | description.reserve( 4 + m_matchers.size()*32 ); |
| 2296 | description += "( "; |
| 2297 | bool first = true; |
| 2298 | for( auto matcher : m_matchers ) { |
| 2299 | if( first ) |
| 2300 | first = false; |
| 2301 | else |
| 2302 | description += " or "; |
| 2303 | description += matcher->toString(); |
| 2304 | } |
| 2305 | description += " )"; |
| 2306 | return description; |
| 2307 | } |
| 2308 | |
| 2309 | MatchAnyOf<ArgT>& operator || ( MatcherBase<ArgT> const& other ) { |
| 2310 | m_matchers.push_back( &other ); |
| 2311 | return *this; |
| 2312 | } |
| 2313 | |
| 2314 | std::vector<MatcherBase<ArgT> const*> m_matchers; |
| 2315 | }; |
| 2316 | |
| 2317 | template<typename ArgT> |
| 2318 | struct MatchNotOf : MatcherBase<ArgT> { |
| 2319 | |
| 2320 | MatchNotOf( MatcherBase<ArgT> const& underlyingMatcher ) : m_underlyingMatcher( underlyingMatcher ) {} |
| 2321 | |
| 2322 | bool match( ArgT const& arg ) const override { |
| 2323 | return !m_underlyingMatcher.match( arg ); |
| 2324 | } |
| 2325 | |
| 2326 | std::string describe() const override { |
| 2327 | return "not " + m_underlyingMatcher.toString(); |
| 2328 | } |
| 2329 | MatcherBase<ArgT> const& m_underlyingMatcher; |
| 2330 | }; |
| 2331 | |
| 2332 | template<typename T> |
| 2333 | MatchAllOf<T> MatcherBase<T>::operator && ( MatcherBase const& other ) const { |
| 2334 | return MatchAllOf<T>() && *this && other; |
| 2335 | } |
| 2336 | template<typename T> |
| 2337 | MatchAnyOf<T> MatcherBase<T>::operator || ( MatcherBase const& other ) const { |
| 2338 | return MatchAnyOf<T>() || *this || other; |
| 2339 | } |
| 2340 | template<typename T> |
| 2341 | MatchNotOf<T> MatcherBase<T>::operator ! () const { |
| 2342 | return MatchNotOf<T>( *this ); |
| 2343 | } |
| 2344 | |
| 2345 | } // namespace Impl |
| 2346 | |
| 2347 | } // namespace Matchers |
| 2348 | |
| 2349 | using namespace Matchers; |
| 2350 | using Matchers::Impl::MatcherBase; |
| 2351 | |
| 2352 | } // namespace Catch |
| 2353 | |
| 2354 | // end catch_matchers.h |
| 2355 | // start catch_matchers_floating.h |
| 2356 | |
| 2357 | #include <type_traits> |
| 2358 | #include <cmath> |
| 2359 | |
| 2360 | namespace Catch { |
| 2361 | namespace Matchers { |
| 2362 | |
| 2363 | namespace Floating { |
| 2364 | |
| 2365 | enum class FloatingPointKind : uint8_t; |
| 2366 | |
| 2367 | struct WithinAbsMatcher : MatcherBase<double> { |
| 2368 | WithinAbsMatcher(double target, double margin); |
| 2369 | bool match(double const& matchee) const override; |
| 2370 | std::string describe() const override; |
| 2371 | private: |
| 2372 | double m_target; |
| 2373 | double m_margin; |
| 2374 | }; |
| 2375 | |
| 2376 | struct WithinUlpsMatcher : MatcherBase<double> { |
| 2377 | WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType); |
| 2378 | bool match(double const& matchee) const override; |
| 2379 | std::string describe() const override; |
| 2380 | private: |
| 2381 | double m_target; |
| 2382 | int m_ulps; |
| 2383 | FloatingPointKind m_type; |
| 2384 | }; |
| 2385 | |
| 2386 | } // namespace Floating |
| 2387 | |
| 2388 | // The following functions create the actual matcher objects. |
| 2389 | // This allows the types to be inferred |
| 2390 | Floating::WithinUlpsMatcher WithinULP(double target, int maxUlpDiff); |
| 2391 | Floating::WithinUlpsMatcher WithinULP(float target, int maxUlpDiff); |
| 2392 | Floating::WithinAbsMatcher WithinAbs(double target, double margin); |
| 2393 | |
| 2394 | } // namespace Matchers |
| 2395 | } // namespace Catch |
| 2396 | |
| 2397 | // end catch_matchers_floating.h |
| 2398 | // start catch_matchers_generic.hpp |
| 2399 | |
| 2400 | #include <functional> |
| 2401 | #include <string> |
| 2402 | |
| 2403 | namespace Catch { |
| 2404 | namespace Matchers { |
| 2405 | namespace Generic { |
| 2406 | |
| 2407 | namespace Detail { |
| 2408 | std::string finalizeDescription(const std::string& desc); |
| 2409 | } |
| 2410 | |
| 2411 | template <typename T> |
| 2412 | class PredicateMatcher : public MatcherBase<T> { |
| 2413 | std::function<bool(T const&)> m_predicate; |
| 2414 | std::string m_description; |
| 2415 | public: |
| 2416 | |
| 2417 | PredicateMatcher(std::function<bool(T const&)> const& elem, std::string const& descr) |
| 2418 | :m_predicate(std::move(elem)), |
| 2419 | m_description(Detail::finalizeDescription(descr)) |
| 2420 | {} |
| 2421 | |
| 2422 | bool match( T const& item ) const override { |
| 2423 | return m_predicate(item); |
| 2424 | } |
| 2425 | |
| 2426 | std::string describe() const override { |
| 2427 | return m_description; |
| 2428 | } |
| 2429 | }; |
| 2430 | |
| 2431 | } // namespace Generic |
| 2432 | |
| 2433 | // The following functions create the actual matcher objects. |
| 2434 | // The user has to explicitly specify type to the function, because |
| 2435 | // infering std::function<bool(T const&)> is hard (but possible) and |
| 2436 | // requires a lot of TMP. |
| 2437 | template<typename T> |
| 2438 | Generic::PredicateMatcher<T> Predicate(std::function<bool(T const&)> const& predicate, std::string const& description = "") { |
| 2439 | return Generic::PredicateMatcher<T>(predicate, description); |
| 2440 | } |
| 2441 | |
| 2442 | } // namespace Matchers |
| 2443 | } // namespace Catch |
| 2444 | |
| 2445 | // end catch_matchers_generic.hpp |
| 2446 | // start catch_matchers_string.h |
| 2447 | |
| 2448 | #include <string> |
| 2449 | |
| 2450 | namespace Catch { |
| 2451 | namespace Matchers { |
| 2452 | |
| 2453 | namespace StdString { |
| 2454 | |
| 2455 | struct CasedString |
| 2456 | { |
| 2457 | CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ); |
| 2458 | std::string adjustString( std::string const& str ) const; |
| 2459 | std::string caseSensitivitySuffix() const; |
| 2460 | |
| 2461 | CaseSensitive::Choice m_caseSensitivity; |
| 2462 | std::string m_str; |
| 2463 | }; |
| 2464 | |
| 2465 | struct StringMatcherBase : MatcherBase<std::string> { |
| 2466 | StringMatcherBase( std::string const& operation, CasedString const& comparator ); |
| 2467 | std::string describe() const override; |
| 2468 | |
| 2469 | CasedString m_comparator; |
| 2470 | std::string m_operation; |
| 2471 | }; |
| 2472 | |
| 2473 | struct EqualsMatcher : StringMatcherBase { |
| 2474 | EqualsMatcher( CasedString const& comparator ); |
| 2475 | bool match( std::string const& source ) const override; |
| 2476 | }; |
| 2477 | struct ContainsMatcher : StringMatcherBase { |
| 2478 | ContainsMatcher( CasedString const& comparator ); |
| 2479 | bool match( std::string const& source ) const override; |
| 2480 | }; |
| 2481 | struct StartsWithMatcher : StringMatcherBase { |
| 2482 | StartsWithMatcher( CasedString const& comparator ); |
| 2483 | bool match( std::string const& source ) const override; |
| 2484 | }; |
| 2485 | struct EndsWithMatcher : StringMatcherBase { |
| 2486 | EndsWithMatcher( CasedString const& comparator ); |
| 2487 | bool match( std::string const& source ) const override; |
| 2488 | }; |
| 2489 | |
| 2490 | struct RegexMatcher : MatcherBase<std::string> { |
| 2491 | RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity ); |
| 2492 | bool match( std::string const& matchee ) const override; |
| 2493 | std::string describe() const override; |
| 2494 | |
| 2495 | private: |
| 2496 | std::string m_regex; |
| 2497 | CaseSensitive::Choice m_caseSensitivity; |
| 2498 | }; |
| 2499 | |
| 2500 | } // namespace StdString |
| 2501 | |
| 2502 | // The following functions create the actual matcher objects. |
| 2503 | // This allows the types to be inferred |
| 2504 | |
| 2505 | StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); |
| 2506 | StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); |
| 2507 | StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); |
| 2508 | StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); |
| 2509 | StdString::RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); |
| 2510 | |
| 2511 | } // namespace Matchers |
| 2512 | } // namespace Catch |
| 2513 | |
| 2514 | // end catch_matchers_string.h |
| 2515 | // start catch_matchers_vector.h |
| 2516 | |
| 2517 | #include <algorithm> |
| 2518 | |
| 2519 | namespace Catch { |
| 2520 | namespace Matchers { |
| 2521 | |
| 2522 | namespace Vector { |
| 2523 | namespace Detail { |
| 2524 | template <typename InputIterator, typename T> |
| 2525 | size_t count(InputIterator first, InputIterator last, T const& item) { |
| 2526 | size_t cnt = 0; |
| 2527 | for (; first != last; ++first) { |
| 2528 | if (*first == item) { |
| 2529 | ++cnt; |
| 2530 | } |
| 2531 | } |
| 2532 | return cnt; |
| 2533 | } |
| 2534 | template <typename InputIterator, typename T> |
| 2535 | bool contains(InputIterator first, InputIterator last, T const& item) { |
| 2536 | for (; first != last; ++first) { |
| 2537 | if (*first == item) { |
| 2538 | return true; |
| 2539 | } |
| 2540 | } |
| 2541 | return false; |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | template<typename T> |
| 2546 | struct ContainsElementMatcher : MatcherBase<std::vector<T>> { |
| 2547 | |
| 2548 | ContainsElementMatcher(T const &comparator) : m_comparator( comparator) {} |
| 2549 | |
| 2550 | bool match(std::vector<T> const &v) const override { |
| 2551 | for (auto const& el : v) { |
| 2552 | if (el == m_comparator) { |
| 2553 | return true; |
| 2554 | } |
| 2555 | } |
| 2556 | return false; |
| 2557 | } |
| 2558 | |
| 2559 | std::string describe() const override { |
| 2560 | return "Contains: " + ::Catch::Detail::stringify( m_comparator ); |
| 2561 | } |
| 2562 | |
| 2563 | T const& m_comparator; |
| 2564 | }; |
| 2565 | |
| 2566 | template<typename T> |
| 2567 | struct ContainsMatcher : MatcherBase<std::vector<T>> { |
| 2568 | |
| 2569 | ContainsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {} |
| 2570 | |
| 2571 | bool match(std::vector<T> const &v) const override { |
| 2572 | // !TBD: see note in EqualsMatcher |
| 2573 | if (m_comparator.size() > v.size()) |
| 2574 | return false; |
| 2575 | for (auto const& comparator : m_comparator) { |
| 2576 | auto present = false; |
| 2577 | for (const auto& el : v) { |
| 2578 | if (el == comparator) { |
| 2579 | present = true; |
| 2580 | break; |
| 2581 | } |
| 2582 | } |
| 2583 | if (!present) { |
| 2584 | return false; |
| 2585 | } |
| 2586 | } |
| 2587 | return true; |
| 2588 | } |
| 2589 | std::string describe() const override { |
| 2590 | return "Contains: " + ::Catch::Detail::stringify( m_comparator ); |
| 2591 | } |
| 2592 | |
| 2593 | std::vector<T> const& m_comparator; |
| 2594 | }; |
| 2595 | |
| 2596 | template<typename T> |
| 2597 | struct EqualsMatcher : MatcherBase<std::vector<T>> { |
| 2598 | |
| 2599 | EqualsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {} |
| 2600 | |
| 2601 | bool match(std::vector<T> const &v) const override { |
| 2602 | // !TBD: This currently works if all elements can be compared using != |
| 2603 | // - a more general approach would be via a compare template that defaults |
| 2604 | // to using !=. but could be specialised for, e.g. std::vector<T> etc |
| 2605 | // - then just call that directly |
| 2606 | if (m_comparator.size() != v.size()) |
| 2607 | return false; |
| 2608 | for (std::size_t i = 0; i < v.size(); ++i) |
| 2609 | if (m_comparator[i] != v[i]) |
| 2610 | return false; |
| 2611 | return true; |
| 2612 | } |
| 2613 | std::string describe() const override { |
| 2614 | return "Equals: " + ::Catch::Detail::stringify( m_comparator ); |
| 2615 | } |
| 2616 | std::vector<T> const& m_comparator; |
| 2617 | }; |
| 2618 | |
| 2619 | template<typename T> |
| 2620 | struct UnorderedEqualsMatcher : MatcherBase<std::vector<T>> { |
| 2621 | UnorderedEqualsMatcher(std::vector<T> const& target) : m_target(target) {} |
| 2622 | bool match(std::vector<T> const& vec) const override { |
| 2623 | // Note: This is a reimplementation of std::is_permutation, |
| 2624 | // because I don't want to include <algorithm> inside the common path |
| 2625 | if (m_target.size() != vec.size()) { |
| 2626 | return false; |
| 2627 | } |
| 2628 | auto lfirst = m_target.begin(), llast = m_target.end(); |
| 2629 | auto rfirst = vec.begin(), rlast = vec.end(); |
| 2630 | // Cut common prefix to optimize checking of permuted parts |
| 2631 | while (lfirst != llast && *lfirst != *rfirst) { |
| 2632 | ++lfirst; ++rfirst; |
| 2633 | } |
| 2634 | if (lfirst == llast) { |
| 2635 | return true; |
| 2636 | } |
| 2637 | |
| 2638 | for (auto mid = lfirst; mid != llast; ++mid) { |
| 2639 | // Skip already counted items |
| 2640 | if (Detail::contains(lfirst, mid, *mid)) { |
| 2641 | continue; |
| 2642 | } |
| 2643 | size_t num_vec = Detail::count(rfirst, rlast, *mid); |
| 2644 | if (num_vec == 0 || Detail::count(lfirst, llast, *mid) != num_vec) { |
| 2645 | return false; |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | return true; |
| 2650 | } |
| 2651 | |
| 2652 | std::string describe() const override { |
| 2653 | return "UnorderedEquals: " + ::Catch::Detail::stringify(m_target); |
| 2654 | } |
| 2655 | private: |
| 2656 | std::vector<T> const& m_target; |
| 2657 | }; |
| 2658 | |
| 2659 | } // namespace Vector |
| 2660 | |
| 2661 | // The following functions create the actual matcher objects. |
| 2662 | // This allows the types to be inferred |
| 2663 | |
| 2664 | template<typename T> |
| 2665 | Vector::ContainsMatcher<T> Contains( std::vector<T> const& comparator ) { |
| 2666 | return Vector::ContainsMatcher<T>( comparator ); |
| 2667 | } |
| 2668 | |
| 2669 | template<typename T> |
| 2670 | Vector::ContainsElementMatcher<T> VectorContains( T const& comparator ) { |
| 2671 | return Vector::ContainsElementMatcher<T>( comparator ); |
| 2672 | } |
| 2673 | |
| 2674 | template<typename T> |
| 2675 | Vector::EqualsMatcher<T> Equals( std::vector<T> const& comparator ) { |
| 2676 | return Vector::EqualsMatcher<T>( comparator ); |
| 2677 | } |
| 2678 | |
| 2679 | template<typename T> |
| 2680 | Vector::UnorderedEqualsMatcher<T> UnorderedEquals(std::vector<T> const& target) { |
| 2681 | return Vector::UnorderedEqualsMatcher<T>(target); |
| 2682 | } |
| 2683 | |
| 2684 | } // namespace Matchers |
| 2685 | } // namespace Catch |
| 2686 | |
| 2687 | // end catch_matchers_vector.h |
| 2688 | namespace Catch { |
| 2689 | |
| 2690 | template<typename ArgT, typename MatcherT> |
| 2691 | class MatchExpr : public ITransientExpression { |
| 2692 | ArgT const& m_arg; |
| 2693 | MatcherT m_matcher; |
| 2694 | StringRef m_matcherString; |
| 2695 | public: |
| 2696 | MatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString ) |
| 2697 | : ITransientExpression{ true, matcher.match( arg ) }, |
| 2698 | m_arg( arg ), |
| 2699 | m_matcher( matcher ), |
| 2700 | m_matcherString( matcherString ) |
| 2701 | {} |
| 2702 | |
| 2703 | void streamReconstructedExpression( std::ostream &os ) const override { |
| 2704 | auto matcherAsString = m_matcher.toString(); |
| 2705 | os << Catch::Detail::stringify( m_arg ) << ' '; |
| 2706 | if( matcherAsString == Detail::unprintableString ) |
| 2707 | os << m_matcherString; |
| 2708 | else |
| 2709 | os << matcherAsString; |
| 2710 | } |
| 2711 | }; |
| 2712 | |
| 2713 | using StringMatcher = Matchers::Impl::MatcherBase<std::string>; |
| 2714 | |
| 2715 | void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef matcherString ); |
| 2716 | |
| 2717 | template<typename ArgT, typename MatcherT> |
| 2718 | auto makeMatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString ) -> MatchExpr<ArgT, MatcherT> { |
| 2719 | return MatchExpr<ArgT, MatcherT>( arg, matcher, matcherString ); |
| 2720 | } |
| 2721 | |
| 2722 | } // namespace Catch |
| 2723 | |
| 2724 | /////////////////////////////////////////////////////////////////////////////// |
| 2725 | #define INTERNAL_CHECK_THAT( macroName, matcher, resultDisposition, arg ) \ |
| 2726 | do { \ |
| 2727 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(arg) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ |
| 2728 | INTERNAL_CATCH_TRY { \ |
| 2729 | catchAssertionHandler.handleExpr( Catch::makeMatchExpr( arg, matcher, #matcher ) ); \ |
| 2730 | } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ |
| 2731 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 2732 | } while( false ) |
| 2733 | |
| 2734 | /////////////////////////////////////////////////////////////////////////////// |
| 2735 | #define INTERNAL_CATCH_THROWS_MATCHES( macroName, exceptionType, resultDisposition, matcher, ... ) \ |
| 2736 | do { \ |
| 2737 | Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(exceptionType) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ |
| 2738 | if( catchAssertionHandler.allowThrows() ) \ |
| 2739 | try { \ |
| 2740 | static_cast<void>(__VA_ARGS__ ); \ |
| 2741 | catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ |
| 2742 | } \ |
| 2743 | catch( exceptionType const& ex ) { \ |
| 2744 | catchAssertionHandler.handleExpr( Catch::makeMatchExpr( ex, matcher, #matcher ) ); \ |
| 2745 | } \ |
| 2746 | catch( ... ) { \ |
| 2747 | catchAssertionHandler.handleUnexpectedInflightException(); \ |
| 2748 | } \ |
| 2749 | else \ |
| 2750 | catchAssertionHandler.handleThrowingCallSkipped(); \ |
| 2751 | INTERNAL_CATCH_REACT( catchAssertionHandler ) \ |
| 2752 | } while( false ) |
| 2753 | |
| 2754 | // end catch_capture_matchers.h |
| 2755 | #endif |
| 2756 | |
| 2757 | // These files are included here so the single_include script doesn't put them |
| 2758 | // in the conditionally compiled sections |
| 2759 | // start catch_test_case_info.h |
| 2760 | |
| 2761 | #include <string> |
| 2762 | #include <vector> |
| 2763 | #include <memory> |
| 2764 | |
| 2765 | #ifdef __clang__ |
| 2766 | #pragma clang diagnostic push |
| 2767 | #pragma clang diagnostic ignored "-Wpadded" |
| 2768 | #endif |
| 2769 | |
| 2770 | namespace Catch { |
| 2771 | |
| 2772 | struct ITestInvoker; |
| 2773 | |
| 2774 | struct TestCaseInfo { |
| 2775 | enum SpecialProperties{ |
| 2776 | None = 0, |
| 2777 | IsHidden = 1 << 1, |
| 2778 | ShouldFail = 1 << 2, |
| 2779 | MayFail = 1 << 3, |
| 2780 | Throws = 1 << 4, |
| 2781 | NonPortable = 1 << 5, |
| 2782 | Benchmark = 1 << 6 |
| 2783 | }; |
| 2784 | |
| 2785 | TestCaseInfo( std::string const& _name, |
| 2786 | std::string const& _className, |
| 2787 | std::string const& _description, |
| 2788 | std::vector<std::string> const& _tags, |
| 2789 | SourceLineInfo const& _lineInfo ); |
| 2790 | |
| 2791 | friend void setTags( TestCaseInfo& testCaseInfo, std::vector<std::string> tags ); |
| 2792 | |
| 2793 | bool isHidden() const; |
| 2794 | bool throws() const; |
| 2795 | bool okToFail() const; |
| 2796 | bool expectedToFail() const; |
| 2797 | |
| 2798 | std::string tagsAsString() const; |
| 2799 | |
| 2800 | std::string name; |
| 2801 | std::string className; |
| 2802 | std::string description; |
| 2803 | std::vector<std::string> tags; |
| 2804 | std::vector<std::string> lcaseTags; |
| 2805 | SourceLineInfo lineInfo; |
| 2806 | SpecialProperties properties; |
| 2807 | }; |
| 2808 | |
| 2809 | class TestCase : public TestCaseInfo { |
| 2810 | public: |
| 2811 | |
| 2812 | TestCase( ITestInvoker* testCase, TestCaseInfo&& info ); |
| 2813 | |
| 2814 | TestCase withName( std::string const& _newName ) const; |
| 2815 | |
| 2816 | void invoke() const; |
| 2817 | |
| 2818 | TestCaseInfo const& getTestCaseInfo() const; |
| 2819 | |
| 2820 | bool operator == ( TestCase const& other ) const; |
| 2821 | bool operator < ( TestCase const& other ) const; |
| 2822 | |
| 2823 | private: |
| 2824 | std::shared_ptr<ITestInvoker> test; |
| 2825 | }; |
| 2826 | |
| 2827 | TestCase makeTestCase( ITestInvoker* testCase, |
| 2828 | std::string const& className, |
| 2829 | NameAndTags const& nameAndTags, |
| 2830 | SourceLineInfo const& lineInfo ); |
| 2831 | } |
| 2832 | |
| 2833 | #ifdef __clang__ |
| 2834 | #pragma clang diagnostic pop |
| 2835 | #endif |
| 2836 | |
| 2837 | // end catch_test_case_info.h |
| 2838 | // start catch_interfaces_runner.h |
| 2839 | |
| 2840 | namespace Catch { |
| 2841 | |
| 2842 | struct IRunner { |
| 2843 | virtual ~IRunner(); |
| 2844 | virtual bool aborting() const = 0; |
| 2845 | }; |
| 2846 | } |
| 2847 | |
| 2848 | // end catch_interfaces_runner.h |
| 2849 | |
| 2850 | #ifdef __OBJC__ |
| 2851 | // start catch_objc.hpp |
| 2852 | |
| 2853 | #import <objc/runtime.h> |
| 2854 | |
| 2855 | #include <string> |
| 2856 | |
| 2857 | // NB. Any general catch headers included here must be included |
| 2858 | // in catch.hpp first to make sure they are included by the single |
| 2859 | // header for non obj-usage |
| 2860 | |
| 2861 | /////////////////////////////////////////////////////////////////////////////// |
| 2862 | // This protocol is really only here for (self) documenting purposes, since |
| 2863 | // all its methods are optional. |
| 2864 | @protocol OcFixture |
| 2865 | |
| 2866 | @optional |
| 2867 | |
| 2868 | -(void) setUp; |
| 2869 | -(void) tearDown; |
| 2870 | |
| 2871 | @end |
| 2872 | |
| 2873 | namespace Catch { |
| 2874 | |
| 2875 | class OcMethod : public ITestInvoker { |
| 2876 | |
| 2877 | public: |
| 2878 | OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {} |
| 2879 | |
| 2880 | virtual void invoke() const { |
| 2881 | id obj = [[m_cls alloc] init]; |
| 2882 | |
| 2883 | performOptionalSelector( obj, @selector(setUp) ); |
| 2884 | performOptionalSelector( obj, m_sel ); |
| 2885 | performOptionalSelector( obj, @selector(tearDown) ); |
| 2886 | |
| 2887 | arcSafeRelease( obj ); |
| 2888 | } |
| 2889 | private: |
| 2890 | virtual ~OcMethod() {} |
| 2891 | |
| 2892 | Class m_cls; |
| 2893 | SEL m_sel; |
| 2894 | }; |
| 2895 | |
| 2896 | namespace Detail{ |
| 2897 | |
| 2898 | inline std::string getAnnotation( Class cls, |
| 2899 | std::string const& annotationName, |
| 2900 | std::string const& testCaseName ) { |
| 2901 | NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; |
| 2902 | SEL sel = NSSelectorFromString( selStr ); |
| 2903 | arcSafeRelease( selStr ); |
| 2904 | id value = performOptionalSelector( cls, sel ); |
| 2905 | if( value ) |
| 2906 | return [(NSString*)value UTF8String]; |
| 2907 | return ""; |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | inline std::size_t registerTestMethods() { |
| 2912 | std::size_t noTestMethods = 0; |
| 2913 | int noClasses = objc_getClassList( nullptr, 0 ); |
| 2914 | |
| 2915 | Class* classes = (CATCH_UNSAFE_UNRETAINED Class *)malloc( sizeof(Class) * noClasses); |
| 2916 | objc_getClassList( classes, noClasses ); |
| 2917 | |
| 2918 | for( int c = 0; c < noClasses; c++ ) { |
| 2919 | Class cls = classes[c]; |
| 2920 | { |
| 2921 | u_int count; |
| 2922 | Method* methods = class_copyMethodList( cls, &count ); |
| 2923 | for( u_int m = 0; m < count ; m++ ) { |
| 2924 | SEL selector = method_getName(methods[m]); |
| 2925 | std::string methodName = sel_getName(selector); |
| 2926 | if( startsWith( methodName, "Catch_TestCase_" ) ) { |
| 2927 | std::string testCaseName = methodName.substr( 15 ); |
| 2928 | std::string name = Detail::getAnnotation( cls, "Name", testCaseName ); |
| 2929 | std::string desc = Detail::getAnnotation( cls, "Description", testCaseName ); |
| 2930 | const char* className = class_getName( cls ); |
| 2931 | |
| 2932 | getMutableRegistryHub().registerTest( makeTestCase( new OcMethod( cls, selector ), className, name.c_str(), desc.c_str(), SourceLineInfo("",0) ) ); |
| 2933 | noTestMethods++; |
| 2934 | } |
| 2935 | } |
| 2936 | free(methods); |
| 2937 | } |
| 2938 | } |
| 2939 | return noTestMethods; |
| 2940 | } |
| 2941 | |
| 2942 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 2943 | |
| 2944 | namespace Matchers { |
| 2945 | namespace Impl { |
| 2946 | namespace NSStringMatchers { |
| 2947 | |
| 2948 | struct StringHolder : MatcherBase<NSString*>{ |
| 2949 | StringHolder( NSString* substr ) : m_substr( [substr copy] ){} |
| 2950 | StringHolder( StringHolder const& other ) : m_substr( [other.m_substr copy] ){} |
| 2951 | StringHolder() { |
| 2952 | arcSafeRelease( m_substr ); |
| 2953 | } |
| 2954 | |
| 2955 | bool match( NSString* arg ) const override { |
| 2956 | return false; |
| 2957 | } |
| 2958 | |
| 2959 | NSString* CATCH_ARC_STRONG m_substr; |
| 2960 | }; |
| 2961 | |
| 2962 | struct Equals : StringHolder { |
| 2963 | Equals( NSString* substr ) : StringHolder( substr ){} |
| 2964 | |
| 2965 | bool match( NSString* str ) const override { |
| 2966 | return (str != nil || m_substr == nil ) && |
| 2967 | [str isEqualToString:m_substr]; |
| 2968 | } |
| 2969 | |
| 2970 | std::string describe() const override { |
| 2971 | return "equals string: " + Catch::Detail::stringify( m_substr ); |
| 2972 | } |
| 2973 | }; |
| 2974 | |
| 2975 | struct Contains : StringHolder { |
| 2976 | Contains( NSString* substr ) : StringHolder( substr ){} |
| 2977 | |
| 2978 | bool match( NSString* str ) const { |
| 2979 | return (str != nil || m_substr == nil ) && |
| 2980 | [str rangeOfString:m_substr].location != NSNotFound; |
| 2981 | } |
| 2982 | |
| 2983 | std::string describe() const override { |
| 2984 | return "contains string: " + Catch::Detail::stringify( m_substr ); |
| 2985 | } |
| 2986 | }; |
| 2987 | |
| 2988 | struct StartsWith : StringHolder { |
| 2989 | StartsWith( NSString* substr ) : StringHolder( substr ){} |
| 2990 | |
| 2991 | bool match( NSString* str ) const override { |
| 2992 | return (str != nil || m_substr == nil ) && |
| 2993 | [str rangeOfString:m_substr].location == 0; |
| 2994 | } |
| 2995 | |
| 2996 | std::string describe() const override { |
| 2997 | return "starts with: " + Catch::Detail::stringify( m_substr ); |
| 2998 | } |
| 2999 | }; |
| 3000 | struct EndsWith : StringHolder { |
| 3001 | EndsWith( NSString* substr ) : StringHolder( substr ){} |
| 3002 | |
| 3003 | bool match( NSString* str ) const override { |
| 3004 | return (str != nil || m_substr == nil ) && |
| 3005 | [str rangeOfString:m_substr].location == [str length] - [m_substr length]; |
| 3006 | } |
| 3007 | |
| 3008 | std::string describe() const override { |
| 3009 | return "ends with: " + Catch::Detail::stringify( m_substr ); |
| 3010 | } |
| 3011 | }; |
| 3012 | |
| 3013 | } // namespace NSStringMatchers |
| 3014 | } // namespace Impl |
| 3015 | |
| 3016 | inline Impl::NSStringMatchers::Equals |
| 3017 | Equals( NSString* substr ){ return Impl::NSStringMatchers::Equals( substr ); } |
| 3018 | |
| 3019 | inline Impl::NSStringMatchers::Contains |
| 3020 | Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); } |
| 3021 | |
| 3022 | inline Impl::NSStringMatchers::StartsWith |
| 3023 | StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); } |
| 3024 | |
| 3025 | inline Impl::NSStringMatchers::EndsWith |
| 3026 | EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); } |
| 3027 | |
| 3028 | } // namespace Matchers |
| 3029 | |
| 3030 | using namespace Matchers; |
| 3031 | |
| 3032 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 3033 | |
| 3034 | } // namespace Catch |
| 3035 | |
| 3036 | /////////////////////////////////////////////////////////////////////////////// |
| 3037 | #define OC_MAKE_UNIQUE_NAME( root, uniqueSuffix ) root##uniqueSuffix |
| 3038 | #define OC_TEST_CASE2( name, desc, uniqueSuffix ) \ |
| 3039 | +(NSString*) OC_MAKE_UNIQUE_NAME( Catch_Name_test_, uniqueSuffix ) \ |
| 3040 | { \ |
| 3041 | return @ name; \ |
| 3042 | } \ |
| 3043 | +(NSString*) OC_MAKE_UNIQUE_NAME( Catch_Description_test_, uniqueSuffix ) \ |
| 3044 | { \ |
| 3045 | return @ desc; \ |
| 3046 | } \ |
| 3047 | -(void) OC_MAKE_UNIQUE_NAME( Catch_TestCase_test_, uniqueSuffix ) |
| 3048 | |
| 3049 | #define OC_TEST_CASE( name, desc ) OC_TEST_CASE2( name, desc, __LINE__ ) |
| 3050 | |
| 3051 | // end catch_objc.hpp |
| 3052 | #endif |
| 3053 | |
| 3054 | #ifdef CATCH_CONFIG_EXTERNAL_INTERFACES |
| 3055 | // start catch_external_interfaces.h |
| 3056 | |
| 3057 | // start catch_reporter_bases.hpp |
| 3058 | |
| 3059 | // start catch_interfaces_reporter.h |
| 3060 | |
| 3061 | // start catch_config.hpp |
| 3062 | |
| 3063 | // start catch_test_spec_parser.h |
| 3064 | |
| 3065 | #ifdef __clang__ |
| 3066 | #pragma clang diagnostic push |
| 3067 | #pragma clang diagnostic ignored "-Wpadded" |
| 3068 | #endif |
| 3069 | |
| 3070 | // start catch_test_spec.h |
| 3071 | |
| 3072 | #ifdef __clang__ |
| 3073 | #pragma clang diagnostic push |
| 3074 | #pragma clang diagnostic ignored "-Wpadded" |
| 3075 | #endif |
| 3076 | |
| 3077 | // start catch_wildcard_pattern.h |
| 3078 | |
| 3079 | namespace Catch |
| 3080 | { |
| 3081 | class WildcardPattern { |
| 3082 | enum WildcardPosition { |
| 3083 | NoWildcard = 0, |
| 3084 | WildcardAtStart = 1, |
| 3085 | WildcardAtEnd = 2, |
| 3086 | WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd |
| 3087 | }; |
| 3088 | |
| 3089 | public: |
| 3090 | |
| 3091 | WildcardPattern( std::string const& pattern, CaseSensitive::Choice caseSensitivity ); |
| 3092 | virtual ~WildcardPattern() = default; |
| 3093 | virtual bool matches( std::string const& str ) const; |
| 3094 | |
| 3095 | private: |
| 3096 | std::string adjustCase( std::string const& str ) const; |
| 3097 | CaseSensitive::Choice m_caseSensitivity; |
| 3098 | WildcardPosition m_wildcard = NoWildcard; |
| 3099 | std::string m_pattern; |
| 3100 | }; |
| 3101 | } |
| 3102 | |
| 3103 | // end catch_wildcard_pattern.h |
| 3104 | #include <string> |
| 3105 | #include <vector> |
| 3106 | #include <memory> |
| 3107 | |
| 3108 | namespace Catch { |
| 3109 | |
| 3110 | class TestSpec { |
| 3111 | struct Pattern { |
| 3112 | virtual ~Pattern(); |
| 3113 | virtual bool matches( TestCaseInfo const& testCase ) const = 0; |
| 3114 | }; |
| 3115 | using PatternPtr = std::shared_ptr<Pattern>; |
| 3116 | |
| 3117 | class NamePattern : public Pattern { |
| 3118 | public: |
| 3119 | NamePattern( std::string const& name ); |
| 3120 | virtual ~NamePattern(); |
| 3121 | virtual bool matches( TestCaseInfo const& testCase ) const override; |
| 3122 | private: |
| 3123 | WildcardPattern m_wildcardPattern; |
| 3124 | }; |
| 3125 | |
| 3126 | class TagPattern : public Pattern { |
| 3127 | public: |
| 3128 | TagPattern( std::string const& tag ); |
| 3129 | virtual ~TagPattern(); |
| 3130 | virtual bool matches( TestCaseInfo const& testCase ) const override; |
| 3131 | private: |
| 3132 | std::string m_tag; |
| 3133 | }; |
| 3134 | |
| 3135 | class ExcludedPattern : public Pattern { |
| 3136 | public: |
| 3137 | ExcludedPattern( PatternPtr const& underlyingPattern ); |
| 3138 | virtual ~ExcludedPattern(); |
| 3139 | virtual bool matches( TestCaseInfo const& testCase ) const override; |
| 3140 | private: |
| 3141 | PatternPtr m_underlyingPattern; |
| 3142 | }; |
| 3143 | |
| 3144 | struct Filter { |
| 3145 | std::vector<PatternPtr> m_patterns; |
| 3146 | |
| 3147 | bool matches( TestCaseInfo const& testCase ) const; |
| 3148 | }; |
| 3149 | |
| 3150 | public: |
| 3151 | bool hasFilters() const; |
| 3152 | bool matches( TestCaseInfo const& testCase ) const; |
| 3153 | |
| 3154 | private: |
| 3155 | std::vector<Filter> m_filters; |
| 3156 | |
| 3157 | friend class TestSpecParser; |
| 3158 | }; |
| 3159 | } |
| 3160 | |
| 3161 | #ifdef __clang__ |
| 3162 | #pragma clang diagnostic pop |
| 3163 | #endif |
| 3164 | |
| 3165 | // end catch_test_spec.h |
| 3166 | // start catch_interfaces_tag_alias_registry.h |
| 3167 | |
| 3168 | #include <string> |
| 3169 | |
| 3170 | namespace Catch { |
| 3171 | |
| 3172 | struct TagAlias; |
| 3173 | |
| 3174 | struct ITagAliasRegistry { |
| 3175 | virtual ~ITagAliasRegistry(); |
| 3176 | // Nullptr if not present |
| 3177 | virtual TagAlias const* find( std::string const& alias ) const = 0; |
| 3178 | virtual std::string expandAliases( std::string const& unexpandedTestSpec ) const = 0; |
| 3179 | |
| 3180 | static ITagAliasRegistry const& get(); |
| 3181 | }; |
| 3182 | |
| 3183 | } // end namespace Catch |
| 3184 | |
| 3185 | // end catch_interfaces_tag_alias_registry.h |
| 3186 | namespace Catch { |
| 3187 | |
| 3188 | class TestSpecParser { |
| 3189 | enum Mode{ None, Name, QuotedName, Tag, EscapedName }; |
| 3190 | Mode m_mode = None; |
| 3191 | bool m_exclusion = false; |
| 3192 | std::size_t m_start = std::string::npos, m_pos = 0; |
| 3193 | std::string m_arg; |
| 3194 | std::vector<std::size_t> m_escapeChars; |
| 3195 | TestSpec::Filter m_currentFilter; |
| 3196 | TestSpec m_testSpec; |
| 3197 | ITagAliasRegistry const* m_tagAliases = nullptr; |
| 3198 | |
| 3199 | public: |
| 3200 | TestSpecParser( ITagAliasRegistry const& tagAliases ); |
| 3201 | |
| 3202 | TestSpecParser& parse( std::string const& arg ); |
| 3203 | TestSpec testSpec(); |
| 3204 | |
| 3205 | private: |
| 3206 | void visitChar( char c ); |
| 3207 | void startNewMode( Mode mode, std::size_t start ); |
| 3208 | void escape(); |
| 3209 | std::string subString() const; |
| 3210 | |
| 3211 | template<typename T> |
| 3212 | void addPattern() { |
| 3213 | std::string token = subString(); |
| 3214 | for( std::size_t i = 0; i < m_escapeChars.size(); ++i ) |
| 3215 | token = token.substr( 0, m_escapeChars[i]-m_start-i ) + token.substr( m_escapeChars[i]-m_start-i+1 ); |
| 3216 | m_escapeChars.clear(); |
| 3217 | if( startsWith( token, "exclude:" ) ) { |
| 3218 | m_exclusion = true; |
| 3219 | token = token.substr( 8 ); |
| 3220 | } |
| 3221 | if( !token.empty() ) { |
| 3222 | TestSpec::PatternPtr pattern = std::make_shared<T>( token ); |
| 3223 | if( m_exclusion ) |
| 3224 | pattern = std::make_shared<TestSpec::ExcludedPattern>( pattern ); |
| 3225 | m_currentFilter.m_patterns.push_back( pattern ); |
| 3226 | } |
| 3227 | m_exclusion = false; |
| 3228 | m_mode = None; |
| 3229 | } |
| 3230 | |
| 3231 | void addFilter(); |
| 3232 | }; |
| 3233 | TestSpec parseTestSpec( std::string const& arg ); |
| 3234 | |
| 3235 | } // namespace Catch |
| 3236 | |
| 3237 | #ifdef __clang__ |
| 3238 | #pragma clang diagnostic pop |
| 3239 | #endif |
| 3240 | |
| 3241 | // end catch_test_spec_parser.h |
| 3242 | // start catch_interfaces_config.h |
| 3243 | |
| 3244 | #include <iosfwd> |
| 3245 | #include <string> |
| 3246 | #include <vector> |
| 3247 | #include <memory> |
| 3248 | |
| 3249 | namespace Catch { |
| 3250 | |
| 3251 | enum class Verbosity { |
| 3252 | Quiet = 0, |
| 3253 | Normal, |
| 3254 | High |
| 3255 | }; |
| 3256 | |
| 3257 | struct WarnAbout { enum What { |
| 3258 | Nothing = 0x00, |
| 3259 | NoAssertions = 0x01, |
| 3260 | NoTests = 0x02 |
| 3261 | }; }; |
| 3262 | |
| 3263 | struct ShowDurations { enum OrNot { |
| 3264 | DefaultForReporter, |
| 3265 | Always, |
| 3266 | Never |
| 3267 | }; }; |
| 3268 | struct RunTests { enum InWhatOrder { |
| 3269 | InDeclarationOrder, |
| 3270 | InLexicographicalOrder, |
| 3271 | InRandomOrder |
| 3272 | }; }; |
| 3273 | struct UseColour { enum YesOrNo { |
| 3274 | Auto, |
| 3275 | Yes, |
| 3276 | No |
| 3277 | }; }; |
| 3278 | struct WaitForKeypress { enum When { |
| 3279 | Never, |
| 3280 | BeforeStart = 1, |
| 3281 | BeforeExit = 2, |
| 3282 | BeforeStartAndExit = BeforeStart | BeforeExit |
| 3283 | }; }; |
| 3284 | |
| 3285 | class TestSpec; |
| 3286 | |
| 3287 | struct IConfig : NonCopyable { |
| 3288 | |
| 3289 | virtual ~IConfig(); |
| 3290 | |
| 3291 | virtual bool allowThrows() const = 0; |
| 3292 | virtual std::ostream& stream() const = 0; |
| 3293 | virtual std::string name() const = 0; |
| 3294 | virtual bool includeSuccessfulResults() const = 0; |
| 3295 | virtual bool shouldDebugBreak() const = 0; |
| 3296 | virtual bool warnAboutMissingAssertions() const = 0; |
| 3297 | virtual bool warnAboutNoTests() const = 0; |
| 3298 | virtual int abortAfter() const = 0; |
| 3299 | virtual bool showInvisibles() const = 0; |
| 3300 | virtual ShowDurations::OrNot showDurations() const = 0; |
| 3301 | virtual TestSpec const& testSpec() const = 0; |
| 3302 | virtual bool hasTestFilters() const = 0; |
| 3303 | virtual RunTests::InWhatOrder runOrder() const = 0; |
| 3304 | virtual unsigned int rngSeed() const = 0; |
| 3305 | virtual int benchmarkResolutionMultiple() const = 0; |
| 3306 | virtual UseColour::YesOrNo useColour() const = 0; |
| 3307 | virtual std::vector<std::string> const& getSectionsToRun() const = 0; |
| 3308 | virtual Verbosity verbosity() const = 0; |
| 3309 | }; |
| 3310 | |
| 3311 | using IConfigPtr = std::shared_ptr<IConfig const>; |
| 3312 | } |
| 3313 | |
| 3314 | // end catch_interfaces_config.h |
| 3315 | // Libstdc++ doesn't like incomplete classes for unique_ptr |
| 3316 | |
| 3317 | #include <memory> |
| 3318 | #include <vector> |
| 3319 | #include <string> |
| 3320 | |
| 3321 | #ifndef CATCH_CONFIG_CONSOLE_WIDTH |
| 3322 | #define CATCH_CONFIG_CONSOLE_WIDTH 80 |
| 3323 | #endif |
| 3324 | |
| 3325 | namespace Catch { |
| 3326 | |
| 3327 | struct IStream; |
| 3328 | |
| 3329 | struct ConfigData { |
| 3330 | bool listTests = false; |
| 3331 | bool listTags = false; |
| 3332 | bool listReporters = false; |
| 3333 | bool listTestNamesOnly = false; |
| 3334 | |
| 3335 | bool showSuccessfulTests = false; |
| 3336 | bool shouldDebugBreak = false; |
| 3337 | bool noThrow = false; |
| 3338 | bool showHelp = false; |
| 3339 | bool showInvisibles = false; |
| 3340 | bool filenamesAsTags = false; |
| 3341 | bool libIdentify = false; |
| 3342 | |
| 3343 | int abortAfter = -1; |
| 3344 | unsigned int rngSeed = 0; |
| 3345 | int benchmarkResolutionMultiple = 100; |
| 3346 | |
| 3347 | Verbosity verbosity = Verbosity::Normal; |
| 3348 | WarnAbout::What warnings = WarnAbout::Nothing; |
| 3349 | ShowDurations::OrNot showDurations = ShowDurations::DefaultForReporter; |
| 3350 | RunTests::InWhatOrder runOrder = RunTests::InDeclarationOrder; |
| 3351 | UseColour::YesOrNo useColour = UseColour::Auto; |
| 3352 | WaitForKeypress::When waitForKeypress = WaitForKeypress::Never; |
| 3353 | |
| 3354 | std::string outputFilename; |
| 3355 | std::string name; |
| 3356 | std::string processName; |
| 3357 | |
| 3358 | std::vector<std::string> reporterNames; |
| 3359 | std::vector<std::string> testsOrTags; |
| 3360 | std::vector<std::string> sectionsToRun; |
| 3361 | }; |
| 3362 | |
| 3363 | class Config : public IConfig { |
| 3364 | public: |
| 3365 | |
| 3366 | Config() = default; |
| 3367 | Config( ConfigData const& data ); |
| 3368 | virtual ~Config() = default; |
| 3369 | |
| 3370 | std::string const& getFilename() const; |
| 3371 | |
| 3372 | bool listTests() const; |
| 3373 | bool listTestNamesOnly() const; |
| 3374 | bool listTags() const; |
| 3375 | bool listReporters() const; |
| 3376 | |
| 3377 | std::string getProcessName() const; |
| 3378 | |
| 3379 | std::vector<std::string> const& getReporterNames() const; |
| 3380 | std::vector<std::string> const& getTestsOrTags() const; |
| 3381 | std::vector<std::string> const& getSectionsToRun() const override; |
| 3382 | |
| 3383 | virtual TestSpec const& testSpec() const override; |
| 3384 | bool hasTestFilters() const override; |
| 3385 | |
| 3386 | bool showHelp() const; |
| 3387 | |
| 3388 | // IConfig interface |
| 3389 | bool allowThrows() const override; |
| 3390 | std::ostream& stream() const override; |
| 3391 | std::string name() const override; |
| 3392 | bool includeSuccessfulResults() const override; |
| 3393 | bool warnAboutMissingAssertions() const override; |
| 3394 | bool warnAboutNoTests() const override; |
| 3395 | ShowDurations::OrNot showDurations() const override; |
| 3396 | RunTests::InWhatOrder runOrder() const override; |
| 3397 | unsigned int rngSeed() const override; |
| 3398 | int benchmarkResolutionMultiple() const override; |
| 3399 | UseColour::YesOrNo useColour() const override; |
| 3400 | bool shouldDebugBreak() const override; |
| 3401 | int abortAfter() const override; |
| 3402 | bool showInvisibles() const override; |
| 3403 | Verbosity verbosity() const override; |
| 3404 | |
| 3405 | private: |
| 3406 | |
| 3407 | IStream const* openStream(); |
| 3408 | ConfigData m_data; |
| 3409 | |
| 3410 | std::unique_ptr<IStream const> m_stream; |
| 3411 | TestSpec m_testSpec; |
| 3412 | bool m_hasTestFilters = false; |
| 3413 | }; |
| 3414 | |
| 3415 | } // end namespace Catch |
| 3416 | |
| 3417 | // end catch_config.hpp |
| 3418 | // start catch_assertionresult.h |
| 3419 | |
| 3420 | #include <string> |
| 3421 | |
| 3422 | namespace Catch { |
| 3423 | |
| 3424 | struct AssertionResultData |
| 3425 | { |
| 3426 | AssertionResultData() = delete; |
| 3427 | |
| 3428 | AssertionResultData( ResultWas::OfType _resultType, LazyExpression const& _lazyExpression ); |
| 3429 | |
| 3430 | std::string message; |
| 3431 | mutable std::string reconstructedExpression; |
| 3432 | LazyExpression lazyExpression; |
| 3433 | ResultWas::OfType resultType; |
| 3434 | |
| 3435 | std::string reconstructExpression() const; |
| 3436 | }; |
| 3437 | |
| 3438 | class AssertionResult { |
| 3439 | public: |
| 3440 | AssertionResult() = delete; |
| 3441 | AssertionResult( AssertionInfo const& info, AssertionResultData const& data ); |
| 3442 | |
| 3443 | bool isOk() const; |
| 3444 | bool succeeded() const; |
| 3445 | ResultWas::OfType getResultType() const; |
| 3446 | bool hasExpression() const; |
| 3447 | bool hasMessage() const; |
| 3448 | std::string getExpression() const; |
| 3449 | std::string getExpressionInMacro() const; |
| 3450 | bool hasExpandedExpression() const; |
| 3451 | std::string getExpandedExpression() const; |
| 3452 | std::string getMessage() const; |
| 3453 | SourceLineInfo getSourceInfo() const; |
| 3454 | StringRef getTestMacroName() const; |
| 3455 | |
| 3456 | //protected: |
| 3457 | AssertionInfo m_info; |
| 3458 | AssertionResultData m_resultData; |
| 3459 | }; |
| 3460 | |
| 3461 | } // end namespace Catch |
| 3462 | |
| 3463 | // end catch_assertionresult.h |
| 3464 | // start catch_option.hpp |
| 3465 | |
| 3466 | namespace Catch { |
| 3467 | |
| 3468 | // An optional type |
| 3469 | template<typename T> |
| 3470 | class Option { |
| 3471 | public: |
| 3472 | Option() : nullableValue( nullptr ) {} |
| 3473 | Option( T const& _value ) |
| 3474 | : nullableValue( new( storage ) T( _value ) ) |
| 3475 | {} |
| 3476 | Option( Option const& _other ) |
| 3477 | : nullableValue( _other ? new( storage ) T( *_other ) : nullptr ) |
| 3478 | {} |
| 3479 | |
| 3480 | ~Option() { |
| 3481 | reset(); |
| 3482 | } |
| 3483 | |
| 3484 | Option& operator= ( Option const& _other ) { |
| 3485 | if( &_other != this ) { |
| 3486 | reset(); |
| 3487 | if( _other ) |
| 3488 | nullableValue = new( storage ) T( *_other ); |
| 3489 | } |
| 3490 | return *this; |
| 3491 | } |
| 3492 | Option& operator = ( T const& _value ) { |
| 3493 | reset(); |
| 3494 | nullableValue = new( storage ) T( _value ); |
| 3495 | return *this; |
| 3496 | } |
| 3497 | |
| 3498 | void reset() { |
| 3499 | if( nullableValue ) |
| 3500 | nullableValue->~T(); |
| 3501 | nullableValue = nullptr; |
| 3502 | } |
| 3503 | |
| 3504 | T& operator*() { return *nullableValue; } |
| 3505 | T const& operator*() const { return *nullableValue; } |
| 3506 | T* operator->() { return nullableValue; } |
| 3507 | const T* operator->() const { return nullableValue; } |
| 3508 | |
| 3509 | T valueOr( T const& defaultValue ) const { |
| 3510 | return nullableValue ? *nullableValue : defaultValue; |
| 3511 | } |
| 3512 | |
| 3513 | bool some() const { return nullableValue != nullptr; } |
| 3514 | bool none() const { return nullableValue == nullptr; } |
| 3515 | |
| 3516 | bool operator !() const { return nullableValue == nullptr; } |
| 3517 | explicit operator bool() const { |
| 3518 | return some(); |
| 3519 | } |
| 3520 | |
| 3521 | private: |
| 3522 | T *nullableValue; |
| 3523 | alignas(alignof(T)) char storage[sizeof(T)]; |
| 3524 | }; |
| 3525 | |
| 3526 | } // end namespace Catch |
| 3527 | |
| 3528 | // end catch_option.hpp |
| 3529 | #include <string> |
| 3530 | #include <iosfwd> |
| 3531 | #include <map> |
| 3532 | #include <set> |
| 3533 | #include <memory> |
| 3534 | |
| 3535 | namespace Catch { |
| 3536 | |
| 3537 | struct ReporterConfig { |
| 3538 | explicit ReporterConfig( IConfigPtr const& _fullConfig ); |
| 3539 | |
| 3540 | ReporterConfig( IConfigPtr const& _fullConfig, std::ostream& _stream ); |
| 3541 | |
| 3542 | std::ostream& stream() const; |
| 3543 | IConfigPtr fullConfig() const; |
| 3544 | |
| 3545 | private: |
| 3546 | std::ostream* m_stream; |
| 3547 | IConfigPtr m_fullConfig; |
| 3548 | }; |
| 3549 | |
| 3550 | struct ReporterPreferences { |
| 3551 | bool shouldRedirectStdOut = false; |
| 3552 | }; |
| 3553 | |
| 3554 | template<typename T> |
| 3555 | struct LazyStat : Option<T> { |
| 3556 | LazyStat& operator=( T const& _value ) { |
| 3557 | Option<T>::operator=( _value ); |
| 3558 | used = false; |
| 3559 | return *this; |
| 3560 | } |
| 3561 | void reset() { |
| 3562 | Option<T>::reset(); |
| 3563 | used = false; |
| 3564 | } |
| 3565 | bool used = false; |
| 3566 | }; |
| 3567 | |
| 3568 | struct TestRunInfo { |
| 3569 | TestRunInfo( std::string const& _name ); |
| 3570 | std::string name; |
| 3571 | }; |
| 3572 | struct GroupInfo { |
| 3573 | GroupInfo( std::string const& _name, |
| 3574 | std::size_t _groupIndex, |
| 3575 | std::size_t _groupsCount ); |
| 3576 | |
| 3577 | std::string name; |
| 3578 | std::size_t groupIndex; |
| 3579 | std::size_t groupsCounts; |
| 3580 | }; |
| 3581 | |
| 3582 | struct AssertionStats { |
| 3583 | AssertionStats( AssertionResult const& _assertionResult, |
| 3584 | std::vector<MessageInfo> const& _infoMessages, |
| 3585 | Totals const& _totals ); |
| 3586 | |
| 3587 | AssertionStats( AssertionStats const& ) = default; |
| 3588 | AssertionStats( AssertionStats && ) = default; |
| 3589 | AssertionStats& operator = ( AssertionStats const& ) = default; |
| 3590 | AssertionStats& operator = ( AssertionStats && ) = default; |
| 3591 | virtual ~AssertionStats(); |
| 3592 | |
| 3593 | AssertionResult assertionResult; |
| 3594 | std::vector<MessageInfo> infoMessages; |
| 3595 | Totals totals; |
| 3596 | }; |
| 3597 | |
| 3598 | struct SectionStats { |
| 3599 | SectionStats( SectionInfo const& _sectionInfo, |
| 3600 | Counts const& _assertions, |
| 3601 | double _durationInSeconds, |
| 3602 | bool _missingAssertions ); |
| 3603 | SectionStats( SectionStats const& ) = default; |
| 3604 | SectionStats( SectionStats && ) = default; |
| 3605 | SectionStats& operator = ( SectionStats const& ) = default; |
| 3606 | SectionStats& operator = ( SectionStats && ) = default; |
| 3607 | virtual ~SectionStats(); |
| 3608 | |
| 3609 | SectionInfo sectionInfo; |
| 3610 | Counts assertions; |
| 3611 | double durationInSeconds; |
| 3612 | bool missingAssertions; |
| 3613 | }; |
| 3614 | |
| 3615 | struct TestCaseStats { |
| 3616 | TestCaseStats( TestCaseInfo const& _testInfo, |
| 3617 | Totals const& _totals, |
| 3618 | std::string const& _stdOut, |
| 3619 | std::string const& _stdErr, |
| 3620 | bool _aborting ); |
| 3621 | |
| 3622 | TestCaseStats( TestCaseStats const& ) = default; |
| 3623 | TestCaseStats( TestCaseStats && ) = default; |
| 3624 | TestCaseStats& operator = ( TestCaseStats const& ) = default; |
| 3625 | TestCaseStats& operator = ( TestCaseStats && ) = default; |
| 3626 | virtual ~TestCaseStats(); |
| 3627 | |
| 3628 | TestCaseInfo testInfo; |
| 3629 | Totals totals; |
| 3630 | std::string stdOut; |
| 3631 | std::string stdErr; |
| 3632 | bool aborting; |
| 3633 | }; |
| 3634 | |
| 3635 | struct TestGroupStats { |
| 3636 | TestGroupStats( GroupInfo const& _groupInfo, |
| 3637 | Totals const& _totals, |
| 3638 | bool _aborting ); |
| 3639 | TestGroupStats( GroupInfo const& _groupInfo ); |
| 3640 | |
| 3641 | TestGroupStats( TestGroupStats const& ) = default; |
| 3642 | TestGroupStats( TestGroupStats && ) = default; |
| 3643 | TestGroupStats& operator = ( TestGroupStats const& ) = default; |
| 3644 | TestGroupStats& operator = ( TestGroupStats && ) = default; |
| 3645 | virtual ~TestGroupStats(); |
| 3646 | |
| 3647 | GroupInfo groupInfo; |
| 3648 | Totals totals; |
| 3649 | bool aborting; |
| 3650 | }; |
| 3651 | |
| 3652 | struct TestRunStats { |
| 3653 | TestRunStats( TestRunInfo const& _runInfo, |
| 3654 | Totals const& _totals, |
| 3655 | bool _aborting ); |
| 3656 | |
| 3657 | TestRunStats( TestRunStats const& ) = default; |
| 3658 | TestRunStats( TestRunStats && ) = default; |
| 3659 | TestRunStats& operator = ( TestRunStats const& ) = default; |
| 3660 | TestRunStats& operator = ( TestRunStats && ) = default; |
| 3661 | virtual ~TestRunStats(); |
| 3662 | |
| 3663 | TestRunInfo runInfo; |
| 3664 | Totals totals; |
| 3665 | bool aborting; |
| 3666 | }; |
| 3667 | |
| 3668 | struct BenchmarkInfo { |
| 3669 | std::string name; |
| 3670 | }; |
| 3671 | struct BenchmarkStats { |
| 3672 | BenchmarkInfo info; |
| 3673 | std::size_t iterations; |
| 3674 | uint64_t elapsedTimeInNanoseconds; |
| 3675 | }; |
| 3676 | |
| 3677 | struct IStreamingReporter { |
| 3678 | virtual ~IStreamingReporter() = default; |
| 3679 | |
| 3680 | // Implementing class must also provide the following static methods: |
| 3681 | // static std::string getDescription(); |
| 3682 | // static std::set<Verbosity> getSupportedVerbosities() |
| 3683 | |
| 3684 | virtual ReporterPreferences getPreferences() const = 0; |
| 3685 | |
| 3686 | virtual void noMatchingTestCases( std::string const& spec ) = 0; |
| 3687 | |
| 3688 | virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0; |
| 3689 | virtual void testGroupStarting( GroupInfo const& groupInfo ) = 0; |
| 3690 | |
| 3691 | virtual void testCaseStarting( TestCaseInfo const& testInfo ) = 0; |
| 3692 | virtual void sectionStarting( SectionInfo const& sectionInfo ) = 0; |
| 3693 | |
| 3694 | // *** experimental *** |
| 3695 | virtual void benchmarkStarting( BenchmarkInfo const& ) {} |
| 3696 | |
| 3697 | virtual void assertionStarting( AssertionInfo const& assertionInfo ) = 0; |
| 3698 | |
| 3699 | // The return value indicates if the messages buffer should be cleared: |
| 3700 | virtual bool assertionEnded( AssertionStats const& assertionStats ) = 0; |
| 3701 | |
| 3702 | // *** experimental *** |
| 3703 | virtual void benchmarkEnded( BenchmarkStats const& ) {} |
| 3704 | |
| 3705 | virtual void sectionEnded( SectionStats const& sectionStats ) = 0; |
| 3706 | virtual void testCaseEnded( TestCaseStats const& testCaseStats ) = 0; |
| 3707 | virtual void testGroupEnded( TestGroupStats const& testGroupStats ) = 0; |
| 3708 | virtual void testRunEnded( TestRunStats const& testRunStats ) = 0; |
| 3709 | |
| 3710 | virtual void skipTest( TestCaseInfo const& testInfo ) = 0; |
| 3711 | |
| 3712 | // Default empty implementation provided |
| 3713 | virtual void fatalErrorEncountered( StringRef name ); |
| 3714 | |
| 3715 | virtual bool isMulti() const; |
| 3716 | }; |
| 3717 | using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>; |
| 3718 | |
| 3719 | struct IReporterFactory { |
| 3720 | virtual ~IReporterFactory(); |
| 3721 | virtual IStreamingReporterPtr create( ReporterConfig const& config ) const = 0; |
| 3722 | virtual std::string getDescription() const = 0; |
| 3723 | }; |
| 3724 | using IReporterFactoryPtr = std::shared_ptr<IReporterFactory>; |
| 3725 | |
| 3726 | struct IReporterRegistry { |
| 3727 | using FactoryMap = std::map<std::string, IReporterFactoryPtr>; |
| 3728 | using Listeners = std::vector<IReporterFactoryPtr>; |
| 3729 | |
| 3730 | virtual ~IReporterRegistry(); |
| 3731 | virtual IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const = 0; |
| 3732 | virtual FactoryMap const& getFactories() const = 0; |
| 3733 | virtual Listeners const& getListeners() const = 0; |
| 3734 | }; |
| 3735 | |
| 3736 | void addReporter( IStreamingReporterPtr& existingReporter, IStreamingReporterPtr&& additionalReporter ); |
| 3737 | |
| 3738 | } // end namespace Catch |
| 3739 | |
| 3740 | // end catch_interfaces_reporter.h |
| 3741 | #include <algorithm> |
| 3742 | #include <cstring> |
| 3743 | #include <cfloat> |
| 3744 | #include <cstdio> |
| 3745 | #include <assert.h> |
| 3746 | #include <memory> |
| 3747 | #include <ostream> |
| 3748 | |
| 3749 | namespace Catch { |
| 3750 | void prepareExpandedExpression(AssertionResult& result); |
| 3751 | |
| 3752 | // Returns double formatted as %.3f (format expected on output) |
| 3753 | std::string getFormattedDuration( double duration ); |
| 3754 | |
| 3755 | template<typename DerivedT> |
| 3756 | struct StreamingReporterBase : IStreamingReporter { |
| 3757 | |
| 3758 | StreamingReporterBase( ReporterConfig const& _config ) |
| 3759 | : m_config( _config.fullConfig() ), |
| 3760 | stream( _config.stream() ) |
| 3761 | { |
| 3762 | m_reporterPrefs.shouldRedirectStdOut = false; |
| 3763 | if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) ) |
| 3764 | throw std::domain_error( "Verbosity level not supported by this reporter" ); |
| 3765 | } |
| 3766 | |
| 3767 | ReporterPreferences getPreferences() const override { |
| 3768 | return m_reporterPrefs; |
| 3769 | } |
| 3770 | |
| 3771 | static std::set<Verbosity> getSupportedVerbosities() { |
| 3772 | return { Verbosity::Normal }; |
| 3773 | } |
| 3774 | |
| 3775 | ~StreamingReporterBase() override = default; |
| 3776 | |
| 3777 | void noMatchingTestCases(std::string const&) override {} |
| 3778 | |
| 3779 | void testRunStarting(TestRunInfo const& _testRunInfo) override { |
| 3780 | currentTestRunInfo = _testRunInfo; |
| 3781 | } |
| 3782 | void testGroupStarting(GroupInfo const& _groupInfo) override { |
| 3783 | currentGroupInfo = _groupInfo; |
| 3784 | } |
| 3785 | |
| 3786 | void testCaseStarting(TestCaseInfo const& _testInfo) override { |
| 3787 | currentTestCaseInfo = _testInfo; |
| 3788 | } |
| 3789 | void sectionStarting(SectionInfo const& _sectionInfo) override { |
| 3790 | m_sectionStack.push_back(_sectionInfo); |
| 3791 | } |
| 3792 | |
| 3793 | void sectionEnded(SectionStats const& /* _sectionStats */) override { |
| 3794 | m_sectionStack.pop_back(); |
| 3795 | } |
| 3796 | void testCaseEnded(TestCaseStats const& /* _testCaseStats */) override { |
| 3797 | currentTestCaseInfo.reset(); |
| 3798 | } |
| 3799 | void testGroupEnded(TestGroupStats const& /* _testGroupStats */) override { |
| 3800 | currentGroupInfo.reset(); |
| 3801 | } |
| 3802 | void testRunEnded(TestRunStats const& /* _testRunStats */) override { |
| 3803 | currentTestCaseInfo.reset(); |
| 3804 | currentGroupInfo.reset(); |
| 3805 | currentTestRunInfo.reset(); |
| 3806 | } |
| 3807 | |
| 3808 | void skipTest(TestCaseInfo const&) override { |
| 3809 | // Don't do anything with this by default. |
| 3810 | // It can optionally be overridden in the derived class. |
| 3811 | } |
| 3812 | |
| 3813 | IConfigPtr m_config; |
| 3814 | std::ostream& stream; |
| 3815 | |
| 3816 | LazyStat<TestRunInfo> currentTestRunInfo; |
| 3817 | LazyStat<GroupInfo> currentGroupInfo; |
| 3818 | LazyStat<TestCaseInfo> currentTestCaseInfo; |
| 3819 | |
| 3820 | std::vector<SectionInfo> m_sectionStack; |
| 3821 | ReporterPreferences m_reporterPrefs; |
| 3822 | }; |
| 3823 | |
| 3824 | template<typename DerivedT> |
| 3825 | struct CumulativeReporterBase : IStreamingReporter { |
| 3826 | template<typename T, typename ChildNodeT> |
| 3827 | struct Node { |
| 3828 | explicit Node( T const& _value ) : value( _value ) {} |
| 3829 | virtual ~Node() {} |
| 3830 | |
| 3831 | using ChildNodes = std::vector<std::shared_ptr<ChildNodeT>>; |
| 3832 | T value; |
| 3833 | ChildNodes children; |
| 3834 | }; |
| 3835 | struct SectionNode { |
| 3836 | explicit SectionNode(SectionStats const& _stats) : stats(_stats) {} |
| 3837 | virtual ~SectionNode() = default; |
| 3838 | |
| 3839 | bool operator == (SectionNode const& other) const { |
| 3840 | return stats.sectionInfo.lineInfo == other.stats.sectionInfo.lineInfo; |
| 3841 | } |
| 3842 | bool operator == (std::shared_ptr<SectionNode> const& other) const { |
| 3843 | return operator==(*other); |
| 3844 | } |
| 3845 | |
| 3846 | SectionStats stats; |
| 3847 | using ChildSections = std::vector<std::shared_ptr<SectionNode>>; |
| 3848 | using Assertions = std::vector<AssertionStats>; |
| 3849 | ChildSections childSections; |
| 3850 | Assertions assertions; |
| 3851 | std::string stdOut; |
| 3852 | std::string stdErr; |
| 3853 | }; |
| 3854 | |
| 3855 | struct BySectionInfo { |
| 3856 | BySectionInfo( SectionInfo const& other ) : m_other( other ) {} |
| 3857 | BySectionInfo( BySectionInfo const& other ) : m_other( other.m_other ) {} |
| 3858 | bool operator() (std::shared_ptr<SectionNode> const& node) const { |
| 3859 | return ((node->stats.sectionInfo.name == m_other.name) && |
| 3860 | (node->stats.sectionInfo.lineInfo == m_other.lineInfo)); |
| 3861 | } |
| 3862 | void operator=(BySectionInfo const&) = delete; |
| 3863 | |
| 3864 | private: |
| 3865 | SectionInfo const& m_other; |
| 3866 | }; |
| 3867 | |
| 3868 | using TestCaseNode = Node<TestCaseStats, SectionNode>; |
| 3869 | using TestGroupNode = Node<TestGroupStats, TestCaseNode>; |
| 3870 | using TestRunNode = Node<TestRunStats, TestGroupNode>; |
| 3871 | |
| 3872 | CumulativeReporterBase( ReporterConfig const& _config ) |
| 3873 | : m_config( _config.fullConfig() ), |
| 3874 | stream( _config.stream() ) |
| 3875 | { |
| 3876 | m_reporterPrefs.shouldRedirectStdOut = false; |
| 3877 | if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) ) |
| 3878 | throw std::domain_error( "Verbosity level not supported by this reporter" ); |
| 3879 | } |
| 3880 | ~CumulativeReporterBase() override = default; |
| 3881 | |
| 3882 | ReporterPreferences getPreferences() const override { |
| 3883 | return m_reporterPrefs; |
| 3884 | } |
| 3885 | |
| 3886 | static std::set<Verbosity> getSupportedVerbosities() { |
| 3887 | return { Verbosity::Normal }; |
| 3888 | } |
| 3889 | |
| 3890 | void testRunStarting( TestRunInfo const& ) override {} |
| 3891 | void testGroupStarting( GroupInfo const& ) override {} |
| 3892 | |
| 3893 | void testCaseStarting( TestCaseInfo const& ) override {} |
| 3894 | |
| 3895 | void sectionStarting( SectionInfo const& sectionInfo ) override { |
| 3896 | SectionStats incompleteStats( sectionInfo, Counts(), 0, false ); |
| 3897 | std::shared_ptr<SectionNode> node; |
| 3898 | if( m_sectionStack.empty() ) { |
| 3899 | if( !m_rootSection ) |
| 3900 | m_rootSection = std::make_shared<SectionNode>( incompleteStats ); |
| 3901 | node = m_rootSection; |
| 3902 | } |
| 3903 | else { |
| 3904 | SectionNode& parentNode = *m_sectionStack.back(); |
| 3905 | auto it = |
| 3906 | std::find_if( parentNode.childSections.begin(), |
| 3907 | parentNode.childSections.end(), |
| 3908 | BySectionInfo( sectionInfo ) ); |
| 3909 | if( it == parentNode.childSections.end() ) { |
| 3910 | node = std::make_shared<SectionNode>( incompleteStats ); |
| 3911 | parentNode.childSections.push_back( node ); |
| 3912 | } |
| 3913 | else |
| 3914 | node = *it; |
| 3915 | } |
| 3916 | m_sectionStack.push_back( node ); |
| 3917 | m_deepestSection = std::move(node); |
| 3918 | } |
| 3919 | |
| 3920 | void assertionStarting(AssertionInfo const&) override {} |
| 3921 | |
| 3922 | bool assertionEnded(AssertionStats const& assertionStats) override { |
| 3923 | assert(!m_sectionStack.empty()); |
| 3924 | // AssertionResult holds a pointer to a temporary DecomposedExpression, |
| 3925 | // which getExpandedExpression() calls to build the expression string. |
| 3926 | // Our section stack copy of the assertionResult will likely outlive the |
| 3927 | // temporary, so it must be expanded or discarded now to avoid calling |
| 3928 | // a destroyed object later. |
| 3929 | prepareExpandedExpression(const_cast<AssertionResult&>( assertionStats.assertionResult ) ); |
| 3930 | SectionNode& sectionNode = *m_sectionStack.back(); |
| 3931 | sectionNode.assertions.push_back(assertionStats); |
| 3932 | return true; |
| 3933 | } |
| 3934 | void sectionEnded(SectionStats const& sectionStats) override { |
| 3935 | assert(!m_sectionStack.empty()); |
| 3936 | SectionNode& node = *m_sectionStack.back(); |
| 3937 | node.stats = sectionStats; |
| 3938 | m_sectionStack.pop_back(); |
| 3939 | } |
| 3940 | void testCaseEnded(TestCaseStats const& testCaseStats) override { |
| 3941 | auto node = std::make_shared<TestCaseNode>(testCaseStats); |
| 3942 | assert(m_sectionStack.size() == 0); |
| 3943 | node->children.push_back(m_rootSection); |
| 3944 | m_testCases.push_back(node); |
| 3945 | m_rootSection.reset(); |
| 3946 | |
| 3947 | assert(m_deepestSection); |
| 3948 | m_deepestSection->stdOut = testCaseStats.stdOut; |
| 3949 | m_deepestSection->stdErr = testCaseStats.stdErr; |
| 3950 | } |
| 3951 | void testGroupEnded(TestGroupStats const& testGroupStats) override { |
| 3952 | auto node = std::make_shared<TestGroupNode>(testGroupStats); |
| 3953 | node->children.swap(m_testCases); |
| 3954 | m_testGroups.push_back(node); |
| 3955 | } |
| 3956 | void testRunEnded(TestRunStats const& testRunStats) override { |
| 3957 | auto node = std::make_shared<TestRunNode>(testRunStats); |
| 3958 | node->children.swap(m_testGroups); |
| 3959 | m_testRuns.push_back(node); |
| 3960 | testRunEndedCumulative(); |
| 3961 | } |
| 3962 | virtual void testRunEndedCumulative() = 0; |
| 3963 | |
| 3964 | void skipTest(TestCaseInfo const&) override {} |
| 3965 | |
| 3966 | IConfigPtr m_config; |
| 3967 | std::ostream& stream; |
| 3968 | std::vector<AssertionStats> m_assertions; |
| 3969 | std::vector<std::vector<std::shared_ptr<SectionNode>>> m_sections; |
| 3970 | std::vector<std::shared_ptr<TestCaseNode>> m_testCases; |
| 3971 | std::vector<std::shared_ptr<TestGroupNode>> m_testGroups; |
| 3972 | |
| 3973 | std::vector<std::shared_ptr<TestRunNode>> m_testRuns; |
| 3974 | |
| 3975 | std::shared_ptr<SectionNode> m_rootSection; |
| 3976 | std::shared_ptr<SectionNode> m_deepestSection; |
| 3977 | std::vector<std::shared_ptr<SectionNode>> m_sectionStack; |
| 3978 | ReporterPreferences m_reporterPrefs; |
| 3979 | }; |
| 3980 | |
| 3981 | template<char C> |
| 3982 | char const* getLineOfChars() { |
| 3983 | static char line[CATCH_CONFIG_CONSOLE_WIDTH] = {0}; |
| 3984 | if( !*line ) { |
| 3985 | std::memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 ); |
| 3986 | line[CATCH_CONFIG_CONSOLE_WIDTH-1] = 0; |
| 3987 | } |
| 3988 | return line; |
| 3989 | } |
| 3990 | |
| 3991 | struct TestEventListenerBase : StreamingReporterBase<TestEventListenerBase> { |
| 3992 | TestEventListenerBase( ReporterConfig const& _config ); |
| 3993 | |
| 3994 | void assertionStarting(AssertionInfo const&) override; |
| 3995 | bool assertionEnded(AssertionStats const&) override; |
| 3996 | }; |
| 3997 | |
| 3998 | } // end namespace Catch |
| 3999 | |
| 4000 | // end catch_reporter_bases.hpp |
| 4001 | // start catch_console_colour.h |
| 4002 | |
| 4003 | namespace Catch { |
| 4004 | |
| 4005 | struct Colour { |
| 4006 | enum Code { |
| 4007 | None = 0, |
| 4008 | |
| 4009 | White, |
| 4010 | Red, |
| 4011 | Green, |
| 4012 | Blue, |
| 4013 | Cyan, |
| 4014 | Yellow, |
| 4015 | Grey, |
| 4016 | |
| 4017 | Bright = 0x10, |
| 4018 | |
| 4019 | BrightRed = Bright | Red, |
| 4020 | BrightGreen = Bright | Green, |
| 4021 | LightGrey = Bright | Grey, |
| 4022 | BrightWhite = Bright | White, |
| 4023 | BrightYellow = Bright | Yellow, |
| 4024 | |
| 4025 | // By intention |
| 4026 | FileName = LightGrey, |
| 4027 | Warning = BrightYellow, |
| 4028 | ResultError = BrightRed, |
| 4029 | ResultSuccess = BrightGreen, |
| 4030 | ResultExpectedFailure = Warning, |
| 4031 | |
| 4032 | Error = BrightRed, |
| 4033 | Success = Green, |
| 4034 | |
| 4035 | OriginalExpression = Cyan, |
| 4036 | ReconstructedExpression = BrightYellow, |
| 4037 | |
| 4038 | SecondaryText = LightGrey, |
| 4039 | Headers = White |
| 4040 | }; |
| 4041 | |
| 4042 | // Use constructed object for RAII guard |
| 4043 | Colour( Code _colourCode ); |
| 4044 | Colour( Colour&& other ) noexcept; |
| 4045 | Colour& operator=( Colour&& other ) noexcept; |
| 4046 | ~Colour(); |
| 4047 | |
| 4048 | // Use static method for one-shot changes |
| 4049 | static void use( Code _colourCode ); |
| 4050 | |
| 4051 | private: |
| 4052 | bool m_moved = false; |
| 4053 | }; |
| 4054 | |
| 4055 | std::ostream& operator << ( std::ostream& os, Colour const& ); |
| 4056 | |
| 4057 | } // end namespace Catch |
| 4058 | |
| 4059 | // end catch_console_colour.h |
| 4060 | // start catch_reporter_registrars.hpp |
| 4061 | |
| 4062 | |
| 4063 | namespace Catch { |
| 4064 | |
| 4065 | template<typename T> |
| 4066 | class ReporterRegistrar { |
| 4067 | |
| 4068 | class ReporterFactory : public IReporterFactory { |
| 4069 | |
| 4070 | virtual IStreamingReporterPtr create( ReporterConfig const& config ) const override { |
| 4071 | return std::unique_ptr<T>( new T( config ) ); |
| 4072 | } |
| 4073 | |
| 4074 | virtual std::string getDescription() const override { |
| 4075 | return T::getDescription(); |
| 4076 | } |
| 4077 | }; |
| 4078 | |
| 4079 | public: |
| 4080 | |
| 4081 | explicit ReporterRegistrar( std::string const& name ) { |
| 4082 | getMutableRegistryHub().registerReporter( name, std::make_shared<ReporterFactory>() ); |
| 4083 | } |
| 4084 | }; |
| 4085 | |
| 4086 | template<typename T> |
| 4087 | class ListenerRegistrar { |
| 4088 | |
| 4089 | class ListenerFactory : public IReporterFactory { |
| 4090 | |
| 4091 | virtual IStreamingReporterPtr create( ReporterConfig const& config ) const override { |
| 4092 | return std::unique_ptr<T>( new T( config ) ); |
| 4093 | } |
| 4094 | virtual std::string getDescription() const override { |
| 4095 | return std::string(); |
| 4096 | } |
| 4097 | }; |
| 4098 | |
| 4099 | public: |
| 4100 | |
| 4101 | ListenerRegistrar() { |
| 4102 | getMutableRegistryHub().registerListener( std::make_shared<ListenerFactory>() ); |
| 4103 | } |
| 4104 | }; |
| 4105 | } |
| 4106 | |
| 4107 | #if !defined(CATCH_CONFIG_DISABLE) |
| 4108 | |
| 4109 | #define CATCH_REGISTER_REPORTER( name, reporterType ) \ |
| 4110 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 4111 | namespace{ Catch::ReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name ); } \ |
| 4112 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS |
| 4113 | |
| 4114 | #define CATCH_REGISTER_LISTENER( listenerType ) \ |
| 4115 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ |
| 4116 | namespace{ Catch::ListenerRegistrar<listenerType> catch_internal_RegistrarFor##listenerType; } \ |
| 4117 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS |
| 4118 | #else // CATCH_CONFIG_DISABLE |
| 4119 | |
| 4120 | #define CATCH_REGISTER_REPORTER(name, reporterType) |
| 4121 | #define CATCH_REGISTER_LISTENER(listenerType) |
| 4122 | |
| 4123 | #endif // CATCH_CONFIG_DISABLE |
| 4124 | |
| 4125 | // end catch_reporter_registrars.hpp |
| 4126 | // Allow users to base their work off existing reporters |
| 4127 | // start catch_reporter_compact.h |
| 4128 | |
| 4129 | namespace Catch { |
| 4130 | |
| 4131 | struct CompactReporter : StreamingReporterBase<CompactReporter> { |
| 4132 | |
| 4133 | using StreamingReporterBase::StreamingReporterBase; |
| 4134 | |
| 4135 | ~CompactReporter() override; |
| 4136 | |
| 4137 | static std::string getDescription(); |
| 4138 | |
| 4139 | ReporterPreferences getPreferences() const override; |
| 4140 | |
| 4141 | void noMatchingTestCases(std::string const& spec) override; |
| 4142 | |
| 4143 | void assertionStarting(AssertionInfo const&) override; |
| 4144 | |
| 4145 | bool assertionEnded(AssertionStats const& _assertionStats) override; |
| 4146 | |
| 4147 | void sectionEnded(SectionStats const& _sectionStats) override; |
| 4148 | |
| 4149 | void testRunEnded(TestRunStats const& _testRunStats) override; |
| 4150 | |
| 4151 | }; |
| 4152 | |
| 4153 | } // end namespace Catch |
| 4154 | |
| 4155 | // end catch_reporter_compact.h |
| 4156 | // start catch_reporter_console.h |
| 4157 | |
| 4158 | #if defined(_MSC_VER) |
| 4159 | #pragma warning(push) |
| 4160 | #pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch |
| 4161 | // Note that 4062 (not all labels are handled |
| 4162 | // and default is missing) is enabled |
| 4163 | #endif |
| 4164 | |
| 4165 | namespace Catch { |
| 4166 | // Fwd decls |
| 4167 | struct SummaryColumn; |
| 4168 | class TablePrinter; |
| 4169 | |
| 4170 | struct ConsoleReporter : StreamingReporterBase<ConsoleReporter> { |
| 4171 | std::unique_ptr<TablePrinter> m_tablePrinter; |
| 4172 | |
| 4173 | ConsoleReporter(ReporterConfig const& config); |
| 4174 | ~ConsoleReporter() override; |
| 4175 | static std::string getDescription(); |
| 4176 | |
| 4177 | void noMatchingTestCases(std::string const& spec) override; |
| 4178 | |
| 4179 | void assertionStarting(AssertionInfo const&) override; |
| 4180 | |
| 4181 | bool assertionEnded(AssertionStats const& _assertionStats) override; |
| 4182 | |
| 4183 | void sectionStarting(SectionInfo const& _sectionInfo) override; |
| 4184 | void sectionEnded(SectionStats const& _sectionStats) override; |
| 4185 | |
| 4186 | void benchmarkStarting(BenchmarkInfo const& info) override; |
| 4187 | void benchmarkEnded(BenchmarkStats const& stats) override; |
| 4188 | |
| 4189 | void testCaseEnded(TestCaseStats const& _testCaseStats) override; |
| 4190 | void testGroupEnded(TestGroupStats const& _testGroupStats) override; |
| 4191 | void testRunEnded(TestRunStats const& _testRunStats) override; |
| 4192 | |
| 4193 | private: |
| 4194 | |
| 4195 | void lazyPrint(); |
| 4196 | |
| 4197 | void lazyPrintWithoutClosingBenchmarkTable(); |
| 4198 | void lazyPrintRunInfo(); |
| 4199 | void lazyPrintGroupInfo(); |
| 4200 | void printTestCaseAndSectionHeader(); |
| 4201 | |
| 4202 | void printClosedHeader(std::string const& _name); |
| 4203 | void printOpenHeader(std::string const& _name); |
| 4204 | |
| 4205 | // if string has a : in first line will set indent to follow it on |
| 4206 | // subsequent lines |
| 4207 | void printHeaderString(std::string const& _string, std::size_t indent = 0); |
| 4208 | |
| 4209 | void printTotals(Totals const& totals); |
| 4210 | void printSummaryRow(std::string const& label, std::vector<SummaryColumn> const& cols, std::size_t row); |
| 4211 | |
| 4212 | void printTotalsDivider(Totals const& totals); |
| 4213 | void printSummaryDivider(); |
| 4214 | |
| 4215 | private: |
| 4216 | bool m_headerPrinted = false; |
| 4217 | }; |
| 4218 | |
| 4219 | } // end namespace Catch |
| 4220 | |
| 4221 | #if defined(_MSC_VER) |
| 4222 | #pragma warning(pop) |
| 4223 | #endif |
| 4224 | |
| 4225 | // end catch_reporter_console.h |
| 4226 | // start catch_reporter_junit.h |
| 4227 | |
| 4228 | // start catch_xmlwriter.h |
| 4229 | |
| 4230 | #include <vector> |
| 4231 | |
| 4232 | namespace Catch { |
| 4233 | |
| 4234 | class XmlEncode { |
| 4235 | public: |
| 4236 | enum ForWhat { ForTextNodes, ForAttributes }; |
| 4237 | |
| 4238 | XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes ); |
| 4239 | |
| 4240 | void encodeTo( std::ostream& os ) const; |
| 4241 | |
| 4242 | friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ); |
| 4243 | |
| 4244 | private: |
| 4245 | std::string m_str; |
| 4246 | ForWhat m_forWhat; |
| 4247 | }; |
| 4248 | |
| 4249 | class XmlWriter { |
| 4250 | public: |
| 4251 | |
| 4252 | class ScopedElement { |
| 4253 | public: |
| 4254 | ScopedElement( XmlWriter* writer ); |
| 4255 | |
| 4256 | ScopedElement( ScopedElement&& other ) noexcept; |
| 4257 | ScopedElement& operator=( ScopedElement&& other ) noexcept; |
| 4258 | |
| 4259 | ~ScopedElement(); |
| 4260 | |
| 4261 | ScopedElement& writeText( std::string const& text, bool indent = true ); |
| 4262 | |
| 4263 | template<typename T> |
| 4264 | ScopedElement& writeAttribute( std::string const& name, T const& attribute ) { |
| 4265 | m_writer->writeAttribute( name, attribute ); |
| 4266 | return *this; |
| 4267 | } |
| 4268 | |
| 4269 | private: |
| 4270 | mutable XmlWriter* m_writer = nullptr; |
| 4271 | }; |
| 4272 | |
| 4273 | XmlWriter( std::ostream& os = Catch::cout() ); |
| 4274 | ~XmlWriter(); |
| 4275 | |
| 4276 | XmlWriter( XmlWriter const& ) = delete; |
| 4277 | XmlWriter& operator=( XmlWriter const& ) = delete; |
| 4278 | |
| 4279 | XmlWriter& startElement( std::string const& name ); |
| 4280 | |
| 4281 | ScopedElement scopedElement( std::string const& name ); |
| 4282 | |
| 4283 | XmlWriter& endElement(); |
| 4284 | |
| 4285 | XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ); |
| 4286 | |
| 4287 | XmlWriter& writeAttribute( std::string const& name, bool attribute ); |
| 4288 | |
| 4289 | template<typename T> |
| 4290 | XmlWriter& writeAttribute( std::string const& name, T const& attribute ) { |
| 4291 | ReusableStringStream rss; |
| 4292 | rss << attribute; |
| 4293 | return writeAttribute( name, rss.str() ); |
| 4294 | } |
| 4295 | |
| 4296 | XmlWriter& writeText( std::string const& text, bool indent = true ); |
| 4297 | |
| 4298 | XmlWriter& writeComment( std::string const& text ); |
| 4299 | |
| 4300 | void writeStylesheetRef( std::string const& url ); |
| 4301 | |
| 4302 | XmlWriter& writeBlankLine(); |
| 4303 | |
| 4304 | void ensureTagClosed(); |
| 4305 | |
| 4306 | private: |
| 4307 | |
| 4308 | void writeDeclaration(); |
| 4309 | |
| 4310 | void newlineIfNecessary(); |
| 4311 | |
| 4312 | bool m_tagIsOpen = false; |
| 4313 | bool m_needsNewline = false; |
| 4314 | std::vector<std::string> m_tags; |
| 4315 | std::string m_indent; |
| 4316 | std::ostream& m_os; |
| 4317 | }; |
| 4318 | |
| 4319 | } |
| 4320 | |
| 4321 | // end catch_xmlwriter.h |
| 4322 | namespace Catch { |
| 4323 | |
| 4324 | class JunitReporter : public CumulativeReporterBase<JunitReporter> { |
| 4325 | public: |
| 4326 | JunitReporter(ReporterConfig const& _config); |
| 4327 | |
| 4328 | ~JunitReporter() override; |
| 4329 | |
| 4330 | static std::string getDescription(); |
| 4331 | |
| 4332 | void noMatchingTestCases(std::string const& /*spec*/) override; |
| 4333 | |
| 4334 | void testRunStarting(TestRunInfo const& runInfo) override; |
| 4335 | |
| 4336 | void testGroupStarting(GroupInfo const& groupInfo) override; |
| 4337 | |
| 4338 | void testCaseStarting(TestCaseInfo const& testCaseInfo) override; |
| 4339 | bool assertionEnded(AssertionStats const& assertionStats) override; |
| 4340 | |
| 4341 | void testCaseEnded(TestCaseStats const& testCaseStats) override; |
| 4342 | |
| 4343 | void testGroupEnded(TestGroupStats const& testGroupStats) override; |
| 4344 | |
| 4345 | void testRunEndedCumulative() override; |
| 4346 | |
| 4347 | void writeGroup(TestGroupNode const& groupNode, double suiteTime); |
| 4348 | |
| 4349 | void writeTestCase(TestCaseNode const& testCaseNode); |
| 4350 | |
| 4351 | void writeSection(std::string const& className, |
| 4352 | std::string const& rootName, |
| 4353 | SectionNode const& sectionNode); |
| 4354 | |
| 4355 | void writeAssertions(SectionNode const& sectionNode); |
| 4356 | void writeAssertion(AssertionStats const& stats); |
| 4357 | |
| 4358 | XmlWriter xml; |
| 4359 | Timer suiteTimer; |
| 4360 | std::string stdOutForSuite; |
| 4361 | std::string stdErrForSuite; |
| 4362 | unsigned int unexpectedExceptions = 0; |
| 4363 | bool m_okToFail = false; |
| 4364 | }; |
| 4365 | |
| 4366 | } // end namespace Catch |
| 4367 | |
| 4368 | // end catch_reporter_junit.h |
| 4369 | // start catch_reporter_xml.h |
| 4370 | |
| 4371 | namespace Catch { |
| 4372 | class XmlReporter : public StreamingReporterBase<XmlReporter> { |
| 4373 | public: |
| 4374 | XmlReporter(ReporterConfig const& _config); |
| 4375 | |
| 4376 | ~XmlReporter() override; |
| 4377 | |
| 4378 | static std::string getDescription(); |
| 4379 | |
| 4380 | virtual std::string getStylesheetRef() const; |
| 4381 | |
| 4382 | void writeSourceInfo(SourceLineInfo const& sourceInfo); |
| 4383 | |
| 4384 | public: // StreamingReporterBase |
| 4385 | |
| 4386 | void noMatchingTestCases(std::string const& s) override; |
| 4387 | |
| 4388 | void testRunStarting(TestRunInfo const& testInfo) override; |
| 4389 | |
| 4390 | void testGroupStarting(GroupInfo const& groupInfo) override; |
| 4391 | |
| 4392 | void testCaseStarting(TestCaseInfo const& testInfo) override; |
| 4393 | |
| 4394 | void sectionStarting(SectionInfo const& sectionInfo) override; |
| 4395 | |
| 4396 | void assertionStarting(AssertionInfo const&) override; |
| 4397 | |
| 4398 | bool assertionEnded(AssertionStats const& assertionStats) override; |
| 4399 | |
| 4400 | void sectionEnded(SectionStats const& sectionStats) override; |
| 4401 | |
| 4402 | void testCaseEnded(TestCaseStats const& testCaseStats) override; |
| 4403 | |
| 4404 | void testGroupEnded(TestGroupStats const& testGroupStats) override; |
| 4405 | |
| 4406 | void testRunEnded(TestRunStats const& testRunStats) override; |
| 4407 | |
| 4408 | private: |
| 4409 | Timer m_testCaseTimer; |
| 4410 | XmlWriter m_xml; |
| 4411 | int m_sectionDepth = 0; |
| 4412 | }; |
| 4413 | |
| 4414 | } // end namespace Catch |
| 4415 | |
| 4416 | // end catch_reporter_xml.h |
| 4417 | |
| 4418 | // end catch_external_interfaces.h |
| 4419 | #endif |
| 4420 | |
| 4421 | #endif // ! CATCH_CONFIG_IMPL_ONLY |
| 4422 | |
| 4423 | #ifdef CATCH_IMPL |
| 4424 | // start catch_impl.hpp |
| 4425 | |
| 4426 | #ifdef __clang__ |
| 4427 | #pragma clang diagnostic push |
| 4428 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 4429 | #endif |
| 4430 | |
| 4431 | // Keep these here for external reporters |
| 4432 | // start catch_test_case_tracker.h |
| 4433 | |
| 4434 | #include <string> |
| 4435 | #include <vector> |
| 4436 | #include <memory> |
| 4437 | |
| 4438 | namespace Catch { |
| 4439 | namespace TestCaseTracking { |
| 4440 | |
| 4441 | struct NameAndLocation { |
| 4442 | std::string name; |
| 4443 | SourceLineInfo location; |
| 4444 | |
| 4445 | NameAndLocation( std::string const& _name, SourceLineInfo const& _location ); |
| 4446 | }; |
| 4447 | |
| 4448 | struct ITracker; |
| 4449 | |
| 4450 | using ITrackerPtr = std::shared_ptr<ITracker>; |
| 4451 | |
| 4452 | struct ITracker { |
| 4453 | virtual ~ITracker(); |
| 4454 | |
| 4455 | // static queries |
| 4456 | virtual NameAndLocation const& nameAndLocation() const = 0; |
| 4457 | |
| 4458 | // dynamic queries |
| 4459 | virtual bool isComplete() const = 0; // Successfully completed or failed |
| 4460 | virtual bool isSuccessfullyCompleted() const = 0; |
| 4461 | virtual bool isOpen() const = 0; // Started but not complete |
| 4462 | virtual bool hasChildren() const = 0; |
| 4463 | |
| 4464 | virtual ITracker& parent() = 0; |
| 4465 | |
| 4466 | // actions |
| 4467 | virtual void close() = 0; // Successfully complete |
| 4468 | virtual void fail() = 0; |
| 4469 | virtual void markAsNeedingAnotherRun() = 0; |
| 4470 | |
| 4471 | virtual void addChild( ITrackerPtr const& child ) = 0; |
| 4472 | virtual ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) = 0; |
| 4473 | virtual void openChild() = 0; |
| 4474 | |
| 4475 | // Debug/ checking |
| 4476 | virtual bool isSectionTracker() const = 0; |
| 4477 | virtual bool isIndexTracker() const = 0; |
| 4478 | }; |
| 4479 | |
| 4480 | class TrackerContext { |
| 4481 | |
| 4482 | enum RunState { |
| 4483 | NotStarted, |
| 4484 | Executing, |
| 4485 | CompletedCycle |
| 4486 | }; |
| 4487 | |
| 4488 | ITrackerPtr m_rootTracker; |
| 4489 | ITracker* m_currentTracker = nullptr; |
| 4490 | RunState m_runState = NotStarted; |
| 4491 | |
| 4492 | public: |
| 4493 | |
| 4494 | static TrackerContext& instance(); |
| 4495 | |
| 4496 | ITracker& startRun(); |
| 4497 | void endRun(); |
| 4498 | |
| 4499 | void startCycle(); |
| 4500 | void completeCycle(); |
| 4501 | |
| 4502 | bool completedCycle() const; |
| 4503 | ITracker& currentTracker(); |
| 4504 | void setCurrentTracker( ITracker* tracker ); |
| 4505 | }; |
| 4506 | |
| 4507 | class TrackerBase : public ITracker { |
| 4508 | protected: |
| 4509 | enum CycleState { |
| 4510 | NotStarted, |
| 4511 | Executing, |
| 4512 | ExecutingChildren, |
| 4513 | NeedsAnotherRun, |
| 4514 | CompletedSuccessfully, |
| 4515 | Failed |
| 4516 | }; |
| 4517 | |
| 4518 | class TrackerHasName { |
| 4519 | NameAndLocation m_nameAndLocation; |
| 4520 | public: |
| 4521 | TrackerHasName( NameAndLocation const& nameAndLocation ); |
| 4522 | bool operator ()( ITrackerPtr const& tracker ) const; |
| 4523 | }; |
| 4524 | |
| 4525 | using Children = std::vector<ITrackerPtr>; |
| 4526 | NameAndLocation m_nameAndLocation; |
| 4527 | TrackerContext& m_ctx; |
| 4528 | ITracker* m_parent; |
| 4529 | Children m_children; |
| 4530 | CycleState m_runState = NotStarted; |
| 4531 | |
| 4532 | public: |
| 4533 | TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); |
| 4534 | |
| 4535 | NameAndLocation const& nameAndLocation() const override; |
| 4536 | bool isComplete() const override; |
| 4537 | bool isSuccessfullyCompleted() const override; |
| 4538 | bool isOpen() const override; |
| 4539 | bool hasChildren() const override; |
| 4540 | |
| 4541 | void addChild( ITrackerPtr const& child ) override; |
| 4542 | |
| 4543 | ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) override; |
| 4544 | ITracker& parent() override; |
| 4545 | |
| 4546 | void openChild() override; |
| 4547 | |
| 4548 | bool isSectionTracker() const override; |
| 4549 | bool isIndexTracker() const override; |
| 4550 | |
| 4551 | void open(); |
| 4552 | |
| 4553 | void close() override; |
| 4554 | void fail() override; |
| 4555 | void markAsNeedingAnotherRun() override; |
| 4556 | |
| 4557 | private: |
| 4558 | void moveToParent(); |
| 4559 | void moveToThis(); |
| 4560 | }; |
| 4561 | |
| 4562 | class SectionTracker : public TrackerBase { |
| 4563 | std::vector<std::string> m_filters; |
| 4564 | public: |
| 4565 | SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); |
| 4566 | |
| 4567 | bool isSectionTracker() const override; |
| 4568 | |
| 4569 | static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ); |
| 4570 | |
| 4571 | void tryOpen(); |
| 4572 | |
| 4573 | void addInitialFilters( std::vector<std::string> const& filters ); |
| 4574 | void addNextFilters( std::vector<std::string> const& filters ); |
| 4575 | }; |
| 4576 | |
| 4577 | class IndexTracker : public TrackerBase { |
| 4578 | int m_size; |
| 4579 | int m_index = -1; |
| 4580 | public: |
| 4581 | IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size ); |
| 4582 | |
| 4583 | bool isIndexTracker() const override; |
| 4584 | void close() override; |
| 4585 | |
| 4586 | static IndexTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ); |
| 4587 | |
| 4588 | int index() const; |
| 4589 | |
| 4590 | void moveNext(); |
| 4591 | }; |
| 4592 | |
| 4593 | } // namespace TestCaseTracking |
| 4594 | |
| 4595 | using TestCaseTracking::ITracker; |
| 4596 | using TestCaseTracking::TrackerContext; |
| 4597 | using TestCaseTracking::SectionTracker; |
| 4598 | using TestCaseTracking::IndexTracker; |
| 4599 | |
| 4600 | } // namespace Catch |
| 4601 | |
| 4602 | // end catch_test_case_tracker.h |
| 4603 | |
| 4604 | // start catch_leak_detector.h |
| 4605 | |
| 4606 | namespace Catch { |
| 4607 | |
| 4608 | struct LeakDetector { |
| 4609 | LeakDetector(); |
| 4610 | }; |
| 4611 | |
| 4612 | } |
| 4613 | // end catch_leak_detector.h |
| 4614 | // Cpp files will be included in the single-header file here |
| 4615 | // start catch_approx.cpp |
| 4616 | |
| 4617 | #include <cmath> |
| 4618 | #include <limits> |
| 4619 | |
| 4620 | namespace { |
| 4621 | |
| 4622 | // Performs equivalent check of std::fabs(lhs - rhs) <= margin |
| 4623 | // But without the subtraction to allow for INFINITY in comparison |
| 4624 | bool marginComparison(double lhs, double rhs, double margin) { |
| 4625 | return (lhs + margin >= rhs) && (rhs + margin >= lhs); |
| 4626 | } |
| 4627 | |
| 4628 | } |
| 4629 | |
| 4630 | namespace Catch { |
| 4631 | namespace Detail { |
| 4632 | |
| 4633 | Approx::Approx ( double value ) |
| 4634 | : m_epsilon( std::numeric_limits<float>::epsilon()*100 ), |
| 4635 | m_margin( 0.0 ), |
| 4636 | m_scale( 0.0 ), |
| 4637 | m_value( value ) |
| 4638 | {} |
| 4639 | |
| 4640 | Approx Approx::custom() { |
| 4641 | return Approx( 0 ); |
| 4642 | } |
| 4643 | |
| 4644 | std::string Approx::toString() const { |
| 4645 | ReusableStringStream rss; |
| 4646 | rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )"; |
| 4647 | return rss.str(); |
| 4648 | } |
| 4649 | |
| 4650 | bool Approx::equalityComparisonImpl(const double other) const { |
| 4651 | // First try with fixed margin, then compute margin based on epsilon, scale and Approx's value |
| 4652 | // Thanks to Richard Harris for his help refining the scaled margin value |
| 4653 | return marginComparison(m_value, other, m_margin) || marginComparison(m_value, other, m_epsilon * (m_scale + std::fabs(m_value))); |
| 4654 | } |
| 4655 | |
| 4656 | } // end namespace Detail |
| 4657 | |
| 4658 | std::string StringMaker<Catch::Detail::Approx>::convert(Catch::Detail::Approx const& value) { |
| 4659 | return value.toString(); |
| 4660 | } |
| 4661 | |
| 4662 | } // end namespace Catch |
| 4663 | // end catch_approx.cpp |
| 4664 | // start catch_assertionhandler.cpp |
| 4665 | |
| 4666 | // start catch_context.h |
| 4667 | |
| 4668 | #include <memory> |
| 4669 | |
| 4670 | namespace Catch { |
| 4671 | |
| 4672 | struct IResultCapture; |
| 4673 | struct IRunner; |
| 4674 | struct IConfig; |
| 4675 | struct IMutableContext; |
| 4676 | |
| 4677 | using IConfigPtr = std::shared_ptr<IConfig const>; |
| 4678 | |
| 4679 | struct IContext |
| 4680 | { |
| 4681 | virtual ~IContext(); |
| 4682 | |
| 4683 | virtual IResultCapture* getResultCapture() = 0; |
| 4684 | virtual IRunner* getRunner() = 0; |
| 4685 | virtual IConfigPtr const& getConfig() const = 0; |
| 4686 | }; |
| 4687 | |
| 4688 | struct IMutableContext : IContext |
| 4689 | { |
| 4690 | virtual ~IMutableContext(); |
| 4691 | virtual void setResultCapture( IResultCapture* resultCapture ) = 0; |
| 4692 | virtual void setRunner( IRunner* runner ) = 0; |
| 4693 | virtual void setConfig( IConfigPtr const& config ) = 0; |
| 4694 | |
| 4695 | private: |
| 4696 | static IMutableContext *currentContext; |
| 4697 | friend IMutableContext& getCurrentMutableContext(); |
| 4698 | friend void cleanUpContext(); |
| 4699 | static void createContext(); |
| 4700 | }; |
| 4701 | |
| 4702 | inline IMutableContext& getCurrentMutableContext() |
| 4703 | { |
| 4704 | if( !IMutableContext::currentContext ) |
| 4705 | IMutableContext::createContext(); |
| 4706 | return *IMutableContext::currentContext; |
| 4707 | } |
| 4708 | |
| 4709 | inline IContext& getCurrentContext() |
| 4710 | { |
| 4711 | return getCurrentMutableContext(); |
| 4712 | } |
| 4713 | |
| 4714 | void cleanUpContext(); |
| 4715 | } |
| 4716 | |
| 4717 | // end catch_context.h |
| 4718 | // start catch_debugger.h |
| 4719 | |
| 4720 | namespace Catch { |
| 4721 | bool isDebuggerActive(); |
| 4722 | } |
| 4723 | |
| 4724 | #ifdef CATCH_PLATFORM_MAC |
| 4725 | |
| 4726 | #define CATCH_TRAP() __asm__("int $3\n" : : ) /* NOLINT */ |
| 4727 | |
| 4728 | #elif defined(CATCH_PLATFORM_LINUX) |
| 4729 | // If we can use inline assembler, do it because this allows us to break |
| 4730 | // directly at the location of the failing check instead of breaking inside |
| 4731 | // raise() called from it, i.e. one stack frame below. |
| 4732 | #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) |
| 4733 | #define CATCH_TRAP() asm volatile ("int $3") /* NOLINT */ |
| 4734 | #else // Fall back to the generic way. |
| 4735 | #include <signal.h> |
| 4736 | |
| 4737 | #define CATCH_TRAP() raise(SIGTRAP) |
| 4738 | #endif |
| 4739 | #elif defined(_MSC_VER) |
| 4740 | #define CATCH_TRAP() __debugbreak() |
| 4741 | #elif defined(__MINGW32__) |
| 4742 | extern "C" __declspec(dllimport) void __stdcall DebugBreak(); |
| 4743 | #define CATCH_TRAP() DebugBreak() |
| 4744 | #endif |
| 4745 | |
| 4746 | #ifdef CATCH_TRAP |
| 4747 | #define CATCH_BREAK_INTO_DEBUGGER() if( Catch::isDebuggerActive() ) { CATCH_TRAP(); } |
| 4748 | #else |
| 4749 | namespace Catch { |
| 4750 | inline void doNothing() {} |
| 4751 | } |
| 4752 | #define CATCH_BREAK_INTO_DEBUGGER() Catch::doNothing() |
| 4753 | #endif |
| 4754 | |
| 4755 | // end catch_debugger.h |
| 4756 | // start catch_run_context.h |
| 4757 | |
| 4758 | // start catch_fatal_condition.h |
| 4759 | |
| 4760 | // start catch_windows_h_proxy.h |
| 4761 | |
| 4762 | |
| 4763 | #if defined(CATCH_PLATFORM_WINDOWS) |
| 4764 | |
| 4765 | #if !defined(NOMINMAX) && !defined(CATCH_CONFIG_NO_NOMINMAX) |
| 4766 | # define CATCH_DEFINED_NOMINMAX |
| 4767 | # define NOMINMAX |
| 4768 | #endif |
| 4769 | #if !defined(WIN32_LEAN_AND_MEAN) && !defined(CATCH_CONFIG_NO_WIN32_LEAN_AND_MEAN) |
| 4770 | # define CATCH_DEFINED_WIN32_LEAN_AND_MEAN |
| 4771 | # define WIN32_LEAN_AND_MEAN |
| 4772 | #endif |
| 4773 | |
| 4774 | #ifdef __AFXDLL |
| 4775 | #include <AfxWin.h> |
| 4776 | #else |
| 4777 | #include <windows.h> |
| 4778 | #endif |
| 4779 | |
| 4780 | #ifdef CATCH_DEFINED_NOMINMAX |
| 4781 | # undef NOMINMAX |
| 4782 | #endif |
| 4783 | #ifdef CATCH_DEFINED_WIN32_LEAN_AND_MEAN |
| 4784 | # undef WIN32_LEAN_AND_MEAN |
| 4785 | #endif |
| 4786 | |
| 4787 | #endif // defined(CATCH_PLATFORM_WINDOWS) |
| 4788 | |
| 4789 | // end catch_windows_h_proxy.h |
| 4790 | #if defined( CATCH_CONFIG_WINDOWS_SEH ) |
| 4791 | |
| 4792 | namespace Catch { |
| 4793 | |
| 4794 | struct FatalConditionHandler { |
| 4795 | |
| 4796 | static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo); |
| 4797 | FatalConditionHandler(); |
| 4798 | static void reset(); |
| 4799 | ~FatalConditionHandler(); |
| 4800 | |
| 4801 | private: |
| 4802 | static bool isSet; |
| 4803 | static ULONG guaranteeSize; |
| 4804 | static PVOID exceptionHandlerHandle; |
| 4805 | }; |
| 4806 | |
| 4807 | } // namespace Catch |
| 4808 | |
| 4809 | #elif defined ( CATCH_CONFIG_POSIX_SIGNALS ) |
| 4810 | |
| 4811 | #include <signal.h> |
| 4812 | |
| 4813 | namespace Catch { |
| 4814 | |
| 4815 | struct FatalConditionHandler { |
| 4816 | |
| 4817 | static bool isSet; |
| 4818 | static struct sigaction oldSigActions[]; |
| 4819 | static stack_t oldSigStack; |
| 4820 | static char altStackMem[]; |
| 4821 | |
| 4822 | static void handleSignal( int sig ); |
| 4823 | |
| 4824 | FatalConditionHandler(); |
| 4825 | ~FatalConditionHandler(); |
| 4826 | static void reset(); |
| 4827 | }; |
| 4828 | |
| 4829 | } // namespace Catch |
| 4830 | |
| 4831 | #else |
| 4832 | |
| 4833 | namespace Catch { |
| 4834 | struct FatalConditionHandler { |
| 4835 | void reset(); |
| 4836 | }; |
| 4837 | } |
| 4838 | |
| 4839 | #endif |
| 4840 | |
| 4841 | // end catch_fatal_condition.h |
| 4842 | #include <string> |
| 4843 | |
| 4844 | namespace Catch { |
| 4845 | |
| 4846 | struct IMutableContext; |
| 4847 | |
| 4848 | /////////////////////////////////////////////////////////////////////////// |
| 4849 | |
| 4850 | class RunContext : public IResultCapture, public IRunner { |
| 4851 | |
| 4852 | public: |
| 4853 | RunContext( RunContext const& ) = delete; |
| 4854 | RunContext& operator =( RunContext const& ) = delete; |
| 4855 | |
| 4856 | explicit RunContext( IConfigPtr const& _config, IStreamingReporterPtr&& reporter ); |
| 4857 | |
| 4858 | ~RunContext() override; |
| 4859 | |
| 4860 | void testGroupStarting( std::string const& testSpec, std::size_t groupIndex, std::size_t groupsCount ); |
| 4861 | void testGroupEnded( std::string const& testSpec, Totals const& totals, std::size_t groupIndex, std::size_t groupsCount ); |
| 4862 | |
| 4863 | Totals runTest(TestCase const& testCase); |
| 4864 | |
| 4865 | IConfigPtr config() const; |
| 4866 | IStreamingReporter& reporter() const; |
| 4867 | |
| 4868 | public: // IResultCapture |
| 4869 | |
| 4870 | // Assertion handlers |
| 4871 | void handleExpr |
| 4872 | ( AssertionInfo const& info, |
| 4873 | ITransientExpression const& expr, |
| 4874 | AssertionReaction& reaction ) override; |
| 4875 | void handleMessage |
| 4876 | ( AssertionInfo const& info, |
| 4877 | ResultWas::OfType resultType, |
| 4878 | StringRef const& message, |
| 4879 | AssertionReaction& reaction ) override; |
| 4880 | void handleUnexpectedExceptionNotThrown |
| 4881 | ( AssertionInfo const& info, |
| 4882 | AssertionReaction& reaction ) override; |
| 4883 | void handleUnexpectedInflightException |
| 4884 | ( AssertionInfo const& info, |
| 4885 | std::string const& message, |
| 4886 | AssertionReaction& reaction ) override; |
| 4887 | void handleIncomplete |
| 4888 | ( AssertionInfo const& info ) override; |
| 4889 | void handleNonExpr |
| 4890 | ( AssertionInfo const &info, |
| 4891 | ResultWas::OfType resultType, |
| 4892 | AssertionReaction &reaction ) override; |
| 4893 | |
| 4894 | bool sectionStarted( SectionInfo const& sectionInfo, Counts& assertions ) override; |
| 4895 | |
| 4896 | void sectionEnded( SectionEndInfo const& endInfo ) override; |
| 4897 | void sectionEndedEarly( SectionEndInfo const& endInfo ) override; |
| 4898 | |
| 4899 | void benchmarkStarting( BenchmarkInfo const& info ) override; |
| 4900 | void benchmarkEnded( BenchmarkStats const& stats ) override; |
| 4901 | |
| 4902 | void pushScopedMessage( MessageInfo const& message ) override; |
| 4903 | void popScopedMessage( MessageInfo const& message ) override; |
| 4904 | |
| 4905 | std::string getCurrentTestName() const override; |
| 4906 | |
| 4907 | const AssertionResult* getLastResult() const override; |
| 4908 | |
| 4909 | void exceptionEarlyReported() override; |
| 4910 | |
| 4911 | void handleFatalErrorCondition( StringRef message ) override; |
| 4912 | |
| 4913 | bool lastAssertionPassed() override; |
| 4914 | |
| 4915 | void assertionPassed() override; |
| 4916 | |
| 4917 | public: |
| 4918 | // !TBD We need to do this another way! |
| 4919 | bool aborting() const final; |
| 4920 | |
| 4921 | private: |
| 4922 | |
| 4923 | void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ); |
| 4924 | void invokeActiveTestCase(); |
| 4925 | |
| 4926 | void resetAssertionInfo(); |
| 4927 | bool testForMissingAssertions( Counts& assertions ); |
| 4928 | |
| 4929 | void assertionEnded( AssertionResult const& result ); |
| 4930 | void reportExpr |
| 4931 | ( AssertionInfo const &info, |
| 4932 | ResultWas::OfType resultType, |
| 4933 | ITransientExpression const *expr, |
| 4934 | bool negated ); |
| 4935 | |
| 4936 | void populateReaction( AssertionReaction& reaction ); |
| 4937 | |
| 4938 | private: |
| 4939 | |
| 4940 | void handleUnfinishedSections(); |
| 4941 | |
| 4942 | TestRunInfo m_runInfo; |
| 4943 | IMutableContext& m_context; |
| 4944 | TestCase const* m_activeTestCase = nullptr; |
| 4945 | ITracker* m_testCaseTracker; |
| 4946 | Option<AssertionResult> m_lastResult; |
| 4947 | |
| 4948 | IConfigPtr m_config; |
| 4949 | Totals m_totals; |
| 4950 | IStreamingReporterPtr m_reporter; |
| 4951 | std::vector<MessageInfo> m_messages; |
| 4952 | AssertionInfo m_lastAssertionInfo; |
| 4953 | std::vector<SectionEndInfo> m_unfinishedSections; |
| 4954 | std::vector<ITracker*> m_activeSections; |
| 4955 | TrackerContext m_trackerContext; |
| 4956 | bool m_lastAssertionPassed = false; |
| 4957 | bool m_shouldReportUnexpected = true; |
| 4958 | bool m_includeSuccessfulResults; |
| 4959 | }; |
| 4960 | |
| 4961 | } // end namespace Catch |
| 4962 | |
| 4963 | // end catch_run_context.h |
| 4964 | namespace Catch { |
| 4965 | |
| 4966 | auto operator <<( std::ostream& os, ITransientExpression const& expr ) -> std::ostream& { |
| 4967 | expr.streamReconstructedExpression( os ); |
| 4968 | return os; |
| 4969 | } |
| 4970 | |
| 4971 | LazyExpression::LazyExpression( bool isNegated ) |
| 4972 | : m_isNegated( isNegated ) |
| 4973 | {} |
| 4974 | |
| 4975 | LazyExpression::LazyExpression( LazyExpression const& other ) : m_isNegated( other.m_isNegated ) {} |
| 4976 | |
| 4977 | LazyExpression::operator bool() const { |
| 4978 | return m_transientExpression != nullptr; |
| 4979 | } |
| 4980 | |
| 4981 | auto operator << ( std::ostream& os, LazyExpression const& lazyExpr ) -> std::ostream& { |
| 4982 | if( lazyExpr.m_isNegated ) |
| 4983 | os << "!"; |
| 4984 | |
| 4985 | if( lazyExpr ) { |
| 4986 | if( lazyExpr.m_isNegated && lazyExpr.m_transientExpression->isBinaryExpression() ) |
| 4987 | os << "(" << *lazyExpr.m_transientExpression << ")"; |
| 4988 | else |
| 4989 | os << *lazyExpr.m_transientExpression; |
| 4990 | } |
| 4991 | else { |
| 4992 | os << "{** error - unchecked empty expression requested **}"; |
| 4993 | } |
| 4994 | return os; |
| 4995 | } |
| 4996 | |
| 4997 | AssertionHandler::AssertionHandler |
| 4998 | ( StringRef macroName, |
| 4999 | SourceLineInfo const& lineInfo, |
| 5000 | StringRef capturedExpression, |
| 5001 | ResultDisposition::Flags resultDisposition ) |
| 5002 | : m_assertionInfo{ macroName, lineInfo, capturedExpression, resultDisposition }, |
| 5003 | m_resultCapture( getResultCapture() ) |
| 5004 | {} |
| 5005 | |
| 5006 | void AssertionHandler::handleExpr( ITransientExpression const& expr ) { |
| 5007 | m_resultCapture.handleExpr( m_assertionInfo, expr, m_reaction ); |
| 5008 | } |
| 5009 | void AssertionHandler::handleMessage(ResultWas::OfType resultType, StringRef const& message) { |
| 5010 | m_resultCapture.handleMessage( m_assertionInfo, resultType, message, m_reaction ); |
| 5011 | } |
| 5012 | |
| 5013 | auto AssertionHandler::allowThrows() const -> bool { |
| 5014 | return getCurrentContext().getConfig()->allowThrows(); |
| 5015 | } |
| 5016 | |
| 5017 | void AssertionHandler::complete() { |
| 5018 | setCompleted(); |
| 5019 | if( m_reaction.shouldDebugBreak ) { |
| 5020 | |
| 5021 | // If you find your debugger stopping you here then go one level up on the |
| 5022 | // call-stack for the code that caused it (typically a failed assertion) |
| 5023 | |
| 5024 | // (To go back to the test and change execution, jump over the throw, next) |
| 5025 | CATCH_BREAK_INTO_DEBUGGER(); |
| 5026 | } |
| 5027 | if( m_reaction.shouldThrow ) |
| 5028 | throw Catch::TestFailureException(); |
| 5029 | } |
| 5030 | void AssertionHandler::setCompleted() { |
| 5031 | m_completed = true; |
| 5032 | } |
| 5033 | |
| 5034 | void AssertionHandler::handleUnexpectedInflightException() { |
| 5035 | m_resultCapture.handleUnexpectedInflightException( m_assertionInfo, Catch::translateActiveException(), m_reaction ); |
| 5036 | } |
| 5037 | |
| 5038 | void AssertionHandler::handleExceptionThrownAsExpected() { |
| 5039 | m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); |
| 5040 | } |
| 5041 | void AssertionHandler::handleExceptionNotThrownAsExpected() { |
| 5042 | m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); |
| 5043 | } |
| 5044 | |
| 5045 | void AssertionHandler::handleUnexpectedExceptionNotThrown() { |
| 5046 | m_resultCapture.handleUnexpectedExceptionNotThrown( m_assertionInfo, m_reaction ); |
| 5047 | } |
| 5048 | |
| 5049 | void AssertionHandler::handleThrowingCallSkipped() { |
| 5050 | m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); |
| 5051 | } |
| 5052 | |
| 5053 | // This is the overload that takes a string and infers the Equals matcher from it |
| 5054 | // The more general overload, that takes any string matcher, is in catch_capture_matchers.cpp |
| 5055 | void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef matcherString ) { |
| 5056 | handleExceptionMatchExpr( handler, Matchers::Equals( str ), matcherString ); |
| 5057 | } |
| 5058 | |
| 5059 | } // namespace Catch |
| 5060 | // end catch_assertionhandler.cpp |
| 5061 | // start catch_assertionresult.cpp |
| 5062 | |
| 5063 | namespace Catch { |
| 5064 | AssertionResultData::AssertionResultData(ResultWas::OfType _resultType, LazyExpression const & _lazyExpression): |
| 5065 | lazyExpression(_lazyExpression), |
| 5066 | resultType(_resultType) {} |
| 5067 | |
| 5068 | std::string AssertionResultData::reconstructExpression() const { |
| 5069 | |
| 5070 | if( reconstructedExpression.empty() ) { |
| 5071 | if( lazyExpression ) { |
| 5072 | ReusableStringStream rss; |
| 5073 | rss << lazyExpression; |
| 5074 | reconstructedExpression = rss.str(); |
| 5075 | } |
| 5076 | } |
| 5077 | return reconstructedExpression; |
| 5078 | } |
| 5079 | |
| 5080 | AssertionResult::AssertionResult( AssertionInfo const& info, AssertionResultData const& data ) |
| 5081 | : m_info( info ), |
| 5082 | m_resultData( data ) |
| 5083 | {} |
| 5084 | |
| 5085 | // Result was a success |
| 5086 | bool AssertionResult::succeeded() const { |
| 5087 | return Catch::isOk( m_resultData.resultType ); |
| 5088 | } |
| 5089 | |
| 5090 | // Result was a success, or failure is suppressed |
| 5091 | bool AssertionResult::isOk() const { |
| 5092 | return Catch::isOk( m_resultData.resultType ) || shouldSuppressFailure( m_info.resultDisposition ); |
| 5093 | } |
| 5094 | |
| 5095 | ResultWas::OfType AssertionResult::getResultType() const { |
| 5096 | return m_resultData.resultType; |
| 5097 | } |
| 5098 | |
| 5099 | bool AssertionResult::hasExpression() const { |
| 5100 | return m_info.capturedExpression[0] != 0; |
| 5101 | } |
| 5102 | |
| 5103 | bool AssertionResult::hasMessage() const { |
| 5104 | return !m_resultData.message.empty(); |
| 5105 | } |
| 5106 | |
| 5107 | std::string AssertionResult::getExpression() const { |
| 5108 | if( isFalseTest( m_info.resultDisposition ) ) |
| 5109 | return "!(" + m_info.capturedExpression + ")"; |
| 5110 | else |
| 5111 | return m_info.capturedExpression; |
| 5112 | } |
| 5113 | |
| 5114 | std::string AssertionResult::getExpressionInMacro() const { |
| 5115 | std::string expr; |
| 5116 | if( m_info.macroName[0] == 0 ) |
| 5117 | expr = m_info.capturedExpression; |
| 5118 | else { |
| 5119 | expr.reserve( m_info.macroName.size() + m_info.capturedExpression.size() + 4 ); |
| 5120 | expr += m_info.macroName; |
| 5121 | expr += "( "; |
| 5122 | expr += m_info.capturedExpression; |
| 5123 | expr += " )"; |
| 5124 | } |
| 5125 | return expr; |
| 5126 | } |
| 5127 | |
| 5128 | bool AssertionResult::hasExpandedExpression() const { |
| 5129 | return hasExpression() && getExpandedExpression() != getExpression(); |
| 5130 | } |
| 5131 | |
| 5132 | std::string AssertionResult::getExpandedExpression() const { |
| 5133 | std::string expr = m_resultData.reconstructExpression(); |
| 5134 | return expr.empty() |
| 5135 | ? getExpression() |
| 5136 | : expr; |
| 5137 | } |
| 5138 | |
| 5139 | std::string AssertionResult::getMessage() const { |
| 5140 | return m_resultData.message; |
| 5141 | } |
| 5142 | SourceLineInfo AssertionResult::getSourceInfo() const { |
| 5143 | return m_info.lineInfo; |
| 5144 | } |
| 5145 | |
| 5146 | StringRef AssertionResult::getTestMacroName() const { |
| 5147 | return m_info.macroName; |
| 5148 | } |
| 5149 | |
| 5150 | } // end namespace Catch |
| 5151 | // end catch_assertionresult.cpp |
| 5152 | // start catch_benchmark.cpp |
| 5153 | |
| 5154 | namespace Catch { |
| 5155 | |
| 5156 | auto BenchmarkLooper::getResolution() -> uint64_t { |
| 5157 | return getEstimatedClockResolution() * getCurrentContext().getConfig()->benchmarkResolutionMultiple(); |
| 5158 | } |
| 5159 | |
| 5160 | void BenchmarkLooper::reportStart() { |
| 5161 | getResultCapture().benchmarkStarting( { m_name } ); |
| 5162 | } |
| 5163 | auto BenchmarkLooper::needsMoreIterations() -> bool { |
| 5164 | auto elapsed = m_timer.getElapsedNanoseconds(); |
| 5165 | |
| 5166 | // Exponentially increasing iterations until we're confident in our timer resolution |
| 5167 | if( elapsed < m_resolution ) { |
| 5168 | m_iterationsToRun *= 10; |
| 5169 | return true; |
| 5170 | } |
| 5171 | |
| 5172 | getResultCapture().benchmarkEnded( { { m_name }, m_count, elapsed } ); |
| 5173 | return false; |
| 5174 | } |
| 5175 | |
| 5176 | } // end namespace Catch |
| 5177 | // end catch_benchmark.cpp |
| 5178 | // start catch_capture_matchers.cpp |
| 5179 | |
| 5180 | namespace Catch { |
| 5181 | |
| 5182 | using StringMatcher = Matchers::Impl::MatcherBase<std::string>; |
| 5183 | |
| 5184 | // This is the general overload that takes a any string matcher |
| 5185 | // There is another overload, in catch_assertionhandler.h/.cpp, that only takes a string and infers |
| 5186 | // the Equals matcher (so the header does not mention matchers) |
| 5187 | void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef matcherString ) { |
| 5188 | std::string exceptionMessage = Catch::translateActiveException(); |
| 5189 | MatchExpr<std::string, StringMatcher const&> expr( exceptionMessage, matcher, matcherString ); |
| 5190 | handler.handleExpr( expr ); |
| 5191 | } |
| 5192 | |
| 5193 | } // namespace Catch |
| 5194 | // end catch_capture_matchers.cpp |
| 5195 | // start catch_commandline.cpp |
| 5196 | |
| 5197 | // start catch_commandline.h |
| 5198 | |
| 5199 | // start catch_clara.h |
| 5200 | |
| 5201 | // Use Catch's value for console width (store Clara's off to the side, if present) |
| 5202 | #ifdef CLARA_CONFIG_CONSOLE_WIDTH |
| 5203 | #define CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH |
| 5204 | #undef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH |
| 5205 | #endif |
| 5206 | #define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_CONFIG_CONSOLE_WIDTH-1 |
| 5207 | |
| 5208 | #ifdef __clang__ |
| 5209 | #pragma clang diagnostic push |
| 5210 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 5211 | #pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 5212 | #pragma clang diagnostic ignored "-Wshadow" |
| 5213 | #endif |
| 5214 | |
| 5215 | // start clara.hpp |
| 5216 | // Copyright 2017 Two Blue Cubes Ltd. All rights reserved. |
| 5217 | // |
| 5218 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5219 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5220 | // |
| 5221 | // See https://github.com/philsquared/Clara for more details |
| 5222 | |
| 5223 | // Clara v1.1.4 |
| 5224 | |
| 5225 | |
| 5226 | #ifndef CATCH_CLARA_CONFIG_CONSOLE_WIDTH |
| 5227 | #define CATCH_CLARA_CONFIG_CONSOLE_WIDTH 80 |
| 5228 | #endif |
| 5229 | |
| 5230 | #ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH |
| 5231 | #define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_CLARA_CONFIG_CONSOLE_WIDTH |
| 5232 | #endif |
| 5233 | |
| 5234 | #ifndef CLARA_CONFIG_OPTIONAL_TYPE |
| 5235 | #ifdef __has_include |
| 5236 | #if __has_include(<optional>) && __cplusplus >= 201703L |
| 5237 | #include <optional> |
| 5238 | #define CLARA_CONFIG_OPTIONAL_TYPE std::optional |
| 5239 | #endif |
| 5240 | #endif |
| 5241 | #endif |
| 5242 | |
| 5243 | // ----------- #included from clara_textflow.hpp ----------- |
| 5244 | |
| 5245 | // TextFlowCpp |
| 5246 | // |
| 5247 | // A single-header library for wrapping and laying out basic text, by Phil Nash |
| 5248 | // |
| 5249 | // This work is licensed under the BSD 2-Clause license. |
| 5250 | // See the accompanying LICENSE file, or the one at https://opensource.org/licenses/BSD-2-Clause |
| 5251 | // |
| 5252 | // This project is hosted at https://github.com/philsquared/textflowcpp |
| 5253 | |
| 5254 | |
| 5255 | #include <cassert> |
| 5256 | #include <ostream> |
| 5257 | #include <sstream> |
| 5258 | #include <vector> |
| 5259 | |
| 5260 | #ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH |
| 5261 | #define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH 80 |
| 5262 | #endif |
| 5263 | |
| 5264 | namespace Catch { namespace clara { namespace TextFlow { |
| 5265 | |
| 5266 | inline auto isWhitespace( char c ) -> bool { |
| 5267 | static std::string chars = " \t\n\r"; |
| 5268 | return chars.find( c ) != std::string::npos; |
| 5269 | } |
| 5270 | inline auto isBreakableBefore( char c ) -> bool { |
| 5271 | static std::string chars = "[({<|"; |
| 5272 | return chars.find( c ) != std::string::npos; |
| 5273 | } |
| 5274 | inline auto isBreakableAfter( char c ) -> bool { |
| 5275 | static std::string chars = "])}>.,:;*+-=&/\\"; |
| 5276 | return chars.find( c ) != std::string::npos; |
| 5277 | } |
| 5278 | |
| 5279 | class Columns; |
| 5280 | |
| 5281 | class Column { |
| 5282 | std::vector<std::string> m_strings; |
| 5283 | size_t m_width = CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH; |
| 5284 | size_t m_indent = 0; |
| 5285 | size_t m_initialIndent = std::string::npos; |
| 5286 | |
| 5287 | public: |
| 5288 | class iterator { |
| 5289 | friend Column; |
| 5290 | |
| 5291 | Column const& m_column; |
| 5292 | size_t m_stringIndex = 0; |
| 5293 | size_t m_pos = 0; |
| 5294 | |
| 5295 | size_t m_len = 0; |
| 5296 | size_t m_end = 0; |
| 5297 | bool m_suffix = false; |
| 5298 | |
| 5299 | iterator( Column const& column, size_t stringIndex ) |
| 5300 | : m_column( column ), |
| 5301 | m_stringIndex( stringIndex ) |
| 5302 | {} |
| 5303 | |
| 5304 | auto line() const -> std::string const& { return m_column.m_strings[m_stringIndex]; } |
| 5305 | |
| 5306 | auto isBoundary( size_t at ) const -> bool { |
| 5307 | assert( at > 0 ); |
| 5308 | assert( at <= line().size() ); |
| 5309 | |
| 5310 | return at == line().size() || |
| 5311 | ( isWhitespace( line()[at] ) && !isWhitespace( line()[at-1] ) ) || |
| 5312 | isBreakableBefore( line()[at] ) || |
| 5313 | isBreakableAfter( line()[at-1] ); |
| 5314 | } |
| 5315 | |
| 5316 | void calcLength() { |
| 5317 | assert( m_stringIndex < m_column.m_strings.size() ); |
| 5318 | |
| 5319 | m_suffix = false; |
| 5320 | auto width = m_column.m_width-indent(); |
| 5321 | m_end = m_pos; |
| 5322 | while( m_end < line().size() && line()[m_end] != '\n' ) |
| 5323 | ++m_end; |
| 5324 | |
| 5325 | if( m_end < m_pos + width ) { |
| 5326 | m_len = m_end - m_pos; |
| 5327 | } |
| 5328 | else { |
| 5329 | size_t len = width; |
| 5330 | while (len > 0 && !isBoundary(m_pos + len)) |
| 5331 | --len; |
| 5332 | while (len > 0 && isWhitespace( line()[m_pos + len - 1] )) |
| 5333 | --len; |
| 5334 | |
| 5335 | if (len > 0) { |
| 5336 | m_len = len; |
| 5337 | } else { |
| 5338 | m_suffix = true; |
| 5339 | m_len = width - 1; |
| 5340 | } |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | auto indent() const -> size_t { |
| 5345 | auto initial = m_pos == 0 && m_stringIndex == 0 ? m_column.m_initialIndent : std::string::npos; |
| 5346 | return initial == std::string::npos ? m_column.m_indent : initial; |
| 5347 | } |
| 5348 | |
| 5349 | auto addIndentAndSuffix(std::string const &plain) const -> std::string { |
| 5350 | return std::string( indent(), ' ' ) + (m_suffix ? plain + "-" : plain); |
| 5351 | } |
| 5352 | |
| 5353 | public: |
| 5354 | explicit iterator( Column const& column ) : m_column( column ) { |
| 5355 | assert( m_column.m_width > m_column.m_indent ); |
| 5356 | assert( m_column.m_initialIndent == std::string::npos || m_column.m_width > m_column.m_initialIndent ); |
| 5357 | calcLength(); |
| 5358 | if( m_len == 0 ) |
| 5359 | m_stringIndex++; // Empty string |
| 5360 | } |
| 5361 | |
| 5362 | auto operator *() const -> std::string { |
| 5363 | assert( m_stringIndex < m_column.m_strings.size() ); |
| 5364 | assert( m_pos <= m_end ); |
| 5365 | if( m_pos + m_column.m_width < m_end ) |
| 5366 | return addIndentAndSuffix(line().substr(m_pos, m_len)); |
| 5367 | else |
| 5368 | return addIndentAndSuffix(line().substr(m_pos, m_end - m_pos)); |
| 5369 | } |
| 5370 | |
| 5371 | auto operator ++() -> iterator& { |
| 5372 | m_pos += m_len; |
| 5373 | if( m_pos < line().size() && line()[m_pos] == '\n' ) |
| 5374 | m_pos += 1; |
| 5375 | else |
| 5376 | while( m_pos < line().size() && isWhitespace( line()[m_pos] ) ) |
| 5377 | ++m_pos; |
| 5378 | |
| 5379 | if( m_pos == line().size() ) { |
| 5380 | m_pos = 0; |
| 5381 | ++m_stringIndex; |
| 5382 | } |
| 5383 | if( m_stringIndex < m_column.m_strings.size() ) |
| 5384 | calcLength(); |
| 5385 | return *this; |
| 5386 | } |
| 5387 | auto operator ++(int) -> iterator { |
| 5388 | iterator prev( *this ); |
| 5389 | operator++(); |
| 5390 | return prev; |
| 5391 | } |
| 5392 | |
| 5393 | auto operator ==( iterator const& other ) const -> bool { |
| 5394 | return |
| 5395 | m_pos == other.m_pos && |
| 5396 | m_stringIndex == other.m_stringIndex && |
| 5397 | &m_column == &other.m_column; |
| 5398 | } |
| 5399 | auto operator !=( iterator const& other ) const -> bool { |
| 5400 | return !operator==( other ); |
| 5401 | } |
| 5402 | }; |
| 5403 | using const_iterator = iterator; |
| 5404 | |
| 5405 | explicit Column( std::string const& text ) { m_strings.push_back( text ); } |
| 5406 | |
| 5407 | auto width( size_t newWidth ) -> Column& { |
| 5408 | assert( newWidth > 0 ); |
| 5409 | m_width = newWidth; |
| 5410 | return *this; |
| 5411 | } |
| 5412 | auto indent( size_t newIndent ) -> Column& { |
| 5413 | m_indent = newIndent; |
| 5414 | return *this; |
| 5415 | } |
| 5416 | auto initialIndent( size_t newIndent ) -> Column& { |
| 5417 | m_initialIndent = newIndent; |
| 5418 | return *this; |
| 5419 | } |
| 5420 | |
| 5421 | auto width() const -> size_t { return m_width; } |
| 5422 | auto begin() const -> iterator { return iterator( *this ); } |
| 5423 | auto end() const -> iterator { return { *this, m_strings.size() }; } |
| 5424 | |
| 5425 | inline friend std::ostream& operator << ( std::ostream& os, Column const& col ) { |
| 5426 | bool first = true; |
| 5427 | for( auto line : col ) { |
| 5428 | if( first ) |
| 5429 | first = false; |
| 5430 | else |
| 5431 | os << "\n"; |
| 5432 | os << line; |
| 5433 | } |
| 5434 | return os; |
| 5435 | } |
| 5436 | |
| 5437 | auto operator + ( Column const& other ) -> Columns; |
| 5438 | |
| 5439 | auto toString() const -> std::string { |
| 5440 | std::ostringstream oss; |
| 5441 | oss << *this; |
| 5442 | return oss.str(); |
| 5443 | } |
| 5444 | }; |
| 5445 | |
| 5446 | class Spacer : public Column { |
| 5447 | |
| 5448 | public: |
| 5449 | explicit Spacer( size_t spaceWidth ) : Column( "" ) { |
| 5450 | width( spaceWidth ); |
| 5451 | } |
| 5452 | }; |
| 5453 | |
| 5454 | class Columns { |
| 5455 | std::vector<Column> m_columns; |
| 5456 | |
| 5457 | public: |
| 5458 | |
| 5459 | class iterator { |
| 5460 | friend Columns; |
| 5461 | struct EndTag {}; |
| 5462 | |
| 5463 | std::vector<Column> const& m_columns; |
| 5464 | std::vector<Column::iterator> m_iterators; |
| 5465 | size_t m_activeIterators; |
| 5466 | |
| 5467 | iterator( Columns const& columns, EndTag ) |
| 5468 | : m_columns( columns.m_columns ), |
| 5469 | m_activeIterators( 0 ) |
| 5470 | { |
| 5471 | m_iterators.reserve( m_columns.size() ); |
| 5472 | |
| 5473 | for( auto const& col : m_columns ) |
| 5474 | m_iterators.push_back( col.end() ); |
| 5475 | } |
| 5476 | |
| 5477 | public: |
| 5478 | explicit iterator( Columns const& columns ) |
| 5479 | : m_columns( columns.m_columns ), |
| 5480 | m_activeIterators( m_columns.size() ) |
| 5481 | { |
| 5482 | m_iterators.reserve( m_columns.size() ); |
| 5483 | |
| 5484 | for( auto const& col : m_columns ) |
| 5485 | m_iterators.push_back( col.begin() ); |
| 5486 | } |
| 5487 | |
| 5488 | auto operator ==( iterator const& other ) const -> bool { |
| 5489 | return m_iterators == other.m_iterators; |
| 5490 | } |
| 5491 | auto operator !=( iterator const& other ) const -> bool { |
| 5492 | return m_iterators != other.m_iterators; |
| 5493 | } |
| 5494 | auto operator *() const -> std::string { |
| 5495 | std::string row, padding; |
| 5496 | |
| 5497 | for( size_t i = 0; i < m_columns.size(); ++i ) { |
| 5498 | auto width = m_columns[i].width(); |
| 5499 | if( m_iterators[i] != m_columns[i].end() ) { |
| 5500 | std::string col = *m_iterators[i]; |
| 5501 | row += padding + col; |
| 5502 | if( col.size() < width ) |
| 5503 | padding = std::string( width - col.size(), ' ' ); |
| 5504 | else |
| 5505 | padding = ""; |
| 5506 | } |
| 5507 | else { |
| 5508 | padding += std::string( width, ' ' ); |
| 5509 | } |
| 5510 | } |
| 5511 | return row; |
| 5512 | } |
| 5513 | auto operator ++() -> iterator& { |
| 5514 | for( size_t i = 0; i < m_columns.size(); ++i ) { |
| 5515 | if (m_iterators[i] != m_columns[i].end()) |
| 5516 | ++m_iterators[i]; |
| 5517 | } |
| 5518 | return *this; |
| 5519 | } |
| 5520 | auto operator ++(int) -> iterator { |
| 5521 | iterator prev( *this ); |
| 5522 | operator++(); |
| 5523 | return prev; |
| 5524 | } |
| 5525 | }; |
| 5526 | using const_iterator = iterator; |
| 5527 | |
| 5528 | auto begin() const -> iterator { return iterator( *this ); } |
| 5529 | auto end() const -> iterator { return { *this, iterator::EndTag() }; } |
| 5530 | |
| 5531 | auto operator += ( Column const& col ) -> Columns& { |
| 5532 | m_columns.push_back( col ); |
| 5533 | return *this; |
| 5534 | } |
| 5535 | auto operator + ( Column const& col ) -> Columns { |
| 5536 | Columns combined = *this; |
| 5537 | combined += col; |
| 5538 | return combined; |
| 5539 | } |
| 5540 | |
| 5541 | inline friend std::ostream& operator << ( std::ostream& os, Columns const& cols ) { |
| 5542 | |
| 5543 | bool first = true; |
| 5544 | for( auto line : cols ) { |
| 5545 | if( first ) |
| 5546 | first = false; |
| 5547 | else |
| 5548 | os << "\n"; |
| 5549 | os << line; |
| 5550 | } |
| 5551 | return os; |
| 5552 | } |
| 5553 | |
| 5554 | auto toString() const -> std::string { |
| 5555 | std::ostringstream oss; |
| 5556 | oss << *this; |
| 5557 | return oss.str(); |
| 5558 | } |
| 5559 | }; |
| 5560 | |
| 5561 | inline auto Column::operator + ( Column const& other ) -> Columns { |
| 5562 | Columns cols; |
| 5563 | cols += *this; |
| 5564 | cols += other; |
| 5565 | return cols; |
| 5566 | } |
| 5567 | }}} // namespace Catch::clara::TextFlow |
| 5568 | |
| 5569 | // ----------- end of #include from clara_textflow.hpp ----------- |
| 5570 | // ........... back in clara.hpp |
| 5571 | |
| 5572 | #include <memory> |
| 5573 | #include <set> |
| 5574 | #include <algorithm> |
| 5575 | |
| 5576 | #if !defined(CATCH_PLATFORM_WINDOWS) && ( defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) ) |
| 5577 | #define CATCH_PLATFORM_WINDOWS |
| 5578 | #endif |
| 5579 | |
| 5580 | namespace Catch { namespace clara { |
| 5581 | namespace detail { |
| 5582 | |
| 5583 | // Traits for extracting arg and return type of lambdas (for single argument lambdas) |
| 5584 | template<typename L> |
| 5585 | struct UnaryLambdaTraits : UnaryLambdaTraits<decltype( &L::operator() )> {}; |
| 5586 | |
| 5587 | template<typename ClassT, typename ReturnT, typename... Args> |
| 5588 | struct UnaryLambdaTraits<ReturnT( ClassT::* )( Args... ) const> { |
| 5589 | static const bool isValid = false; |
| 5590 | }; |
| 5591 | |
| 5592 | template<typename ClassT, typename ReturnT, typename ArgT> |
| 5593 | struct UnaryLambdaTraits<ReturnT( ClassT::* )( ArgT ) const> { |
| 5594 | static const bool isValid = true; |
| 5595 | using ArgType = typename std::remove_const<typename std::remove_reference<ArgT>::type>::type; |
| 5596 | using ReturnType = ReturnT; |
| 5597 | }; |
| 5598 | |
| 5599 | class TokenStream; |
| 5600 | |
| 5601 | // Transport for raw args (copied from main args, or supplied via init list for testing) |
| 5602 | class Args { |
| 5603 | friend TokenStream; |
| 5604 | std::string m_exeName; |
| 5605 | std::vector<std::string> m_args; |
| 5606 | |
| 5607 | public: |
| 5608 | Args( int argc, char const* const* argv ) |
| 5609 | : m_exeName(argv[0]), |
| 5610 | m_args(argv + 1, argv + argc) {} |
| 5611 | |
| 5612 | Args( std::initializer_list<std::string> args ) |
| 5613 | : m_exeName( *args.begin() ), |
| 5614 | m_args( args.begin()+1, args.end() ) |
| 5615 | {} |
| 5616 | |
| 5617 | auto exeName() const -> std::string { |
| 5618 | return m_exeName; |
| 5619 | } |
| 5620 | }; |
| 5621 | |
| 5622 | // Wraps a token coming from a token stream. These may not directly correspond to strings as a single string |
| 5623 | // may encode an option + its argument if the : or = form is used |
| 5624 | enum class TokenType { |
| 5625 | Option, Argument |
| 5626 | }; |
| 5627 | struct Token { |
| 5628 | TokenType type; |
| 5629 | std::string token; |
| 5630 | }; |
| 5631 | |
| 5632 | inline auto isOptPrefix( char c ) -> bool { |
| 5633 | return c == '-' |
| 5634 | #ifdef CATCH_PLATFORM_WINDOWS |
| 5635 | || c == '/' |
| 5636 | #endif |
| 5637 | ; |
| 5638 | } |
| 5639 | |
| 5640 | // Abstracts iterators into args as a stream of tokens, with option arguments uniformly handled |
| 5641 | class TokenStream { |
| 5642 | using Iterator = std::vector<std::string>::const_iterator; |
| 5643 | Iterator it; |
| 5644 | Iterator itEnd; |
| 5645 | std::vector<Token> m_tokenBuffer; |
| 5646 | |
| 5647 | void loadBuffer() { |
| 5648 | m_tokenBuffer.resize( 0 ); |
| 5649 | |
| 5650 | // Skip any empty strings |
| 5651 | while( it != itEnd && it->empty() ) |
| 5652 | ++it; |
| 5653 | |
| 5654 | if( it != itEnd ) { |
| 5655 | auto const &next = *it; |
| 5656 | if( isOptPrefix( next[0] ) ) { |
| 5657 | auto delimiterPos = next.find_first_of( " :=" ); |
| 5658 | if( delimiterPos != std::string::npos ) { |
| 5659 | m_tokenBuffer.push_back( { TokenType::Option, next.substr( 0, delimiterPos ) } ); |
| 5660 | m_tokenBuffer.push_back( { TokenType::Argument, next.substr( delimiterPos + 1 ) } ); |
| 5661 | } else { |
| 5662 | if( next[1] != '-' && next.size() > 2 ) { |
| 5663 | std::string opt = "- "; |
| 5664 | for( size_t i = 1; i < next.size(); ++i ) { |
| 5665 | opt[1] = next[i]; |
| 5666 | m_tokenBuffer.push_back( { TokenType::Option, opt } ); |
| 5667 | } |
| 5668 | } else { |
| 5669 | m_tokenBuffer.push_back( { TokenType::Option, next } ); |
| 5670 | } |
| 5671 | } |
| 5672 | } else { |
| 5673 | m_tokenBuffer.push_back( { TokenType::Argument, next } ); |
| 5674 | } |
| 5675 | } |
| 5676 | } |
| 5677 | |
| 5678 | public: |
| 5679 | explicit TokenStream( Args const &args ) : TokenStream( args.m_args.begin(), args.m_args.end() ) {} |
| 5680 | |
| 5681 | TokenStream( Iterator it, Iterator itEnd ) : it( it ), itEnd( itEnd ) { |
| 5682 | loadBuffer(); |
| 5683 | } |
| 5684 | |
| 5685 | explicit operator bool() const { |
| 5686 | return !m_tokenBuffer.empty() || it != itEnd; |
| 5687 | } |
| 5688 | |
| 5689 | auto count() const -> size_t { return m_tokenBuffer.size() + (itEnd - it); } |
| 5690 | |
| 5691 | auto operator*() const -> Token { |
| 5692 | assert( !m_tokenBuffer.empty() ); |
| 5693 | return m_tokenBuffer.front(); |
| 5694 | } |
| 5695 | |
| 5696 | auto operator->() const -> Token const * { |
| 5697 | assert( !m_tokenBuffer.empty() ); |
| 5698 | return &m_tokenBuffer.front(); |
| 5699 | } |
| 5700 | |
| 5701 | auto operator++() -> TokenStream & { |
| 5702 | if( m_tokenBuffer.size() >= 2 ) { |
| 5703 | m_tokenBuffer.erase( m_tokenBuffer.begin() ); |
| 5704 | } else { |
| 5705 | if( it != itEnd ) |
| 5706 | ++it; |
| 5707 | loadBuffer(); |
| 5708 | } |
| 5709 | return *this; |
| 5710 | } |
| 5711 | }; |
| 5712 | |
| 5713 | class ResultBase { |
| 5714 | public: |
| 5715 | enum Type { |
| 5716 | Ok, LogicError, RuntimeError |
| 5717 | }; |
| 5718 | |
| 5719 | protected: |
| 5720 | ResultBase( Type type ) : m_type( type ) {} |
| 5721 | virtual ~ResultBase() = default; |
| 5722 | |
| 5723 | virtual void enforceOk() const = 0; |
| 5724 | |
| 5725 | Type m_type; |
| 5726 | }; |
| 5727 | |
| 5728 | template<typename T> |
| 5729 | class ResultValueBase : public ResultBase { |
| 5730 | public: |
| 5731 | auto value() const -> T const & { |
| 5732 | enforceOk(); |
| 5733 | return m_value; |
| 5734 | } |
| 5735 | |
| 5736 | protected: |
| 5737 | ResultValueBase( Type type ) : ResultBase( type ) {} |
| 5738 | |
| 5739 | ResultValueBase( ResultValueBase const &other ) : ResultBase( other ) { |
| 5740 | if( m_type == ResultBase::Ok ) |
| 5741 | new( &m_value ) T( other.m_value ); |
| 5742 | } |
| 5743 | |
| 5744 | ResultValueBase( Type, T const &value ) : ResultBase( Ok ) { |
| 5745 | new( &m_value ) T( value ); |
| 5746 | } |
| 5747 | |
| 5748 | auto operator=( ResultValueBase const &other ) -> ResultValueBase & { |
| 5749 | if( m_type == ResultBase::Ok ) |
| 5750 | m_value.~T(); |
| 5751 | ResultBase::operator=(other); |
| 5752 | if( m_type == ResultBase::Ok ) |
| 5753 | new( &m_value ) T( other.m_value ); |
| 5754 | return *this; |
| 5755 | } |
| 5756 | |
| 5757 | ~ResultValueBase() override { |
| 5758 | if( m_type == Ok ) |
| 5759 | m_value.~T(); |
| 5760 | } |
| 5761 | |
| 5762 | union { |
| 5763 | T m_value; |
| 5764 | }; |
| 5765 | }; |
| 5766 | |
| 5767 | template<> |
| 5768 | class ResultValueBase<void> : public ResultBase { |
| 5769 | protected: |
| 5770 | using ResultBase::ResultBase; |
| 5771 | }; |
| 5772 | |
| 5773 | template<typename T = void> |
| 5774 | class BasicResult : public ResultValueBase<T> { |
| 5775 | public: |
| 5776 | template<typename U> |
| 5777 | explicit BasicResult( BasicResult<U> const &other ) |
| 5778 | : ResultValueBase<T>( other.type() ), |
| 5779 | m_errorMessage( other.errorMessage() ) |
| 5780 | { |
| 5781 | assert( type() != ResultBase::Ok ); |
| 5782 | } |
| 5783 | |
| 5784 | template<typename U> |
| 5785 | static auto ok( U const &value ) -> BasicResult { return { ResultBase::Ok, value }; } |
| 5786 | static auto ok() -> BasicResult { return { ResultBase::Ok }; } |
| 5787 | static auto logicError( std::string const &message ) -> BasicResult { return { ResultBase::LogicError, message }; } |
| 5788 | static auto runtimeError( std::string const &message ) -> BasicResult { return { ResultBase::RuntimeError, message }; } |
| 5789 | |
| 5790 | explicit operator bool() const { return m_type == ResultBase::Ok; } |
| 5791 | auto type() const -> ResultBase::Type { return m_type; } |
| 5792 | auto errorMessage() const -> std::string { return m_errorMessage; } |
| 5793 | |
| 5794 | protected: |
| 5795 | void enforceOk() const override { |
| 5796 | |
| 5797 | // Errors shouldn't reach this point, but if they do |
| 5798 | // the actual error message will be in m_errorMessage |
| 5799 | assert( m_type != ResultBase::LogicError ); |
| 5800 | assert( m_type != ResultBase::RuntimeError ); |
| 5801 | if( m_type != ResultBase::Ok ) |
| 5802 | std::abort(); |
| 5803 | } |
| 5804 | |
| 5805 | std::string m_errorMessage; // Only populated if resultType is an error |
| 5806 | |
| 5807 | BasicResult( ResultBase::Type type, std::string const &message ) |
| 5808 | : ResultValueBase<T>(type), |
| 5809 | m_errorMessage(message) |
| 5810 | { |
| 5811 | assert( m_type != ResultBase::Ok ); |
| 5812 | } |
| 5813 | |
| 5814 | using ResultValueBase<T>::ResultValueBase; |
| 5815 | using ResultBase::m_type; |
| 5816 | }; |
| 5817 | |
| 5818 | enum class ParseResultType { |
| 5819 | Matched, NoMatch, ShortCircuitAll, ShortCircuitSame |
| 5820 | }; |
| 5821 | |
| 5822 | class ParseState { |
| 5823 | public: |
| 5824 | |
| 5825 | ParseState( ParseResultType type, TokenStream const &remainingTokens ) |
| 5826 | : m_type(type), |
| 5827 | m_remainingTokens( remainingTokens ) |
| 5828 | {} |
| 5829 | |
| 5830 | auto type() const -> ParseResultType { return m_type; } |
| 5831 | auto remainingTokens() const -> TokenStream { return m_remainingTokens; } |
| 5832 | |
| 5833 | private: |
| 5834 | ParseResultType m_type; |
| 5835 | TokenStream m_remainingTokens; |
| 5836 | }; |
| 5837 | |
| 5838 | using Result = BasicResult<void>; |
| 5839 | using ParserResult = BasicResult<ParseResultType>; |
| 5840 | using InternalParseResult = BasicResult<ParseState>; |
| 5841 | |
| 5842 | struct HelpColumns { |
| 5843 | std::string left; |
| 5844 | std::string right; |
| 5845 | }; |
| 5846 | |
| 5847 | template<typename T> |
| 5848 | inline auto convertInto( std::string const &source, T& target ) -> ParserResult { |
| 5849 | std::stringstream ss; |
| 5850 | ss << source; |
| 5851 | ss >> target; |
| 5852 | if( ss.fail() ) |
| 5853 | return ParserResult::runtimeError( "Unable to convert '" + source + "' to destination type" ); |
| 5854 | else |
| 5855 | return ParserResult::ok( ParseResultType::Matched ); |
| 5856 | } |
| 5857 | inline auto convertInto( std::string const &source, std::string& target ) -> ParserResult { |
| 5858 | target = source; |
| 5859 | return ParserResult::ok( ParseResultType::Matched ); |
| 5860 | } |
| 5861 | inline auto convertInto( std::string const &source, bool &target ) -> ParserResult { |
| 5862 | std::string srcLC = source; |
| 5863 | std::transform( srcLC.begin(), srcLC.end(), srcLC.begin(), []( char c ) { return static_cast<char>( ::tolower(c) ); } ); |
| 5864 | if (srcLC == "y" || srcLC == "1" || srcLC == "true" || srcLC == "yes" || srcLC == "on") |
| 5865 | target = true; |
| 5866 | else if (srcLC == "n" || srcLC == "0" || srcLC == "false" || srcLC == "no" || srcLC == "off") |
| 5867 | target = false; |
| 5868 | else |
| 5869 | return ParserResult::runtimeError( "Expected a boolean value but did not recognise: '" + source + "'" ); |
| 5870 | return ParserResult::ok( ParseResultType::Matched ); |
| 5871 | } |
| 5872 | #ifdef CLARA_CONFIG_OPTIONAL_TYPE |
| 5873 | template<typename T> |
| 5874 | inline auto convertInto( std::string const &source, CLARA_CONFIG_OPTIONAL_TYPE<T>& target ) -> ParserResult { |
| 5875 | T temp; |
| 5876 | auto result = convertInto( source, temp ); |
| 5877 | if( result ) |
| 5878 | target = std::move(temp); |
| 5879 | return result; |
| 5880 | } |
| 5881 | #endif // CLARA_CONFIG_OPTIONAL_TYPE |
| 5882 | |
| 5883 | struct NonCopyable { |
| 5884 | NonCopyable() = default; |
| 5885 | NonCopyable( NonCopyable const & ) = delete; |
| 5886 | NonCopyable( NonCopyable && ) = delete; |
| 5887 | NonCopyable &operator=( NonCopyable const & ) = delete; |
| 5888 | NonCopyable &operator=( NonCopyable && ) = delete; |
| 5889 | }; |
| 5890 | |
| 5891 | struct BoundRef : NonCopyable { |
| 5892 | virtual ~BoundRef() = default; |
| 5893 | virtual auto isContainer() const -> bool { return false; } |
| 5894 | virtual auto isFlag() const -> bool { return false; } |
| 5895 | }; |
| 5896 | struct BoundValueRefBase : BoundRef { |
| 5897 | virtual auto setValue( std::string const &arg ) -> ParserResult = 0; |
| 5898 | }; |
| 5899 | struct BoundFlagRefBase : BoundRef { |
| 5900 | virtual auto setFlag( bool flag ) -> ParserResult = 0; |
| 5901 | virtual auto isFlag() const -> bool { return true; } |
| 5902 | }; |
| 5903 | |
| 5904 | template<typename T> |
| 5905 | struct BoundValueRef : BoundValueRefBase { |
| 5906 | T &m_ref; |
| 5907 | |
| 5908 | explicit BoundValueRef( T &ref ) : m_ref( ref ) {} |
| 5909 | |
| 5910 | auto setValue( std::string const &arg ) -> ParserResult override { |
| 5911 | return convertInto( arg, m_ref ); |
| 5912 | } |
| 5913 | }; |
| 5914 | |
| 5915 | template<typename T> |
| 5916 | struct BoundValueRef<std::vector<T>> : BoundValueRefBase { |
| 5917 | std::vector<T> &m_ref; |
| 5918 | |
| 5919 | explicit BoundValueRef( std::vector<T> &ref ) : m_ref( ref ) {} |
| 5920 | |
| 5921 | auto isContainer() const -> bool override { return true; } |
| 5922 | |
| 5923 | auto setValue( std::string const &arg ) -> ParserResult override { |
| 5924 | T temp; |
| 5925 | auto result = convertInto( arg, temp ); |
| 5926 | if( result ) |
| 5927 | m_ref.push_back( temp ); |
| 5928 | return result; |
| 5929 | } |
| 5930 | }; |
| 5931 | |
| 5932 | struct BoundFlagRef : BoundFlagRefBase { |
| 5933 | bool &m_ref; |
| 5934 | |
| 5935 | explicit BoundFlagRef( bool &ref ) : m_ref( ref ) {} |
| 5936 | |
| 5937 | auto setFlag( bool flag ) -> ParserResult override { |
| 5938 | m_ref = flag; |
| 5939 | return ParserResult::ok( ParseResultType::Matched ); |
| 5940 | } |
| 5941 | }; |
| 5942 | |
| 5943 | template<typename ReturnType> |
| 5944 | struct LambdaInvoker { |
| 5945 | static_assert( std::is_same<ReturnType, ParserResult>::value, "Lambda must return void or clara::ParserResult" ); |
| 5946 | |
| 5947 | template<typename L, typename ArgType> |
| 5948 | static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult { |
| 5949 | return lambda( arg ); |
| 5950 | } |
| 5951 | }; |
| 5952 | |
| 5953 | template<> |
| 5954 | struct LambdaInvoker<void> { |
| 5955 | template<typename L, typename ArgType> |
| 5956 | static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult { |
| 5957 | lambda( arg ); |
| 5958 | return ParserResult::ok( ParseResultType::Matched ); |
| 5959 | } |
| 5960 | }; |
| 5961 | |
| 5962 | template<typename ArgType, typename L> |
| 5963 | inline auto invokeLambda( L const &lambda, std::string const &arg ) -> ParserResult { |
| 5964 | ArgType temp{}; |
| 5965 | auto result = convertInto( arg, temp ); |
| 5966 | return !result |
| 5967 | ? result |
| 5968 | : LambdaInvoker<typename UnaryLambdaTraits<L>::ReturnType>::invoke( lambda, temp ); |
| 5969 | } |
| 5970 | |
| 5971 | template<typename L> |
| 5972 | struct BoundLambda : BoundValueRefBase { |
| 5973 | L m_lambda; |
| 5974 | |
| 5975 | static_assert( UnaryLambdaTraits<L>::isValid, "Supplied lambda must take exactly one argument" ); |
| 5976 | explicit BoundLambda( L const &lambda ) : m_lambda( lambda ) {} |
| 5977 | |
| 5978 | auto setValue( std::string const &arg ) -> ParserResult override { |
| 5979 | return invokeLambda<typename UnaryLambdaTraits<L>::ArgType>( m_lambda, arg ); |
| 5980 | } |
| 5981 | }; |
| 5982 | |
| 5983 | template<typename L> |
| 5984 | struct BoundFlagLambda : BoundFlagRefBase { |
| 5985 | L m_lambda; |
| 5986 | |
| 5987 | static_assert( UnaryLambdaTraits<L>::isValid, "Supplied lambda must take exactly one argument" ); |
| 5988 | static_assert( std::is_same<typename UnaryLambdaTraits<L>::ArgType, bool>::value, "flags must be boolean" ); |
| 5989 | |
| 5990 | explicit BoundFlagLambda( L const &lambda ) : m_lambda( lambda ) {} |
| 5991 | |
| 5992 | auto setFlag( bool flag ) -> ParserResult override { |
| 5993 | return LambdaInvoker<typename UnaryLambdaTraits<L>::ReturnType>::invoke( m_lambda, flag ); |
| 5994 | } |
| 5995 | }; |
| 5996 | |
| 5997 | enum class Optionality { Optional, Required }; |
| 5998 | |
| 5999 | struct Parser; |
| 6000 | |
| 6001 | class ParserBase { |
| 6002 | public: |
| 6003 | virtual ~ParserBase() = default; |
| 6004 | virtual auto validate() const -> Result { return Result::ok(); } |
| 6005 | virtual auto parse( std::string const& exeName, TokenStream const &tokens) const -> InternalParseResult = 0; |
| 6006 | virtual auto cardinality() const -> size_t { return 1; } |
| 6007 | |
| 6008 | auto parse( Args const &args ) const -> InternalParseResult { |
| 6009 | return parse( args.exeName(), TokenStream( args ) ); |
| 6010 | } |
| 6011 | }; |
| 6012 | |
| 6013 | template<typename DerivedT> |
| 6014 | class ComposableParserImpl : public ParserBase { |
| 6015 | public: |
| 6016 | template<typename T> |
| 6017 | auto operator|( T const &other ) const -> Parser; |
| 6018 | |
| 6019 | template<typename T> |
| 6020 | auto operator+( T const &other ) const -> Parser; |
| 6021 | }; |
| 6022 | |
| 6023 | // Common code and state for Args and Opts |
| 6024 | template<typename DerivedT> |
| 6025 | class ParserRefImpl : public ComposableParserImpl<DerivedT> { |
| 6026 | protected: |
| 6027 | Optionality m_optionality = Optionality::Optional; |
| 6028 | std::shared_ptr<BoundRef> m_ref; |
| 6029 | std::string m_hint; |
| 6030 | std::string m_description; |
| 6031 | |
| 6032 | explicit ParserRefImpl( std::shared_ptr<BoundRef> const &ref ) : m_ref( ref ) {} |
| 6033 | |
| 6034 | public: |
| 6035 | template<typename T> |
| 6036 | ParserRefImpl( T &ref, std::string const &hint ) |
| 6037 | : m_ref( std::make_shared<BoundValueRef<T>>( ref ) ), |
| 6038 | m_hint( hint ) |
| 6039 | {} |
| 6040 | |
| 6041 | template<typename LambdaT> |
| 6042 | ParserRefImpl( LambdaT const &ref, std::string const &hint ) |
| 6043 | : m_ref( std::make_shared<BoundLambda<LambdaT>>( ref ) ), |
| 6044 | m_hint(hint) |
| 6045 | {} |
| 6046 | |
| 6047 | auto operator()( std::string const &description ) -> DerivedT & { |
| 6048 | m_description = description; |
| 6049 | return static_cast<DerivedT &>( *this ); |
| 6050 | } |
| 6051 | |
| 6052 | auto optional() -> DerivedT & { |
| 6053 | m_optionality = Optionality::Optional; |
| 6054 | return static_cast<DerivedT &>( *this ); |
| 6055 | }; |
| 6056 | |
| 6057 | auto required() -> DerivedT & { |
| 6058 | m_optionality = Optionality::Required; |
| 6059 | return static_cast<DerivedT &>( *this ); |
| 6060 | }; |
| 6061 | |
| 6062 | auto isOptional() const -> bool { |
| 6063 | return m_optionality == Optionality::Optional; |
| 6064 | } |
| 6065 | |
| 6066 | auto cardinality() const -> size_t override { |
| 6067 | if( m_ref->isContainer() ) |
| 6068 | return 0; |
| 6069 | else |
| 6070 | return 1; |
| 6071 | } |
| 6072 | |
| 6073 | auto hint() const -> std::string { return m_hint; } |
| 6074 | }; |
| 6075 | |
| 6076 | class ExeName : public ComposableParserImpl<ExeName> { |
| 6077 | std::shared_ptr<std::string> m_name; |
| 6078 | std::shared_ptr<BoundValueRefBase> m_ref; |
| 6079 | |
| 6080 | template<typename LambdaT> |
| 6081 | static auto makeRef(LambdaT const &lambda) -> std::shared_ptr<BoundValueRefBase> { |
| 6082 | return std::make_shared<BoundLambda<LambdaT>>( lambda) ; |
| 6083 | } |
| 6084 | |
| 6085 | public: |
| 6086 | ExeName() : m_name( std::make_shared<std::string>( "<executable>" ) ) {} |
| 6087 | |
| 6088 | explicit ExeName( std::string &ref ) : ExeName() { |
| 6089 | m_ref = std::make_shared<BoundValueRef<std::string>>( ref ); |
| 6090 | } |
| 6091 | |
| 6092 | template<typename LambdaT> |
| 6093 | explicit ExeName( LambdaT const& lambda ) : ExeName() { |
| 6094 | m_ref = std::make_shared<BoundLambda<LambdaT>>( lambda ); |
| 6095 | } |
| 6096 | |
| 6097 | // The exe name is not parsed out of the normal tokens, but is handled specially |
| 6098 | auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override { |
| 6099 | return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) ); |
| 6100 | } |
| 6101 | |
| 6102 | auto name() const -> std::string { return *m_name; } |
| 6103 | auto set( std::string const& newName ) -> ParserResult { |
| 6104 | |
| 6105 | auto lastSlash = newName.find_last_of( "\\/" ); |
| 6106 | auto filename = ( lastSlash == std::string::npos ) |
| 6107 | ? newName |
| 6108 | : newName.substr( lastSlash+1 ); |
| 6109 | |
| 6110 | *m_name = filename; |
| 6111 | if( m_ref ) |
| 6112 | return m_ref->setValue( filename ); |
| 6113 | else |
| 6114 | return ParserResult::ok( ParseResultType::Matched ); |
| 6115 | } |
| 6116 | }; |
| 6117 | |
| 6118 | class Arg : public ParserRefImpl<Arg> { |
| 6119 | public: |
| 6120 | using ParserRefImpl::ParserRefImpl; |
| 6121 | |
| 6122 | auto parse( std::string const &, TokenStream const &tokens ) const -> InternalParseResult override { |
| 6123 | auto validationResult = validate(); |
| 6124 | if( !validationResult ) |
| 6125 | return InternalParseResult( validationResult ); |
| 6126 | |
| 6127 | auto remainingTokens = tokens; |
| 6128 | auto const &token = *remainingTokens; |
| 6129 | if( token.type != TokenType::Argument ) |
| 6130 | return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) ); |
| 6131 | |
| 6132 | assert( !m_ref->isFlag() ); |
| 6133 | auto valueRef = static_cast<detail::BoundValueRefBase*>( m_ref.get() ); |
| 6134 | |
| 6135 | auto result = valueRef->setValue( remainingTokens->token ); |
| 6136 | if( !result ) |
| 6137 | return InternalParseResult( result ); |
| 6138 | else |
| 6139 | return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) ); |
| 6140 | } |
| 6141 | }; |
| 6142 | |
| 6143 | inline auto normaliseOpt( std::string const &optName ) -> std::string { |
| 6144 | #ifdef CATCH_PLATFORM_WINDOWS |
| 6145 | if( optName[0] == '/' ) |
| 6146 | return "-" + optName.substr( 1 ); |
| 6147 | else |
| 6148 | #endif |
| 6149 | return optName; |
| 6150 | } |
| 6151 | |
| 6152 | class Opt : public ParserRefImpl<Opt> { |
| 6153 | protected: |
| 6154 | std::vector<std::string> m_optNames; |
| 6155 | |
| 6156 | public: |
| 6157 | template<typename LambdaT> |
| 6158 | explicit Opt( LambdaT const &ref ) : ParserRefImpl( std::make_shared<BoundFlagLambda<LambdaT>>( ref ) ) {} |
| 6159 | |
| 6160 | explicit Opt( bool &ref ) : ParserRefImpl( std::make_shared<BoundFlagRef>( ref ) ) {} |
| 6161 | |
| 6162 | template<typename LambdaT> |
| 6163 | Opt( LambdaT const &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {} |
| 6164 | |
| 6165 | template<typename T> |
| 6166 | Opt( T &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {} |
| 6167 | |
| 6168 | auto operator[]( std::string const &optName ) -> Opt & { |
| 6169 | m_optNames.push_back( optName ); |
| 6170 | return *this; |
| 6171 | } |
| 6172 | |
| 6173 | auto getHelpColumns() const -> std::vector<HelpColumns> { |
| 6174 | std::ostringstream oss; |
| 6175 | bool first = true; |
| 6176 | for( auto const &opt : m_optNames ) { |
| 6177 | if (first) |
| 6178 | first = false; |
| 6179 | else |
| 6180 | oss << ", "; |
| 6181 | oss << opt; |
| 6182 | } |
| 6183 | if( !m_hint.empty() ) |
| 6184 | oss << " <" << m_hint << ">"; |
| 6185 | return { { oss.str(), m_description } }; |
| 6186 | } |
| 6187 | |
| 6188 | auto isMatch( std::string const &optToken ) const -> bool { |
| 6189 | auto normalisedToken = normaliseOpt( optToken ); |
| 6190 | for( auto const &name : m_optNames ) { |
| 6191 | if( normaliseOpt( name ) == normalisedToken ) |
| 6192 | return true; |
| 6193 | } |
| 6194 | return false; |
| 6195 | } |
| 6196 | |
| 6197 | using ParserBase::parse; |
| 6198 | |
| 6199 | auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override { |
| 6200 | auto validationResult = validate(); |
| 6201 | if( !validationResult ) |
| 6202 | return InternalParseResult( validationResult ); |
| 6203 | |
| 6204 | auto remainingTokens = tokens; |
| 6205 | if( remainingTokens && remainingTokens->type == TokenType::Option ) { |
| 6206 | auto const &token = *remainingTokens; |
| 6207 | if( isMatch(token.token ) ) { |
| 6208 | if( m_ref->isFlag() ) { |
| 6209 | auto flagRef = static_cast<detail::BoundFlagRefBase*>( m_ref.get() ); |
| 6210 | auto result = flagRef->setFlag( true ); |
| 6211 | if( !result ) |
| 6212 | return InternalParseResult( result ); |
| 6213 | if( result.value() == ParseResultType::ShortCircuitAll ) |
| 6214 | return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) ); |
| 6215 | } else { |
| 6216 | auto valueRef = static_cast<detail::BoundValueRefBase*>( m_ref.get() ); |
| 6217 | ++remainingTokens; |
| 6218 | if( !remainingTokens ) |
| 6219 | return InternalParseResult::runtimeError( "Expected argument following " + token.token ); |
| 6220 | auto const &argToken = *remainingTokens; |
| 6221 | if( argToken.type != TokenType::Argument ) |
| 6222 | return InternalParseResult::runtimeError( "Expected argument following " + token.token ); |
| 6223 | auto result = valueRef->setValue( argToken.token ); |
| 6224 | if( !result ) |
| 6225 | return InternalParseResult( result ); |
| 6226 | if( result.value() == ParseResultType::ShortCircuitAll ) |
| 6227 | return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) ); |
| 6228 | } |
| 6229 | return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) ); |
| 6230 | } |
| 6231 | } |
| 6232 | return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) ); |
| 6233 | } |
| 6234 | |
| 6235 | auto validate() const -> Result override { |
| 6236 | if( m_optNames.empty() ) |
| 6237 | return Result::logicError( "No options supplied to Opt" ); |
| 6238 | for( auto const &name : m_optNames ) { |
| 6239 | if( name.empty() ) |
| 6240 | return Result::logicError( "Option name cannot be empty" ); |
| 6241 | #ifdef CATCH_PLATFORM_WINDOWS |
| 6242 | if( name[0] != '-' && name[0] != '/' ) |
| 6243 | return Result::logicError( "Option name must begin with '-' or '/'" ); |
| 6244 | #else |
| 6245 | if( name[0] != '-' ) |
| 6246 | return Result::logicError( "Option name must begin with '-'" ); |
| 6247 | #endif |
| 6248 | } |
| 6249 | return ParserRefImpl::validate(); |
| 6250 | } |
| 6251 | }; |
| 6252 | |
| 6253 | struct Help : Opt { |
| 6254 | Help( bool &showHelpFlag ) |
| 6255 | : Opt([&]( bool flag ) { |
| 6256 | showHelpFlag = flag; |
| 6257 | return ParserResult::ok( ParseResultType::ShortCircuitAll ); |
| 6258 | }) |
| 6259 | { |
| 6260 | static_cast<Opt &>( *this ) |
| 6261 | ("display usage information") |
| 6262 | ["-?"]["-h"]["--help"] |
| 6263 | .optional(); |
| 6264 | } |
| 6265 | }; |
| 6266 | |
| 6267 | struct Parser : ParserBase { |
| 6268 | |
| 6269 | mutable ExeName m_exeName; |
| 6270 | std::vector<Opt> m_options; |
| 6271 | std::vector<Arg> m_args; |
| 6272 | |
| 6273 | auto operator|=( ExeName const &exeName ) -> Parser & { |
| 6274 | m_exeName = exeName; |
| 6275 | return *this; |
| 6276 | } |
| 6277 | |
| 6278 | auto operator|=( Arg const &arg ) -> Parser & { |
| 6279 | m_args.push_back(arg); |
| 6280 | return *this; |
| 6281 | } |
| 6282 | |
| 6283 | auto operator|=( Opt const &opt ) -> Parser & { |
| 6284 | m_options.push_back(opt); |
| 6285 | return *this; |
| 6286 | } |
| 6287 | |
| 6288 | auto operator|=( Parser const &other ) -> Parser & { |
| 6289 | m_options.insert(m_options.end(), other.m_options.begin(), other.m_options.end()); |
| 6290 | m_args.insert(m_args.end(), other.m_args.begin(), other.m_args.end()); |
| 6291 | return *this; |
| 6292 | } |
| 6293 | |
| 6294 | template<typename T> |
| 6295 | auto operator|( T const &other ) const -> Parser { |
| 6296 | return Parser( *this ) |= other; |
| 6297 | } |
| 6298 | |
| 6299 | // Forward deprecated interface with '+' instead of '|' |
| 6300 | template<typename T> |
| 6301 | auto operator+=( T const &other ) -> Parser & { return operator|=( other ); } |
| 6302 | template<typename T> |
| 6303 | auto operator+( T const &other ) const -> Parser { return operator|( other ); } |
| 6304 | |
| 6305 | auto getHelpColumns() const -> std::vector<HelpColumns> { |
| 6306 | std::vector<HelpColumns> cols; |
| 6307 | for (auto const &o : m_options) { |
| 6308 | auto childCols = o.getHelpColumns(); |
| 6309 | cols.insert( cols.end(), childCols.begin(), childCols.end() ); |
| 6310 | } |
| 6311 | return cols; |
| 6312 | } |
| 6313 | |
| 6314 | void writeToStream( std::ostream &os ) const { |
| 6315 | if (!m_exeName.name().empty()) { |
| 6316 | os << "usage:\n" << " " << m_exeName.name() << " "; |
| 6317 | bool required = true, first = true; |
| 6318 | for( auto const &arg : m_args ) { |
| 6319 | if (first) |
| 6320 | first = false; |
| 6321 | else |
| 6322 | os << " "; |
| 6323 | if( arg.isOptional() && required ) { |
| 6324 | os << "["; |
| 6325 | required = false; |
| 6326 | } |
| 6327 | os << "<" << arg.hint() << ">"; |
| 6328 | if( arg.cardinality() == 0 ) |
| 6329 | os << " ... "; |
| 6330 | } |
| 6331 | if( !required ) |
| 6332 | os << "]"; |
| 6333 | if( !m_options.empty() ) |
| 6334 | os << " options"; |
| 6335 | os << "\n\nwhere options are:" << std::endl; |
| 6336 | } |
| 6337 | |
| 6338 | auto rows = getHelpColumns(); |
| 6339 | size_t consoleWidth = CATCH_CLARA_CONFIG_CONSOLE_WIDTH; |
| 6340 | size_t optWidth = 0; |
| 6341 | for( auto const &cols : rows ) |
| 6342 | optWidth = (std::max)(optWidth, cols.left.size() + 2); |
| 6343 | |
| 6344 | optWidth = (std::min)(optWidth, consoleWidth/2); |
| 6345 | |
| 6346 | for( auto const &cols : rows ) { |
| 6347 | auto row = |
| 6348 | TextFlow::Column( cols.left ).width( optWidth ).indent( 2 ) + |
| 6349 | TextFlow::Spacer(4) + |
| 6350 | TextFlow::Column( cols.right ).width( consoleWidth - 7 - optWidth ); |
| 6351 | os << row << std::endl; |
| 6352 | } |
| 6353 | } |
| 6354 | |
| 6355 | friend auto operator<<( std::ostream &os, Parser const &parser ) -> std::ostream& { |
| 6356 | parser.writeToStream( os ); |
| 6357 | return os; |
| 6358 | } |
| 6359 | |
| 6360 | auto validate() const -> Result override { |
| 6361 | for( auto const &opt : m_options ) { |
| 6362 | auto result = opt.validate(); |
| 6363 | if( !result ) |
| 6364 | return result; |
| 6365 | } |
| 6366 | for( auto const &arg : m_args ) { |
| 6367 | auto result = arg.validate(); |
| 6368 | if( !result ) |
| 6369 | return result; |
| 6370 | } |
| 6371 | return Result::ok(); |
| 6372 | } |
| 6373 | |
| 6374 | using ParserBase::parse; |
| 6375 | |
| 6376 | auto parse( std::string const& exeName, TokenStream const &tokens ) const -> InternalParseResult override { |
| 6377 | |
| 6378 | struct ParserInfo { |
| 6379 | ParserBase const* parser = nullptr; |
| 6380 | size_t count = 0; |
| 6381 | }; |
| 6382 | const size_t totalParsers = m_options.size() + m_args.size(); |
| 6383 | assert( totalParsers < 512 ); |
| 6384 | // ParserInfo parseInfos[totalParsers]; // <-- this is what we really want to do |
| 6385 | ParserInfo parseInfos[512]; |
| 6386 | |
| 6387 | { |
| 6388 | size_t i = 0; |
| 6389 | for (auto const &opt : m_options) parseInfos[i++].parser = &opt; |
| 6390 | for (auto const &arg : m_args) parseInfos[i++].parser = &arg; |
| 6391 | } |
| 6392 | |
| 6393 | m_exeName.set( exeName ); |
| 6394 | |
| 6395 | auto result = InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) ); |
| 6396 | while( result.value().remainingTokens() ) { |
| 6397 | bool tokenParsed = false; |
| 6398 | |
| 6399 | for( size_t i = 0; i < totalParsers; ++i ) { |
| 6400 | auto& parseInfo = parseInfos[i]; |
| 6401 | if( parseInfo.parser->cardinality() == 0 || parseInfo.count < parseInfo.parser->cardinality() ) { |
| 6402 | result = parseInfo.parser->parse(exeName, result.value().remainingTokens()); |
| 6403 | if (!result) |
| 6404 | return result; |
| 6405 | if (result.value().type() != ParseResultType::NoMatch) { |
| 6406 | tokenParsed = true; |
| 6407 | ++parseInfo.count; |
| 6408 | break; |
| 6409 | } |
| 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | if( result.value().type() == ParseResultType::ShortCircuitAll ) |
| 6414 | return result; |
| 6415 | if( !tokenParsed ) |
| 6416 | return InternalParseResult::runtimeError( "Unrecognised token: " + result.value().remainingTokens()->token ); |
| 6417 | } |
| 6418 | // !TBD Check missing required options |
| 6419 | return result; |
| 6420 | } |
| 6421 | }; |
| 6422 | |
| 6423 | template<typename DerivedT> |
| 6424 | template<typename T> |
| 6425 | auto ComposableParserImpl<DerivedT>::operator|( T const &other ) const -> Parser { |
| 6426 | return Parser() | static_cast<DerivedT const &>( *this ) | other; |
| 6427 | } |
| 6428 | } // namespace detail |
| 6429 | |
| 6430 | // A Combined parser |
| 6431 | using detail::Parser; |
| 6432 | |
| 6433 | // A parser for options |
| 6434 | using detail::Opt; |
| 6435 | |
| 6436 | // A parser for arguments |
| 6437 | using detail::Arg; |
| 6438 | |
| 6439 | // Wrapper for argc, argv from main() |
| 6440 | using detail::Args; |
| 6441 | |
| 6442 | // Specifies the name of the executable |
| 6443 | using detail::ExeName; |
| 6444 | |
| 6445 | // Convenience wrapper for option parser that specifies the help option |
| 6446 | using detail::Help; |
| 6447 | |
| 6448 | // enum of result types from a parse |
| 6449 | using detail::ParseResultType; |
| 6450 | |
| 6451 | // Result type for parser operation |
| 6452 | using detail::ParserResult; |
| 6453 | |
| 6454 | }} // namespace Catch::clara |
| 6455 | |
| 6456 | // end clara.hpp |
| 6457 | #ifdef __clang__ |
| 6458 | #pragma clang diagnostic pop |
| 6459 | #endif |
| 6460 | |
| 6461 | // Restore Clara's value for console width, if present |
| 6462 | #ifdef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH |
| 6463 | #define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH |
| 6464 | #undef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH |
| 6465 | #endif |
| 6466 | |
| 6467 | // end catch_clara.h |
| 6468 | namespace Catch { |
| 6469 | |
| 6470 | clara::Parser makeCommandLineParser( ConfigData& config ); |
| 6471 | |
| 6472 | } // end namespace Catch |
| 6473 | |
| 6474 | // end catch_commandline.h |
| 6475 | #include <fstream> |
| 6476 | #include <ctime> |
| 6477 | |
| 6478 | namespace Catch { |
| 6479 | |
| 6480 | clara::Parser makeCommandLineParser( ConfigData& config ) { |
| 6481 | |
| 6482 | using namespace clara; |
| 6483 | |
| 6484 | auto const setWarning = [&]( std::string const& warning ) { |
| 6485 | auto warningSet = [&]() { |
| 6486 | if( warning == "NoAssertions" ) |
| 6487 | return WarnAbout::NoAssertions; |
| 6488 | |
| 6489 | if ( warning == "NoTests" ) |
| 6490 | return WarnAbout::NoTests; |
| 6491 | |
| 6492 | return WarnAbout::Nothing; |
| 6493 | }(); |
| 6494 | |
| 6495 | if (warningSet == WarnAbout::Nothing) |
| 6496 | return ParserResult::runtimeError( "Unrecognised warning: '" + warning + "'" ); |
| 6497 | config.warnings = static_cast<WarnAbout::What>( config.warnings | warningSet ); |
| 6498 | return ParserResult::ok( ParseResultType::Matched ); |
| 6499 | }; |
| 6500 | auto const loadTestNamesFromFile = [&]( std::string const& filename ) { |
| 6501 | std::ifstream f( filename.c_str() ); |
| 6502 | if( !f.is_open() ) |
| 6503 | return ParserResult::runtimeError( "Unable to load input file: '" + filename + "'" ); |
| 6504 | |
| 6505 | std::string line; |
| 6506 | while( std::getline( f, line ) ) { |
| 6507 | line = trim(line); |
| 6508 | if( !line.empty() && !startsWith( line, '#' ) ) { |
| 6509 | if( !startsWith( line, '"' ) ) |
| 6510 | line = '"' + line + '"'; |
| 6511 | config.testsOrTags.push_back( line + ',' ); |
| 6512 | } |
| 6513 | } |
| 6514 | return ParserResult::ok( ParseResultType::Matched ); |
| 6515 | }; |
| 6516 | auto const setTestOrder = [&]( std::string const& order ) { |
| 6517 | if( startsWith( "declared", order ) ) |
| 6518 | config.runOrder = RunTests::InDeclarationOrder; |
| 6519 | else if( startsWith( "lexical", order ) ) |
| 6520 | config.runOrder = RunTests::InLexicographicalOrder; |
| 6521 | else if( startsWith( "random", order ) ) |
| 6522 | config.runOrder = RunTests::InRandomOrder; |
| 6523 | else |
| 6524 | return clara::ParserResult::runtimeError( "Unrecognised ordering: '" + order + "'" ); |
| 6525 | return ParserResult::ok( ParseResultType::Matched ); |
| 6526 | }; |
| 6527 | auto const setRngSeed = [&]( std::string const& seed ) { |
| 6528 | if( seed != "time" ) |
| 6529 | return clara::detail::convertInto( seed, config.rngSeed ); |
| 6530 | config.rngSeed = static_cast<unsigned int>( std::time(nullptr) ); |
| 6531 | return ParserResult::ok( ParseResultType::Matched ); |
| 6532 | }; |
| 6533 | auto const setColourUsage = [&]( std::string const& useColour ) { |
| 6534 | auto mode = toLower( useColour ); |
| 6535 | |
| 6536 | if( mode == "yes" ) |
| 6537 | config.useColour = UseColour::Yes; |
| 6538 | else if( mode == "no" ) |
| 6539 | config.useColour = UseColour::No; |
| 6540 | else if( mode == "auto" ) |
| 6541 | config.useColour = UseColour::Auto; |
| 6542 | else |
| 6543 | return ParserResult::runtimeError( "colour mode must be one of: auto, yes or no. '" + useColour + "' not recognised" ); |
| 6544 | return ParserResult::ok( ParseResultType::Matched ); |
| 6545 | }; |
| 6546 | auto const setWaitForKeypress = [&]( std::string const& keypress ) { |
| 6547 | auto keypressLc = toLower( keypress ); |
| 6548 | if( keypressLc == "start" ) |
| 6549 | config.waitForKeypress = WaitForKeypress::BeforeStart; |
| 6550 | else if( keypressLc == "exit" ) |
| 6551 | config.waitForKeypress = WaitForKeypress::BeforeExit; |
| 6552 | else if( keypressLc == "both" ) |
| 6553 | config.waitForKeypress = WaitForKeypress::BeforeStartAndExit; |
| 6554 | else |
| 6555 | return ParserResult::runtimeError( "keypress argument must be one of: start, exit or both. '" + keypress + "' not recognised" ); |
| 6556 | return ParserResult::ok( ParseResultType::Matched ); |
| 6557 | }; |
| 6558 | auto const setVerbosity = [&]( std::string const& verbosity ) { |
| 6559 | auto lcVerbosity = toLower( verbosity ); |
| 6560 | if( lcVerbosity == "quiet" ) |
| 6561 | config.verbosity = Verbosity::Quiet; |
| 6562 | else if( lcVerbosity == "normal" ) |
| 6563 | config.verbosity = Verbosity::Normal; |
| 6564 | else if( lcVerbosity == "high" ) |
| 6565 | config.verbosity = Verbosity::High; |
| 6566 | else |
| 6567 | return ParserResult::runtimeError( "Unrecognised verbosity, '" + verbosity + "'" ); |
| 6568 | return ParserResult::ok( ParseResultType::Matched ); |
| 6569 | }; |
| 6570 | |
| 6571 | auto cli |
| 6572 | = ExeName( config.processName ) |
| 6573 | | Help( config.showHelp ) |
| 6574 | | Opt( config.listTests ) |
| 6575 | ["-l"]["--list-tests"] |
| 6576 | ( "list all/matching test cases" ) |
| 6577 | | Opt( config.listTags ) |
| 6578 | ["-t"]["--list-tags"] |
| 6579 | ( "list all/matching tags" ) |
| 6580 | | Opt( config.showSuccessfulTests ) |
| 6581 | ["-s"]["--success"] |
| 6582 | ( "include successful tests in output" ) |
| 6583 | | Opt( config.shouldDebugBreak ) |
| 6584 | ["-b"]["--break"] |
| 6585 | ( "break into debugger on failure" ) |
| 6586 | | Opt( config.noThrow ) |
| 6587 | ["-e"]["--nothrow"] |
| 6588 | ( "skip exception tests" ) |
| 6589 | | Opt( config.showInvisibles ) |
| 6590 | ["-i"]["--invisibles"] |
| 6591 | ( "show invisibles (tabs, newlines)" ) |
| 6592 | | Opt( config.outputFilename, "filename" ) |
| 6593 | ["-o"]["--out"] |
| 6594 | ( "output filename" ) |
| 6595 | | Opt( config.reporterNames, "name" ) |
| 6596 | ["-r"]["--reporter"] |
| 6597 | ( "reporter to use (defaults to console)" ) |
| 6598 | | Opt( config.name, "name" ) |
| 6599 | ["-n"]["--name"] |
| 6600 | ( "suite name" ) |
| 6601 | | Opt( [&]( bool ){ config.abortAfter = 1; } ) |
| 6602 | ["-a"]["--abort"] |
| 6603 | ( "abort at first failure" ) |
| 6604 | | Opt( [&]( int x ){ config.abortAfter = x; }, "no. failures" ) |
| 6605 | ["-x"]["--abortx"] |
| 6606 | ( "abort after x failures" ) |
| 6607 | | Opt( setWarning, "warning name" ) |
| 6608 | ["-w"]["--warn"] |
| 6609 | ( "enable warnings" ) |
| 6610 | | Opt( [&]( bool flag ) { config.showDurations = flag ? ShowDurations::Always : ShowDurations::Never; }, "yes|no" ) |
| 6611 | ["-d"]["--durations"] |
| 6612 | ( "show test durations" ) |
| 6613 | | Opt( loadTestNamesFromFile, "filename" ) |
| 6614 | ["-f"]["--input-file"] |
| 6615 | ( "load test names to run from a file" ) |
| 6616 | | Opt( config.filenamesAsTags ) |
| 6617 | ["-#"]["--filenames-as-tags"] |
| 6618 | ( "adds a tag for the filename" ) |
| 6619 | | Opt( config.sectionsToRun, "section name" ) |
| 6620 | ["-c"]["--section"] |
| 6621 | ( "specify section to run" ) |
| 6622 | | Opt( setVerbosity, "quiet|normal|high" ) |
| 6623 | ["-v"]["--verbosity"] |
| 6624 | ( "set output verbosity" ) |
| 6625 | | Opt( config.listTestNamesOnly ) |
| 6626 | ["--list-test-names-only"] |
| 6627 | ( "list all/matching test cases names only" ) |
| 6628 | | Opt( config.listReporters ) |
| 6629 | ["--list-reporters"] |
| 6630 | ( "list all reporters" ) |
| 6631 | | Opt( setTestOrder, "decl|lex|rand" ) |
| 6632 | ["--order"] |
| 6633 | ( "test case order (defaults to decl)" ) |
| 6634 | | Opt( setRngSeed, "'time'|number" ) |
| 6635 | ["--rng-seed"] |
| 6636 | ( "set a specific seed for random numbers" ) |
| 6637 | | Opt( setColourUsage, "yes|no" ) |
| 6638 | ["--use-colour"] |
| 6639 | ( "should output be colourised" ) |
| 6640 | | Opt( config.libIdentify ) |
| 6641 | ["--libidentify"] |
| 6642 | ( "report name and version according to libidentify standard" ) |
| 6643 | | Opt( setWaitForKeypress, "start|exit|both" ) |
| 6644 | ["--wait-for-keypress"] |
| 6645 | ( "waits for a keypress before exiting" ) |
| 6646 | | Opt( config.benchmarkResolutionMultiple, "multiplier" ) |
| 6647 | ["--benchmark-resolution-multiple"] |
| 6648 | ( "multiple of clock resolution to run benchmarks" ) |
| 6649 | |
| 6650 | | Arg( config.testsOrTags, "test name|pattern|tags" ) |
| 6651 | ( "which test or tests to use" ); |
| 6652 | |
| 6653 | return cli; |
| 6654 | } |
| 6655 | |
| 6656 | } // end namespace Catch |
| 6657 | // end catch_commandline.cpp |
| 6658 | // start catch_common.cpp |
| 6659 | |
| 6660 | #include <cstring> |
| 6661 | #include <ostream> |
| 6662 | |
| 6663 | namespace Catch { |
| 6664 | |
| 6665 | bool SourceLineInfo::empty() const noexcept { |
| 6666 | return file[0] == '\0'; |
| 6667 | } |
| 6668 | bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const noexcept { |
| 6669 | return line == other.line && (file == other.file || std::strcmp(file, other.file) == 0); |
| 6670 | } |
| 6671 | bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const noexcept { |
| 6672 | return line < other.line || ( line == other.line && (std::strcmp(file, other.file) < 0)); |
| 6673 | } |
| 6674 | |
| 6675 | std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) { |
| 6676 | #ifndef __GNUG__ |
| 6677 | os << info.file << '(' << info.line << ')'; |
| 6678 | #else |
| 6679 | os << info.file << ':' << info.line; |
| 6680 | #endif |
| 6681 | return os; |
| 6682 | } |
| 6683 | |
| 6684 | std::string StreamEndStop::operator+() const { |
| 6685 | return std::string(); |
| 6686 | } |
| 6687 | |
| 6688 | NonCopyable::NonCopyable() = default; |
| 6689 | NonCopyable::~NonCopyable() = default; |
| 6690 | |
| 6691 | } |
| 6692 | // end catch_common.cpp |
| 6693 | // start catch_config.cpp |
| 6694 | |
| 6695 | // start catch_enforce.h |
| 6696 | |
| 6697 | #include <stdexcept> |
| 6698 | |
| 6699 | #define CATCH_PREPARE_EXCEPTION( type, msg ) \ |
| 6700 | type( ( Catch::ReusableStringStream() << msg ).str() ) |
| 6701 | #define CATCH_INTERNAL_ERROR( msg ) \ |
| 6702 | throw CATCH_PREPARE_EXCEPTION( std::logic_error, CATCH_INTERNAL_LINEINFO << ": Internal Catch error: " << msg); |
| 6703 | #define CATCH_ERROR( msg ) \ |
| 6704 | throw CATCH_PREPARE_EXCEPTION( std::domain_error, msg ) |
| 6705 | #define CATCH_ENFORCE( condition, msg ) \ |
| 6706 | do{ if( !(condition) ) CATCH_ERROR( msg ); } while(false) |
| 6707 | |
| 6708 | // end catch_enforce.h |
| 6709 | namespace Catch { |
| 6710 | |
| 6711 | Config::Config( ConfigData const& data ) |
| 6712 | : m_data( data ), |
| 6713 | m_stream( openStream() ) |
| 6714 | { |
| 6715 | TestSpecParser parser(ITagAliasRegistry::get()); |
| 6716 | if (data.testsOrTags.empty()) { |
| 6717 | parser.parse("~[.]"); // All not hidden tests |
| 6718 | } |
| 6719 | else { |
| 6720 | m_hasTestFilters = true; |
| 6721 | for( auto const& testOrTags : data.testsOrTags ) |
| 6722 | parser.parse( testOrTags ); |
| 6723 | } |
| 6724 | m_testSpec = parser.testSpec(); |
| 6725 | } |
| 6726 | |
| 6727 | std::string const& Config::getFilename() const { |
| 6728 | return m_data.outputFilename ; |
| 6729 | } |
| 6730 | |
| 6731 | bool Config::listTests() const { return m_data.listTests; } |
| 6732 | bool Config::listTestNamesOnly() const { return m_data.listTestNamesOnly; } |
| 6733 | bool Config::listTags() const { return m_data.listTags; } |
| 6734 | bool Config::listReporters() const { return m_data.listReporters; } |
| 6735 | |
| 6736 | std::string Config::getProcessName() const { return m_data.processName; } |
| 6737 | |
| 6738 | std::vector<std::string> const& Config::getReporterNames() const { return m_data.reporterNames; } |
| 6739 | std::vector<std::string> const& Config::getTestsOrTags() const { return m_data.testsOrTags; } |
| 6740 | std::vector<std::string> const& Config::getSectionsToRun() const { return m_data.sectionsToRun; } |
| 6741 | |
| 6742 | TestSpec const& Config::testSpec() const { return m_testSpec; } |
| 6743 | bool Config::hasTestFilters() const { return m_hasTestFilters; } |
| 6744 | |
| 6745 | bool Config::showHelp() const { return m_data.showHelp; } |
| 6746 | |
| 6747 | // IConfig interface |
| 6748 | bool Config::allowThrows() const { return !m_data.noThrow; } |
| 6749 | std::ostream& Config::stream() const { return m_stream->stream(); } |
| 6750 | std::string Config::name() const { return m_data.name.empty() ? m_data.processName : m_data.name; } |
| 6751 | bool Config::includeSuccessfulResults() const { return m_data.showSuccessfulTests; } |
| 6752 | bool Config::warnAboutMissingAssertions() const { return !!(m_data.warnings & WarnAbout::NoAssertions); } |
| 6753 | bool Config::warnAboutNoTests() const { return !!(m_data.warnings & WarnAbout::NoTests); } |
| 6754 | ShowDurations::OrNot Config::showDurations() const { return m_data.showDurations; } |
| 6755 | RunTests::InWhatOrder Config::runOrder() const { return m_data.runOrder; } |
| 6756 | unsigned int Config::rngSeed() const { return m_data.rngSeed; } |
| 6757 | int Config::benchmarkResolutionMultiple() const { return m_data.benchmarkResolutionMultiple; } |
| 6758 | UseColour::YesOrNo Config::useColour() const { return m_data.useColour; } |
| 6759 | bool Config::shouldDebugBreak() const { return m_data.shouldDebugBreak; } |
| 6760 | int Config::abortAfter() const { return m_data.abortAfter; } |
| 6761 | bool Config::showInvisibles() const { return m_data.showInvisibles; } |
| 6762 | Verbosity Config::verbosity() const { return m_data.verbosity; } |
| 6763 | |
| 6764 | IStream const* Config::openStream() { |
| 6765 | return Catch::makeStream(m_data.outputFilename); |
| 6766 | } |
| 6767 | |
| 6768 | } // end namespace Catch |
| 6769 | // end catch_config.cpp |
| 6770 | // start catch_console_colour.cpp |
| 6771 | |
| 6772 | #if defined(__clang__) |
| 6773 | # pragma clang diagnostic push |
| 6774 | # pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 6775 | #endif |
| 6776 | |
| 6777 | // start catch_errno_guard.h |
| 6778 | |
| 6779 | namespace Catch { |
| 6780 | |
| 6781 | class ErrnoGuard { |
| 6782 | public: |
| 6783 | ErrnoGuard(); |
| 6784 | ~ErrnoGuard(); |
| 6785 | private: |
| 6786 | int m_oldErrno; |
| 6787 | }; |
| 6788 | |
| 6789 | } |
| 6790 | |
| 6791 | // end catch_errno_guard.h |
| 6792 | #include <sstream> |
| 6793 | |
| 6794 | namespace Catch { |
| 6795 | namespace { |
| 6796 | |
| 6797 | struct IColourImpl { |
| 6798 | virtual ~IColourImpl() = default; |
| 6799 | virtual void use( Colour::Code _colourCode ) = 0; |
| 6800 | }; |
| 6801 | |
| 6802 | struct NoColourImpl : IColourImpl { |
| 6803 | void use( Colour::Code ) {} |
| 6804 | |
| 6805 | static IColourImpl* instance() { |
| 6806 | static NoColourImpl s_instance; |
| 6807 | return &s_instance; |
| 6808 | } |
| 6809 | }; |
| 6810 | |
| 6811 | } // anon namespace |
| 6812 | } // namespace Catch |
| 6813 | |
| 6814 | #if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) |
| 6815 | # ifdef CATCH_PLATFORM_WINDOWS |
| 6816 | # define CATCH_CONFIG_COLOUR_WINDOWS |
| 6817 | # else |
| 6818 | # define CATCH_CONFIG_COLOUR_ANSI |
| 6819 | # endif |
| 6820 | #endif |
| 6821 | |
| 6822 | #if defined ( CATCH_CONFIG_COLOUR_WINDOWS ) ///////////////////////////////////////// |
| 6823 | |
| 6824 | namespace Catch { |
| 6825 | namespace { |
| 6826 | |
| 6827 | class Win32ColourImpl : public IColourImpl { |
| 6828 | public: |
| 6829 | Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) ) |
| 6830 | { |
| 6831 | CONSOLE_SCREEN_BUFFER_INFO csbiInfo; |
| 6832 | GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo ); |
| 6833 | originalForegroundAttributes = csbiInfo.wAttributes & ~( BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY ); |
| 6834 | originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY ); |
| 6835 | } |
| 6836 | |
| 6837 | virtual void use( Colour::Code _colourCode ) override { |
| 6838 | switch( _colourCode ) { |
| 6839 | case Colour::None: return setTextAttribute( originalForegroundAttributes ); |
| 6840 | case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); |
| 6841 | case Colour::Red: return setTextAttribute( FOREGROUND_RED ); |
| 6842 | case Colour::Green: return setTextAttribute( FOREGROUND_GREEN ); |
| 6843 | case Colour::Blue: return setTextAttribute( FOREGROUND_BLUE ); |
| 6844 | case Colour::Cyan: return setTextAttribute( FOREGROUND_BLUE | FOREGROUND_GREEN ); |
| 6845 | case Colour::Yellow: return setTextAttribute( FOREGROUND_RED | FOREGROUND_GREEN ); |
| 6846 | case Colour::Grey: return setTextAttribute( 0 ); |
| 6847 | |
| 6848 | case Colour::LightGrey: return setTextAttribute( FOREGROUND_INTENSITY ); |
| 6849 | case Colour::BrightRed: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED ); |
| 6850 | case Colour::BrightGreen: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN ); |
| 6851 | case Colour::BrightWhite: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); |
| 6852 | case Colour::BrightYellow: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN ); |
| 6853 | |
| 6854 | case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" ); |
| 6855 | |
| 6856 | default: |
| 6857 | CATCH_ERROR( "Unknown colour requested" ); |
| 6858 | } |
| 6859 | } |
| 6860 | |
| 6861 | private: |
| 6862 | void setTextAttribute( WORD _textAttribute ) { |
| 6863 | SetConsoleTextAttribute( stdoutHandle, _textAttribute | originalBackgroundAttributes ); |
| 6864 | } |
| 6865 | HANDLE stdoutHandle; |
| 6866 | WORD originalForegroundAttributes; |
| 6867 | WORD originalBackgroundAttributes; |
| 6868 | }; |
| 6869 | |
| 6870 | IColourImpl* platformColourInstance() { |
| 6871 | static Win32ColourImpl s_instance; |
| 6872 | |
| 6873 | IConfigPtr config = getCurrentContext().getConfig(); |
| 6874 | UseColour::YesOrNo colourMode = config |
| 6875 | ? config->useColour() |
| 6876 | : UseColour::Auto; |
| 6877 | if( colourMode == UseColour::Auto ) |
| 6878 | colourMode = UseColour::Yes; |
| 6879 | return colourMode == UseColour::Yes |
| 6880 | ? &s_instance |
| 6881 | : NoColourImpl::instance(); |
| 6882 | } |
| 6883 | |
| 6884 | } // end anon namespace |
| 6885 | } // end namespace Catch |
| 6886 | |
| 6887 | #elif defined( CATCH_CONFIG_COLOUR_ANSI ) ////////////////////////////////////// |
| 6888 | |
| 6889 | #include <unistd.h> |
| 6890 | |
| 6891 | namespace Catch { |
| 6892 | namespace { |
| 6893 | |
| 6894 | // use POSIX/ ANSI console terminal codes |
| 6895 | // Thanks to Adam Strzelecki for original contribution |
| 6896 | // (http://github.com/nanoant) |
| 6897 | // https://github.com/philsquared/Catch/pull/131 |
| 6898 | class PosixColourImpl : public IColourImpl { |
| 6899 | public: |
| 6900 | virtual void use( Colour::Code _colourCode ) override { |
| 6901 | switch( _colourCode ) { |
| 6902 | case Colour::None: |
| 6903 | case Colour::White: return setColour( "[0m" ); |
| 6904 | case Colour::Red: return setColour( "[0;31m" ); |
| 6905 | case Colour::Green: return setColour( "[0;32m" ); |
| 6906 | case Colour::Blue: return setColour( "[0;34m" ); |
| 6907 | case Colour::Cyan: return setColour( "[0;36m" ); |
| 6908 | case Colour::Yellow: return setColour( "[0;33m" ); |
| 6909 | case Colour::Grey: return setColour( "[1;30m" ); |
| 6910 | |
| 6911 | case Colour::LightGrey: return setColour( "[0;37m" ); |
| 6912 | case Colour::BrightRed: return setColour( "[1;31m" ); |
| 6913 | case Colour::BrightGreen: return setColour( "[1;32m" ); |
| 6914 | case Colour::BrightWhite: return setColour( "[1;37m" ); |
| 6915 | case Colour::BrightYellow: return setColour( "[1;33m" ); |
| 6916 | |
| 6917 | case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" ); |
| 6918 | default: CATCH_INTERNAL_ERROR( "Unknown colour requested" ); |
| 6919 | } |
| 6920 | } |
| 6921 | static IColourImpl* instance() { |
| 6922 | static PosixColourImpl s_instance; |
| 6923 | return &s_instance; |
| 6924 | } |
| 6925 | |
| 6926 | private: |
| 6927 | void setColour( const char* _escapeCode ) { |
| 6928 | Catch::cout() << '\033' << _escapeCode; |
| 6929 | } |
| 6930 | }; |
| 6931 | |
| 6932 | bool useColourOnPlatform() { |
| 6933 | return |
| 6934 | #ifdef CATCH_PLATFORM_MAC |
| 6935 | !isDebuggerActive() && |
| 6936 | #endif |
| 6937 | #if !(defined(__DJGPP__) && defined(__STRICT_ANSI__)) |
| 6938 | isatty(STDOUT_FILENO) |
| 6939 | #else |
| 6940 | false |
| 6941 | #endif |
| 6942 | ; |
| 6943 | } |
| 6944 | IColourImpl* platformColourInstance() { |
| 6945 | ErrnoGuard guard; |
| 6946 | IConfigPtr config = getCurrentContext().getConfig(); |
| 6947 | UseColour::YesOrNo colourMode = config |
| 6948 | ? config->useColour() |
| 6949 | : UseColour::Auto; |
| 6950 | if( colourMode == UseColour::Auto ) |
| 6951 | colourMode = useColourOnPlatform() |
| 6952 | ? UseColour::Yes |
| 6953 | : UseColour::No; |
| 6954 | return colourMode == UseColour::Yes |
| 6955 | ? PosixColourImpl::instance() |
| 6956 | : NoColourImpl::instance(); |
| 6957 | } |
| 6958 | |
| 6959 | } // end anon namespace |
| 6960 | } // end namespace Catch |
| 6961 | |
| 6962 | #else // not Windows or ANSI /////////////////////////////////////////////// |
| 6963 | |
| 6964 | namespace Catch { |
| 6965 | |
| 6966 | static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); } |
| 6967 | |
| 6968 | } // end namespace Catch |
| 6969 | |
| 6970 | #endif // Windows/ ANSI/ None |
| 6971 | |
| 6972 | namespace Catch { |
| 6973 | |
| 6974 | Colour::Colour( Code _colourCode ) { use( _colourCode ); } |
| 6975 | Colour::Colour( Colour&& rhs ) noexcept { |
| 6976 | m_moved = rhs.m_moved; |
| 6977 | rhs.m_moved = true; |
| 6978 | } |
| 6979 | Colour& Colour::operator=( Colour&& rhs ) noexcept { |
| 6980 | m_moved = rhs.m_moved; |
| 6981 | rhs.m_moved = true; |
| 6982 | return *this; |
| 6983 | } |
| 6984 | |
| 6985 | Colour::~Colour(){ if( !m_moved ) use( None ); } |
| 6986 | |
| 6987 | void Colour::use( Code _colourCode ) { |
| 6988 | static IColourImpl* impl = platformColourInstance(); |
| 6989 | impl->use( _colourCode ); |
| 6990 | } |
| 6991 | |
| 6992 | std::ostream& operator << ( std::ostream& os, Colour const& ) { |
| 6993 | return os; |
| 6994 | } |
| 6995 | |
| 6996 | } // end namespace Catch |
| 6997 | |
| 6998 | #if defined(__clang__) |
| 6999 | # pragma clang diagnostic pop |
| 7000 | #endif |
| 7001 | |
| 7002 | // end catch_console_colour.cpp |
| 7003 | // start catch_context.cpp |
| 7004 | |
| 7005 | namespace Catch { |
| 7006 | |
| 7007 | class Context : public IMutableContext, NonCopyable { |
| 7008 | |
| 7009 | public: // IContext |
| 7010 | virtual IResultCapture* getResultCapture() override { |
| 7011 | return m_resultCapture; |
| 7012 | } |
| 7013 | virtual IRunner* getRunner() override { |
| 7014 | return m_runner; |
| 7015 | } |
| 7016 | |
| 7017 | virtual IConfigPtr const& getConfig() const override { |
| 7018 | return m_config; |
| 7019 | } |
| 7020 | |
| 7021 | virtual ~Context() override; |
| 7022 | |
| 7023 | public: // IMutableContext |
| 7024 | virtual void setResultCapture( IResultCapture* resultCapture ) override { |
| 7025 | m_resultCapture = resultCapture; |
| 7026 | } |
| 7027 | virtual void setRunner( IRunner* runner ) override { |
| 7028 | m_runner = runner; |
| 7029 | } |
| 7030 | virtual void setConfig( IConfigPtr const& config ) override { |
| 7031 | m_config = config; |
| 7032 | } |
| 7033 | |
| 7034 | friend IMutableContext& getCurrentMutableContext(); |
| 7035 | |
| 7036 | private: |
| 7037 | IConfigPtr m_config; |
| 7038 | IRunner* m_runner = nullptr; |
| 7039 | IResultCapture* m_resultCapture = nullptr; |
| 7040 | }; |
| 7041 | |
| 7042 | IMutableContext *IMutableContext::currentContext = nullptr; |
| 7043 | |
| 7044 | void IMutableContext::createContext() |
| 7045 | { |
| 7046 | currentContext = new Context(); |
| 7047 | } |
| 7048 | |
| 7049 | void cleanUpContext() { |
| 7050 | delete IMutableContext::currentContext; |
| 7051 | IMutableContext::currentContext = nullptr; |
| 7052 | } |
| 7053 | IContext::~IContext() = default; |
| 7054 | IMutableContext::~IMutableContext() = default; |
| 7055 | Context::~Context() = default; |
| 7056 | } |
| 7057 | // end catch_context.cpp |
| 7058 | // start catch_debug_console.cpp |
| 7059 | |
| 7060 | // start catch_debug_console.h |
| 7061 | |
| 7062 | #include <string> |
| 7063 | |
| 7064 | namespace Catch { |
| 7065 | void writeToDebugConsole( std::string const& text ); |
| 7066 | } |
| 7067 | |
| 7068 | // end catch_debug_console.h |
| 7069 | #ifdef CATCH_PLATFORM_WINDOWS |
| 7070 | |
| 7071 | namespace Catch { |
| 7072 | void writeToDebugConsole( std::string const& text ) { |
| 7073 | ::OutputDebugStringA( text.c_str() ); |
| 7074 | } |
| 7075 | } |
| 7076 | |
| 7077 | #else |
| 7078 | |
| 7079 | namespace Catch { |
| 7080 | void writeToDebugConsole( std::string const& text ) { |
| 7081 | // !TBD: Need a version for Mac/ XCode and other IDEs |
| 7082 | Catch::cout() << text; |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | #endif // Platform |
| 7087 | // end catch_debug_console.cpp |
| 7088 | // start catch_debugger.cpp |
| 7089 | |
| 7090 | #ifdef CATCH_PLATFORM_MAC |
| 7091 | |
| 7092 | # include <assert.h> |
| 7093 | # include <stdbool.h> |
| 7094 | # include <sys/types.h> |
| 7095 | # include <unistd.h> |
| 7096 | # include <sys/sysctl.h> |
| 7097 | # include <cstddef> |
| 7098 | # include <ostream> |
| 7099 | |
| 7100 | namespace Catch { |
| 7101 | |
| 7102 | // The following function is taken directly from the following technical note: |
| 7103 | // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html |
| 7104 | |
| 7105 | // Returns true if the current process is being debugged (either |
| 7106 | // running under the debugger or has a debugger attached post facto). |
| 7107 | bool isDebuggerActive(){ |
| 7108 | |
| 7109 | int mib[4]; |
| 7110 | struct kinfo_proc info; |
| 7111 | std::size_t size; |
| 7112 | |
| 7113 | // Initialize the flags so that, if sysctl fails for some bizarre |
| 7114 | // reason, we get a predictable result. |
| 7115 | |
| 7116 | info.kp_proc.p_flag = 0; |
| 7117 | |
| 7118 | // Initialize mib, which tells sysctl the info we want, in this case |
| 7119 | // we're looking for information about a specific process ID. |
| 7120 | |
| 7121 | mib[0] = CTL_KERN; |
| 7122 | mib[1] = KERN_PROC; |
| 7123 | mib[2] = KERN_PROC_PID; |
| 7124 | mib[3] = getpid(); |
| 7125 | |
| 7126 | // Call sysctl. |
| 7127 | |
| 7128 | size = sizeof(info); |
| 7129 | if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { |
| 7130 | Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; |
| 7131 | return false; |
| 7132 | } |
| 7133 | |
| 7134 | // We're being debugged if the P_TRACED flag is set. |
| 7135 | |
| 7136 | return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); |
| 7137 | } |
| 7138 | } // namespace Catch |
| 7139 | |
| 7140 | #elif defined(CATCH_PLATFORM_LINUX) |
| 7141 | #include <fstream> |
| 7142 | #include <string> |
| 7143 | |
| 7144 | namespace Catch{ |
| 7145 | // The standard POSIX way of detecting a debugger is to attempt to |
| 7146 | // ptrace() the process, but this needs to be done from a child and not |
| 7147 | // this process itself to still allow attaching to this process later |
| 7148 | // if wanted, so is rather heavy. Under Linux we have the PID of the |
| 7149 | // "debugger" (which doesn't need to be gdb, of course, it could also |
| 7150 | // be strace, for example) in /proc/$PID/status, so just get it from |
| 7151 | // there instead. |
| 7152 | bool isDebuggerActive(){ |
| 7153 | // Libstdc++ has a bug, where std::ifstream sets errno to 0 |
| 7154 | // This way our users can properly assert over errno values |
| 7155 | ErrnoGuard guard; |
| 7156 | std::ifstream in("/proc/self/status"); |
| 7157 | for( std::string line; std::getline(in, line); ) { |
| 7158 | static const int PREFIX_LEN = 11; |
| 7159 | if( line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0 ) { |
| 7160 | // We're traced if the PID is not 0 and no other PID starts |
| 7161 | // with 0 digit, so it's enough to check for just a single |
| 7162 | // character. |
| 7163 | return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0'; |
| 7164 | } |
| 7165 | } |
| 7166 | |
| 7167 | return false; |
| 7168 | } |
| 7169 | } // namespace Catch |
| 7170 | #elif defined(_MSC_VER) |
| 7171 | extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); |
| 7172 | namespace Catch { |
| 7173 | bool isDebuggerActive() { |
| 7174 | return IsDebuggerPresent() != 0; |
| 7175 | } |
| 7176 | } |
| 7177 | #elif defined(__MINGW32__) |
| 7178 | extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); |
| 7179 | namespace Catch { |
| 7180 | bool isDebuggerActive() { |
| 7181 | return IsDebuggerPresent() != 0; |
| 7182 | } |
| 7183 | } |
| 7184 | #else |
| 7185 | namespace Catch { |
| 7186 | bool isDebuggerActive() { return false; } |
| 7187 | } |
| 7188 | #endif // Platform |
| 7189 | // end catch_debugger.cpp |
| 7190 | // start catch_decomposer.cpp |
| 7191 | |
| 7192 | namespace Catch { |
| 7193 | |
| 7194 | ITransientExpression::~ITransientExpression() = default; |
| 7195 | |
| 7196 | void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ) { |
| 7197 | if( lhs.size() + rhs.size() < 40 && |
| 7198 | lhs.find('\n') == std::string::npos && |
| 7199 | rhs.find('\n') == std::string::npos ) |
| 7200 | os << lhs << " " << op << " " << rhs; |
| 7201 | else |
| 7202 | os << lhs << "\n" << op << "\n" << rhs; |
| 7203 | } |
| 7204 | } |
| 7205 | // end catch_decomposer.cpp |
| 7206 | // start catch_errno_guard.cpp |
| 7207 | |
| 7208 | #include <cerrno> |
| 7209 | |
| 7210 | namespace Catch { |
| 7211 | ErrnoGuard::ErrnoGuard():m_oldErrno(errno){} |
| 7212 | ErrnoGuard::~ErrnoGuard() { errno = m_oldErrno; } |
| 7213 | } |
| 7214 | // end catch_errno_guard.cpp |
| 7215 | // start catch_exception_translator_registry.cpp |
| 7216 | |
| 7217 | // start catch_exception_translator_registry.h |
| 7218 | |
| 7219 | #include <vector> |
| 7220 | #include <string> |
| 7221 | #include <memory> |
| 7222 | |
| 7223 | namespace Catch { |
| 7224 | |
| 7225 | class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { |
| 7226 | public: |
| 7227 | ~ExceptionTranslatorRegistry(); |
| 7228 | virtual void registerTranslator( const IExceptionTranslator* translator ); |
| 7229 | virtual std::string translateActiveException() const override; |
| 7230 | std::string tryTranslators() const; |
| 7231 | |
| 7232 | private: |
| 7233 | std::vector<std::unique_ptr<IExceptionTranslator const>> m_translators; |
| 7234 | }; |
| 7235 | } |
| 7236 | |
| 7237 | // end catch_exception_translator_registry.h |
| 7238 | #ifdef __OBJC__ |
| 7239 | #import "Foundation/Foundation.h" |
| 7240 | #endif |
| 7241 | |
| 7242 | namespace Catch { |
| 7243 | |
| 7244 | ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() { |
| 7245 | } |
| 7246 | |
| 7247 | void ExceptionTranslatorRegistry::registerTranslator( const IExceptionTranslator* translator ) { |
| 7248 | m_translators.push_back( std::unique_ptr<const IExceptionTranslator>( translator ) ); |
| 7249 | } |
| 7250 | |
| 7251 | std::string ExceptionTranslatorRegistry::translateActiveException() const { |
| 7252 | try { |
| 7253 | #ifdef __OBJC__ |
| 7254 | // In Objective-C try objective-c exceptions first |
| 7255 | @try { |
| 7256 | return tryTranslators(); |
| 7257 | } |
| 7258 | @catch (NSException *exception) { |
| 7259 | return Catch::Detail::stringify( [exception description] ); |
| 7260 | } |
| 7261 | #else |
| 7262 | // Compiling a mixed mode project with MSVC means that CLR |
| 7263 | // exceptions will be caught in (...) as well. However, these |
| 7264 | // do not fill-in std::current_exception and thus lead to crash |
| 7265 | // when attempting rethrow. |
| 7266 | // /EHa switch also causes structured exceptions to be caught |
| 7267 | // here, but they fill-in current_exception properly, so |
| 7268 | // at worst the output should be a little weird, instead of |
| 7269 | // causing a crash. |
| 7270 | if (std::current_exception() == nullptr) { |
| 7271 | return "Non C++ exception. Possibly a CLR exception."; |
| 7272 | } |
| 7273 | return tryTranslators(); |
| 7274 | #endif |
| 7275 | } |
| 7276 | catch( TestFailureException& ) { |
| 7277 | std::rethrow_exception(std::current_exception()); |
| 7278 | } |
| 7279 | catch( std::exception& ex ) { |
| 7280 | return ex.what(); |
| 7281 | } |
| 7282 | catch( std::string& msg ) { |
| 7283 | return msg; |
| 7284 | } |
| 7285 | catch( const char* msg ) { |
| 7286 | return msg; |
| 7287 | } |
| 7288 | catch(...) { |
| 7289 | return "Unknown exception"; |
| 7290 | } |
| 7291 | } |
| 7292 | |
| 7293 | std::string ExceptionTranslatorRegistry::tryTranslators() const { |
| 7294 | if( m_translators.empty() ) |
| 7295 | std::rethrow_exception(std::current_exception()); |
| 7296 | else |
| 7297 | return m_translators[0]->translate( m_translators.begin()+1, m_translators.end() ); |
| 7298 | } |
| 7299 | } |
| 7300 | // end catch_exception_translator_registry.cpp |
| 7301 | // start catch_fatal_condition.cpp |
| 7302 | |
| 7303 | #if defined(__GNUC__) |
| 7304 | # pragma GCC diagnostic push |
| 7305 | # pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
| 7306 | #endif |
| 7307 | |
| 7308 | #if defined( CATCH_CONFIG_WINDOWS_SEH ) || defined( CATCH_CONFIG_POSIX_SIGNALS ) |
| 7309 | |
| 7310 | namespace { |
| 7311 | // Report the error condition |
| 7312 | void reportFatal( char const * const message ) { |
| 7313 | Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message ); |
| 7314 | } |
| 7315 | } |
| 7316 | |
| 7317 | #endif // signals/SEH handling |
| 7318 | |
| 7319 | #if defined( CATCH_CONFIG_WINDOWS_SEH ) |
| 7320 | |
| 7321 | namespace Catch { |
| 7322 | struct SignalDefs { DWORD id; const char* name; }; |
| 7323 | |
| 7324 | // There is no 1-1 mapping between signals and windows exceptions. |
| 7325 | // Windows can easily distinguish between SO and SigSegV, |
| 7326 | // but SigInt, SigTerm, etc are handled differently. |
| 7327 | static SignalDefs signalDefs[] = { |
| 7328 | { EXCEPTION_ILLEGAL_INSTRUCTION, "SIGILL - Illegal instruction signal" }, |
| 7329 | { EXCEPTION_STACK_OVERFLOW, "SIGSEGV - Stack overflow" }, |
| 7330 | { EXCEPTION_ACCESS_VIOLATION, "SIGSEGV - Segmentation violation signal" }, |
| 7331 | { EXCEPTION_INT_DIVIDE_BY_ZERO, "Divide by zero error" }, |
| 7332 | }; |
| 7333 | |
| 7334 | LONG CALLBACK FatalConditionHandler::handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) { |
| 7335 | for (auto const& def : signalDefs) { |
| 7336 | if (ExceptionInfo->ExceptionRecord->ExceptionCode == def.id) { |
| 7337 | reportFatal(def.name); |
| 7338 | } |
| 7339 | } |
| 7340 | // If its not an exception we care about, pass it along. |
| 7341 | // This stops us from eating debugger breaks etc. |
| 7342 | return EXCEPTION_CONTINUE_SEARCH; |
| 7343 | } |
| 7344 | |
| 7345 | FatalConditionHandler::FatalConditionHandler() { |
| 7346 | isSet = true; |
| 7347 | // 32k seems enough for Catch to handle stack overflow, |
| 7348 | // but the value was found experimentally, so there is no strong guarantee |
| 7349 | guaranteeSize = 32 * 1024; |
| 7350 | exceptionHandlerHandle = nullptr; |
| 7351 | // Register as first handler in current chain |
| 7352 | exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException); |
| 7353 | // Pass in guarantee size to be filled |
| 7354 | SetThreadStackGuarantee(&guaranteeSize); |
| 7355 | } |
| 7356 | |
| 7357 | void FatalConditionHandler::reset() { |
| 7358 | if (isSet) { |
| 7359 | RemoveVectoredExceptionHandler(exceptionHandlerHandle); |
| 7360 | SetThreadStackGuarantee(&guaranteeSize); |
| 7361 | exceptionHandlerHandle = nullptr; |
| 7362 | isSet = false; |
| 7363 | } |
| 7364 | } |
| 7365 | |
| 7366 | FatalConditionHandler::~FatalConditionHandler() { |
| 7367 | reset(); |
| 7368 | } |
| 7369 | |
| 7370 | bool FatalConditionHandler::isSet = false; |
| 7371 | ULONG FatalConditionHandler::guaranteeSize = 0; |
| 7372 | PVOID FatalConditionHandler::exceptionHandlerHandle = nullptr; |
| 7373 | |
| 7374 | } // namespace Catch |
| 7375 | |
| 7376 | #elif defined( CATCH_CONFIG_POSIX_SIGNALS ) |
| 7377 | |
| 7378 | namespace Catch { |
| 7379 | |
| 7380 | struct SignalDefs { |
| 7381 | int id; |
| 7382 | const char* name; |
| 7383 | }; |
| 7384 | static SignalDefs signalDefs[] = { |
| 7385 | { SIGINT, "SIGINT - Terminal interrupt signal" }, |
| 7386 | { SIGILL, "SIGILL - Illegal instruction signal" }, |
| 7387 | { SIGFPE, "SIGFPE - Floating point error signal" }, |
| 7388 | { SIGSEGV, "SIGSEGV - Segmentation violation signal" }, |
| 7389 | { SIGTERM, "SIGTERM - Termination request signal" }, |
| 7390 | { SIGABRT, "SIGABRT - Abort (abnormal termination) signal" } |
| 7391 | }; |
| 7392 | |
| 7393 | void FatalConditionHandler::handleSignal( int sig ) { |
| 7394 | char const * name = "<unknown signal>"; |
| 7395 | for (auto const& def : signalDefs) { |
| 7396 | if (sig == def.id) { |
| 7397 | name = def.name; |
| 7398 | break; |
| 7399 | } |
| 7400 | } |
| 7401 | reset(); |
| 7402 | reportFatal(name); |
| 7403 | raise( sig ); |
| 7404 | } |
| 7405 | |
| 7406 | FatalConditionHandler::FatalConditionHandler() { |
| 7407 | isSet = true; |
| 7408 | stack_t sigStack; |
| 7409 | sigStack.ss_sp = altStackMem; |
| 7410 | sigStack.ss_size = SIGSTKSZ; |
| 7411 | sigStack.ss_flags = 0; |
| 7412 | sigaltstack(&sigStack, &oldSigStack); |
| 7413 | struct sigaction sa = { }; |
| 7414 | |
| 7415 | sa.sa_handler = handleSignal; |
| 7416 | sa.sa_flags = SA_ONSTACK; |
| 7417 | for (std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i) { |
| 7418 | sigaction(signalDefs[i].id, &sa, &oldSigActions[i]); |
| 7419 | } |
| 7420 | } |
| 7421 | |
| 7422 | FatalConditionHandler::~FatalConditionHandler() { |
| 7423 | reset(); |
| 7424 | } |
| 7425 | |
| 7426 | void FatalConditionHandler::reset() { |
| 7427 | if( isSet ) { |
| 7428 | // Set signals back to previous values -- hopefully nobody overwrote them in the meantime |
| 7429 | for( std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i ) { |
| 7430 | sigaction(signalDefs[i].id, &oldSigActions[i], nullptr); |
| 7431 | } |
| 7432 | // Return the old stack |
| 7433 | sigaltstack(&oldSigStack, nullptr); |
| 7434 | isSet = false; |
| 7435 | } |
| 7436 | } |
| 7437 | |
| 7438 | bool FatalConditionHandler::isSet = false; |
| 7439 | struct sigaction FatalConditionHandler::oldSigActions[sizeof(signalDefs)/sizeof(SignalDefs)] = {}; |
| 7440 | stack_t FatalConditionHandler::oldSigStack = {}; |
| 7441 | char FatalConditionHandler::altStackMem[SIGSTKSZ] = {}; |
| 7442 | |
| 7443 | } // namespace Catch |
| 7444 | |
| 7445 | #else |
| 7446 | |
| 7447 | namespace Catch { |
| 7448 | void FatalConditionHandler::reset() {} |
| 7449 | } |
| 7450 | |
| 7451 | #endif // signals/SEH handling |
| 7452 | |
| 7453 | #if defined(__GNUC__) |
| 7454 | # pragma GCC diagnostic pop |
| 7455 | #endif |
| 7456 | // end catch_fatal_condition.cpp |
| 7457 | // start catch_interfaces_capture.cpp |
| 7458 | |
| 7459 | namespace Catch { |
| 7460 | IResultCapture::~IResultCapture() = default; |
| 7461 | } |
| 7462 | // end catch_interfaces_capture.cpp |
| 7463 | // start catch_interfaces_config.cpp |
| 7464 | |
| 7465 | namespace Catch { |
| 7466 | IConfig::~IConfig() = default; |
| 7467 | } |
| 7468 | // end catch_interfaces_config.cpp |
| 7469 | // start catch_interfaces_exception.cpp |
| 7470 | |
| 7471 | namespace Catch { |
| 7472 | IExceptionTranslator::~IExceptionTranslator() = default; |
| 7473 | IExceptionTranslatorRegistry::~IExceptionTranslatorRegistry() = default; |
| 7474 | } |
| 7475 | // end catch_interfaces_exception.cpp |
| 7476 | // start catch_interfaces_registry_hub.cpp |
| 7477 | |
| 7478 | namespace Catch { |
| 7479 | IRegistryHub::~IRegistryHub() = default; |
| 7480 | IMutableRegistryHub::~IMutableRegistryHub() = default; |
| 7481 | } |
| 7482 | // end catch_interfaces_registry_hub.cpp |
| 7483 | // start catch_interfaces_reporter.cpp |
| 7484 | |
| 7485 | // start catch_reporter_multi.h |
| 7486 | |
| 7487 | namespace Catch { |
| 7488 | |
| 7489 | class MultipleReporters : public IStreamingReporter { |
| 7490 | using Reporters = std::vector<IStreamingReporterPtr>; |
| 7491 | Reporters m_reporters; |
| 7492 | |
| 7493 | public: |
| 7494 | void add( IStreamingReporterPtr&& reporter ); |
| 7495 | |
| 7496 | public: // IStreamingReporter |
| 7497 | |
| 7498 | ReporterPreferences getPreferences() const override; |
| 7499 | |
| 7500 | void noMatchingTestCases( std::string const& spec ) override; |
| 7501 | |
| 7502 | static std::set<Verbosity> getSupportedVerbosities(); |
| 7503 | |
| 7504 | void benchmarkStarting( BenchmarkInfo const& benchmarkInfo ) override; |
| 7505 | void benchmarkEnded( BenchmarkStats const& benchmarkStats ) override; |
| 7506 | |
| 7507 | void testRunStarting( TestRunInfo const& testRunInfo ) override; |
| 7508 | void testGroupStarting( GroupInfo const& groupInfo ) override; |
| 7509 | void testCaseStarting( TestCaseInfo const& testInfo ) override; |
| 7510 | void sectionStarting( SectionInfo const& sectionInfo ) override; |
| 7511 | void assertionStarting( AssertionInfo const& assertionInfo ) override; |
| 7512 | |
| 7513 | // The return value indicates if the messages buffer should be cleared: |
| 7514 | bool assertionEnded( AssertionStats const& assertionStats ) override; |
| 7515 | void sectionEnded( SectionStats const& sectionStats ) override; |
| 7516 | void testCaseEnded( TestCaseStats const& testCaseStats ) override; |
| 7517 | void testGroupEnded( TestGroupStats const& testGroupStats ) override; |
| 7518 | void testRunEnded( TestRunStats const& testRunStats ) override; |
| 7519 | |
| 7520 | void skipTest( TestCaseInfo const& testInfo ) override; |
| 7521 | bool isMulti() const override; |
| 7522 | |
| 7523 | }; |
| 7524 | |
| 7525 | } // end namespace Catch |
| 7526 | |
| 7527 | // end catch_reporter_multi.h |
| 7528 | namespace Catch { |
| 7529 | |
| 7530 | ReporterConfig::ReporterConfig( IConfigPtr const& _fullConfig ) |
| 7531 | : m_stream( &_fullConfig->stream() ), m_fullConfig( _fullConfig ) {} |
| 7532 | |
| 7533 | ReporterConfig::ReporterConfig( IConfigPtr const& _fullConfig, std::ostream& _stream ) |
| 7534 | : m_stream( &_stream ), m_fullConfig( _fullConfig ) {} |
| 7535 | |
| 7536 | std::ostream& ReporterConfig::stream() const { return *m_stream; } |
| 7537 | IConfigPtr ReporterConfig::fullConfig() const { return m_fullConfig; } |
| 7538 | |
| 7539 | TestRunInfo::TestRunInfo( std::string const& _name ) : name( _name ) {} |
| 7540 | |
| 7541 | GroupInfo::GroupInfo( std::string const& _name, |
| 7542 | std::size_t _groupIndex, |
| 7543 | std::size_t _groupsCount ) |
| 7544 | : name( _name ), |
| 7545 | groupIndex( _groupIndex ), |
| 7546 | groupsCounts( _groupsCount ) |
| 7547 | {} |
| 7548 | |
| 7549 | AssertionStats::AssertionStats( AssertionResult const& _assertionResult, |
| 7550 | std::vector<MessageInfo> const& _infoMessages, |
| 7551 | Totals const& _totals ) |
| 7552 | : assertionResult( _assertionResult ), |
| 7553 | infoMessages( _infoMessages ), |
| 7554 | totals( _totals ) |
| 7555 | { |
| 7556 | assertionResult.m_resultData.lazyExpression.m_transientExpression = _assertionResult.m_resultData.lazyExpression.m_transientExpression; |
| 7557 | |
| 7558 | if( assertionResult.hasMessage() ) { |
| 7559 | // Copy message into messages list. |
| 7560 | // !TBD This should have been done earlier, somewhere |
| 7561 | MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() ); |
| 7562 | builder << assertionResult.getMessage(); |
| 7563 | builder.m_info.message = builder.m_stream.str(); |
| 7564 | |
| 7565 | infoMessages.push_back( builder.m_info ); |
| 7566 | } |
| 7567 | } |
| 7568 | |
| 7569 | AssertionStats::~AssertionStats() = default; |
| 7570 | |
| 7571 | SectionStats::SectionStats( SectionInfo const& _sectionInfo, |
| 7572 | Counts const& _assertions, |
| 7573 | double _durationInSeconds, |
| 7574 | bool _missingAssertions ) |
| 7575 | : sectionInfo( _sectionInfo ), |
| 7576 | assertions( _assertions ), |
| 7577 | durationInSeconds( _durationInSeconds ), |
| 7578 | missingAssertions( _missingAssertions ) |
| 7579 | {} |
| 7580 | |
| 7581 | SectionStats::~SectionStats() = default; |
| 7582 | |
| 7583 | TestCaseStats::TestCaseStats( TestCaseInfo const& _testInfo, |
| 7584 | Totals const& _totals, |
| 7585 | std::string const& _stdOut, |
| 7586 | std::string const& _stdErr, |
| 7587 | bool _aborting ) |
| 7588 | : testInfo( _testInfo ), |
| 7589 | totals( _totals ), |
| 7590 | stdOut( _stdOut ), |
| 7591 | stdErr( _stdErr ), |
| 7592 | aborting( _aborting ) |
| 7593 | {} |
| 7594 | |
| 7595 | TestCaseStats::~TestCaseStats() = default; |
| 7596 | |
| 7597 | TestGroupStats::TestGroupStats( GroupInfo const& _groupInfo, |
| 7598 | Totals const& _totals, |
| 7599 | bool _aborting ) |
| 7600 | : groupInfo( _groupInfo ), |
| 7601 | totals( _totals ), |
| 7602 | aborting( _aborting ) |
| 7603 | {} |
| 7604 | |
| 7605 | TestGroupStats::TestGroupStats( GroupInfo const& _groupInfo ) |
| 7606 | : groupInfo( _groupInfo ), |
| 7607 | aborting( false ) |
| 7608 | {} |
| 7609 | |
| 7610 | TestGroupStats::~TestGroupStats() = default; |
| 7611 | |
| 7612 | TestRunStats::TestRunStats( TestRunInfo const& _runInfo, |
| 7613 | Totals const& _totals, |
| 7614 | bool _aborting ) |
| 7615 | : runInfo( _runInfo ), |
| 7616 | totals( _totals ), |
| 7617 | aborting( _aborting ) |
| 7618 | {} |
| 7619 | |
| 7620 | TestRunStats::~TestRunStats() = default; |
| 7621 | |
| 7622 | void IStreamingReporter::fatalErrorEncountered( StringRef ) {} |
| 7623 | bool IStreamingReporter::isMulti() const { return false; } |
| 7624 | |
| 7625 | IReporterFactory::~IReporterFactory() = default; |
| 7626 | IReporterRegistry::~IReporterRegistry() = default; |
| 7627 | |
| 7628 | void addReporter( IStreamingReporterPtr& existingReporter, IStreamingReporterPtr&& additionalReporter ) { |
| 7629 | |
| 7630 | if( !existingReporter ) { |
| 7631 | existingReporter = std::move( additionalReporter ); |
| 7632 | return; |
| 7633 | } |
| 7634 | |
| 7635 | MultipleReporters* multi = nullptr; |
| 7636 | |
| 7637 | if( existingReporter->isMulti() ) { |
| 7638 | multi = static_cast<MultipleReporters*>( existingReporter.get() ); |
| 7639 | } |
| 7640 | else { |
| 7641 | auto newMulti = std::unique_ptr<MultipleReporters>( new MultipleReporters ); |
| 7642 | newMulti->add( std::move( existingReporter ) ); |
| 7643 | multi = newMulti.get(); |
| 7644 | existingReporter = std::move( newMulti ); |
| 7645 | } |
| 7646 | multi->add( std::move( additionalReporter ) ); |
| 7647 | } |
| 7648 | |
| 7649 | } // end namespace Catch |
| 7650 | // end catch_interfaces_reporter.cpp |
| 7651 | // start catch_interfaces_runner.cpp |
| 7652 | |
| 7653 | namespace Catch { |
| 7654 | IRunner::~IRunner() = default; |
| 7655 | } |
| 7656 | // end catch_interfaces_runner.cpp |
| 7657 | // start catch_interfaces_testcase.cpp |
| 7658 | |
| 7659 | namespace Catch { |
| 7660 | ITestInvoker::~ITestInvoker() = default; |
| 7661 | ITestCaseRegistry::~ITestCaseRegistry() = default; |
| 7662 | } |
| 7663 | // end catch_interfaces_testcase.cpp |
| 7664 | // start catch_leak_detector.cpp |
| 7665 | |
| 7666 | #ifdef CATCH_CONFIG_WINDOWS_CRTDBG |
| 7667 | #include <crtdbg.h> |
| 7668 | |
| 7669 | namespace Catch { |
| 7670 | |
| 7671 | LeakDetector::LeakDetector() { |
| 7672 | int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); |
| 7673 | flag |= _CRTDBG_LEAK_CHECK_DF; |
| 7674 | flag |= _CRTDBG_ALLOC_MEM_DF; |
| 7675 | _CrtSetDbgFlag(flag); |
| 7676 | _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); |
| 7677 | _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); |
| 7678 | // Change this to leaking allocation's number to break there |
| 7679 | _CrtSetBreakAlloc(-1); |
| 7680 | } |
| 7681 | } |
| 7682 | |
| 7683 | #else |
| 7684 | |
| 7685 | Catch::LeakDetector::LeakDetector() {} |
| 7686 | |
| 7687 | #endif |
| 7688 | // end catch_leak_detector.cpp |
| 7689 | // start catch_list.cpp |
| 7690 | |
| 7691 | // start catch_list.h |
| 7692 | |
| 7693 | #include <set> |
| 7694 | |
| 7695 | namespace Catch { |
| 7696 | |
| 7697 | std::size_t listTests( Config const& config ); |
| 7698 | |
| 7699 | std::size_t listTestsNamesOnly( Config const& config ); |
| 7700 | |
| 7701 | struct TagInfo { |
| 7702 | void add( std::string const& spelling ); |
| 7703 | std::string all() const; |
| 7704 | |
| 7705 | std::set<std::string> spellings; |
| 7706 | std::size_t count = 0; |
| 7707 | }; |
| 7708 | |
| 7709 | std::size_t listTags( Config const& config ); |
| 7710 | |
| 7711 | std::size_t listReporters( Config const& /*config*/ ); |
| 7712 | |
| 7713 | Option<std::size_t> list( Config const& config ); |
| 7714 | |
| 7715 | } // end namespace Catch |
| 7716 | |
| 7717 | // end catch_list.h |
| 7718 | // start catch_text.h |
| 7719 | |
| 7720 | namespace Catch { |
| 7721 | using namespace clara::TextFlow; |
| 7722 | } |
| 7723 | |
| 7724 | // end catch_text.h |
| 7725 | #include <limits> |
| 7726 | #include <algorithm> |
| 7727 | #include <iomanip> |
| 7728 | |
| 7729 | namespace Catch { |
| 7730 | |
| 7731 | std::size_t listTests( Config const& config ) { |
| 7732 | TestSpec testSpec = config.testSpec(); |
| 7733 | if( config.hasTestFilters() ) |
| 7734 | Catch::cout() << "Matching test cases:\n"; |
| 7735 | else { |
| 7736 | Catch::cout() << "All available test cases:\n"; |
| 7737 | } |
| 7738 | |
| 7739 | auto matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); |
| 7740 | for( auto const& testCaseInfo : matchedTestCases ) { |
| 7741 | Colour::Code colour = testCaseInfo.isHidden() |
| 7742 | ? Colour::SecondaryText |
| 7743 | : Colour::None; |
| 7744 | Colour colourGuard( colour ); |
| 7745 | |
| 7746 | Catch::cout() << Column( testCaseInfo.name ).initialIndent( 2 ).indent( 4 ) << "\n"; |
| 7747 | if( config.verbosity() >= Verbosity::High ) { |
| 7748 | Catch::cout() << Column( Catch::Detail::stringify( testCaseInfo.lineInfo ) ).indent(4) << std::endl; |
| 7749 | std::string description = testCaseInfo.description; |
| 7750 | if( description.empty() ) |
| 7751 | description = "(NO DESCRIPTION)"; |
| 7752 | Catch::cout() << Column( description ).indent(4) << std::endl; |
| 7753 | } |
| 7754 | if( !testCaseInfo.tags.empty() ) |
| 7755 | Catch::cout() << Column( testCaseInfo.tagsAsString() ).indent( 6 ) << "\n"; |
| 7756 | } |
| 7757 | |
| 7758 | if( !config.hasTestFilters() ) |
| 7759 | Catch::cout() << pluralise( matchedTestCases.size(), "test case" ) << '\n' << std::endl; |
| 7760 | else |
| 7761 | Catch::cout() << pluralise( matchedTestCases.size(), "matching test case" ) << '\n' << std::endl; |
| 7762 | return matchedTestCases.size(); |
| 7763 | } |
| 7764 | |
| 7765 | std::size_t listTestsNamesOnly( Config const& config ) { |
| 7766 | TestSpec testSpec = config.testSpec(); |
| 7767 | std::size_t matchedTests = 0; |
| 7768 | std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); |
| 7769 | for( auto const& testCaseInfo : matchedTestCases ) { |
| 7770 | matchedTests++; |
| 7771 | if( startsWith( testCaseInfo.name, '#' ) ) |
| 7772 | Catch::cout() << '"' << testCaseInfo.name << '"'; |
| 7773 | else |
| 7774 | Catch::cout() << testCaseInfo.name; |
| 7775 | if ( config.verbosity() >= Verbosity::High ) |
| 7776 | Catch::cout() << "\t@" << testCaseInfo.lineInfo; |
| 7777 | Catch::cout() << std::endl; |
| 7778 | } |
| 7779 | return matchedTests; |
| 7780 | } |
| 7781 | |
| 7782 | void TagInfo::add( std::string const& spelling ) { |
| 7783 | ++count; |
| 7784 | spellings.insert( spelling ); |
| 7785 | } |
| 7786 | |
| 7787 | std::string TagInfo::all() const { |
| 7788 | std::string out; |
| 7789 | for( auto const& spelling : spellings ) |
| 7790 | out += "[" + spelling + "]"; |
| 7791 | return out; |
| 7792 | } |
| 7793 | |
| 7794 | std::size_t listTags( Config const& config ) { |
| 7795 | TestSpec testSpec = config.testSpec(); |
| 7796 | if( config.hasTestFilters() ) |
| 7797 | Catch::cout() << "Tags for matching test cases:\n"; |
| 7798 | else { |
| 7799 | Catch::cout() << "All available tags:\n"; |
| 7800 | } |
| 7801 | |
| 7802 | std::map<std::string, TagInfo> tagCounts; |
| 7803 | |
| 7804 | std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); |
| 7805 | for( auto const& testCase : matchedTestCases ) { |
| 7806 | for( auto const& tagName : testCase.getTestCaseInfo().tags ) { |
| 7807 | std::string lcaseTagName = toLower( tagName ); |
| 7808 | auto countIt = tagCounts.find( lcaseTagName ); |
| 7809 | if( countIt == tagCounts.end() ) |
| 7810 | countIt = tagCounts.insert( std::make_pair( lcaseTagName, TagInfo() ) ).first; |
| 7811 | countIt->second.add( tagName ); |
| 7812 | } |
| 7813 | } |
| 7814 | |
| 7815 | for( auto const& tagCount : tagCounts ) { |
| 7816 | ReusableStringStream rss; |
| 7817 | rss << " " << std::setw(2) << tagCount.second.count << " "; |
| 7818 | auto str = rss.str(); |
| 7819 | auto wrapper = Column( tagCount.second.all() ) |
| 7820 | .initialIndent( 0 ) |
| 7821 | .indent( str.size() ) |
| 7822 | .width( CATCH_CONFIG_CONSOLE_WIDTH-10 ); |
| 7823 | Catch::cout() << str << wrapper << '\n'; |
| 7824 | } |
| 7825 | Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl; |
| 7826 | return tagCounts.size(); |
| 7827 | } |
| 7828 | |
| 7829 | std::size_t listReporters( Config const& /*config*/ ) { |
| 7830 | Catch::cout() << "Available reporters:\n"; |
| 7831 | IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); |
| 7832 | std::size_t maxNameLen = 0; |
| 7833 | for( auto const& factoryKvp : factories ) |
| 7834 | maxNameLen = (std::max)( maxNameLen, factoryKvp.first.size() ); |
| 7835 | |
| 7836 | for( auto const& factoryKvp : factories ) { |
| 7837 | Catch::cout() |
| 7838 | << Column( factoryKvp.first + ":" ) |
| 7839 | .indent(2) |
| 7840 | .width( 5+maxNameLen ) |
| 7841 | + Column( factoryKvp.second->getDescription() ) |
| 7842 | .initialIndent(0) |
| 7843 | .indent(2) |
| 7844 | .width( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 ) |
| 7845 | << "\n"; |
| 7846 | } |
| 7847 | Catch::cout() << std::endl; |
| 7848 | return factories.size(); |
| 7849 | } |
| 7850 | |
| 7851 | Option<std::size_t> list( Config const& config ) { |
| 7852 | Option<std::size_t> listedCount; |
| 7853 | if( config.listTests() ) |
| 7854 | listedCount = listedCount.valueOr(0) + listTests( config ); |
| 7855 | if( config.listTestNamesOnly() ) |
| 7856 | listedCount = listedCount.valueOr(0) + listTestsNamesOnly( config ); |
| 7857 | if( config.listTags() ) |
| 7858 | listedCount = listedCount.valueOr(0) + listTags( config ); |
| 7859 | if( config.listReporters() ) |
| 7860 | listedCount = listedCount.valueOr(0) + listReporters( config ); |
| 7861 | return listedCount; |
| 7862 | } |
| 7863 | |
| 7864 | } // end namespace Catch |
| 7865 | // end catch_list.cpp |
| 7866 | // start catch_matchers.cpp |
| 7867 | |
| 7868 | namespace Catch { |
| 7869 | namespace Matchers { |
| 7870 | namespace Impl { |
| 7871 | |
| 7872 | std::string MatcherUntypedBase::toString() const { |
| 7873 | if( m_cachedToString.empty() ) |
| 7874 | m_cachedToString = describe(); |
| 7875 | return m_cachedToString; |
| 7876 | } |
| 7877 | |
| 7878 | MatcherUntypedBase::~MatcherUntypedBase() = default; |
| 7879 | |
| 7880 | } // namespace Impl |
| 7881 | } // namespace Matchers |
| 7882 | |
| 7883 | using namespace Matchers; |
| 7884 | using Matchers::Impl::MatcherBase; |
| 7885 | |
| 7886 | } // namespace Catch |
| 7887 | // end catch_matchers.cpp |
| 7888 | // start catch_matchers_floating.cpp |
| 7889 | |
| 7890 | #include <cstdlib> |
| 7891 | #include <cstdint> |
| 7892 | #include <cstring> |
| 7893 | #include <stdexcept> |
| 7894 | |
| 7895 | namespace Catch { |
| 7896 | namespace Matchers { |
| 7897 | namespace Floating { |
| 7898 | enum class FloatingPointKind : uint8_t { |
| 7899 | Float, |
| 7900 | Double |
| 7901 | }; |
| 7902 | } |
| 7903 | } |
| 7904 | } |
| 7905 | |
| 7906 | namespace { |
| 7907 | |
| 7908 | template <typename T> |
| 7909 | struct Converter; |
| 7910 | |
| 7911 | template <> |
| 7912 | struct Converter<float> { |
| 7913 | static_assert(sizeof(float) == sizeof(int32_t), "Important ULP matcher assumption violated"); |
| 7914 | Converter(float f) { |
| 7915 | std::memcpy(&i, &f, sizeof(f)); |
| 7916 | } |
| 7917 | int32_t i; |
| 7918 | }; |
| 7919 | |
| 7920 | template <> |
| 7921 | struct Converter<double> { |
| 7922 | static_assert(sizeof(double) == sizeof(int64_t), "Important ULP matcher assumption violated"); |
| 7923 | Converter(double d) { |
| 7924 | std::memcpy(&i, &d, sizeof(d)); |
| 7925 | } |
| 7926 | int64_t i; |
| 7927 | }; |
| 7928 | |
| 7929 | template <typename T> |
| 7930 | auto convert(T t) -> Converter<T> { |
| 7931 | return Converter<T>(t); |
| 7932 | } |
| 7933 | |
| 7934 | template <typename FP> |
| 7935 | bool almostEqualUlps(FP lhs, FP rhs, int maxUlpDiff) { |
| 7936 | // Comparison with NaN should always be false. |
| 7937 | // This way we can rule it out before getting into the ugly details |
| 7938 | if (std::isnan(lhs) || std::isnan(rhs)) { |
| 7939 | return false; |
| 7940 | } |
| 7941 | |
| 7942 | auto lc = convert(lhs); |
| 7943 | auto rc = convert(rhs); |
| 7944 | |
| 7945 | if ((lc.i < 0) != (rc.i < 0)) { |
| 7946 | // Potentially we can have +0 and -0 |
| 7947 | return lhs == rhs; |
| 7948 | } |
| 7949 | |
| 7950 | auto ulpDiff = std::abs(lc.i - rc.i); |
| 7951 | return ulpDiff <= maxUlpDiff; |
| 7952 | } |
| 7953 | |
| 7954 | } |
| 7955 | |
| 7956 | namespace Catch { |
| 7957 | namespace Matchers { |
| 7958 | namespace Floating { |
| 7959 | WithinAbsMatcher::WithinAbsMatcher(double target, double margin) |
| 7960 | :m_target{ target }, m_margin{ margin } { |
| 7961 | if (m_margin < 0) { |
| 7962 | throw std::domain_error("Allowed margin difference has to be >= 0"); |
| 7963 | } |
| 7964 | } |
| 7965 | |
| 7966 | // Performs equivalent check of std::fabs(lhs - rhs) <= margin |
| 7967 | // But without the subtraction to allow for INFINITY in comparison |
| 7968 | bool WithinAbsMatcher::match(double const& matchee) const { |
| 7969 | return (matchee + m_margin >= m_target) && (m_target + m_margin >= matchee); |
| 7970 | } |
| 7971 | |
| 7972 | std::string WithinAbsMatcher::describe() const { |
| 7973 | return "is within " + ::Catch::Detail::stringify(m_margin) + " of " + ::Catch::Detail::stringify(m_target); |
| 7974 | } |
| 7975 | |
| 7976 | WithinUlpsMatcher::WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType) |
| 7977 | :m_target{ target }, m_ulps{ ulps }, m_type{ baseType } { |
| 7978 | if (m_ulps < 0) { |
| 7979 | throw std::domain_error("Allowed ulp difference has to be >= 0"); |
| 7980 | } |
| 7981 | } |
| 7982 | |
| 7983 | bool WithinUlpsMatcher::match(double const& matchee) const { |
| 7984 | switch (m_type) { |
| 7985 | case FloatingPointKind::Float: |
| 7986 | return almostEqualUlps<float>(static_cast<float>(matchee), static_cast<float>(m_target), m_ulps); |
| 7987 | case FloatingPointKind::Double: |
| 7988 | return almostEqualUlps<double>(matchee, m_target, m_ulps); |
| 7989 | default: |
| 7990 | throw std::domain_error("Unknown FloatingPointKind value"); |
| 7991 | } |
| 7992 | } |
| 7993 | |
| 7994 | std::string WithinUlpsMatcher::describe() const { |
| 7995 | return "is within " + std::to_string(m_ulps) + " ULPs of " + ::Catch::Detail::stringify(m_target) + ((m_type == FloatingPointKind::Float)? "f" : ""); |
| 7996 | } |
| 7997 | |
| 7998 | }// namespace Floating |
| 7999 | |
| 8000 | Floating::WithinUlpsMatcher WithinULP(double target, int maxUlpDiff) { |
| 8001 | return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Double); |
| 8002 | } |
| 8003 | |
| 8004 | Floating::WithinUlpsMatcher WithinULP(float target, int maxUlpDiff) { |
| 8005 | return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Float); |
| 8006 | } |
| 8007 | |
| 8008 | Floating::WithinAbsMatcher WithinAbs(double target, double margin) { |
| 8009 | return Floating::WithinAbsMatcher(target, margin); |
| 8010 | } |
| 8011 | |
| 8012 | } // namespace Matchers |
| 8013 | } // namespace Catch |
| 8014 | |
| 8015 | // end catch_matchers_floating.cpp |
| 8016 | // start catch_matchers_generic.cpp |
| 8017 | |
| 8018 | std::string Catch::Matchers::Generic::Detail::finalizeDescription(const std::string& desc) { |
| 8019 | if (desc.empty()) { |
| 8020 | return "matches undescribed predicate"; |
| 8021 | } else { |
| 8022 | return "matches predicate: \"" + desc + '"'; |
| 8023 | } |
| 8024 | } |
| 8025 | // end catch_matchers_generic.cpp |
| 8026 | // start catch_matchers_string.cpp |
| 8027 | |
| 8028 | #include <regex> |
| 8029 | |
| 8030 | namespace Catch { |
| 8031 | namespace Matchers { |
| 8032 | |
| 8033 | namespace StdString { |
| 8034 | |
| 8035 | CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) |
| 8036 | : m_caseSensitivity( caseSensitivity ), |
| 8037 | m_str( adjustString( str ) ) |
| 8038 | {} |
| 8039 | std::string CasedString::adjustString( std::string const& str ) const { |
| 8040 | return m_caseSensitivity == CaseSensitive::No |
| 8041 | ? toLower( str ) |
| 8042 | : str; |
| 8043 | } |
| 8044 | std::string CasedString::caseSensitivitySuffix() const { |
| 8045 | return m_caseSensitivity == CaseSensitive::No |
| 8046 | ? " (case insensitive)" |
| 8047 | : std::string(); |
| 8048 | } |
| 8049 | |
| 8050 | StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator ) |
| 8051 | : m_comparator( comparator ), |
| 8052 | m_operation( operation ) { |
| 8053 | } |
| 8054 | |
| 8055 | std::string StringMatcherBase::describe() const { |
| 8056 | std::string description; |
| 8057 | description.reserve(5 + m_operation.size() + m_comparator.m_str.size() + |
| 8058 | m_comparator.caseSensitivitySuffix().size()); |
| 8059 | description += m_operation; |
| 8060 | description += ": \""; |
| 8061 | description += m_comparator.m_str; |
| 8062 | description += "\""; |
| 8063 | description += m_comparator.caseSensitivitySuffix(); |
| 8064 | return description; |
| 8065 | } |
| 8066 | |
| 8067 | EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} |
| 8068 | |
| 8069 | bool EqualsMatcher::match( std::string const& source ) const { |
| 8070 | return m_comparator.adjustString( source ) == m_comparator.m_str; |
| 8071 | } |
| 8072 | |
| 8073 | ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} |
| 8074 | |
| 8075 | bool ContainsMatcher::match( std::string const& source ) const { |
| 8076 | return contains( m_comparator.adjustString( source ), m_comparator.m_str ); |
| 8077 | } |
| 8078 | |
| 8079 | StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {} |
| 8080 | |
| 8081 | bool StartsWithMatcher::match( std::string const& source ) const { |
| 8082 | return startsWith( m_comparator.adjustString( source ), m_comparator.m_str ); |
| 8083 | } |
| 8084 | |
| 8085 | EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {} |
| 8086 | |
| 8087 | bool EndsWithMatcher::match( std::string const& source ) const { |
| 8088 | return endsWith( m_comparator.adjustString( source ), m_comparator.m_str ); |
| 8089 | } |
| 8090 | |
| 8091 | RegexMatcher::RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {} |
| 8092 | |
| 8093 | bool RegexMatcher::match(std::string const& matchee) const { |
| 8094 | auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway |
| 8095 | if (m_caseSensitivity == CaseSensitive::Choice::No) { |
| 8096 | flags |= std::regex::icase; |
| 8097 | } |
| 8098 | auto reg = std::regex(m_regex, flags); |
| 8099 | return std::regex_match(matchee, reg); |
| 8100 | } |
| 8101 | |
| 8102 | std::string RegexMatcher::describe() const { |
| 8103 | return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively"); |
| 8104 | } |
| 8105 | |
| 8106 | } // namespace StdString |
| 8107 | |
| 8108 | StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { |
| 8109 | return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) ); |
| 8110 | } |
| 8111 | StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { |
| 8112 | return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) ); |
| 8113 | } |
| 8114 | StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { |
| 8115 | return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) ); |
| 8116 | } |
| 8117 | StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { |
| 8118 | return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) ); |
| 8119 | } |
| 8120 | |
| 8121 | StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) { |
| 8122 | return StdString::RegexMatcher(regex, caseSensitivity); |
| 8123 | } |
| 8124 | |
| 8125 | } // namespace Matchers |
| 8126 | } // namespace Catch |
| 8127 | // end catch_matchers_string.cpp |
| 8128 | // start catch_message.cpp |
| 8129 | |
| 8130 | // start catch_uncaught_exceptions.h |
| 8131 | |
| 8132 | namespace Catch { |
| 8133 | bool uncaught_exceptions(); |
| 8134 | } // end namespace Catch |
| 8135 | |
| 8136 | // end catch_uncaught_exceptions.h |
| 8137 | namespace Catch { |
| 8138 | |
| 8139 | MessageInfo::MessageInfo( std::string const& _macroName, |
| 8140 | SourceLineInfo const& _lineInfo, |
| 8141 | ResultWas::OfType _type ) |
| 8142 | : macroName( _macroName ), |
| 8143 | lineInfo( _lineInfo ), |
| 8144 | type( _type ), |
| 8145 | sequence( ++globalCount ) |
| 8146 | {} |
| 8147 | |
| 8148 | bool MessageInfo::operator==( MessageInfo const& other ) const { |
| 8149 | return sequence == other.sequence; |
| 8150 | } |
| 8151 | |
| 8152 | bool MessageInfo::operator<( MessageInfo const& other ) const { |
| 8153 | return sequence < other.sequence; |
| 8154 | } |
| 8155 | |
| 8156 | // This may need protecting if threading support is added |
| 8157 | unsigned int MessageInfo::globalCount = 0; |
| 8158 | |
| 8159 | //////////////////////////////////////////////////////////////////////////// |
| 8160 | |
| 8161 | Catch::MessageBuilder::MessageBuilder( std::string const& macroName, |
| 8162 | SourceLineInfo const& lineInfo, |
| 8163 | ResultWas::OfType type ) |
| 8164 | :m_info(macroName, lineInfo, type) {} |
| 8165 | |
| 8166 | //////////////////////////////////////////////////////////////////////////// |
| 8167 | |
| 8168 | ScopedMessage::ScopedMessage( MessageBuilder const& builder ) |
| 8169 | : m_info( builder.m_info ) |
| 8170 | { |
| 8171 | m_info.message = builder.m_stream.str(); |
| 8172 | getResultCapture().pushScopedMessage( m_info ); |
| 8173 | } |
| 8174 | |
| 8175 | ScopedMessage::~ScopedMessage() { |
| 8176 | if ( !uncaught_exceptions() ){ |
| 8177 | getResultCapture().popScopedMessage(m_info); |
| 8178 | } |
| 8179 | } |
| 8180 | } // end namespace Catch |
| 8181 | // end catch_message.cpp |
| 8182 | // start catch_random_number_generator.cpp |
| 8183 | |
| 8184 | // start catch_random_number_generator.h |
| 8185 | |
| 8186 | #include <algorithm> |
| 8187 | |
| 8188 | namespace Catch { |
| 8189 | |
| 8190 | struct IConfig; |
| 8191 | |
| 8192 | void seedRng( IConfig const& config ); |
| 8193 | |
| 8194 | unsigned int rngSeed(); |
| 8195 | |
| 8196 | struct RandomNumberGenerator { |
| 8197 | using result_type = unsigned int; |
| 8198 | |
| 8199 | static constexpr result_type (min)() { return 0; } |
| 8200 | static constexpr result_type (max)() { return 1000000; } |
| 8201 | |
| 8202 | result_type operator()( result_type n ) const; |
| 8203 | result_type operator()() const; |
| 8204 | |
| 8205 | template<typename V> |
| 8206 | static void shuffle( V& vector ) { |
| 8207 | RandomNumberGenerator rng; |
| 8208 | std::shuffle( vector.begin(), vector.end(), rng ); |
| 8209 | } |
| 8210 | }; |
| 8211 | |
| 8212 | } |
| 8213 | |
| 8214 | // end catch_random_number_generator.h |
| 8215 | #include <cstdlib> |
| 8216 | |
| 8217 | namespace Catch { |
| 8218 | |
| 8219 | void seedRng( IConfig const& config ) { |
| 8220 | if( config.rngSeed() != 0 ) |
| 8221 | std::srand( config.rngSeed() ); |
| 8222 | } |
| 8223 | unsigned int rngSeed() { |
| 8224 | return getCurrentContext().getConfig()->rngSeed(); |
| 8225 | } |
| 8226 | |
| 8227 | RandomNumberGenerator::result_type RandomNumberGenerator::operator()( result_type n ) const { |
| 8228 | return std::rand() % n; |
| 8229 | } |
| 8230 | RandomNumberGenerator::result_type RandomNumberGenerator::operator()() const { |
| 8231 | return std::rand() % (max)(); |
| 8232 | } |
| 8233 | |
| 8234 | } |
| 8235 | // end catch_random_number_generator.cpp |
| 8236 | // start catch_registry_hub.cpp |
| 8237 | |
| 8238 | // start catch_test_case_registry_impl.h |
| 8239 | |
| 8240 | #include <vector> |
| 8241 | #include <set> |
| 8242 | #include <algorithm> |
| 8243 | #include <ios> |
| 8244 | |
| 8245 | namespace Catch { |
| 8246 | |
| 8247 | class TestCase; |
| 8248 | struct IConfig; |
| 8249 | |
| 8250 | std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ); |
| 8251 | bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); |
| 8252 | |
| 8253 | void enforceNoDuplicateTestCases( std::vector<TestCase> const& functions ); |
| 8254 | |
| 8255 | std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ); |
| 8256 | std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ); |
| 8257 | |
| 8258 | class TestRegistry : public ITestCaseRegistry { |
| 8259 | public: |
| 8260 | virtual ~TestRegistry() = default; |
| 8261 | |
| 8262 | virtual void registerTest( TestCase const& testCase ); |
| 8263 | |
| 8264 | std::vector<TestCase> const& getAllTests() const override; |
| 8265 | std::vector<TestCase> const& getAllTestsSorted( IConfig const& config ) const override; |
| 8266 | |
| 8267 | private: |
| 8268 | std::vector<TestCase> m_functions; |
| 8269 | mutable RunTests::InWhatOrder m_currentSortOrder = RunTests::InDeclarationOrder; |
| 8270 | mutable std::vector<TestCase> m_sortedFunctions; |
| 8271 | std::size_t m_unnamedCount = 0; |
| 8272 | std::ios_base::Init m_ostreamInit; // Forces cout/ cerr to be initialised |
| 8273 | }; |
| 8274 | |
| 8275 | /////////////////////////////////////////////////////////////////////////// |
| 8276 | |
| 8277 | class TestInvokerAsFunction : public ITestInvoker { |
| 8278 | void(*m_testAsFunction)(); |
| 8279 | public: |
| 8280 | TestInvokerAsFunction( void(*testAsFunction)() ) noexcept; |
| 8281 | |
| 8282 | void invoke() const override; |
| 8283 | }; |
| 8284 | |
| 8285 | std::string extractClassName( StringRef const& classOrQualifiedMethodName ); |
| 8286 | |
| 8287 | /////////////////////////////////////////////////////////////////////////// |
| 8288 | |
| 8289 | } // end namespace Catch |
| 8290 | |
| 8291 | // end catch_test_case_registry_impl.h |
| 8292 | // start catch_reporter_registry.h |
| 8293 | |
| 8294 | #include <map> |
| 8295 | |
| 8296 | namespace Catch { |
| 8297 | |
| 8298 | class ReporterRegistry : public IReporterRegistry { |
| 8299 | |
| 8300 | public: |
| 8301 | |
| 8302 | ~ReporterRegistry() override; |
| 8303 | |
| 8304 | IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const override; |
| 8305 | |
| 8306 | void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ); |
| 8307 | void registerListener( IReporterFactoryPtr const& factory ); |
| 8308 | |
| 8309 | FactoryMap const& getFactories() const override; |
| 8310 | Listeners const& getListeners() const override; |
| 8311 | |
| 8312 | private: |
| 8313 | FactoryMap m_factories; |
| 8314 | Listeners m_listeners; |
| 8315 | }; |
| 8316 | } |
| 8317 | |
| 8318 | // end catch_reporter_registry.h |
| 8319 | // start catch_tag_alias_registry.h |
| 8320 | |
| 8321 | // start catch_tag_alias.h |
| 8322 | |
| 8323 | #include <string> |
| 8324 | |
| 8325 | namespace Catch { |
| 8326 | |
| 8327 | struct TagAlias { |
| 8328 | TagAlias(std::string const& _tag, SourceLineInfo _lineInfo); |
| 8329 | |
| 8330 | std::string tag; |
| 8331 | SourceLineInfo lineInfo; |
| 8332 | }; |
| 8333 | |
| 8334 | } // end namespace Catch |
| 8335 | |
| 8336 | // end catch_tag_alias.h |
| 8337 | #include <map> |
| 8338 | |
| 8339 | namespace Catch { |
| 8340 | |
| 8341 | class TagAliasRegistry : public ITagAliasRegistry { |
| 8342 | public: |
| 8343 | ~TagAliasRegistry() override; |
| 8344 | TagAlias const* find( std::string const& alias ) const override; |
| 8345 | std::string expandAliases( std::string const& unexpandedTestSpec ) const override; |
| 8346 | void add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ); |
| 8347 | |
| 8348 | private: |
| 8349 | std::map<std::string, TagAlias> m_registry; |
| 8350 | }; |
| 8351 | |
| 8352 | } // end namespace Catch |
| 8353 | |
| 8354 | // end catch_tag_alias_registry.h |
| 8355 | // start catch_startup_exception_registry.h |
| 8356 | |
| 8357 | #include <vector> |
| 8358 | #include <exception> |
| 8359 | |
| 8360 | namespace Catch { |
| 8361 | |
| 8362 | class StartupExceptionRegistry { |
| 8363 | public: |
| 8364 | void add(std::exception_ptr const& exception) noexcept; |
| 8365 | std::vector<std::exception_ptr> const& getExceptions() const noexcept; |
| 8366 | private: |
| 8367 | std::vector<std::exception_ptr> m_exceptions; |
| 8368 | }; |
| 8369 | |
| 8370 | } // end namespace Catch |
| 8371 | |
| 8372 | // end catch_startup_exception_registry.h |
| 8373 | namespace Catch { |
| 8374 | |
| 8375 | namespace { |
| 8376 | |
| 8377 | class RegistryHub : public IRegistryHub, public IMutableRegistryHub, |
| 8378 | private NonCopyable { |
| 8379 | |
| 8380 | public: // IRegistryHub |
| 8381 | RegistryHub() = default; |
| 8382 | IReporterRegistry const& getReporterRegistry() const override { |
| 8383 | return m_reporterRegistry; |
| 8384 | } |
| 8385 | ITestCaseRegistry const& getTestCaseRegistry() const override { |
| 8386 | return m_testCaseRegistry; |
| 8387 | } |
| 8388 | IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() override { |
| 8389 | return m_exceptionTranslatorRegistry; |
| 8390 | } |
| 8391 | ITagAliasRegistry const& getTagAliasRegistry() const override { |
| 8392 | return m_tagAliasRegistry; |
| 8393 | } |
| 8394 | StartupExceptionRegistry const& getStartupExceptionRegistry() const override { |
| 8395 | return m_exceptionRegistry; |
| 8396 | } |
| 8397 | |
| 8398 | public: // IMutableRegistryHub |
| 8399 | void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) override { |
| 8400 | m_reporterRegistry.registerReporter( name, factory ); |
| 8401 | } |
| 8402 | void registerListener( IReporterFactoryPtr const& factory ) override { |
| 8403 | m_reporterRegistry.registerListener( factory ); |
| 8404 | } |
| 8405 | void registerTest( TestCase const& testInfo ) override { |
| 8406 | m_testCaseRegistry.registerTest( testInfo ); |
| 8407 | } |
| 8408 | void registerTranslator( const IExceptionTranslator* translator ) override { |
| 8409 | m_exceptionTranslatorRegistry.registerTranslator( translator ); |
| 8410 | } |
| 8411 | void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) override { |
| 8412 | m_tagAliasRegistry.add( alias, tag, lineInfo ); |
| 8413 | } |
| 8414 | void registerStartupException() noexcept override { |
| 8415 | m_exceptionRegistry.add(std::current_exception()); |
| 8416 | } |
| 8417 | |
| 8418 | private: |
| 8419 | TestRegistry m_testCaseRegistry; |
| 8420 | ReporterRegistry m_reporterRegistry; |
| 8421 | ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; |
| 8422 | TagAliasRegistry m_tagAliasRegistry; |
| 8423 | StartupExceptionRegistry m_exceptionRegistry; |
| 8424 | }; |
| 8425 | |
| 8426 | // Single, global, instance |
| 8427 | RegistryHub*& getTheRegistryHub() { |
| 8428 | static RegistryHub* theRegistryHub = nullptr; |
| 8429 | if( !theRegistryHub ) |
| 8430 | theRegistryHub = new RegistryHub(); |
| 8431 | return theRegistryHub; |
| 8432 | } |
| 8433 | } |
| 8434 | |
| 8435 | IRegistryHub& getRegistryHub() { |
| 8436 | return *getTheRegistryHub(); |
| 8437 | } |
| 8438 | IMutableRegistryHub& getMutableRegistryHub() { |
| 8439 | return *getTheRegistryHub(); |
| 8440 | } |
| 8441 | void cleanUp() { |
| 8442 | delete getTheRegistryHub(); |
| 8443 | getTheRegistryHub() = nullptr; |
| 8444 | cleanUpContext(); |
| 8445 | ReusableStringStream::cleanup(); |
| 8446 | } |
| 8447 | std::string translateActiveException() { |
| 8448 | return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException(); |
| 8449 | } |
| 8450 | |
| 8451 | } // end namespace Catch |
| 8452 | // end catch_registry_hub.cpp |
| 8453 | // start catch_reporter_registry.cpp |
| 8454 | |
| 8455 | namespace Catch { |
| 8456 | |
| 8457 | ReporterRegistry::~ReporterRegistry() = default; |
| 8458 | |
| 8459 | IStreamingReporterPtr ReporterRegistry::create( std::string const& name, IConfigPtr const& config ) const { |
| 8460 | auto it = m_factories.find( name ); |
| 8461 | if( it == m_factories.end() ) |
| 8462 | return nullptr; |
| 8463 | return it->second->create( ReporterConfig( config ) ); |
| 8464 | } |
| 8465 | |
| 8466 | void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) { |
| 8467 | m_factories.emplace(name, factory); |
| 8468 | } |
| 8469 | void ReporterRegistry::registerListener( IReporterFactoryPtr const& factory ) { |
| 8470 | m_listeners.push_back( factory ); |
| 8471 | } |
| 8472 | |
| 8473 | IReporterRegistry::FactoryMap const& ReporterRegistry::getFactories() const { |
| 8474 | return m_factories; |
| 8475 | } |
| 8476 | IReporterRegistry::Listeners const& ReporterRegistry::getListeners() const { |
| 8477 | return m_listeners; |
| 8478 | } |
| 8479 | |
| 8480 | } |
| 8481 | // end catch_reporter_registry.cpp |
| 8482 | // start catch_result_type.cpp |
| 8483 | |
| 8484 | namespace Catch { |
| 8485 | |
| 8486 | bool isOk( ResultWas::OfType resultType ) { |
| 8487 | return ( resultType & ResultWas::FailureBit ) == 0; |
| 8488 | } |
| 8489 | bool isJustInfo( int flags ) { |
| 8490 | return flags == ResultWas::Info; |
| 8491 | } |
| 8492 | |
| 8493 | ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ) { |
| 8494 | return static_cast<ResultDisposition::Flags>( static_cast<int>( lhs ) | static_cast<int>( rhs ) ); |
| 8495 | } |
| 8496 | |
| 8497 | bool shouldContinueOnFailure( int flags ) { return ( flags & ResultDisposition::ContinueOnFailure ) != 0; } |
| 8498 | bool shouldSuppressFailure( int flags ) { return ( flags & ResultDisposition::SuppressFail ) != 0; } |
| 8499 | |
| 8500 | } // end namespace Catch |
| 8501 | // end catch_result_type.cpp |
| 8502 | // start catch_run_context.cpp |
| 8503 | |
| 8504 | #include <cassert> |
| 8505 | #include <algorithm> |
| 8506 | #include <sstream> |
| 8507 | |
| 8508 | namespace Catch { |
| 8509 | |
| 8510 | class RedirectedStream { |
| 8511 | std::ostream& m_originalStream; |
| 8512 | std::ostream& m_redirectionStream; |
| 8513 | std::streambuf* m_prevBuf; |
| 8514 | |
| 8515 | public: |
| 8516 | RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream ) |
| 8517 | : m_originalStream( originalStream ), |
| 8518 | m_redirectionStream( redirectionStream ), |
| 8519 | m_prevBuf( m_originalStream.rdbuf() ) |
| 8520 | { |
| 8521 | m_originalStream.rdbuf( m_redirectionStream.rdbuf() ); |
| 8522 | } |
| 8523 | ~RedirectedStream() { |
| 8524 | m_originalStream.rdbuf( m_prevBuf ); |
| 8525 | } |
| 8526 | }; |
| 8527 | |
| 8528 | class RedirectedStdOut { |
| 8529 | ReusableStringStream m_rss; |
| 8530 | RedirectedStream m_cout; |
| 8531 | public: |
| 8532 | RedirectedStdOut() : m_cout( Catch::cout(), m_rss.get() ) {} |
| 8533 | auto str() const -> std::string { return m_rss.str(); } |
| 8534 | }; |
| 8535 | |
| 8536 | // StdErr has two constituent streams in C++, std::cerr and std::clog |
| 8537 | // This means that we need to redirect 2 streams into 1 to keep proper |
| 8538 | // order of writes |
| 8539 | class RedirectedStdErr { |
| 8540 | ReusableStringStream m_rss; |
| 8541 | RedirectedStream m_cerr; |
| 8542 | RedirectedStream m_clog; |
| 8543 | public: |
| 8544 | RedirectedStdErr() |
| 8545 | : m_cerr( Catch::cerr(), m_rss.get() ), |
| 8546 | m_clog( Catch::clog(), m_rss.get() ) |
| 8547 | {} |
| 8548 | auto str() const -> std::string { return m_rss.str(); } |
| 8549 | }; |
| 8550 | |
| 8551 | RunContext::RunContext(IConfigPtr const& _config, IStreamingReporterPtr&& reporter) |
| 8552 | : m_runInfo(_config->name()), |
| 8553 | m_context(getCurrentMutableContext()), |
| 8554 | m_config(_config), |
| 8555 | m_reporter(std::move(reporter)), |
| 8556 | m_lastAssertionInfo{ StringRef(), SourceLineInfo("",0), StringRef(), ResultDisposition::Normal }, |
| 8557 | m_includeSuccessfulResults( m_config->includeSuccessfulResults() ) |
| 8558 | { |
| 8559 | m_context.setRunner(this); |
| 8560 | m_context.setConfig(m_config); |
| 8561 | m_context.setResultCapture(this); |
| 8562 | m_reporter->testRunStarting(m_runInfo); |
| 8563 | } |
| 8564 | |
| 8565 | RunContext::~RunContext() { |
| 8566 | m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, aborting())); |
| 8567 | } |
| 8568 | |
| 8569 | void RunContext::testGroupStarting(std::string const& testSpec, std::size_t groupIndex, std::size_t groupsCount) { |
| 8570 | m_reporter->testGroupStarting(GroupInfo(testSpec, groupIndex, groupsCount)); |
| 8571 | } |
| 8572 | |
| 8573 | void RunContext::testGroupEnded(std::string const& testSpec, Totals const& totals, std::size_t groupIndex, std::size_t groupsCount) { |
| 8574 | m_reporter->testGroupEnded(TestGroupStats(GroupInfo(testSpec, groupIndex, groupsCount), totals, aborting())); |
| 8575 | } |
| 8576 | |
| 8577 | Totals RunContext::runTest(TestCase const& testCase) { |
| 8578 | Totals prevTotals = m_totals; |
| 8579 | |
| 8580 | std::string redirectedCout; |
| 8581 | std::string redirectedCerr; |
| 8582 | |
| 8583 | auto const& testInfo = testCase.getTestCaseInfo(); |
| 8584 | |
| 8585 | m_reporter->testCaseStarting(testInfo); |
| 8586 | |
| 8587 | m_activeTestCase = &testCase; |
| 8588 | |
| 8589 | ITracker& rootTracker = m_trackerContext.startRun(); |
| 8590 | assert(rootTracker.isSectionTracker()); |
| 8591 | static_cast<SectionTracker&>(rootTracker).addInitialFilters(m_config->getSectionsToRun()); |
| 8592 | do { |
| 8593 | m_trackerContext.startCycle(); |
| 8594 | m_testCaseTracker = &SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocation(testInfo.name, testInfo.lineInfo)); |
| 8595 | runCurrentTest(redirectedCout, redirectedCerr); |
| 8596 | } while (!m_testCaseTracker->isSuccessfullyCompleted() && !aborting()); |
| 8597 | |
| 8598 | Totals deltaTotals = m_totals.delta(prevTotals); |
| 8599 | if (testInfo.expectedToFail() && deltaTotals.testCases.passed > 0) { |
| 8600 | deltaTotals.assertions.failed++; |
| 8601 | deltaTotals.testCases.passed--; |
| 8602 | deltaTotals.testCases.failed++; |
| 8603 | } |
| 8604 | m_totals.testCases += deltaTotals.testCases; |
| 8605 | m_reporter->testCaseEnded(TestCaseStats(testInfo, |
| 8606 | deltaTotals, |
| 8607 | redirectedCout, |
| 8608 | redirectedCerr, |
| 8609 | aborting())); |
| 8610 | |
| 8611 | m_activeTestCase = nullptr; |
| 8612 | m_testCaseTracker = nullptr; |
| 8613 | |
| 8614 | return deltaTotals; |
| 8615 | } |
| 8616 | |
| 8617 | IConfigPtr RunContext::config() const { |
| 8618 | return m_config; |
| 8619 | } |
| 8620 | |
| 8621 | IStreamingReporter& RunContext::reporter() const { |
| 8622 | return *m_reporter; |
| 8623 | } |
| 8624 | |
| 8625 | void RunContext::assertionEnded(AssertionResult const & result) { |
| 8626 | if (result.getResultType() == ResultWas::Ok) { |
| 8627 | m_totals.assertions.passed++; |
| 8628 | m_lastAssertionPassed = true; |
| 8629 | } else if (!result.isOk()) { |
| 8630 | m_lastAssertionPassed = false; |
| 8631 | if( m_activeTestCase->getTestCaseInfo().okToFail() ) |
| 8632 | m_totals.assertions.failedButOk++; |
| 8633 | else |
| 8634 | m_totals.assertions.failed++; |
| 8635 | } |
| 8636 | else { |
| 8637 | m_lastAssertionPassed = true; |
| 8638 | } |
| 8639 | |
| 8640 | // We have no use for the return value (whether messages should be cleared), because messages were made scoped |
| 8641 | // and should be let to clear themselves out. |
| 8642 | static_cast<void>(m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals))); |
| 8643 | |
| 8644 | // Reset working state |
| 8645 | resetAssertionInfo(); |
| 8646 | m_lastResult = result; |
| 8647 | } |
| 8648 | void RunContext::resetAssertionInfo() { |
| 8649 | m_lastAssertionInfo.macroName = StringRef(); |
| 8650 | m_lastAssertionInfo.capturedExpression = "{Unknown expression after the reported line}"_sr; |
| 8651 | } |
| 8652 | |
| 8653 | bool RunContext::sectionStarted(SectionInfo const & sectionInfo, Counts & assertions) { |
| 8654 | ITracker& sectionTracker = SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocation(sectionInfo.name, sectionInfo.lineInfo)); |
| 8655 | if (!sectionTracker.isOpen()) |
| 8656 | return false; |
| 8657 | m_activeSections.push_back(§ionTracker); |
| 8658 | |
| 8659 | m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo; |
| 8660 | |
| 8661 | m_reporter->sectionStarting(sectionInfo); |
| 8662 | |
| 8663 | assertions = m_totals.assertions; |
| 8664 | |
| 8665 | return true; |
| 8666 | } |
| 8667 | |
| 8668 | bool RunContext::testForMissingAssertions(Counts& assertions) { |
| 8669 | if (assertions.total() != 0) |
| 8670 | return false; |
| 8671 | if (!m_config->warnAboutMissingAssertions()) |
| 8672 | return false; |
| 8673 | if (m_trackerContext.currentTracker().hasChildren()) |
| 8674 | return false; |
| 8675 | m_totals.assertions.failed++; |
| 8676 | assertions.failed++; |
| 8677 | return true; |
| 8678 | } |
| 8679 | |
| 8680 | void RunContext::sectionEnded(SectionEndInfo const & endInfo) { |
| 8681 | Counts assertions = m_totals.assertions - endInfo.prevAssertions; |
| 8682 | bool missingAssertions = testForMissingAssertions(assertions); |
| 8683 | |
| 8684 | if (!m_activeSections.empty()) { |
| 8685 | m_activeSections.back()->close(); |
| 8686 | m_activeSections.pop_back(); |
| 8687 | } |
| 8688 | |
| 8689 | m_reporter->sectionEnded(SectionStats(endInfo.sectionInfo, assertions, endInfo.durationInSeconds, missingAssertions)); |
| 8690 | m_messages.clear(); |
| 8691 | } |
| 8692 | |
| 8693 | void RunContext::sectionEndedEarly(SectionEndInfo const & endInfo) { |
| 8694 | if (m_unfinishedSections.empty()) |
| 8695 | m_activeSections.back()->fail(); |
| 8696 | else |
| 8697 | m_activeSections.back()->close(); |
| 8698 | m_activeSections.pop_back(); |
| 8699 | |
| 8700 | m_unfinishedSections.push_back(endInfo); |
| 8701 | } |
| 8702 | void RunContext::benchmarkStarting( BenchmarkInfo const& info ) { |
| 8703 | m_reporter->benchmarkStarting( info ); |
| 8704 | } |
| 8705 | void RunContext::benchmarkEnded( BenchmarkStats const& stats ) { |
| 8706 | m_reporter->benchmarkEnded( stats ); |
| 8707 | } |
| 8708 | |
| 8709 | void RunContext::pushScopedMessage(MessageInfo const & message) { |
| 8710 | m_messages.push_back(message); |
| 8711 | } |
| 8712 | |
| 8713 | void RunContext::popScopedMessage(MessageInfo const & message) { |
| 8714 | m_messages.erase(std::remove(m_messages.begin(), m_messages.end(), message), m_messages.end()); |
| 8715 | } |
| 8716 | |
| 8717 | std::string RunContext::getCurrentTestName() const { |
| 8718 | return m_activeTestCase |
| 8719 | ? m_activeTestCase->getTestCaseInfo().name |
| 8720 | : std::string(); |
| 8721 | } |
| 8722 | |
| 8723 | const AssertionResult * RunContext::getLastResult() const { |
| 8724 | return &(*m_lastResult); |
| 8725 | } |
| 8726 | |
| 8727 | void RunContext::exceptionEarlyReported() { |
| 8728 | m_shouldReportUnexpected = false; |
| 8729 | } |
| 8730 | |
| 8731 | void RunContext::handleFatalErrorCondition( StringRef message ) { |
| 8732 | // First notify reporter that bad things happened |
| 8733 | m_reporter->fatalErrorEncountered(message); |
| 8734 | |
| 8735 | // Don't rebuild the result -- the stringification itself can cause more fatal errors |
| 8736 | // Instead, fake a result data. |
| 8737 | AssertionResultData tempResult( ResultWas::FatalErrorCondition, { false } ); |
| 8738 | tempResult.message = message; |
| 8739 | AssertionResult result(m_lastAssertionInfo, tempResult); |
| 8740 | |
| 8741 | assertionEnded(result); |
| 8742 | |
| 8743 | handleUnfinishedSections(); |
| 8744 | |
| 8745 | // Recreate section for test case (as we will lose the one that was in scope) |
| 8746 | auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); |
| 8747 | SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description); |
| 8748 | |
| 8749 | Counts assertions; |
| 8750 | assertions.failed = 1; |
| 8751 | SectionStats testCaseSectionStats(testCaseSection, assertions, 0, false); |
| 8752 | m_reporter->sectionEnded(testCaseSectionStats); |
| 8753 | |
| 8754 | auto const& testInfo = m_activeTestCase->getTestCaseInfo(); |
| 8755 | |
| 8756 | Totals deltaTotals; |
| 8757 | deltaTotals.testCases.failed = 1; |
| 8758 | deltaTotals.assertions.failed = 1; |
| 8759 | m_reporter->testCaseEnded(TestCaseStats(testInfo, |
| 8760 | deltaTotals, |
| 8761 | std::string(), |
| 8762 | std::string(), |
| 8763 | false)); |
| 8764 | m_totals.testCases.failed++; |
| 8765 | testGroupEnded(std::string(), m_totals, 1, 1); |
| 8766 | m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, false)); |
| 8767 | } |
| 8768 | |
| 8769 | bool RunContext::lastAssertionPassed() { |
| 8770 | return m_lastAssertionPassed; |
| 8771 | } |
| 8772 | |
| 8773 | void RunContext::assertionPassed() { |
| 8774 | m_lastAssertionPassed = true; |
| 8775 | ++m_totals.assertions.passed; |
| 8776 | resetAssertionInfo(); |
| 8777 | } |
| 8778 | |
| 8779 | bool RunContext::aborting() const { |
| 8780 | return m_totals.assertions.failed == static_cast<std::size_t>(m_config->abortAfter()); |
| 8781 | } |
| 8782 | |
| 8783 | void RunContext::runCurrentTest(std::string & redirectedCout, std::string & redirectedCerr) { |
| 8784 | auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); |
| 8785 | SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description); |
| 8786 | m_reporter->sectionStarting(testCaseSection); |
| 8787 | Counts prevAssertions = m_totals.assertions; |
| 8788 | double duration = 0; |
| 8789 | m_shouldReportUnexpected = true; |
| 8790 | m_lastAssertionInfo = { "TEST_CASE"_sr, testCaseInfo.lineInfo, StringRef(), ResultDisposition::Normal }; |
| 8791 | |
| 8792 | seedRng(*m_config); |
| 8793 | |
| 8794 | Timer timer; |
| 8795 | try { |
| 8796 | if (m_reporter->getPreferences().shouldRedirectStdOut) { |
| 8797 | RedirectedStdOut redirectedStdOut; |
| 8798 | RedirectedStdErr redirectedStdErr; |
| 8799 | timer.start(); |
| 8800 | invokeActiveTestCase(); |
| 8801 | redirectedCout += redirectedStdOut.str(); |
| 8802 | redirectedCerr += redirectedStdErr.str(); |
| 8803 | |
| 8804 | } else { |
| 8805 | timer.start(); |
| 8806 | invokeActiveTestCase(); |
| 8807 | } |
| 8808 | duration = timer.getElapsedSeconds(); |
| 8809 | } catch (TestFailureException&) { |
| 8810 | // This just means the test was aborted due to failure |
| 8811 | } catch (...) { |
| 8812 | // Under CATCH_CONFIG_FAST_COMPILE, unexpected exceptions under REQUIRE assertions |
| 8813 | // are reported without translation at the point of origin. |
| 8814 | if( m_shouldReportUnexpected ) { |
| 8815 | AssertionReaction dummyReaction; |
| 8816 | handleUnexpectedInflightException( m_lastAssertionInfo, translateActiveException(), dummyReaction ); |
| 8817 | } |
| 8818 | } |
| 8819 | Counts assertions = m_totals.assertions - prevAssertions; |
| 8820 | bool missingAssertions = testForMissingAssertions(assertions); |
| 8821 | |
| 8822 | m_testCaseTracker->close(); |
| 8823 | handleUnfinishedSections(); |
| 8824 | m_messages.clear(); |
| 8825 | |
| 8826 | SectionStats testCaseSectionStats(testCaseSection, assertions, duration, missingAssertions); |
| 8827 | m_reporter->sectionEnded(testCaseSectionStats); |
| 8828 | } |
| 8829 | |
| 8830 | void RunContext::invokeActiveTestCase() { |
| 8831 | FatalConditionHandler fatalConditionHandler; // Handle signals |
| 8832 | m_activeTestCase->invoke(); |
| 8833 | fatalConditionHandler.reset(); |
| 8834 | } |
| 8835 | |
| 8836 | void RunContext::handleUnfinishedSections() { |
| 8837 | // If sections ended prematurely due to an exception we stored their |
| 8838 | // infos here so we can tear them down outside the unwind process. |
| 8839 | for (auto it = m_unfinishedSections.rbegin(), |
| 8840 | itEnd = m_unfinishedSections.rend(); |
| 8841 | it != itEnd; |
| 8842 | ++it) |
| 8843 | sectionEnded(*it); |
| 8844 | m_unfinishedSections.clear(); |
| 8845 | } |
| 8846 | |
| 8847 | void RunContext::handleExpr( |
| 8848 | AssertionInfo const& info, |
| 8849 | ITransientExpression const& expr, |
| 8850 | AssertionReaction& reaction |
| 8851 | ) { |
| 8852 | m_reporter->assertionStarting( info ); |
| 8853 | |
| 8854 | bool negated = isFalseTest( info.resultDisposition ); |
| 8855 | bool result = expr.getResult() != negated; |
| 8856 | |
| 8857 | if( result ) { |
| 8858 | if (!m_includeSuccessfulResults) { |
| 8859 | assertionPassed(); |
| 8860 | } |
| 8861 | else { |
| 8862 | reportExpr(info, ResultWas::Ok, &expr, negated); |
| 8863 | } |
| 8864 | } |
| 8865 | else { |
| 8866 | reportExpr(info, ResultWas::ExpressionFailed, &expr, negated ); |
| 8867 | populateReaction( reaction ); |
| 8868 | } |
| 8869 | } |
| 8870 | void RunContext::reportExpr( |
| 8871 | AssertionInfo const &info, |
| 8872 | ResultWas::OfType resultType, |
| 8873 | ITransientExpression const *expr, |
| 8874 | bool negated ) { |
| 8875 | |
| 8876 | m_lastAssertionInfo = info; |
| 8877 | AssertionResultData data( resultType, LazyExpression( negated ) ); |
| 8878 | |
| 8879 | AssertionResult assertionResult{ info, data }; |
| 8880 | assertionResult.m_resultData.lazyExpression.m_transientExpression = expr; |
| 8881 | |
| 8882 | assertionEnded( assertionResult ); |
| 8883 | } |
| 8884 | |
| 8885 | void RunContext::handleMessage( |
| 8886 | AssertionInfo const& info, |
| 8887 | ResultWas::OfType resultType, |
| 8888 | StringRef const& message, |
| 8889 | AssertionReaction& reaction |
| 8890 | ) { |
| 8891 | m_reporter->assertionStarting( info ); |
| 8892 | |
| 8893 | m_lastAssertionInfo = info; |
| 8894 | |
| 8895 | AssertionResultData data( resultType, LazyExpression( false ) ); |
| 8896 | data.message = message; |
| 8897 | AssertionResult assertionResult{ m_lastAssertionInfo, data }; |
| 8898 | assertionEnded( assertionResult ); |
| 8899 | if( !assertionResult.isOk() ) |
| 8900 | populateReaction( reaction ); |
| 8901 | } |
| 8902 | void RunContext::handleUnexpectedExceptionNotThrown( |
| 8903 | AssertionInfo const& info, |
| 8904 | AssertionReaction& reaction |
| 8905 | ) { |
| 8906 | handleNonExpr(info, Catch::ResultWas::DidntThrowException, reaction); |
| 8907 | } |
| 8908 | |
| 8909 | void RunContext::handleUnexpectedInflightException( |
| 8910 | AssertionInfo const& info, |
| 8911 | std::string const& message, |
| 8912 | AssertionReaction& reaction |
| 8913 | ) { |
| 8914 | m_lastAssertionInfo = info; |
| 8915 | |
| 8916 | AssertionResultData data( ResultWas::ThrewException, LazyExpression( false ) ); |
| 8917 | data.message = message; |
| 8918 | AssertionResult assertionResult{ info, data }; |
| 8919 | assertionEnded( assertionResult ); |
| 8920 | populateReaction( reaction ); |
| 8921 | } |
| 8922 | |
| 8923 | void RunContext::populateReaction( AssertionReaction& reaction ) { |
| 8924 | reaction.shouldDebugBreak = m_config->shouldDebugBreak(); |
| 8925 | reaction.shouldThrow = aborting() || (m_lastAssertionInfo.resultDisposition & ResultDisposition::Normal); |
| 8926 | } |
| 8927 | |
| 8928 | void RunContext::handleIncomplete( |
| 8929 | AssertionInfo const& info |
| 8930 | ) { |
| 8931 | m_lastAssertionInfo = info; |
| 8932 | |
| 8933 | AssertionResultData data( ResultWas::ThrewException, LazyExpression( false ) ); |
| 8934 | data.message = "Exception translation was disabled by CATCH_CONFIG_FAST_COMPILE"; |
| 8935 | AssertionResult assertionResult{ info, data }; |
| 8936 | assertionEnded( assertionResult ); |
| 8937 | } |
| 8938 | void RunContext::handleNonExpr( |
| 8939 | AssertionInfo const &info, |
| 8940 | ResultWas::OfType resultType, |
| 8941 | AssertionReaction &reaction |
| 8942 | ) { |
| 8943 | m_lastAssertionInfo = info; |
| 8944 | |
| 8945 | AssertionResultData data( resultType, LazyExpression( false ) ); |
| 8946 | AssertionResult assertionResult{ info, data }; |
| 8947 | assertionEnded( assertionResult ); |
| 8948 | |
| 8949 | if( !assertionResult.isOk() ) |
| 8950 | populateReaction( reaction ); |
| 8951 | } |
| 8952 | |
| 8953 | IResultCapture& getResultCapture() { |
| 8954 | if (auto* capture = getCurrentContext().getResultCapture()) |
| 8955 | return *capture; |
| 8956 | else |
| 8957 | CATCH_INTERNAL_ERROR("No result capture instance"); |
| 8958 | } |
| 8959 | } |
| 8960 | // end catch_run_context.cpp |
| 8961 | // start catch_section.cpp |
| 8962 | |
| 8963 | namespace Catch { |
| 8964 | |
| 8965 | Section::Section( SectionInfo const& info ) |
| 8966 | : m_info( info ), |
| 8967 | m_sectionIncluded( getResultCapture().sectionStarted( m_info, m_assertions ) ) |
| 8968 | { |
| 8969 | m_timer.start(); |
| 8970 | } |
| 8971 | |
| 8972 | Section::~Section() { |
| 8973 | if( m_sectionIncluded ) { |
| 8974 | SectionEndInfo endInfo( m_info, m_assertions, m_timer.getElapsedSeconds() ); |
| 8975 | if( uncaught_exceptions() ) |
| 8976 | getResultCapture().sectionEndedEarly( endInfo ); |
| 8977 | else |
| 8978 | getResultCapture().sectionEnded( endInfo ); |
| 8979 | } |
| 8980 | } |
| 8981 | |
| 8982 | // This indicates whether the section should be executed or not |
| 8983 | Section::operator bool() const { |
| 8984 | return m_sectionIncluded; |
| 8985 | } |
| 8986 | |
| 8987 | } // end namespace Catch |
| 8988 | // end catch_section.cpp |
| 8989 | // start catch_section_info.cpp |
| 8990 | |
| 8991 | namespace Catch { |
| 8992 | |
| 8993 | SectionInfo::SectionInfo |
| 8994 | ( SourceLineInfo const& _lineInfo, |
| 8995 | std::string const& _name, |
| 8996 | std::string const& _description ) |
| 8997 | : name( _name ), |
| 8998 | description( _description ), |
| 8999 | lineInfo( _lineInfo ) |
| 9000 | {} |
| 9001 | |
| 9002 | SectionEndInfo::SectionEndInfo( SectionInfo const& _sectionInfo, Counts const& _prevAssertions, double _durationInSeconds ) |
| 9003 | : sectionInfo( _sectionInfo ), prevAssertions( _prevAssertions ), durationInSeconds( _durationInSeconds ) |
| 9004 | {} |
| 9005 | |
| 9006 | } // end namespace Catch |
| 9007 | // end catch_section_info.cpp |
| 9008 | // start catch_session.cpp |
| 9009 | |
| 9010 | // start catch_session.h |
| 9011 | |
| 9012 | #include <memory> |
| 9013 | |
| 9014 | namespace Catch { |
| 9015 | |
| 9016 | class Session : NonCopyable { |
| 9017 | public: |
| 9018 | |
| 9019 | Session(); |
| 9020 | ~Session() override; |
| 9021 | |
| 9022 | void showHelp() const; |
| 9023 | void libIdentify(); |
| 9024 | |
| 9025 | int applyCommandLine( int argc, char const * const * argv ); |
| 9026 | |
| 9027 | void useConfigData( ConfigData const& configData ); |
| 9028 | |
| 9029 | int run( int argc, char* argv[] ); |
| 9030 | #if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(UNICODE) |
| 9031 | int run( int argc, wchar_t* const argv[] ); |
| 9032 | #endif |
| 9033 | int run(); |
| 9034 | |
| 9035 | clara::Parser const& cli() const; |
| 9036 | void cli( clara::Parser const& newParser ); |
| 9037 | ConfigData& configData(); |
| 9038 | Config& config(); |
| 9039 | private: |
| 9040 | int runInternal(); |
| 9041 | |
| 9042 | clara::Parser m_cli; |
| 9043 | ConfigData m_configData; |
| 9044 | std::shared_ptr<Config> m_config; |
| 9045 | bool m_startupExceptions = false; |
| 9046 | }; |
| 9047 | |
| 9048 | } // end namespace Catch |
| 9049 | |
| 9050 | // end catch_session.h |
| 9051 | // start catch_version.h |
| 9052 | |
| 9053 | #include <iosfwd> |
| 9054 | |
| 9055 | namespace Catch { |
| 9056 | |
| 9057 | // Versioning information |
| 9058 | struct Version { |
| 9059 | Version( Version const& ) = delete; |
| 9060 | Version& operator=( Version const& ) = delete; |
| 9061 | Version( unsigned int _majorVersion, |
| 9062 | unsigned int _minorVersion, |
| 9063 | unsigned int _patchNumber, |
| 9064 | char const * const _branchName, |
| 9065 | unsigned int _buildNumber ); |
| 9066 | |
| 9067 | unsigned int const majorVersion; |
| 9068 | unsigned int const minorVersion; |
| 9069 | unsigned int const patchNumber; |
| 9070 | |
| 9071 | // buildNumber is only used if branchName is not null |
| 9072 | char const * const branchName; |
| 9073 | unsigned int const buildNumber; |
| 9074 | |
| 9075 | friend std::ostream& operator << ( std::ostream& os, Version const& version ); |
| 9076 | }; |
| 9077 | |
| 9078 | Version const& libraryVersion(); |
| 9079 | } |
| 9080 | |
| 9081 | // end catch_version.h |
| 9082 | #include <cstdlib> |
| 9083 | #include <iomanip> |
| 9084 | |
| 9085 | namespace Catch { |
| 9086 | |
| 9087 | namespace { |
| 9088 | const int MaxExitCode = 255; |
| 9089 | |
| 9090 | IStreamingReporterPtr createReporter(std::string const& reporterName, IConfigPtr const& config) { |
| 9091 | auto reporter = Catch::getRegistryHub().getReporterRegistry().create(reporterName, config); |
| 9092 | CATCH_ENFORCE(reporter, "No reporter registered with name: '" << reporterName << "'"); |
| 9093 | |
| 9094 | return reporter; |
| 9095 | } |
| 9096 | |
| 9097 | #ifndef CATCH_CONFIG_DEFAULT_REPORTER |
| 9098 | #define CATCH_CONFIG_DEFAULT_REPORTER "console" |
| 9099 | #endif |
| 9100 | |
| 9101 | IStreamingReporterPtr makeReporter(std::shared_ptr<Config> const& config) { |
| 9102 | auto const& reporterNames = config->getReporterNames(); |
| 9103 | if (reporterNames.empty()) |
| 9104 | return createReporter(CATCH_CONFIG_DEFAULT_REPORTER, config); |
| 9105 | |
| 9106 | IStreamingReporterPtr reporter; |
| 9107 | for (auto const& name : reporterNames) |
| 9108 | addReporter(reporter, createReporter(name, config)); |
| 9109 | return reporter; |
| 9110 | } |
| 9111 | |
| 9112 | #undef CATCH_CONFIG_DEFAULT_REPORTER |
| 9113 | |
| 9114 | void addListeners(IStreamingReporterPtr& reporters, IConfigPtr const& config) { |
| 9115 | auto const& listeners = Catch::getRegistryHub().getReporterRegistry().getListeners(); |
| 9116 | for (auto const& listener : listeners) |
| 9117 | addReporter(reporters, listener->create(Catch::ReporterConfig(config))); |
| 9118 | } |
| 9119 | |
| 9120 | Catch::Totals runTests(std::shared_ptr<Config> const& config) { |
| 9121 | IStreamingReporterPtr reporter = makeReporter(config); |
| 9122 | addListeners(reporter, config); |
| 9123 | |
| 9124 | RunContext context(config, std::move(reporter)); |
| 9125 | |
| 9126 | Totals totals; |
| 9127 | |
| 9128 | context.testGroupStarting(config->name(), 1, 1); |
| 9129 | |
| 9130 | TestSpec testSpec = config->testSpec(); |
| 9131 | |
| 9132 | auto const& allTestCases = getAllTestCasesSorted(*config); |
| 9133 | for (auto const& testCase : allTestCases) { |
| 9134 | if (!context.aborting() && matchTest(testCase, testSpec, *config)) |
| 9135 | totals += context.runTest(testCase); |
| 9136 | else |
| 9137 | context.reporter().skipTest(testCase); |
| 9138 | } |
| 9139 | |
| 9140 | if (config->warnAboutNoTests() && totals.testCases.total() == 0) { |
| 9141 | ReusableStringStream testConfig; |
| 9142 | |
| 9143 | bool first = true; |
| 9144 | for (const auto& input : config->getTestsOrTags()) { |
| 9145 | if (!first) { testConfig << ' '; } |
| 9146 | first = false; |
| 9147 | testConfig << input; |
| 9148 | } |
| 9149 | |
| 9150 | context.reporter().noMatchingTestCases(testConfig.str()); |
| 9151 | totals.error = -1; |
| 9152 | } |
| 9153 | |
| 9154 | context.testGroupEnded(config->name(), totals, 1, 1); |
| 9155 | return totals; |
| 9156 | } |
| 9157 | |
| 9158 | void applyFilenamesAsTags(Catch::IConfig const& config) { |
| 9159 | auto& tests = const_cast<std::vector<TestCase>&>(getAllTestCasesSorted(config)); |
| 9160 | for (auto& testCase : tests) { |
| 9161 | auto tags = testCase.tags; |
| 9162 | |
| 9163 | std::string filename = testCase.lineInfo.file; |
| 9164 | auto lastSlash = filename.find_last_of("\\/"); |
| 9165 | if (lastSlash != std::string::npos) { |
| 9166 | filename.erase(0, lastSlash); |
| 9167 | filename[0] = '#'; |
| 9168 | } |
| 9169 | |
| 9170 | auto lastDot = filename.find_last_of('.'); |
| 9171 | if (lastDot != std::string::npos) { |
| 9172 | filename.erase(lastDot); |
| 9173 | } |
| 9174 | |
| 9175 | tags.push_back(std::move(filename)); |
| 9176 | setTags(testCase, tags); |
| 9177 | } |
| 9178 | } |
| 9179 | |
| 9180 | } // anon namespace |
| 9181 | |
| 9182 | Session::Session() { |
| 9183 | static bool alreadyInstantiated = false; |
| 9184 | if( alreadyInstantiated ) { |
| 9185 | try { CATCH_INTERNAL_ERROR( "Only one instance of Catch::Session can ever be used" ); } |
| 9186 | catch(...) { getMutableRegistryHub().registerStartupException(); } |
| 9187 | } |
| 9188 | |
| 9189 | const auto& exceptions = getRegistryHub().getStartupExceptionRegistry().getExceptions(); |
| 9190 | if ( !exceptions.empty() ) { |
| 9191 | m_startupExceptions = true; |
| 9192 | Colour colourGuard( Colour::Red ); |
| 9193 | Catch::cerr() << "Errors occurred during startup!" << '\n'; |
| 9194 | // iterate over all exceptions and notify user |
| 9195 | for ( const auto& ex_ptr : exceptions ) { |
| 9196 | try { |
| 9197 | std::rethrow_exception(ex_ptr); |
| 9198 | } catch ( std::exception const& ex ) { |
| 9199 | Catch::cerr() << Column( ex.what() ).indent(2) << '\n'; |
| 9200 | } |
| 9201 | } |
| 9202 | } |
| 9203 | |
| 9204 | alreadyInstantiated = true; |
| 9205 | m_cli = makeCommandLineParser( m_configData ); |
| 9206 | } |
| 9207 | Session::~Session() { |
| 9208 | Catch::cleanUp(); |
| 9209 | } |
| 9210 | |
| 9211 | void Session::showHelp() const { |
| 9212 | Catch::cout() |
| 9213 | << "\nCatch v" << libraryVersion() << "\n" |
| 9214 | << m_cli << std::endl |
| 9215 | << "For more detailed usage please see the project docs\n" << std::endl; |
| 9216 | } |
| 9217 | void Session::libIdentify() { |
| 9218 | Catch::cout() |
| 9219 | << std::left << std::setw(16) << "description: " << "A Catch test executable\n" |
| 9220 | << std::left << std::setw(16) << "category: " << "testframework\n" |
| 9221 | << std::left << std::setw(16) << "framework: " << "Catch Test\n" |
| 9222 | << std::left << std::setw(16) << "version: " << libraryVersion() << std::endl; |
| 9223 | } |
| 9224 | |
| 9225 | int Session::applyCommandLine( int argc, char const * const * argv ) { |
| 9226 | if( m_startupExceptions ) |
| 9227 | return 1; |
| 9228 | |
| 9229 | auto result = m_cli.parse( clara::Args( argc, argv ) ); |
| 9230 | if( !result ) { |
| 9231 | Catch::cerr() |
| 9232 | << Colour( Colour::Red ) |
| 9233 | << "\nError(s) in input:\n" |
| 9234 | << Column( result.errorMessage() ).indent( 2 ) |
| 9235 | << "\n\n"; |
| 9236 | Catch::cerr() << "Run with -? for usage\n" << std::endl; |
| 9237 | return MaxExitCode; |
| 9238 | } |
| 9239 | |
| 9240 | if( m_configData.showHelp ) |
| 9241 | showHelp(); |
| 9242 | if( m_configData.libIdentify ) |
| 9243 | libIdentify(); |
| 9244 | m_config.reset(); |
| 9245 | return 0; |
| 9246 | } |
| 9247 | |
| 9248 | void Session::useConfigData( ConfigData const& configData ) { |
| 9249 | m_configData = configData; |
| 9250 | m_config.reset(); |
| 9251 | } |
| 9252 | |
| 9253 | int Session::run( int argc, char* argv[] ) { |
| 9254 | if( m_startupExceptions ) |
| 9255 | return 1; |
| 9256 | int returnCode = applyCommandLine( argc, argv ); |
| 9257 | if( returnCode == 0 ) |
| 9258 | returnCode = run(); |
| 9259 | return returnCode; |
| 9260 | } |
| 9261 | |
| 9262 | #if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(UNICODE) |
| 9263 | int Session::run( int argc, wchar_t* const argv[] ) { |
| 9264 | |
| 9265 | char **utf8Argv = new char *[ argc ]; |
| 9266 | |
| 9267 | for ( int i = 0; i < argc; ++i ) { |
| 9268 | int bufSize = WideCharToMultiByte( CP_UTF8, 0, argv[i], -1, NULL, 0, NULL, NULL ); |
| 9269 | |
| 9270 | utf8Argv[ i ] = new char[ bufSize ]; |
| 9271 | |
| 9272 | WideCharToMultiByte( CP_UTF8, 0, argv[i], -1, utf8Argv[i], bufSize, NULL, NULL ); |
| 9273 | } |
| 9274 | |
| 9275 | int returnCode = run( argc, utf8Argv ); |
| 9276 | |
| 9277 | for ( int i = 0; i < argc; ++i ) |
| 9278 | delete [] utf8Argv[ i ]; |
| 9279 | |
| 9280 | delete [] utf8Argv; |
| 9281 | |
| 9282 | return returnCode; |
| 9283 | } |
| 9284 | #endif |
| 9285 | int Session::run() { |
| 9286 | if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeStart ) != 0 ) { |
| 9287 | Catch::cout() << "...waiting for enter/ return before starting" << std::endl; |
| 9288 | static_cast<void>(std::getchar()); |
| 9289 | } |
| 9290 | int exitCode = runInternal(); |
| 9291 | if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeExit ) != 0 ) { |
| 9292 | Catch::cout() << "...waiting for enter/ return before exiting, with code: " << exitCode << std::endl; |
| 9293 | static_cast<void>(std::getchar()); |
| 9294 | } |
| 9295 | return exitCode; |
| 9296 | } |
| 9297 | |
| 9298 | clara::Parser const& Session::cli() const { |
| 9299 | return m_cli; |
| 9300 | } |
| 9301 | void Session::cli( clara::Parser const& newParser ) { |
| 9302 | m_cli = newParser; |
| 9303 | } |
| 9304 | ConfigData& Session::configData() { |
| 9305 | return m_configData; |
| 9306 | } |
| 9307 | Config& Session::config() { |
| 9308 | if( !m_config ) |
| 9309 | m_config = std::make_shared<Config>( m_configData ); |
| 9310 | return *m_config; |
| 9311 | } |
| 9312 | |
| 9313 | int Session::runInternal() { |
| 9314 | if( m_startupExceptions ) |
| 9315 | return 1; |
| 9316 | |
| 9317 | if( m_configData.showHelp || m_configData.libIdentify ) |
| 9318 | return 0; |
| 9319 | |
| 9320 | try |
| 9321 | { |
| 9322 | config(); // Force config to be constructed |
| 9323 | |
| 9324 | seedRng( *m_config ); |
| 9325 | |
| 9326 | if( m_configData.filenamesAsTags ) |
| 9327 | applyFilenamesAsTags( *m_config ); |
| 9328 | |
| 9329 | // Handle list request |
| 9330 | if( Option<std::size_t> listed = list( config() ) ) |
| 9331 | return static_cast<int>( *listed ); |
| 9332 | |
| 9333 | auto totals = runTests( m_config ); |
| 9334 | // Note that on unices only the lower 8 bits are usually used, clamping |
| 9335 | // the return value to 255 prevents false negative when some multiple |
| 9336 | // of 256 tests has failed |
| 9337 | return (std::min) (MaxExitCode, (std::max) (totals.error, static_cast<int>(totals.assertions.failed))); |
| 9338 | } |
| 9339 | catch( std::exception& ex ) { |
| 9340 | Catch::cerr() << ex.what() << std::endl; |
| 9341 | return MaxExitCode; |
| 9342 | } |
| 9343 | } |
| 9344 | |
| 9345 | } // end namespace Catch |
| 9346 | // end catch_session.cpp |
| 9347 | // start catch_startup_exception_registry.cpp |
| 9348 | |
| 9349 | namespace Catch { |
| 9350 | void StartupExceptionRegistry::add( std::exception_ptr const& exception ) noexcept { |
| 9351 | try { |
| 9352 | m_exceptions.push_back(exception); |
| 9353 | } |
| 9354 | catch(...) { |
| 9355 | // If we run out of memory during start-up there's really not a lot more we can do about it |
| 9356 | std::terminate(); |
| 9357 | } |
| 9358 | } |
| 9359 | |
| 9360 | std::vector<std::exception_ptr> const& StartupExceptionRegistry::getExceptions() const noexcept { |
| 9361 | return m_exceptions; |
| 9362 | } |
| 9363 | |
| 9364 | } // end namespace Catch |
| 9365 | // end catch_startup_exception_registry.cpp |
| 9366 | // start catch_stream.cpp |
| 9367 | |
| 9368 | #include <cstdio> |
| 9369 | #include <iostream> |
| 9370 | #include <fstream> |
| 9371 | #include <sstream> |
| 9372 | #include <vector> |
| 9373 | #include <memory> |
| 9374 | |
| 9375 | #if defined(__clang__) |
| 9376 | # pragma clang diagnostic push |
| 9377 | # pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 9378 | #endif |
| 9379 | |
| 9380 | namespace Catch { |
| 9381 | |
| 9382 | Catch::IStream::~IStream() = default; |
| 9383 | |
| 9384 | namespace detail { namespace { |
| 9385 | template<typename WriterF, std::size_t bufferSize=256> |
| 9386 | class StreamBufImpl : public std::streambuf { |
| 9387 | char data[bufferSize]; |
| 9388 | WriterF m_writer; |
| 9389 | |
| 9390 | public: |
| 9391 | StreamBufImpl() { |
| 9392 | setp( data, data + sizeof(data) ); |
| 9393 | } |
| 9394 | |
| 9395 | ~StreamBufImpl() noexcept { |
| 9396 | StreamBufImpl::sync(); |
| 9397 | } |
| 9398 | |
| 9399 | private: |
| 9400 | int overflow( int c ) override { |
| 9401 | sync(); |
| 9402 | |
| 9403 | if( c != EOF ) { |
| 9404 | if( pbase() == epptr() ) |
| 9405 | m_writer( std::string( 1, static_cast<char>( c ) ) ); |
| 9406 | else |
| 9407 | sputc( static_cast<char>( c ) ); |
| 9408 | } |
| 9409 | return 0; |
| 9410 | } |
| 9411 | |
| 9412 | int sync() override { |
| 9413 | if( pbase() != pptr() ) { |
| 9414 | m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) ); |
| 9415 | setp( pbase(), epptr() ); |
| 9416 | } |
| 9417 | return 0; |
| 9418 | } |
| 9419 | }; |
| 9420 | |
| 9421 | /////////////////////////////////////////////////////////////////////////// |
| 9422 | |
| 9423 | struct OutputDebugWriter { |
| 9424 | |
| 9425 | void operator()( std::string const&str ) { |
| 9426 | writeToDebugConsole( str ); |
| 9427 | } |
| 9428 | }; |
| 9429 | |
| 9430 | /////////////////////////////////////////////////////////////////////////// |
| 9431 | |
| 9432 | class FileStream : public IStream { |
| 9433 | mutable std::ofstream m_ofs; |
| 9434 | public: |
| 9435 | FileStream( StringRef filename ) { |
| 9436 | m_ofs.open( filename.c_str() ); |
| 9437 | CATCH_ENFORCE( !m_ofs.fail(), "Unable to open file: '" << filename << "'" ); |
| 9438 | } |
| 9439 | ~FileStream() override = default; |
| 9440 | public: // IStream |
| 9441 | std::ostream& stream() const override { |
| 9442 | return m_ofs; |
| 9443 | } |
| 9444 | }; |
| 9445 | |
| 9446 | /////////////////////////////////////////////////////////////////////////// |
| 9447 | |
| 9448 | class CoutStream : public IStream { |
| 9449 | mutable std::ostream m_os; |
| 9450 | public: |
| 9451 | // Store the streambuf from cout up-front because |
| 9452 | // cout may get redirected when running tests |
| 9453 | CoutStream() : m_os( Catch::cout().rdbuf() ) {} |
| 9454 | ~CoutStream() override = default; |
| 9455 | |
| 9456 | public: // IStream |
| 9457 | std::ostream& stream() const override { return m_os; } |
| 9458 | }; |
| 9459 | |
| 9460 | /////////////////////////////////////////////////////////////////////////// |
| 9461 | |
| 9462 | class DebugOutStream : public IStream { |
| 9463 | std::unique_ptr<StreamBufImpl<OutputDebugWriter>> m_streamBuf; |
| 9464 | mutable std::ostream m_os; |
| 9465 | public: |
| 9466 | DebugOutStream() |
| 9467 | : m_streamBuf( new StreamBufImpl<OutputDebugWriter>() ), |
| 9468 | m_os( m_streamBuf.get() ) |
| 9469 | {} |
| 9470 | |
| 9471 | ~DebugOutStream() override = default; |
| 9472 | |
| 9473 | public: // IStream |
| 9474 | std::ostream& stream() const override { return m_os; } |
| 9475 | }; |
| 9476 | |
| 9477 | }} // namespace anon::detail |
| 9478 | |
| 9479 | /////////////////////////////////////////////////////////////////////////// |
| 9480 | |
| 9481 | auto makeStream( StringRef const &filename ) -> IStream const* { |
| 9482 | if( filename.empty() ) |
| 9483 | return new detail::CoutStream(); |
| 9484 | else if( filename[0] == '%' ) { |
| 9485 | if( filename == "%debug" ) |
| 9486 | return new detail::DebugOutStream(); |
| 9487 | else |
| 9488 | CATCH_ERROR( "Unrecognised stream: '" << filename << "'" ); |
| 9489 | } |
| 9490 | else |
| 9491 | return new detail::FileStream( filename ); |
| 9492 | } |
| 9493 | |
| 9494 | // This class encapsulates the idea of a pool of ostringstreams that can be reused. |
| 9495 | struct StringStreams { |
| 9496 | std::vector<std::unique_ptr<std::ostringstream>> m_streams; |
| 9497 | std::vector<std::size_t> m_unused; |
| 9498 | std::ostringstream m_referenceStream; // Used for copy state/ flags from |
| 9499 | static StringStreams* s_instance; |
| 9500 | |
| 9501 | auto add() -> std::size_t { |
| 9502 | if( m_unused.empty() ) { |
| 9503 | m_streams.push_back( std::unique_ptr<std::ostringstream>( new std::ostringstream ) ); |
| 9504 | return m_streams.size()-1; |
| 9505 | } |
| 9506 | else { |
| 9507 | auto index = m_unused.back(); |
| 9508 | m_unused.pop_back(); |
| 9509 | return index; |
| 9510 | } |
| 9511 | } |
| 9512 | |
| 9513 | void release( std::size_t index ) { |
| 9514 | m_streams[index]->copyfmt( m_referenceStream ); // Restore initial flags and other state |
| 9515 | m_unused.push_back(index); |
| 9516 | } |
| 9517 | |
| 9518 | // !TBD: put in TLS |
| 9519 | static auto instance() -> StringStreams& { |
| 9520 | if( !s_instance ) |
| 9521 | s_instance = new StringStreams(); |
| 9522 | return *s_instance; |
| 9523 | } |
| 9524 | static void cleanup() { |
| 9525 | delete s_instance; |
| 9526 | s_instance = nullptr; |
| 9527 | } |
| 9528 | }; |
| 9529 | |
| 9530 | StringStreams* StringStreams::s_instance = nullptr; |
| 9531 | |
| 9532 | void ReusableStringStream::cleanup() { |
| 9533 | StringStreams::cleanup(); |
| 9534 | } |
| 9535 | |
| 9536 | ReusableStringStream::ReusableStringStream() |
| 9537 | : m_index( StringStreams::instance().add() ), |
| 9538 | m_oss( StringStreams::instance().m_streams[m_index].get() ) |
| 9539 | {} |
| 9540 | |
| 9541 | ReusableStringStream::~ReusableStringStream() { |
| 9542 | static_cast<std::ostringstream*>( m_oss )->str(""); |
| 9543 | m_oss->clear(); |
| 9544 | StringStreams::instance().release( m_index ); |
| 9545 | } |
| 9546 | |
| 9547 | auto ReusableStringStream::str() const -> std::string { |
| 9548 | return static_cast<std::ostringstream*>( m_oss )->str(); |
| 9549 | } |
| 9550 | |
| 9551 | /////////////////////////////////////////////////////////////////////////// |
| 9552 | |
| 9553 | #ifndef CATCH_CONFIG_NOSTDOUT // If you #define this you must implement these functions |
| 9554 | std::ostream& cout() { return std::cout; } |
| 9555 | std::ostream& cerr() { return std::cerr; } |
| 9556 | std::ostream& clog() { return std::clog; } |
| 9557 | #endif |
| 9558 | } |
| 9559 | |
| 9560 | #if defined(__clang__) |
| 9561 | # pragma clang diagnostic pop |
| 9562 | #endif |
| 9563 | // end catch_stream.cpp |
| 9564 | // start catch_string_manip.cpp |
| 9565 | |
| 9566 | #include <algorithm> |
| 9567 | #include <ostream> |
| 9568 | #include <cstring> |
| 9569 | #include <cctype> |
| 9570 | |
| 9571 | namespace Catch { |
| 9572 | |
| 9573 | bool startsWith( std::string const& s, std::string const& prefix ) { |
| 9574 | return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); |
| 9575 | } |
| 9576 | bool startsWith( std::string const& s, char prefix ) { |
| 9577 | return !s.empty() && s[0] == prefix; |
| 9578 | } |
| 9579 | bool endsWith( std::string const& s, std::string const& suffix ) { |
| 9580 | return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); |
| 9581 | } |
| 9582 | bool endsWith( std::string const& s, char suffix ) { |
| 9583 | return !s.empty() && s[s.size()-1] == suffix; |
| 9584 | } |
| 9585 | bool contains( std::string const& s, std::string const& infix ) { |
| 9586 | return s.find( infix ) != std::string::npos; |
| 9587 | } |
| 9588 | char toLowerCh(char c) { |
| 9589 | return static_cast<char>( std::tolower( c ) ); |
| 9590 | } |
| 9591 | void toLowerInPlace( std::string& s ) { |
| 9592 | std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); |
| 9593 | } |
| 9594 | std::string toLower( std::string const& s ) { |
| 9595 | std::string lc = s; |
| 9596 | toLowerInPlace( lc ); |
| 9597 | return lc; |
| 9598 | } |
| 9599 | std::string trim( std::string const& str ) { |
| 9600 | static char const* whitespaceChars = "\n\r\t "; |
| 9601 | std::string::size_type start = str.find_first_not_of( whitespaceChars ); |
| 9602 | std::string::size_type end = str.find_last_not_of( whitespaceChars ); |
| 9603 | |
| 9604 | return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); |
| 9605 | } |
| 9606 | |
| 9607 | bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { |
| 9608 | bool replaced = false; |
| 9609 | std::size_t i = str.find( replaceThis ); |
| 9610 | while( i != std::string::npos ) { |
| 9611 | replaced = true; |
| 9612 | str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); |
| 9613 | if( i < str.size()-withThis.size() ) |
| 9614 | i = str.find( replaceThis, i+withThis.size() ); |
| 9615 | else |
| 9616 | i = std::string::npos; |
| 9617 | } |
| 9618 | return replaced; |
| 9619 | } |
| 9620 | |
| 9621 | pluralise::pluralise( std::size_t count, std::string const& label ) |
| 9622 | : m_count( count ), |
| 9623 | m_label( label ) |
| 9624 | {} |
| 9625 | |
| 9626 | std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { |
| 9627 | os << pluraliser.m_count << ' ' << pluraliser.m_label; |
| 9628 | if( pluraliser.m_count != 1 ) |
| 9629 | os << 's'; |
| 9630 | return os; |
| 9631 | } |
| 9632 | |
| 9633 | } |
| 9634 | // end catch_string_manip.cpp |
| 9635 | // start catch_stringref.cpp |
| 9636 | |
| 9637 | #if defined(__clang__) |
| 9638 | # pragma clang diagnostic push |
| 9639 | # pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 9640 | #endif |
| 9641 | |
| 9642 | #include <ostream> |
| 9643 | #include <cstring> |
| 9644 | #include <cstdint> |
| 9645 | |
| 9646 | namespace { |
| 9647 | const uint32_t byte_2_lead = 0xC0; |
| 9648 | const uint32_t byte_3_lead = 0xE0; |
| 9649 | const uint32_t byte_4_lead = 0xF0; |
| 9650 | } |
| 9651 | |
| 9652 | namespace Catch { |
| 9653 | StringRef::StringRef( char const* rawChars ) noexcept |
| 9654 | : StringRef( rawChars, static_cast<StringRef::size_type>(std::strlen(rawChars) ) ) |
| 9655 | {} |
| 9656 | |
| 9657 | StringRef::operator std::string() const { |
| 9658 | return std::string( m_start, m_size ); |
| 9659 | } |
| 9660 | |
| 9661 | void StringRef::swap( StringRef& other ) noexcept { |
| 9662 | std::swap( m_start, other.m_start ); |
| 9663 | std::swap( m_size, other.m_size ); |
| 9664 | std::swap( m_data, other.m_data ); |
| 9665 | } |
| 9666 | |
| 9667 | auto StringRef::c_str() const -> char const* { |
| 9668 | if( isSubstring() ) |
| 9669 | const_cast<StringRef*>( this )->takeOwnership(); |
| 9670 | return m_start; |
| 9671 | } |
| 9672 | auto StringRef::currentData() const noexcept -> char const* { |
| 9673 | return m_start; |
| 9674 | } |
| 9675 | |
| 9676 | auto StringRef::isOwned() const noexcept -> bool { |
| 9677 | return m_data != nullptr; |
| 9678 | } |
| 9679 | auto StringRef::isSubstring() const noexcept -> bool { |
| 9680 | return m_start[m_size] != '\0'; |
| 9681 | } |
| 9682 | |
| 9683 | void StringRef::takeOwnership() { |
| 9684 | if( !isOwned() ) { |
| 9685 | m_data = new char[m_size+1]; |
| 9686 | memcpy( m_data, m_start, m_size ); |
| 9687 | m_data[m_size] = '\0'; |
| 9688 | m_start = m_data; |
| 9689 | } |
| 9690 | } |
| 9691 | auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { |
| 9692 | if( start < m_size ) |
| 9693 | return StringRef( m_start+start, size ); |
| 9694 | else |
| 9695 | return StringRef(); |
| 9696 | } |
| 9697 | auto StringRef::operator == ( StringRef const& other ) const noexcept -> bool { |
| 9698 | return |
| 9699 | size() == other.size() && |
| 9700 | (std::strncmp( m_start, other.m_start, size() ) == 0); |
| 9701 | } |
| 9702 | auto StringRef::operator != ( StringRef const& other ) const noexcept -> bool { |
| 9703 | return !operator==( other ); |
| 9704 | } |
| 9705 | |
| 9706 | auto StringRef::operator[](size_type index) const noexcept -> char { |
| 9707 | return m_start[index]; |
| 9708 | } |
| 9709 | |
| 9710 | auto StringRef::numberOfCharacters() const noexcept -> size_type { |
| 9711 | size_type noChars = m_size; |
| 9712 | // Make adjustments for uft encodings |
| 9713 | for( size_type i=0; i < m_size; ++i ) { |
| 9714 | char c = m_start[i]; |
| 9715 | if( ( c & byte_2_lead ) == byte_2_lead ) { |
| 9716 | noChars--; |
| 9717 | if (( c & byte_3_lead ) == byte_3_lead ) |
| 9718 | noChars--; |
| 9719 | if( ( c & byte_4_lead ) == byte_4_lead ) |
| 9720 | noChars--; |
| 9721 | } |
| 9722 | } |
| 9723 | return noChars; |
| 9724 | } |
| 9725 | |
| 9726 | auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string { |
| 9727 | std::string str; |
| 9728 | str.reserve( lhs.size() + rhs.size() ); |
| 9729 | str += lhs; |
| 9730 | str += rhs; |
| 9731 | return str; |
| 9732 | } |
| 9733 | auto operator + ( StringRef const& lhs, const char* rhs ) -> std::string { |
| 9734 | return std::string( lhs ) + std::string( rhs ); |
| 9735 | } |
| 9736 | auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string { |
| 9737 | return std::string( lhs ) + std::string( rhs ); |
| 9738 | } |
| 9739 | |
| 9740 | auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& { |
| 9741 | return os.write(str.currentData(), str.size()); |
| 9742 | } |
| 9743 | |
| 9744 | auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& { |
| 9745 | lhs.append(rhs.currentData(), rhs.size()); |
| 9746 | return lhs; |
| 9747 | } |
| 9748 | |
| 9749 | } // namespace Catch |
| 9750 | |
| 9751 | #if defined(__clang__) |
| 9752 | # pragma clang diagnostic pop |
| 9753 | #endif |
| 9754 | // end catch_stringref.cpp |
| 9755 | // start catch_tag_alias.cpp |
| 9756 | |
| 9757 | namespace Catch { |
| 9758 | TagAlias::TagAlias(std::string const & _tag, SourceLineInfo _lineInfo): tag(_tag), lineInfo(_lineInfo) {} |
| 9759 | } |
| 9760 | // end catch_tag_alias.cpp |
| 9761 | // start catch_tag_alias_autoregistrar.cpp |
| 9762 | |
| 9763 | namespace Catch { |
| 9764 | |
| 9765 | RegistrarForTagAliases::RegistrarForTagAliases(char const* alias, char const* tag, SourceLineInfo const& lineInfo) { |
| 9766 | try { |
| 9767 | getMutableRegistryHub().registerTagAlias(alias, tag, lineInfo); |
| 9768 | } catch (...) { |
| 9769 | // Do not throw when constructing global objects, instead register the exception to be processed later |
| 9770 | getMutableRegistryHub().registerStartupException(); |
| 9771 | } |
| 9772 | } |
| 9773 | |
| 9774 | } |
| 9775 | // end catch_tag_alias_autoregistrar.cpp |
| 9776 | // start catch_tag_alias_registry.cpp |
| 9777 | |
| 9778 | #include <sstream> |
| 9779 | |
| 9780 | namespace Catch { |
| 9781 | |
| 9782 | TagAliasRegistry::~TagAliasRegistry() {} |
| 9783 | |
| 9784 | TagAlias const* TagAliasRegistry::find( std::string const& alias ) const { |
| 9785 | auto it = m_registry.find( alias ); |
| 9786 | if( it != m_registry.end() ) |
| 9787 | return &(it->second); |
| 9788 | else |
| 9789 | return nullptr; |
| 9790 | } |
| 9791 | |
| 9792 | std::string TagAliasRegistry::expandAliases( std::string const& unexpandedTestSpec ) const { |
| 9793 | std::string expandedTestSpec = unexpandedTestSpec; |
| 9794 | for( auto const& registryKvp : m_registry ) { |
| 9795 | std::size_t pos = expandedTestSpec.find( registryKvp.first ); |
| 9796 | if( pos != std::string::npos ) { |
| 9797 | expandedTestSpec = expandedTestSpec.substr( 0, pos ) + |
| 9798 | registryKvp.second.tag + |
| 9799 | expandedTestSpec.substr( pos + registryKvp.first.size() ); |
| 9800 | } |
| 9801 | } |
| 9802 | return expandedTestSpec; |
| 9803 | } |
| 9804 | |
| 9805 | void TagAliasRegistry::add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) { |
| 9806 | CATCH_ENFORCE( startsWith(alias, "[@") && endsWith(alias, ']'), |
| 9807 | "error: tag alias, '" << alias << "' is not of the form [@alias name].\n" << lineInfo ); |
| 9808 | |
| 9809 | CATCH_ENFORCE( m_registry.insert(std::make_pair(alias, TagAlias(tag, lineInfo))).second, |
| 9810 | "error: tag alias, '" << alias << "' already registered.\n" |
| 9811 | << "\tFirst seen at: " << find(alias)->lineInfo << "\n" |
| 9812 | << "\tRedefined at: " << lineInfo ); |
| 9813 | } |
| 9814 | |
| 9815 | ITagAliasRegistry::~ITagAliasRegistry() {} |
| 9816 | |
| 9817 | ITagAliasRegistry const& ITagAliasRegistry::get() { |
| 9818 | return getRegistryHub().getTagAliasRegistry(); |
| 9819 | } |
| 9820 | |
| 9821 | } // end namespace Catch |
| 9822 | // end catch_tag_alias_registry.cpp |
| 9823 | // start catch_test_case_info.cpp |
| 9824 | |
| 9825 | #include <cctype> |
| 9826 | #include <exception> |
| 9827 | #include <algorithm> |
| 9828 | #include <sstream> |
| 9829 | |
| 9830 | namespace Catch { |
| 9831 | |
| 9832 | TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) { |
| 9833 | if( startsWith( tag, '.' ) || |
| 9834 | tag == "!hide" ) |
| 9835 | return TestCaseInfo::IsHidden; |
| 9836 | else if( tag == "!throws" ) |
| 9837 | return TestCaseInfo::Throws; |
| 9838 | else if( tag == "!shouldfail" ) |
| 9839 | return TestCaseInfo::ShouldFail; |
| 9840 | else if( tag == "!mayfail" ) |
| 9841 | return TestCaseInfo::MayFail; |
| 9842 | else if( tag == "!nonportable" ) |
| 9843 | return TestCaseInfo::NonPortable; |
| 9844 | else if( tag == "!benchmark" ) |
| 9845 | return static_cast<TestCaseInfo::SpecialProperties>( TestCaseInfo::Benchmark | TestCaseInfo::IsHidden ); |
| 9846 | else |
| 9847 | return TestCaseInfo::None; |
| 9848 | } |
| 9849 | bool isReservedTag( std::string const& tag ) { |
| 9850 | return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( tag[0] ); |
| 9851 | } |
| 9852 | void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) { |
| 9853 | CATCH_ENFORCE( !isReservedTag(tag), |
| 9854 | "Tag name: [" << tag << "] is not allowed.\n" |
| 9855 | << "Tag names starting with non alpha-numeric characters are reserved\n" |
| 9856 | << _lineInfo ); |
| 9857 | } |
| 9858 | |
| 9859 | TestCase makeTestCase( ITestInvoker* _testCase, |
| 9860 | std::string const& _className, |
| 9861 | NameAndTags const& nameAndTags, |
| 9862 | SourceLineInfo const& _lineInfo ) |
| 9863 | { |
| 9864 | bool isHidden = false; |
| 9865 | |
| 9866 | // Parse out tags |
| 9867 | std::vector<std::string> tags; |
| 9868 | std::string desc, tag; |
| 9869 | bool inTag = false; |
| 9870 | std::string _descOrTags = nameAndTags.tags; |
| 9871 | for (char c : _descOrTags) { |
| 9872 | if( !inTag ) { |
| 9873 | if( c == '[' ) |
| 9874 | inTag = true; |
| 9875 | else |
| 9876 | desc += c; |
| 9877 | } |
| 9878 | else { |
| 9879 | if( c == ']' ) { |
| 9880 | TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag ); |
| 9881 | if( ( prop & TestCaseInfo::IsHidden ) != 0 ) |
| 9882 | isHidden = true; |
| 9883 | else if( prop == TestCaseInfo::None ) |
| 9884 | enforceNotReservedTag( tag, _lineInfo ); |
| 9885 | |
| 9886 | tags.push_back( tag ); |
| 9887 | tag.clear(); |
| 9888 | inTag = false; |
| 9889 | } |
| 9890 | else |
| 9891 | tag += c; |
| 9892 | } |
| 9893 | } |
| 9894 | if( isHidden ) { |
| 9895 | tags.push_back( "." ); |
| 9896 | } |
| 9897 | |
| 9898 | TestCaseInfo info( nameAndTags.name, _className, desc, tags, _lineInfo ); |
| 9899 | return TestCase( _testCase, std::move(info) ); |
| 9900 | } |
| 9901 | |
| 9902 | void setTags( TestCaseInfo& testCaseInfo, std::vector<std::string> tags ) { |
| 9903 | std::sort(begin(tags), end(tags)); |
| 9904 | tags.erase(std::unique(begin(tags), end(tags)), end(tags)); |
| 9905 | testCaseInfo.lcaseTags.clear(); |
| 9906 | |
| 9907 | for( auto const& tag : tags ) { |
| 9908 | std::string lcaseTag = toLower( tag ); |
| 9909 | testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) ); |
| 9910 | testCaseInfo.lcaseTags.push_back( lcaseTag ); |
| 9911 | } |
| 9912 | testCaseInfo.tags = std::move(tags); |
| 9913 | } |
| 9914 | |
| 9915 | TestCaseInfo::TestCaseInfo( std::string const& _name, |
| 9916 | std::string const& _className, |
| 9917 | std::string const& _description, |
| 9918 | std::vector<std::string> const& _tags, |
| 9919 | SourceLineInfo const& _lineInfo ) |
| 9920 | : name( _name ), |
| 9921 | className( _className ), |
| 9922 | description( _description ), |
| 9923 | lineInfo( _lineInfo ), |
| 9924 | properties( None ) |
| 9925 | { |
| 9926 | setTags( *this, _tags ); |
| 9927 | } |
| 9928 | |
| 9929 | bool TestCaseInfo::isHidden() const { |
| 9930 | return ( properties & IsHidden ) != 0; |
| 9931 | } |
| 9932 | bool TestCaseInfo::throws() const { |
| 9933 | return ( properties & Throws ) != 0; |
| 9934 | } |
| 9935 | bool TestCaseInfo::okToFail() const { |
| 9936 | return ( properties & (ShouldFail | MayFail ) ) != 0; |
| 9937 | } |
| 9938 | bool TestCaseInfo::expectedToFail() const { |
| 9939 | return ( properties & (ShouldFail ) ) != 0; |
| 9940 | } |
| 9941 | |
| 9942 | std::string TestCaseInfo::tagsAsString() const { |
| 9943 | std::string ret; |
| 9944 | // '[' and ']' per tag |
| 9945 | std::size_t full_size = 2 * tags.size(); |
| 9946 | for (const auto& tag : tags) { |
| 9947 | full_size += tag.size(); |
| 9948 | } |
| 9949 | ret.reserve(full_size); |
| 9950 | for (const auto& tag : tags) { |
| 9951 | ret.push_back('['); |
| 9952 | ret.append(tag); |
| 9953 | ret.push_back(']'); |
| 9954 | } |
| 9955 | |
| 9956 | return ret; |
| 9957 | } |
| 9958 | |
| 9959 | TestCase::TestCase( ITestInvoker* testCase, TestCaseInfo&& info ) : TestCaseInfo( std::move(info) ), test( testCase ) {} |
| 9960 | |
| 9961 | TestCase TestCase::withName( std::string const& _newName ) const { |
| 9962 | TestCase other( *this ); |
| 9963 | other.name = _newName; |
| 9964 | return other; |
| 9965 | } |
| 9966 | |
| 9967 | void TestCase::invoke() const { |
| 9968 | test->invoke(); |
| 9969 | } |
| 9970 | |
| 9971 | bool TestCase::operator == ( TestCase const& other ) const { |
| 9972 | return test.get() == other.test.get() && |
| 9973 | name == other.name && |
| 9974 | className == other.className; |
| 9975 | } |
| 9976 | |
| 9977 | bool TestCase::operator < ( TestCase const& other ) const { |
| 9978 | return name < other.name; |
| 9979 | } |
| 9980 | |
| 9981 | TestCaseInfo const& TestCase::getTestCaseInfo() const |
| 9982 | { |
| 9983 | return *this; |
| 9984 | } |
| 9985 | |
| 9986 | } // end namespace Catch |
| 9987 | // end catch_test_case_info.cpp |
| 9988 | // start catch_test_case_registry_impl.cpp |
| 9989 | |
| 9990 | #include <sstream> |
| 9991 | |
| 9992 | namespace Catch { |
| 9993 | |
| 9994 | std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) { |
| 9995 | |
| 9996 | std::vector<TestCase> sorted = unsortedTestCases; |
| 9997 | |
| 9998 | switch( config.runOrder() ) { |
| 9999 | case RunTests::InLexicographicalOrder: |
| 10000 | std::sort( sorted.begin(), sorted.end() ); |
| 10001 | break; |
| 10002 | case RunTests::InRandomOrder: |
| 10003 | seedRng( config ); |
| 10004 | RandomNumberGenerator::shuffle( sorted ); |
| 10005 | break; |
| 10006 | case RunTests::InDeclarationOrder: |
| 10007 | // already in declaration order |
| 10008 | break; |
| 10009 | } |
| 10010 | return sorted; |
| 10011 | } |
| 10012 | bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ) { |
| 10013 | return testSpec.matches( testCase ) && ( config.allowThrows() || !testCase.throws() ); |
| 10014 | } |
| 10015 | |
| 10016 | void enforceNoDuplicateTestCases( std::vector<TestCase> const& functions ) { |
| 10017 | std::set<TestCase> seenFunctions; |
| 10018 | for( auto const& function : functions ) { |
| 10019 | auto prev = seenFunctions.insert( function ); |
| 10020 | CATCH_ENFORCE( prev.second, |
| 10021 | "error: TEST_CASE( \"" << function.name << "\" ) already defined.\n" |
| 10022 | << "\tFirst seen at " << prev.first->getTestCaseInfo().lineInfo << "\n" |
| 10023 | << "\tRedefined at " << function.getTestCaseInfo().lineInfo ); |
| 10024 | } |
| 10025 | } |
| 10026 | |
| 10027 | std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ) { |
| 10028 | std::vector<TestCase> filtered; |
| 10029 | filtered.reserve( testCases.size() ); |
| 10030 | for( auto const& testCase : testCases ) |
| 10031 | if( matchTest( testCase, testSpec, config ) ) |
| 10032 | filtered.push_back( testCase ); |
| 10033 | return filtered; |
| 10034 | } |
| 10035 | std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ) { |
| 10036 | return getRegistryHub().getTestCaseRegistry().getAllTestsSorted( config ); |
| 10037 | } |
| 10038 | |
| 10039 | void TestRegistry::registerTest( TestCase const& testCase ) { |
| 10040 | std::string name = testCase.getTestCaseInfo().name; |
| 10041 | if( name.empty() ) { |
| 10042 | ReusableStringStream rss; |
| 10043 | rss << "Anonymous test case " << ++m_unnamedCount; |
| 10044 | return registerTest( testCase.withName( rss.str() ) ); |
| 10045 | } |
| 10046 | m_functions.push_back( testCase ); |
| 10047 | } |
| 10048 | |
| 10049 | std::vector<TestCase> const& TestRegistry::getAllTests() const { |
| 10050 | return m_functions; |
| 10051 | } |
| 10052 | std::vector<TestCase> const& TestRegistry::getAllTestsSorted( IConfig const& config ) const { |
| 10053 | if( m_sortedFunctions.empty() ) |
| 10054 | enforceNoDuplicateTestCases( m_functions ); |
| 10055 | |
| 10056 | if( m_currentSortOrder != config.runOrder() || m_sortedFunctions.empty() ) { |
| 10057 | m_sortedFunctions = sortTests( config, m_functions ); |
| 10058 | m_currentSortOrder = config.runOrder(); |
| 10059 | } |
| 10060 | return m_sortedFunctions; |
| 10061 | } |
| 10062 | |
| 10063 | /////////////////////////////////////////////////////////////////////////// |
| 10064 | TestInvokerAsFunction::TestInvokerAsFunction( void(*testAsFunction)() ) noexcept : m_testAsFunction( testAsFunction ) {} |
| 10065 | |
| 10066 | void TestInvokerAsFunction::invoke() const { |
| 10067 | m_testAsFunction(); |
| 10068 | } |
| 10069 | |
| 10070 | std::string extractClassName( StringRef const& classOrQualifiedMethodName ) { |
| 10071 | std::string className = classOrQualifiedMethodName; |
| 10072 | if( startsWith( className, '&' ) ) |
| 10073 | { |
| 10074 | std::size_t lastColons = className.rfind( "::" ); |
| 10075 | std::size_t penultimateColons = className.rfind( "::", lastColons-1 ); |
| 10076 | if( penultimateColons == std::string::npos ) |
| 10077 | penultimateColons = 1; |
| 10078 | className = className.substr( penultimateColons, lastColons-penultimateColons ); |
| 10079 | } |
| 10080 | return className; |
| 10081 | } |
| 10082 | |
| 10083 | } // end namespace Catch |
| 10084 | // end catch_test_case_registry_impl.cpp |
| 10085 | // start catch_test_case_tracker.cpp |
| 10086 | |
| 10087 | #include <algorithm> |
| 10088 | #include <assert.h> |
| 10089 | #include <stdexcept> |
| 10090 | #include <memory> |
| 10091 | #include <sstream> |
| 10092 | |
| 10093 | #if defined(__clang__) |
| 10094 | # pragma clang diagnostic push |
| 10095 | # pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 10096 | #endif |
| 10097 | |
| 10098 | namespace Catch { |
| 10099 | namespace TestCaseTracking { |
| 10100 | |
| 10101 | NameAndLocation::NameAndLocation( std::string const& _name, SourceLineInfo const& _location ) |
| 10102 | : name( _name ), |
| 10103 | location( _location ) |
| 10104 | {} |
| 10105 | |
| 10106 | ITracker::~ITracker() = default; |
| 10107 | |
| 10108 | TrackerContext& TrackerContext::instance() { |
| 10109 | static TrackerContext s_instance; |
| 10110 | return s_instance; |
| 10111 | } |
| 10112 | |
| 10113 | ITracker& TrackerContext::startRun() { |
| 10114 | m_rootTracker = std::make_shared<SectionTracker>( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, nullptr ); |
| 10115 | m_currentTracker = nullptr; |
| 10116 | m_runState = Executing; |
| 10117 | return *m_rootTracker; |
| 10118 | } |
| 10119 | |
| 10120 | void TrackerContext::endRun() { |
| 10121 | m_rootTracker.reset(); |
| 10122 | m_currentTracker = nullptr; |
| 10123 | m_runState = NotStarted; |
| 10124 | } |
| 10125 | |
| 10126 | void TrackerContext::startCycle() { |
| 10127 | m_currentTracker = m_rootTracker.get(); |
| 10128 | m_runState = Executing; |
| 10129 | } |
| 10130 | void TrackerContext::completeCycle() { |
| 10131 | m_runState = CompletedCycle; |
| 10132 | } |
| 10133 | |
| 10134 | bool TrackerContext::completedCycle() const { |
| 10135 | return m_runState == CompletedCycle; |
| 10136 | } |
| 10137 | ITracker& TrackerContext::currentTracker() { |
| 10138 | return *m_currentTracker; |
| 10139 | } |
| 10140 | void TrackerContext::setCurrentTracker( ITracker* tracker ) { |
| 10141 | m_currentTracker = tracker; |
| 10142 | } |
| 10143 | |
| 10144 | TrackerBase::TrackerHasName::TrackerHasName( NameAndLocation const& nameAndLocation ) : m_nameAndLocation( nameAndLocation ) {} |
| 10145 | bool TrackerBase::TrackerHasName::operator ()( ITrackerPtr const& tracker ) const { |
| 10146 | return |
| 10147 | tracker->nameAndLocation().name == m_nameAndLocation.name && |
| 10148 | tracker->nameAndLocation().location == m_nameAndLocation.location; |
| 10149 | } |
| 10150 | |
| 10151 | TrackerBase::TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) |
| 10152 | : m_nameAndLocation( nameAndLocation ), |
| 10153 | m_ctx( ctx ), |
| 10154 | m_parent( parent ) |
| 10155 | {} |
| 10156 | |
| 10157 | NameAndLocation const& TrackerBase::nameAndLocation() const { |
| 10158 | return m_nameAndLocation; |
| 10159 | } |
| 10160 | bool TrackerBase::isComplete() const { |
| 10161 | return m_runState == CompletedSuccessfully || m_runState == Failed; |
| 10162 | } |
| 10163 | bool TrackerBase::isSuccessfullyCompleted() const { |
| 10164 | return m_runState == CompletedSuccessfully; |
| 10165 | } |
| 10166 | bool TrackerBase::isOpen() const { |
| 10167 | return m_runState != NotStarted && !isComplete(); |
| 10168 | } |
| 10169 | bool TrackerBase::hasChildren() const { |
| 10170 | return !m_children.empty(); |
| 10171 | } |
| 10172 | |
| 10173 | void TrackerBase::addChild( ITrackerPtr const& child ) { |
| 10174 | m_children.push_back( child ); |
| 10175 | } |
| 10176 | |
| 10177 | ITrackerPtr TrackerBase::findChild( NameAndLocation const& nameAndLocation ) { |
| 10178 | auto it = std::find_if( m_children.begin(), m_children.end(), TrackerHasName( nameAndLocation ) ); |
| 10179 | return( it != m_children.end() ) |
| 10180 | ? *it |
| 10181 | : nullptr; |
| 10182 | } |
| 10183 | ITracker& TrackerBase::parent() { |
| 10184 | assert( m_parent ); // Should always be non-null except for root |
| 10185 | return *m_parent; |
| 10186 | } |
| 10187 | |
| 10188 | void TrackerBase::openChild() { |
| 10189 | if( m_runState != ExecutingChildren ) { |
| 10190 | m_runState = ExecutingChildren; |
| 10191 | if( m_parent ) |
| 10192 | m_parent->openChild(); |
| 10193 | } |
| 10194 | } |
| 10195 | |
| 10196 | bool TrackerBase::isSectionTracker() const { return false; } |
| 10197 | bool TrackerBase::isIndexTracker() const { return false; } |
| 10198 | |
| 10199 | void TrackerBase::open() { |
| 10200 | m_runState = Executing; |
| 10201 | moveToThis(); |
| 10202 | if( m_parent ) |
| 10203 | m_parent->openChild(); |
| 10204 | } |
| 10205 | |
| 10206 | void TrackerBase::close() { |
| 10207 | |
| 10208 | // Close any still open children (e.g. generators) |
| 10209 | while( &m_ctx.currentTracker() != this ) |
| 10210 | m_ctx.currentTracker().close(); |
| 10211 | |
| 10212 | switch( m_runState ) { |
| 10213 | case NeedsAnotherRun: |
| 10214 | break; |
| 10215 | |
| 10216 | case Executing: |
| 10217 | m_runState = CompletedSuccessfully; |
| 10218 | break; |
| 10219 | case ExecutingChildren: |
| 10220 | if( m_children.empty() || m_children.back()->isComplete() ) |
| 10221 | m_runState = CompletedSuccessfully; |
| 10222 | break; |
| 10223 | |
| 10224 | case NotStarted: |
| 10225 | case CompletedSuccessfully: |
| 10226 | case Failed: |
| 10227 | CATCH_INTERNAL_ERROR( "Illogical state: " << m_runState ); |
| 10228 | |
| 10229 | default: |
| 10230 | CATCH_INTERNAL_ERROR( "Unknown state: " << m_runState ); |
| 10231 | } |
| 10232 | moveToParent(); |
| 10233 | m_ctx.completeCycle(); |
| 10234 | } |
| 10235 | void TrackerBase::fail() { |
| 10236 | m_runState = Failed; |
| 10237 | if( m_parent ) |
| 10238 | m_parent->markAsNeedingAnotherRun(); |
| 10239 | moveToParent(); |
| 10240 | m_ctx.completeCycle(); |
| 10241 | } |
| 10242 | void TrackerBase::markAsNeedingAnotherRun() { |
| 10243 | m_runState = NeedsAnotherRun; |
| 10244 | } |
| 10245 | |
| 10246 | void TrackerBase::moveToParent() { |
| 10247 | assert( m_parent ); |
| 10248 | m_ctx.setCurrentTracker( m_parent ); |
| 10249 | } |
| 10250 | void TrackerBase::moveToThis() { |
| 10251 | m_ctx.setCurrentTracker( this ); |
| 10252 | } |
| 10253 | |
| 10254 | SectionTracker::SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) |
| 10255 | : TrackerBase( nameAndLocation, ctx, parent ) |
| 10256 | { |
| 10257 | if( parent ) { |
| 10258 | while( !parent->isSectionTracker() ) |
| 10259 | parent = &parent->parent(); |
| 10260 | |
| 10261 | SectionTracker& parentSection = static_cast<SectionTracker&>( *parent ); |
| 10262 | addNextFilters( parentSection.m_filters ); |
| 10263 | } |
| 10264 | } |
| 10265 | |
| 10266 | bool SectionTracker::isSectionTracker() const { return true; } |
| 10267 | |
| 10268 | SectionTracker& SectionTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) { |
| 10269 | std::shared_ptr<SectionTracker> section; |
| 10270 | |
| 10271 | ITracker& currentTracker = ctx.currentTracker(); |
| 10272 | if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { |
| 10273 | assert( childTracker ); |
| 10274 | assert( childTracker->isSectionTracker() ); |
| 10275 | section = std::static_pointer_cast<SectionTracker>( childTracker ); |
| 10276 | } |
| 10277 | else { |
| 10278 | section = std::make_shared<SectionTracker>( nameAndLocation, ctx, ¤tTracker ); |
| 10279 | currentTracker.addChild( section ); |
| 10280 | } |
| 10281 | if( !ctx.completedCycle() ) |
| 10282 | section->tryOpen(); |
| 10283 | return *section; |
| 10284 | } |
| 10285 | |
| 10286 | void SectionTracker::tryOpen() { |
| 10287 | if( !isComplete() && (m_filters.empty() || m_filters[0].empty() || m_filters[0] == m_nameAndLocation.name ) ) |
| 10288 | open(); |
| 10289 | } |
| 10290 | |
| 10291 | void SectionTracker::addInitialFilters( std::vector<std::string> const& filters ) { |
| 10292 | if( !filters.empty() ) { |
| 10293 | m_filters.push_back(""); // Root - should never be consulted |
| 10294 | m_filters.push_back(""); // Test Case - not a section filter |
| 10295 | m_filters.insert( m_filters.end(), filters.begin(), filters.end() ); |
| 10296 | } |
| 10297 | } |
| 10298 | void SectionTracker::addNextFilters( std::vector<std::string> const& filters ) { |
| 10299 | if( filters.size() > 1 ) |
| 10300 | m_filters.insert( m_filters.end(), ++filters.begin(), filters.end() ); |
| 10301 | } |
| 10302 | |
| 10303 | IndexTracker::IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size ) |
| 10304 | : TrackerBase( nameAndLocation, ctx, parent ), |
| 10305 | m_size( size ) |
| 10306 | {} |
| 10307 | |
| 10308 | bool IndexTracker::isIndexTracker() const { return true; } |
| 10309 | |
| 10310 | IndexTracker& IndexTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ) { |
| 10311 | std::shared_ptr<IndexTracker> tracker; |
| 10312 | |
| 10313 | ITracker& currentTracker = ctx.currentTracker(); |
| 10314 | if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { |
| 10315 | assert( childTracker ); |
| 10316 | assert( childTracker->isIndexTracker() ); |
| 10317 | tracker = std::static_pointer_cast<IndexTracker>( childTracker ); |
| 10318 | } |
| 10319 | else { |
| 10320 | tracker = std::make_shared<IndexTracker>( nameAndLocation, ctx, ¤tTracker, size ); |
| 10321 | currentTracker.addChild( tracker ); |
| 10322 | } |
| 10323 | |
| 10324 | if( !ctx.completedCycle() && !tracker->isComplete() ) { |
| 10325 | if( tracker->m_runState != ExecutingChildren && tracker->m_runState != NeedsAnotherRun ) |
| 10326 | tracker->moveNext(); |
| 10327 | tracker->open(); |
| 10328 | } |
| 10329 | |
| 10330 | return *tracker; |
| 10331 | } |
| 10332 | |
| 10333 | int IndexTracker::index() const { return m_index; } |
| 10334 | |
| 10335 | void IndexTracker::moveNext() { |
| 10336 | m_index++; |
| 10337 | m_children.clear(); |
| 10338 | } |
| 10339 | |
| 10340 | void IndexTracker::close() { |
| 10341 | TrackerBase::close(); |
| 10342 | if( m_runState == CompletedSuccessfully && m_index < m_size-1 ) |
| 10343 | m_runState = Executing; |
| 10344 | } |
| 10345 | |
| 10346 | } // namespace TestCaseTracking |
| 10347 | |
| 10348 | using TestCaseTracking::ITracker; |
| 10349 | using TestCaseTracking::TrackerContext; |
| 10350 | using TestCaseTracking::SectionTracker; |
| 10351 | using TestCaseTracking::IndexTracker; |
| 10352 | |
| 10353 | } // namespace Catch |
| 10354 | |
| 10355 | #if defined(__clang__) |
| 10356 | # pragma clang diagnostic pop |
| 10357 | #endif |
| 10358 | // end catch_test_case_tracker.cpp |
| 10359 | // start catch_test_registry.cpp |
| 10360 | |
| 10361 | namespace Catch { |
| 10362 | |
| 10363 | auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker* { |
| 10364 | return new(std::nothrow) TestInvokerAsFunction( testAsFunction ); |
| 10365 | } |
| 10366 | |
| 10367 | NameAndTags::NameAndTags( StringRef const& name_ , StringRef const& tags_ ) noexcept : name( name_ ), tags( tags_ ) {} |
| 10368 | |
| 10369 | AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef const& classOrMethod, NameAndTags const& nameAndTags ) noexcept { |
| 10370 | try { |
| 10371 | getMutableRegistryHub() |
| 10372 | .registerTest( |
| 10373 | makeTestCase( |
| 10374 | invoker, |
| 10375 | extractClassName( classOrMethod ), |
| 10376 | nameAndTags, |
| 10377 | lineInfo)); |
| 10378 | } catch (...) { |
| 10379 | // Do not throw when constructing global objects, instead register the exception to be processed later |
| 10380 | getMutableRegistryHub().registerStartupException(); |
| 10381 | } |
| 10382 | } |
| 10383 | |
| 10384 | AutoReg::~AutoReg() = default; |
| 10385 | } |
| 10386 | // end catch_test_registry.cpp |
| 10387 | // start catch_test_spec.cpp |
| 10388 | |
| 10389 | #include <algorithm> |
| 10390 | #include <string> |
| 10391 | #include <vector> |
| 10392 | #include <memory> |
| 10393 | |
| 10394 | namespace Catch { |
| 10395 | |
| 10396 | TestSpec::Pattern::~Pattern() = default; |
| 10397 | TestSpec::NamePattern::~NamePattern() = default; |
| 10398 | TestSpec::TagPattern::~TagPattern() = default; |
| 10399 | TestSpec::ExcludedPattern::~ExcludedPattern() = default; |
| 10400 | |
| 10401 | TestSpec::NamePattern::NamePattern( std::string const& name ) |
| 10402 | : m_wildcardPattern( toLower( name ), CaseSensitive::No ) |
| 10403 | {} |
| 10404 | bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const { |
| 10405 | return m_wildcardPattern.matches( toLower( testCase.name ) ); |
| 10406 | } |
| 10407 | |
| 10408 | TestSpec::TagPattern::TagPattern( std::string const& tag ) : m_tag( toLower( tag ) ) {} |
| 10409 | bool TestSpec::TagPattern::matches( TestCaseInfo const& testCase ) const { |
| 10410 | return std::find(begin(testCase.lcaseTags), |
| 10411 | end(testCase.lcaseTags), |
| 10412 | m_tag) != end(testCase.lcaseTags); |
| 10413 | } |
| 10414 | |
| 10415 | TestSpec::ExcludedPattern::ExcludedPattern( PatternPtr const& underlyingPattern ) : m_underlyingPattern( underlyingPattern ) {} |
| 10416 | bool TestSpec::ExcludedPattern::matches( TestCaseInfo const& testCase ) const { return !m_underlyingPattern->matches( testCase ); } |
| 10417 | |
| 10418 | bool TestSpec::Filter::matches( TestCaseInfo const& testCase ) const { |
| 10419 | // All patterns in a filter must match for the filter to be a match |
| 10420 | for( auto const& pattern : m_patterns ) { |
| 10421 | if( !pattern->matches( testCase ) ) |
| 10422 | return false; |
| 10423 | } |
| 10424 | return true; |
| 10425 | } |
| 10426 | |
| 10427 | bool TestSpec::hasFilters() const { |
| 10428 | return !m_filters.empty(); |
| 10429 | } |
| 10430 | bool TestSpec::matches( TestCaseInfo const& testCase ) const { |
| 10431 | // A TestSpec matches if any filter matches |
| 10432 | for( auto const& filter : m_filters ) |
| 10433 | if( filter.matches( testCase ) ) |
| 10434 | return true; |
| 10435 | return false; |
| 10436 | } |
| 10437 | } |
| 10438 | // end catch_test_spec.cpp |
| 10439 | // start catch_test_spec_parser.cpp |
| 10440 | |
| 10441 | namespace Catch { |
| 10442 | |
| 10443 | TestSpecParser::TestSpecParser( ITagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {} |
| 10444 | |
| 10445 | TestSpecParser& TestSpecParser::parse( std::string const& arg ) { |
| 10446 | m_mode = None; |
| 10447 | m_exclusion = false; |
| 10448 | m_start = std::string::npos; |
| 10449 | m_arg = m_tagAliases->expandAliases( arg ); |
| 10450 | m_escapeChars.clear(); |
| 10451 | for( m_pos = 0; m_pos < m_arg.size(); ++m_pos ) |
| 10452 | visitChar( m_arg[m_pos] ); |
| 10453 | if( m_mode == Name ) |
| 10454 | addPattern<TestSpec::NamePattern>(); |
| 10455 | return *this; |
| 10456 | } |
| 10457 | TestSpec TestSpecParser::testSpec() { |
| 10458 | addFilter(); |
| 10459 | return m_testSpec; |
| 10460 | } |
| 10461 | |
| 10462 | void TestSpecParser::visitChar( char c ) { |
| 10463 | if( m_mode == None ) { |
| 10464 | switch( c ) { |
| 10465 | case ' ': return; |
| 10466 | case '~': m_exclusion = true; return; |
| 10467 | case '[': return startNewMode( Tag, ++m_pos ); |
| 10468 | case '"': return startNewMode( QuotedName, ++m_pos ); |
| 10469 | case '\\': return escape(); |
| 10470 | default: startNewMode( Name, m_pos ); break; |
| 10471 | } |
| 10472 | } |
| 10473 | if( m_mode == Name ) { |
| 10474 | if( c == ',' ) { |
| 10475 | addPattern<TestSpec::NamePattern>(); |
| 10476 | addFilter(); |
| 10477 | } |
| 10478 | else if( c == '[' ) { |
| 10479 | if( subString() == "exclude:" ) |
| 10480 | m_exclusion = true; |
| 10481 | else |
| 10482 | addPattern<TestSpec::NamePattern>(); |
| 10483 | startNewMode( Tag, ++m_pos ); |
| 10484 | } |
| 10485 | else if( c == '\\' ) |
| 10486 | escape(); |
| 10487 | } |
| 10488 | else if( m_mode == EscapedName ) |
| 10489 | m_mode = Name; |
| 10490 | else if( m_mode == QuotedName && c == '"' ) |
| 10491 | addPattern<TestSpec::NamePattern>(); |
| 10492 | else if( m_mode == Tag && c == ']' ) |
| 10493 | addPattern<TestSpec::TagPattern>(); |
| 10494 | } |
| 10495 | void TestSpecParser::startNewMode( Mode mode, std::size_t start ) { |
| 10496 | m_mode = mode; |
| 10497 | m_start = start; |
| 10498 | } |
| 10499 | void TestSpecParser::escape() { |
| 10500 | if( m_mode == None ) |
| 10501 | m_start = m_pos; |
| 10502 | m_mode = EscapedName; |
| 10503 | m_escapeChars.push_back( m_pos ); |
| 10504 | } |
| 10505 | std::string TestSpecParser::subString() const { return m_arg.substr( m_start, m_pos - m_start ); } |
| 10506 | |
| 10507 | void TestSpecParser::addFilter() { |
| 10508 | if( !m_currentFilter.m_patterns.empty() ) { |
| 10509 | m_testSpec.m_filters.push_back( m_currentFilter ); |
| 10510 | m_currentFilter = TestSpec::Filter(); |
| 10511 | } |
| 10512 | } |
| 10513 | |
| 10514 | TestSpec parseTestSpec( std::string const& arg ) { |
| 10515 | return TestSpecParser( ITagAliasRegistry::get() ).parse( arg ).testSpec(); |
| 10516 | } |
| 10517 | |
| 10518 | } // namespace Catch |
| 10519 | // end catch_test_spec_parser.cpp |
| 10520 | // start catch_timer.cpp |
| 10521 | |
| 10522 | #include <chrono> |
| 10523 | |
| 10524 | static const uint64_t nanosecondsInSecond = 1000000000; |
| 10525 | |
| 10526 | namespace Catch { |
| 10527 | |
| 10528 | auto getCurrentNanosecondsSinceEpoch() -> uint64_t { |
| 10529 | return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count(); |
| 10530 | } |
| 10531 | |
| 10532 | auto estimateClockResolution() -> uint64_t { |
| 10533 | uint64_t sum = 0; |
| 10534 | static const uint64_t iterations = 1000000; |
| 10535 | |
| 10536 | auto startTime = getCurrentNanosecondsSinceEpoch(); |
| 10537 | |
| 10538 | for( std::size_t i = 0; i < iterations; ++i ) { |
| 10539 | |
| 10540 | uint64_t ticks; |
| 10541 | uint64_t baseTicks = getCurrentNanosecondsSinceEpoch(); |
| 10542 | do { |
| 10543 | ticks = getCurrentNanosecondsSinceEpoch(); |
| 10544 | } while( ticks == baseTicks ); |
| 10545 | |
| 10546 | auto delta = ticks - baseTicks; |
| 10547 | sum += delta; |
| 10548 | |
| 10549 | // If we have been calibrating for over 3 seconds -- the clock |
| 10550 | // is terrible and we should move on. |
| 10551 | // TBD: How to signal that the measured resolution is probably wrong? |
| 10552 | if (ticks > startTime + 3 * nanosecondsInSecond) { |
| 10553 | return sum / i; |
| 10554 | } |
| 10555 | } |
| 10556 | |
| 10557 | // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers |
| 10558 | // - and potentially do more iterations if there's a high variance. |
| 10559 | return sum/iterations; |
| 10560 | } |
| 10561 | auto getEstimatedClockResolution() -> uint64_t { |
| 10562 | static auto s_resolution = estimateClockResolution(); |
| 10563 | return s_resolution; |
| 10564 | } |
| 10565 | |
| 10566 | void Timer::start() { |
| 10567 | m_nanoseconds = getCurrentNanosecondsSinceEpoch(); |
| 10568 | } |
| 10569 | auto Timer::getElapsedNanoseconds() const -> uint64_t { |
| 10570 | return getCurrentNanosecondsSinceEpoch() - m_nanoseconds; |
| 10571 | } |
| 10572 | auto Timer::getElapsedMicroseconds() const -> uint64_t { |
| 10573 | return getElapsedNanoseconds()/1000; |
| 10574 | } |
| 10575 | auto Timer::getElapsedMilliseconds() const -> unsigned int { |
| 10576 | return static_cast<unsigned int>(getElapsedMicroseconds()/1000); |
| 10577 | } |
| 10578 | auto Timer::getElapsedSeconds() const -> double { |
| 10579 | return getElapsedMicroseconds()/1000000.0; |
| 10580 | } |
| 10581 | |
| 10582 | } // namespace Catch |
| 10583 | // end catch_timer.cpp |
| 10584 | // start catch_tostring.cpp |
| 10585 | |
| 10586 | #if defined(__clang__) |
| 10587 | # pragma clang diagnostic push |
| 10588 | # pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 10589 | # pragma clang diagnostic ignored "-Wglobal-constructors" |
| 10590 | #endif |
| 10591 | |
| 10592 | // Enable specific decls locally |
| 10593 | #if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) |
| 10594 | #define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER |
| 10595 | #endif |
| 10596 | |
| 10597 | #include <cmath> |
| 10598 | #include <iomanip> |
| 10599 | |
| 10600 | namespace Catch { |
| 10601 | |
| 10602 | namespace Detail { |
| 10603 | |
| 10604 | const std::string unprintableString = "{?}"; |
| 10605 | |
| 10606 | namespace { |
| 10607 | const int hexThreshold = 255; |
| 10608 | |
| 10609 | struct Endianness { |
| 10610 | enum Arch { Big, Little }; |
| 10611 | |
| 10612 | static Arch which() { |
| 10613 | union _{ |
| 10614 | int asInt; |
| 10615 | char asChar[sizeof (int)]; |
| 10616 | } u; |
| 10617 | |
| 10618 | u.asInt = 1; |
| 10619 | return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little; |
| 10620 | } |
| 10621 | }; |
| 10622 | } |
| 10623 | |
| 10624 | std::string rawMemoryToString( const void *object, std::size_t size ) { |
| 10625 | // Reverse order for little endian architectures |
| 10626 | int i = 0, end = static_cast<int>( size ), inc = 1; |
| 10627 | if( Endianness::which() == Endianness::Little ) { |
| 10628 | i = end-1; |
| 10629 | end = inc = -1; |
| 10630 | } |
| 10631 | |
| 10632 | unsigned char const *bytes = static_cast<unsigned char const *>(object); |
| 10633 | ReusableStringStream rss; |
| 10634 | rss << "0x" << std::setfill('0') << std::hex; |
| 10635 | for( ; i != end; i += inc ) |
| 10636 | rss << std::setw(2) << static_cast<unsigned>(bytes[i]); |
| 10637 | return rss.str(); |
| 10638 | } |
| 10639 | } |
| 10640 | |
| 10641 | template<typename T> |
| 10642 | std::string fpToString( T value, int precision ) { |
| 10643 | if (std::isnan(value)) { |
| 10644 | return "nan"; |
| 10645 | } |
| 10646 | |
| 10647 | ReusableStringStream rss; |
| 10648 | rss << std::setprecision( precision ) |
| 10649 | << std::fixed |
| 10650 | << value; |
| 10651 | std::string d = rss.str(); |
| 10652 | std::size_t i = d.find_last_not_of( '0' ); |
| 10653 | if( i != std::string::npos && i != d.size()-1 ) { |
| 10654 | if( d[i] == '.' ) |
| 10655 | i++; |
| 10656 | d = d.substr( 0, i+1 ); |
| 10657 | } |
| 10658 | return d; |
| 10659 | } |
| 10660 | |
| 10661 | //// ======================================================= //// |
| 10662 | // |
| 10663 | // Out-of-line defs for full specialization of StringMaker |
| 10664 | // |
| 10665 | //// ======================================================= //// |
| 10666 | |
| 10667 | std::string StringMaker<std::string>::convert(const std::string& str) { |
| 10668 | if (!getCurrentContext().getConfig()->showInvisibles()) { |
| 10669 | return '"' + str + '"'; |
| 10670 | } |
| 10671 | |
| 10672 | std::string s("\""); |
| 10673 | for (char c : str) { |
| 10674 | switch (c) { |
| 10675 | case '\n': |
| 10676 | s.append("\\n"); |
| 10677 | break; |
| 10678 | case '\t': |
| 10679 | s.append("\\t"); |
| 10680 | break; |
| 10681 | default: |
| 10682 | s.push_back(c); |
| 10683 | break; |
| 10684 | } |
| 10685 | } |
| 10686 | s.append("\""); |
| 10687 | return s; |
| 10688 | } |
| 10689 | |
| 10690 | #ifdef CATCH_CONFIG_WCHAR |
| 10691 | std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) { |
| 10692 | std::string s; |
| 10693 | s.reserve(wstr.size()); |
| 10694 | for (auto c : wstr) { |
| 10695 | s += (c <= 0xff) ? static_cast<char>(c) : '?'; |
| 10696 | } |
| 10697 | return ::Catch::Detail::stringify(s); |
| 10698 | } |
| 10699 | #endif |
| 10700 | |
| 10701 | std::string StringMaker<char const*>::convert(char const* str) { |
| 10702 | if (str) { |
| 10703 | return ::Catch::Detail::stringify(std::string{ str }); |
| 10704 | } else { |
| 10705 | return{ "{null string}" }; |
| 10706 | } |
| 10707 | } |
| 10708 | std::string StringMaker<char*>::convert(char* str) { |
| 10709 | if (str) { |
| 10710 | return ::Catch::Detail::stringify(std::string{ str }); |
| 10711 | } else { |
| 10712 | return{ "{null string}" }; |
| 10713 | } |
| 10714 | } |
| 10715 | #ifdef CATCH_CONFIG_WCHAR |
| 10716 | std::string StringMaker<wchar_t const*>::convert(wchar_t const * str) { |
| 10717 | if (str) { |
| 10718 | return ::Catch::Detail::stringify(std::wstring{ str }); |
| 10719 | } else { |
| 10720 | return{ "{null string}" }; |
| 10721 | } |
| 10722 | } |
| 10723 | std::string StringMaker<wchar_t *>::convert(wchar_t * str) { |
| 10724 | if (str) { |
| 10725 | return ::Catch::Detail::stringify(std::wstring{ str }); |
| 10726 | } else { |
| 10727 | return{ "{null string}" }; |
| 10728 | } |
| 10729 | } |
| 10730 | #endif |
| 10731 | |
| 10732 | std::string StringMaker<int>::convert(int value) { |
| 10733 | return ::Catch::Detail::stringify(static_cast<long long>(value)); |
| 10734 | } |
| 10735 | std::string StringMaker<long>::convert(long value) { |
| 10736 | return ::Catch::Detail::stringify(static_cast<long long>(value)); |
| 10737 | } |
| 10738 | std::string StringMaker<long long>::convert(long long value) { |
| 10739 | ReusableStringStream rss; |
| 10740 | rss << value; |
| 10741 | if (value > Detail::hexThreshold) { |
| 10742 | rss << " (0x" << std::hex << value << ')'; |
| 10743 | } |
| 10744 | return rss.str(); |
| 10745 | } |
| 10746 | |
| 10747 | std::string StringMaker<unsigned int>::convert(unsigned int value) { |
| 10748 | return ::Catch::Detail::stringify(static_cast<unsigned long long>(value)); |
| 10749 | } |
| 10750 | std::string StringMaker<unsigned long>::convert(unsigned long value) { |
| 10751 | return ::Catch::Detail::stringify(static_cast<unsigned long long>(value)); |
| 10752 | } |
| 10753 | std::string StringMaker<unsigned long long>::convert(unsigned long long value) { |
| 10754 | ReusableStringStream rss; |
| 10755 | rss << value; |
| 10756 | if (value > Detail::hexThreshold) { |
| 10757 | rss << " (0x" << std::hex << value << ')'; |
| 10758 | } |
| 10759 | return rss.str(); |
| 10760 | } |
| 10761 | |
| 10762 | std::string StringMaker<bool>::convert(bool b) { |
| 10763 | return b ? "true" : "false"; |
| 10764 | } |
| 10765 | |
| 10766 | std::string StringMaker<char>::convert(char value) { |
| 10767 | if (value == '\r') { |
| 10768 | return "'\\r'"; |
| 10769 | } else if (value == '\f') { |
| 10770 | return "'\\f'"; |
| 10771 | } else if (value == '\n') { |
| 10772 | return "'\\n'"; |
| 10773 | } else if (value == '\t') { |
| 10774 | return "'\\t'"; |
| 10775 | } else if ('\0' <= value && value < ' ') { |
| 10776 | return ::Catch::Detail::stringify(static_cast<unsigned int>(value)); |
| 10777 | } else { |
| 10778 | char chstr[] = "' '"; |
| 10779 | chstr[1] = value; |
| 10780 | return chstr; |
| 10781 | } |
| 10782 | } |
| 10783 | std::string StringMaker<signed char>::convert(signed char c) { |
| 10784 | return ::Catch::Detail::stringify(static_cast<char>(c)); |
| 10785 | } |
| 10786 | std::string StringMaker<unsigned char>::convert(unsigned char c) { |
| 10787 | return ::Catch::Detail::stringify(static_cast<char>(c)); |
| 10788 | } |
| 10789 | |
| 10790 | std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) { |
| 10791 | return "nullptr"; |
| 10792 | } |
| 10793 | |
| 10794 | std::string StringMaker<float>::convert(float value) { |
| 10795 | return fpToString(value, 5) + 'f'; |
| 10796 | } |
| 10797 | std::string StringMaker<double>::convert(double value) { |
| 10798 | return fpToString(value, 10); |
| 10799 | } |
| 10800 | |
| 10801 | std::string ratio_string<std::atto>::symbol() { return "a"; } |
| 10802 | std::string ratio_string<std::femto>::symbol() { return "f"; } |
| 10803 | std::string ratio_string<std::pico>::symbol() { return "p"; } |
| 10804 | std::string ratio_string<std::nano>::symbol() { return "n"; } |
| 10805 | std::string ratio_string<std::micro>::symbol() { return "u"; } |
| 10806 | std::string ratio_string<std::milli>::symbol() { return "m"; } |
| 10807 | |
| 10808 | } // end namespace Catch |
| 10809 | |
| 10810 | #if defined(__clang__) |
| 10811 | # pragma clang diagnostic pop |
| 10812 | #endif |
| 10813 | |
| 10814 | // end catch_tostring.cpp |
| 10815 | // start catch_totals.cpp |
| 10816 | |
| 10817 | namespace Catch { |
| 10818 | |
| 10819 | Counts Counts::operator - ( Counts const& other ) const { |
| 10820 | Counts diff; |
| 10821 | diff.passed = passed - other.passed; |
| 10822 | diff.failed = failed - other.failed; |
| 10823 | diff.failedButOk = failedButOk - other.failedButOk; |
| 10824 | return diff; |
| 10825 | } |
| 10826 | |
| 10827 | Counts& Counts::operator += ( Counts const& other ) { |
| 10828 | passed += other.passed; |
| 10829 | failed += other.failed; |
| 10830 | failedButOk += other.failedButOk; |
| 10831 | return *this; |
| 10832 | } |
| 10833 | |
| 10834 | std::size_t Counts::total() const { |
| 10835 | return passed + failed + failedButOk; |
| 10836 | } |
| 10837 | bool Counts::allPassed() const { |
| 10838 | return failed == 0 && failedButOk == 0; |
| 10839 | } |
| 10840 | bool Counts::allOk() const { |
| 10841 | return failed == 0; |
| 10842 | } |
| 10843 | |
| 10844 | Totals Totals::operator - ( Totals const& other ) const { |
| 10845 | Totals diff; |
| 10846 | diff.assertions = assertions - other.assertions; |
| 10847 | diff.testCases = testCases - other.testCases; |
| 10848 | return diff; |
| 10849 | } |
| 10850 | |
| 10851 | Totals& Totals::operator += ( Totals const& other ) { |
| 10852 | assertions += other.assertions; |
| 10853 | testCases += other.testCases; |
| 10854 | return *this; |
| 10855 | } |
| 10856 | |
| 10857 | Totals Totals::delta( Totals const& prevTotals ) const { |
| 10858 | Totals diff = *this - prevTotals; |
| 10859 | if( diff.assertions.failed > 0 ) |
| 10860 | ++diff.testCases.failed; |
| 10861 | else if( diff.assertions.failedButOk > 0 ) |
| 10862 | ++diff.testCases.failedButOk; |
| 10863 | else |
| 10864 | ++diff.testCases.passed; |
| 10865 | return diff; |
| 10866 | } |
| 10867 | |
| 10868 | } |
| 10869 | // end catch_totals.cpp |
| 10870 | // start catch_uncaught_exceptions.cpp |
| 10871 | |
| 10872 | #include <exception> |
| 10873 | |
| 10874 | namespace Catch { |
| 10875 | bool uncaught_exceptions() { |
| 10876 | #if defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) |
| 10877 | return std::uncaught_exceptions() > 0; |
| 10878 | #else |
| 10879 | return std::uncaught_exception(); |
| 10880 | #endif |
| 10881 | } |
| 10882 | } // end namespace Catch |
| 10883 | // end catch_uncaught_exceptions.cpp |
| 10884 | // start catch_version.cpp |
| 10885 | |
| 10886 | #include <ostream> |
| 10887 | |
| 10888 | namespace Catch { |
| 10889 | |
| 10890 | Version::Version |
| 10891 | ( unsigned int _majorVersion, |
| 10892 | unsigned int _minorVersion, |
| 10893 | unsigned int _patchNumber, |
| 10894 | char const * const _branchName, |
| 10895 | unsigned int _buildNumber ) |
| 10896 | : majorVersion( _majorVersion ), |
| 10897 | minorVersion( _minorVersion ), |
| 10898 | patchNumber( _patchNumber ), |
| 10899 | branchName( _branchName ), |
| 10900 | buildNumber( _buildNumber ) |
| 10901 | {} |
| 10902 | |
| 10903 | std::ostream& operator << ( std::ostream& os, Version const& version ) { |
| 10904 | os << version.majorVersion << '.' |
| 10905 | << version.minorVersion << '.' |
| 10906 | << version.patchNumber; |
| 10907 | // branchName is never null -> 0th char is \0 if it is empty |
| 10908 | if (version.branchName[0]) { |
| 10909 | os << '-' << version.branchName |
| 10910 | << '.' << version.buildNumber; |
| 10911 | } |
| 10912 | return os; |
| 10913 | } |
| 10914 | |
| 10915 | Version const& libraryVersion() { |
| 10916 | static Version version( 2, 2, 2, "", 0 ); |
| 10917 | return version; |
| 10918 | } |
| 10919 | |
| 10920 | } |
| 10921 | // end catch_version.cpp |
| 10922 | // start catch_wildcard_pattern.cpp |
| 10923 | |
| 10924 | #include <sstream> |
| 10925 | |
| 10926 | namespace Catch { |
| 10927 | |
| 10928 | WildcardPattern::WildcardPattern( std::string const& pattern, |
| 10929 | CaseSensitive::Choice caseSensitivity ) |
| 10930 | : m_caseSensitivity( caseSensitivity ), |
| 10931 | m_pattern( adjustCase( pattern ) ) |
| 10932 | { |
| 10933 | if( startsWith( m_pattern, '*' ) ) { |
| 10934 | m_pattern = m_pattern.substr( 1 ); |
| 10935 | m_wildcard = WildcardAtStart; |
| 10936 | } |
| 10937 | if( endsWith( m_pattern, '*' ) ) { |
| 10938 | m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); |
| 10939 | m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); |
| 10940 | } |
| 10941 | } |
| 10942 | |
| 10943 | bool WildcardPattern::matches( std::string const& str ) const { |
| 10944 | switch( m_wildcard ) { |
| 10945 | case NoWildcard: |
| 10946 | return m_pattern == adjustCase( str ); |
| 10947 | case WildcardAtStart: |
| 10948 | return endsWith( adjustCase( str ), m_pattern ); |
| 10949 | case WildcardAtEnd: |
| 10950 | return startsWith( adjustCase( str ), m_pattern ); |
| 10951 | case WildcardAtBothEnds: |
| 10952 | return contains( adjustCase( str ), m_pattern ); |
| 10953 | default: |
| 10954 | CATCH_INTERNAL_ERROR( "Unknown enum" ); |
| 10955 | } |
| 10956 | } |
| 10957 | |
| 10958 | std::string WildcardPattern::adjustCase( std::string const& str ) const { |
| 10959 | return m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str; |
| 10960 | } |
| 10961 | } |
| 10962 | // end catch_wildcard_pattern.cpp |
| 10963 | // start catch_xmlwriter.cpp |
| 10964 | |
| 10965 | #include <iomanip> |
| 10966 | |
| 10967 | using uchar = unsigned char; |
| 10968 | |
| 10969 | namespace Catch { |
| 10970 | |
| 10971 | namespace { |
| 10972 | |
| 10973 | size_t trailingBytes(unsigned char c) { |
| 10974 | if ((c & 0xE0) == 0xC0) { |
| 10975 | return 2; |
| 10976 | } |
| 10977 | if ((c & 0xF0) == 0xE0) { |
| 10978 | return 3; |
| 10979 | } |
| 10980 | if ((c & 0xF8) == 0xF0) { |
| 10981 | return 4; |
| 10982 | } |
| 10983 | CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered"); |
| 10984 | } |
| 10985 | |
| 10986 | uint32_t headerValue(unsigned char c) { |
| 10987 | if ((c & 0xE0) == 0xC0) { |
| 10988 | return c & 0x1F; |
| 10989 | } |
| 10990 | if ((c & 0xF0) == 0xE0) { |
| 10991 | return c & 0x0F; |
| 10992 | } |
| 10993 | if ((c & 0xF8) == 0xF0) { |
| 10994 | return c & 0x07; |
| 10995 | } |
| 10996 | CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered"); |
| 10997 | } |
| 10998 | |
| 10999 | void hexEscapeChar(std::ostream& os, unsigned char c) { |
| 11000 | os << "\\x" |
| 11001 | << std::uppercase << std::hex << std::setfill('0') << std::setw(2) |
| 11002 | << static_cast<int>(c); |
| 11003 | } |
| 11004 | |
| 11005 | } // anonymous namespace |
| 11006 | |
| 11007 | XmlEncode::XmlEncode( std::string const& str, ForWhat forWhat ) |
| 11008 | : m_str( str ), |
| 11009 | m_forWhat( forWhat ) |
| 11010 | {} |
| 11011 | |
| 11012 | void XmlEncode::encodeTo( std::ostream& os ) const { |
| 11013 | // Apostrophe escaping not necessary if we always use " to write attributes |
| 11014 | // (see: http://www.w3.org/TR/xml/#syntax) |
| 11015 | |
| 11016 | for( std::size_t idx = 0; idx < m_str.size(); ++ idx ) { |
| 11017 | uchar c = m_str[idx]; |
| 11018 | switch (c) { |
| 11019 | case '<': os << "<"; break; |
| 11020 | case '&': os << "&"; break; |
| 11021 | |
| 11022 | case '>': |
| 11023 | // See: http://www.w3.org/TR/xml/#syntax |
| 11024 | if (idx > 2 && m_str[idx - 1] == ']' && m_str[idx - 2] == ']') |
| 11025 | os << ">"; |
| 11026 | else |
| 11027 | os << c; |
| 11028 | break; |
| 11029 | |
| 11030 | case '\"': |
| 11031 | if (m_forWhat == ForAttributes) |
| 11032 | os << """; |
| 11033 | else |
| 11034 | os << c; |
| 11035 | break; |
| 11036 | |
| 11037 | default: |
| 11038 | // Check for control characters and invalid utf-8 |
| 11039 | |
| 11040 | // Escape control characters in standard ascii |
| 11041 | // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0 |
| 11042 | if (c < 0x09 || (c > 0x0D && c < 0x20) || c == 0x7F) { |
| 11043 | hexEscapeChar(os, c); |
| 11044 | break; |
| 11045 | } |
| 11046 | |
| 11047 | // Plain ASCII: Write it to stream |
| 11048 | if (c < 0x7F) { |
| 11049 | os << c; |
| 11050 | break; |
| 11051 | } |
| 11052 | |
| 11053 | // UTF-8 territory |
| 11054 | // Check if the encoding is valid and if it is not, hex escape bytes. |
| 11055 | // Important: We do not check the exact decoded values for validity, only the encoding format |
| 11056 | // First check that this bytes is a valid lead byte: |
| 11057 | // This means that it is not encoded as 1111 1XXX |
| 11058 | // Or as 10XX XXXX |
| 11059 | if (c < 0xC0 || |
| 11060 | c >= 0xF8) { |
| 11061 | hexEscapeChar(os, c); |
| 11062 | break; |
| 11063 | } |
| 11064 | |
| 11065 | auto encBytes = trailingBytes(c); |
| 11066 | // Are there enough bytes left to avoid accessing out-of-bounds memory? |
| 11067 | if (idx + encBytes - 1 >= m_str.size()) { |
| 11068 | hexEscapeChar(os, c); |
| 11069 | break; |
| 11070 | } |
| 11071 | // The header is valid, check data |
| 11072 | // The next encBytes bytes must together be a valid utf-8 |
| 11073 | // This means: bitpattern 10XX XXXX and the extracted value is sane (ish) |
| 11074 | bool valid = true; |
| 11075 | uint32_t value = headerValue(c); |
| 11076 | for (std::size_t n = 1; n < encBytes; ++n) { |
| 11077 | uchar nc = m_str[idx + n]; |
| 11078 | valid &= ((nc & 0xC0) == 0x80); |
| 11079 | value = (value << 6) | (nc & 0x3F); |
| 11080 | } |
| 11081 | |
| 11082 | if ( |
| 11083 | // Wrong bit pattern of following bytes |
| 11084 | (!valid) || |
| 11085 | // Overlong encodings |
| 11086 | (value < 0x80) || |
| 11087 | (0x80 <= value && value < 0x800 && encBytes > 2) || |
| 11088 | (0x800 < value && value < 0x10000 && encBytes > 3) || |
| 11089 | // Encoded value out of range |
| 11090 | (value >= 0x110000) |
| 11091 | ) { |
| 11092 | hexEscapeChar(os, c); |
| 11093 | break; |
| 11094 | } |
| 11095 | |
| 11096 | // If we got here, this is in fact a valid(ish) utf-8 sequence |
| 11097 | for (std::size_t n = 0; n < encBytes; ++n) { |
| 11098 | os << m_str[idx + n]; |
| 11099 | } |
| 11100 | idx += encBytes - 1; |
| 11101 | break; |
| 11102 | } |
| 11103 | } |
| 11104 | } |
| 11105 | |
| 11106 | std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ) { |
| 11107 | xmlEncode.encodeTo( os ); |
| 11108 | return os; |
| 11109 | } |
| 11110 | |
| 11111 | XmlWriter::ScopedElement::ScopedElement( XmlWriter* writer ) |
| 11112 | : m_writer( writer ) |
| 11113 | {} |
| 11114 | |
| 11115 | XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) noexcept |
| 11116 | : m_writer( other.m_writer ){ |
| 11117 | other.m_writer = nullptr; |
| 11118 | } |
| 11119 | XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) noexcept { |
| 11120 | if ( m_writer ) { |
| 11121 | m_writer->endElement(); |
| 11122 | } |
| 11123 | m_writer = other.m_writer; |
| 11124 | other.m_writer = nullptr; |
| 11125 | return *this; |
| 11126 | } |
| 11127 | |
| 11128 | XmlWriter::ScopedElement::~ScopedElement() { |
| 11129 | if( m_writer ) |
| 11130 | m_writer->endElement(); |
| 11131 | } |
| 11132 | |
| 11133 | XmlWriter::ScopedElement& XmlWriter::ScopedElement::writeText( std::string const& text, bool indent ) { |
| 11134 | m_writer->writeText( text, indent ); |
| 11135 | return *this; |
| 11136 | } |
| 11137 | |
| 11138 | XmlWriter::XmlWriter( std::ostream& os ) : m_os( os ) |
| 11139 | { |
| 11140 | writeDeclaration(); |
| 11141 | } |
| 11142 | |
| 11143 | XmlWriter::~XmlWriter() { |
| 11144 | while( !m_tags.empty() ) |
| 11145 | endElement(); |
| 11146 | } |
| 11147 | |
| 11148 | XmlWriter& XmlWriter::startElement( std::string const& name ) { |
| 11149 | ensureTagClosed(); |
| 11150 | newlineIfNecessary(); |
| 11151 | m_os << m_indent << '<' << name; |
| 11152 | m_tags.push_back( name ); |
| 11153 | m_indent += " "; |
| 11154 | m_tagIsOpen = true; |
| 11155 | return *this; |
| 11156 | } |
| 11157 | |
| 11158 | XmlWriter::ScopedElement XmlWriter::scopedElement( std::string const& name ) { |
| 11159 | ScopedElement scoped( this ); |
| 11160 | startElement( name ); |
| 11161 | return scoped; |
| 11162 | } |
| 11163 | |
| 11164 | XmlWriter& XmlWriter::endElement() { |
| 11165 | newlineIfNecessary(); |
| 11166 | m_indent = m_indent.substr( 0, m_indent.size()-2 ); |
| 11167 | if( m_tagIsOpen ) { |
| 11168 | m_os << "/>"; |
| 11169 | m_tagIsOpen = false; |
| 11170 | } |
| 11171 | else { |
| 11172 | m_os << m_indent << "</" << m_tags.back() << ">"; |
| 11173 | } |
| 11174 | m_os << std::endl; |
| 11175 | m_tags.pop_back(); |
| 11176 | return *this; |
| 11177 | } |
| 11178 | |
| 11179 | XmlWriter& XmlWriter::writeAttribute( std::string const& name, std::string const& attribute ) { |
| 11180 | if( !name.empty() && !attribute.empty() ) |
| 11181 | m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; |
| 11182 | return *this; |
| 11183 | } |
| 11184 | |
| 11185 | XmlWriter& XmlWriter::writeAttribute( std::string const& name, bool attribute ) { |
| 11186 | m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; |
| 11187 | return *this; |
| 11188 | } |
| 11189 | |
| 11190 | XmlWriter& XmlWriter::writeText( std::string const& text, bool indent ) { |
| 11191 | if( !text.empty() ){ |
| 11192 | bool tagWasOpen = m_tagIsOpen; |
| 11193 | ensureTagClosed(); |
| 11194 | if( tagWasOpen && indent ) |
| 11195 | m_os << m_indent; |
| 11196 | m_os << XmlEncode( text ); |
| 11197 | m_needsNewline = true; |
| 11198 | } |
| 11199 | return *this; |
| 11200 | } |
| 11201 | |
| 11202 | XmlWriter& XmlWriter::writeComment( std::string const& text ) { |
| 11203 | ensureTagClosed(); |
| 11204 | m_os << m_indent << "<!--" << text << "-->"; |
| 11205 | m_needsNewline = true; |
| 11206 | return *this; |
| 11207 | } |
| 11208 | |
| 11209 | void XmlWriter::writeStylesheetRef( std::string const& url ) { |
| 11210 | m_os << "<?xml-stylesheet type=\"text/xsl\" href=\"" << url << "\"?>\n"; |
| 11211 | } |
| 11212 | |
| 11213 | XmlWriter& XmlWriter::writeBlankLine() { |
| 11214 | ensureTagClosed(); |
| 11215 | m_os << '\n'; |
| 11216 | return *this; |
| 11217 | } |
| 11218 | |
| 11219 | void XmlWriter::ensureTagClosed() { |
| 11220 | if( m_tagIsOpen ) { |
| 11221 | m_os << ">" << std::endl; |
| 11222 | m_tagIsOpen = false; |
| 11223 | } |
| 11224 | } |
| 11225 | |
| 11226 | void XmlWriter::writeDeclaration() { |
| 11227 | m_os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 11228 | } |
| 11229 | |
| 11230 | void XmlWriter::newlineIfNecessary() { |
| 11231 | if( m_needsNewline ) { |
| 11232 | m_os << std::endl; |
| 11233 | m_needsNewline = false; |
| 11234 | } |
| 11235 | } |
| 11236 | } |
| 11237 | // end catch_xmlwriter.cpp |
| 11238 | // start catch_reporter_bases.cpp |
| 11239 | |
| 11240 | #include <cstring> |
| 11241 | #include <cfloat> |
| 11242 | #include <cstdio> |
| 11243 | #include <assert.h> |
| 11244 | #include <memory> |
| 11245 | |
| 11246 | namespace Catch { |
| 11247 | void prepareExpandedExpression(AssertionResult& result) { |
| 11248 | result.getExpandedExpression(); |
| 11249 | } |
| 11250 | |
| 11251 | // Because formatting using c++ streams is stateful, drop down to C is required |
| 11252 | // Alternatively we could use stringstream, but its performance is... not good. |
| 11253 | std::string getFormattedDuration( double duration ) { |
| 11254 | // Max exponent + 1 is required to represent the whole part |
| 11255 | // + 1 for decimal point |
| 11256 | // + 3 for the 3 decimal places |
| 11257 | // + 1 for null terminator |
| 11258 | const std::size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1; |
| 11259 | char buffer[maxDoubleSize]; |
| 11260 | |
| 11261 | // Save previous errno, to prevent sprintf from overwriting it |
| 11262 | ErrnoGuard guard; |
| 11263 | #ifdef _MSC_VER |
| 11264 | sprintf_s(buffer, "%.3f", duration); |
| 11265 | #else |
| 11266 | sprintf(buffer, "%.3f", duration); |
| 11267 | #endif |
| 11268 | return std::string(buffer); |
| 11269 | } |
| 11270 | |
| 11271 | TestEventListenerBase::TestEventListenerBase(ReporterConfig const & _config) |
| 11272 | :StreamingReporterBase(_config) {} |
| 11273 | |
| 11274 | void TestEventListenerBase::assertionStarting(AssertionInfo const &) {} |
| 11275 | |
| 11276 | bool TestEventListenerBase::assertionEnded(AssertionStats const &) { |
| 11277 | return false; |
| 11278 | } |
| 11279 | |
| 11280 | } // end namespace Catch |
| 11281 | // end catch_reporter_bases.cpp |
| 11282 | // start catch_reporter_compact.cpp |
| 11283 | |
| 11284 | namespace { |
| 11285 | |
| 11286 | #ifdef CATCH_PLATFORM_MAC |
| 11287 | const char* failedString() { return "FAILED"; } |
| 11288 | const char* passedString() { return "PASSED"; } |
| 11289 | #else |
| 11290 | const char* failedString() { return "failed"; } |
| 11291 | const char* passedString() { return "passed"; } |
| 11292 | #endif |
| 11293 | |
| 11294 | // Colour::LightGrey |
| 11295 | Catch::Colour::Code dimColour() { return Catch::Colour::FileName; } |
| 11296 | |
| 11297 | std::string bothOrAll( std::size_t count ) { |
| 11298 | return count == 1 ? std::string() : |
| 11299 | count == 2 ? "both " : "all " ; |
| 11300 | } |
| 11301 | |
| 11302 | } // anon namespace |
| 11303 | |
| 11304 | namespace Catch { |
| 11305 | namespace { |
| 11306 | // Colour, message variants: |
| 11307 | // - white: No tests ran. |
| 11308 | // - red: Failed [both/all] N test cases, failed [both/all] M assertions. |
| 11309 | // - white: Passed [both/all] N test cases (no assertions). |
| 11310 | // - red: Failed N tests cases, failed M assertions. |
| 11311 | // - green: Passed [both/all] N tests cases with M assertions. |
| 11312 | void printTotals(std::ostream& out, const Totals& totals) { |
| 11313 | if (totals.testCases.total() == 0) { |
| 11314 | out << "No tests ran."; |
| 11315 | } else if (totals.testCases.failed == totals.testCases.total()) { |
| 11316 | Colour colour(Colour::ResultError); |
| 11317 | const std::string qualify_assertions_failed = |
| 11318 | totals.assertions.failed == totals.assertions.total() ? |
| 11319 | bothOrAll(totals.assertions.failed) : std::string(); |
| 11320 | out << |
| 11321 | "Failed " << bothOrAll(totals.testCases.failed) |
| 11322 | << pluralise(totals.testCases.failed, "test case") << ", " |
| 11323 | "failed " << qualify_assertions_failed << |
| 11324 | pluralise(totals.assertions.failed, "assertion") << '.'; |
| 11325 | } else if (totals.assertions.total() == 0) { |
| 11326 | out << |
| 11327 | "Passed " << bothOrAll(totals.testCases.total()) |
| 11328 | << pluralise(totals.testCases.total(), "test case") |
| 11329 | << " (no assertions)."; |
| 11330 | } else if (totals.assertions.failed) { |
| 11331 | Colour colour(Colour::ResultError); |
| 11332 | out << |
| 11333 | "Failed " << pluralise(totals.testCases.failed, "test case") << ", " |
| 11334 | "failed " << pluralise(totals.assertions.failed, "assertion") << '.'; |
| 11335 | } else { |
| 11336 | Colour colour(Colour::ResultSuccess); |
| 11337 | out << |
| 11338 | "Passed " << bothOrAll(totals.testCases.passed) |
| 11339 | << pluralise(totals.testCases.passed, "test case") << |
| 11340 | " with " << pluralise(totals.assertions.passed, "assertion") << '.'; |
| 11341 | } |
| 11342 | } |
| 11343 | |
| 11344 | // Implementation of CompactReporter formatting |
| 11345 | class AssertionPrinter { |
| 11346 | public: |
| 11347 | AssertionPrinter& operator= (AssertionPrinter const&) = delete; |
| 11348 | AssertionPrinter(AssertionPrinter const&) = delete; |
| 11349 | AssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) |
| 11350 | : stream(_stream) |
| 11351 | , result(_stats.assertionResult) |
| 11352 | , messages(_stats.infoMessages) |
| 11353 | , itMessage(_stats.infoMessages.begin()) |
| 11354 | , printInfoMessages(_printInfoMessages) {} |
| 11355 | |
| 11356 | void print() { |
| 11357 | printSourceInfo(); |
| 11358 | |
| 11359 | itMessage = messages.begin(); |
| 11360 | |
| 11361 | switch (result.getResultType()) { |
| 11362 | case ResultWas::Ok: |
| 11363 | printResultType(Colour::ResultSuccess, passedString()); |
| 11364 | printOriginalExpression(); |
| 11365 | printReconstructedExpression(); |
| 11366 | if (!result.hasExpression()) |
| 11367 | printRemainingMessages(Colour::None); |
| 11368 | else |
| 11369 | printRemainingMessages(); |
| 11370 | break; |
| 11371 | case ResultWas::ExpressionFailed: |
| 11372 | if (result.isOk()) |
| 11373 | printResultType(Colour::ResultSuccess, failedString() + std::string(" - but was ok")); |
| 11374 | else |
| 11375 | printResultType(Colour::Error, failedString()); |
| 11376 | printOriginalExpression(); |
| 11377 | printReconstructedExpression(); |
| 11378 | printRemainingMessages(); |
| 11379 | break; |
| 11380 | case ResultWas::ThrewException: |
| 11381 | printResultType(Colour::Error, failedString()); |
| 11382 | printIssue("unexpected exception with message:"); |
| 11383 | printMessage(); |
| 11384 | printExpressionWas(); |
| 11385 | printRemainingMessages(); |
| 11386 | break; |
| 11387 | case ResultWas::FatalErrorCondition: |
| 11388 | printResultType(Colour::Error, failedString()); |
| 11389 | printIssue("fatal error condition with message:"); |
| 11390 | printMessage(); |
| 11391 | printExpressionWas(); |
| 11392 | printRemainingMessages(); |
| 11393 | break; |
| 11394 | case ResultWas::DidntThrowException: |
| 11395 | printResultType(Colour::Error, failedString()); |
| 11396 | printIssue("expected exception, got none"); |
| 11397 | printExpressionWas(); |
| 11398 | printRemainingMessages(); |
| 11399 | break; |
| 11400 | case ResultWas::Info: |
| 11401 | printResultType(Colour::None, "info"); |
| 11402 | printMessage(); |
| 11403 | printRemainingMessages(); |
| 11404 | break; |
| 11405 | case ResultWas::Warning: |
| 11406 | printResultType(Colour::None, "warning"); |
| 11407 | printMessage(); |
| 11408 | printRemainingMessages(); |
| 11409 | break; |
| 11410 | case ResultWas::ExplicitFailure: |
| 11411 | printResultType(Colour::Error, failedString()); |
| 11412 | printIssue("explicitly"); |
| 11413 | printRemainingMessages(Colour::None); |
| 11414 | break; |
| 11415 | // These cases are here to prevent compiler warnings |
| 11416 | case ResultWas::Unknown: |
| 11417 | case ResultWas::FailureBit: |
| 11418 | case ResultWas::Exception: |
| 11419 | printResultType(Colour::Error, "** internal error **"); |
| 11420 | break; |
| 11421 | } |
| 11422 | } |
| 11423 | |
| 11424 | private: |
| 11425 | void printSourceInfo() const { |
| 11426 | Colour colourGuard(Colour::FileName); |
| 11427 | stream << result.getSourceInfo() << ':'; |
| 11428 | } |
| 11429 | |
| 11430 | void printResultType(Colour::Code colour, std::string const& passOrFail) const { |
| 11431 | if (!passOrFail.empty()) { |
| 11432 | { |
| 11433 | Colour colourGuard(colour); |
| 11434 | stream << ' ' << passOrFail; |
| 11435 | } |
| 11436 | stream << ':'; |
| 11437 | } |
| 11438 | } |
| 11439 | |
| 11440 | void printIssue(std::string const& issue) const { |
| 11441 | stream << ' ' << issue; |
| 11442 | } |
| 11443 | |
| 11444 | void printExpressionWas() { |
| 11445 | if (result.hasExpression()) { |
| 11446 | stream << ';'; |
| 11447 | { |
| 11448 | Colour colour(dimColour()); |
| 11449 | stream << " expression was:"; |
| 11450 | } |
| 11451 | printOriginalExpression(); |
| 11452 | } |
| 11453 | } |
| 11454 | |
| 11455 | void printOriginalExpression() const { |
| 11456 | if (result.hasExpression()) { |
| 11457 | stream << ' ' << result.getExpression(); |
| 11458 | } |
| 11459 | } |
| 11460 | |
| 11461 | void printReconstructedExpression() const { |
| 11462 | if (result.hasExpandedExpression()) { |
| 11463 | { |
| 11464 | Colour colour(dimColour()); |
| 11465 | stream << " for: "; |
| 11466 | } |
| 11467 | stream << result.getExpandedExpression(); |
| 11468 | } |
| 11469 | } |
| 11470 | |
| 11471 | void printMessage() { |
| 11472 | if (itMessage != messages.end()) { |
| 11473 | stream << " '" << itMessage->message << '\''; |
| 11474 | ++itMessage; |
| 11475 | } |
| 11476 | } |
| 11477 | |
| 11478 | void printRemainingMessages(Colour::Code colour = dimColour()) { |
| 11479 | if (itMessage == messages.end()) |
| 11480 | return; |
| 11481 | |
| 11482 | // using messages.end() directly yields (or auto) compilation error: |
| 11483 | std::vector<MessageInfo>::const_iterator itEnd = messages.end(); |
| 11484 | const std::size_t N = static_cast<std::size_t>(std::distance(itMessage, itEnd)); |
| 11485 | |
| 11486 | { |
| 11487 | Colour colourGuard(colour); |
| 11488 | stream << " with " << pluralise(N, "message") << ':'; |
| 11489 | } |
| 11490 | |
| 11491 | for (; itMessage != itEnd; ) { |
| 11492 | // If this assertion is a warning ignore any INFO messages |
| 11493 | if (printInfoMessages || itMessage->type != ResultWas::Info) { |
| 11494 | stream << " '" << itMessage->message << '\''; |
| 11495 | if (++itMessage != itEnd) { |
| 11496 | Colour colourGuard(dimColour()); |
| 11497 | stream << " and"; |
| 11498 | } |
| 11499 | } |
| 11500 | } |
| 11501 | } |
| 11502 | |
| 11503 | private: |
| 11504 | std::ostream& stream; |
| 11505 | AssertionResult const& result; |
| 11506 | std::vector<MessageInfo> messages; |
| 11507 | std::vector<MessageInfo>::const_iterator itMessage; |
| 11508 | bool printInfoMessages; |
| 11509 | }; |
| 11510 | |
| 11511 | } // anon namespace |
| 11512 | |
| 11513 | std::string CompactReporter::getDescription() { |
| 11514 | return "Reports test results on a single line, suitable for IDEs"; |
| 11515 | } |
| 11516 | |
| 11517 | ReporterPreferences CompactReporter::getPreferences() const { |
| 11518 | ReporterPreferences prefs; |
| 11519 | prefs.shouldRedirectStdOut = false; |
| 11520 | return prefs; |
| 11521 | } |
| 11522 | |
| 11523 | void CompactReporter::noMatchingTestCases( std::string const& spec ) { |
| 11524 | stream << "No test cases matched '" << spec << '\'' << std::endl; |
| 11525 | } |
| 11526 | |
| 11527 | void CompactReporter::assertionStarting( AssertionInfo const& ) {} |
| 11528 | |
| 11529 | bool CompactReporter::assertionEnded( AssertionStats const& _assertionStats ) { |
| 11530 | AssertionResult const& result = _assertionStats.assertionResult; |
| 11531 | |
| 11532 | bool printInfoMessages = true; |
| 11533 | |
| 11534 | // Drop out if result was successful and we're not printing those |
| 11535 | if( !m_config->includeSuccessfulResults() && result.isOk() ) { |
| 11536 | if( result.getResultType() != ResultWas::Warning ) |
| 11537 | return false; |
| 11538 | printInfoMessages = false; |
| 11539 | } |
| 11540 | |
| 11541 | AssertionPrinter printer( stream, _assertionStats, printInfoMessages ); |
| 11542 | printer.print(); |
| 11543 | |
| 11544 | stream << std::endl; |
| 11545 | return true; |
| 11546 | } |
| 11547 | |
| 11548 | void CompactReporter::sectionEnded(SectionStats const& _sectionStats) { |
| 11549 | if (m_config->showDurations() == ShowDurations::Always) { |
| 11550 | stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl; |
| 11551 | } |
| 11552 | } |
| 11553 | |
| 11554 | void CompactReporter::testRunEnded( TestRunStats const& _testRunStats ) { |
| 11555 | printTotals( stream, _testRunStats.totals ); |
| 11556 | stream << '\n' << std::endl; |
| 11557 | StreamingReporterBase::testRunEnded( _testRunStats ); |
| 11558 | } |
| 11559 | |
| 11560 | CompactReporter::~CompactReporter() {} |
| 11561 | |
| 11562 | CATCH_REGISTER_REPORTER( "compact", CompactReporter ) |
| 11563 | |
| 11564 | } // end namespace Catch |
| 11565 | // end catch_reporter_compact.cpp |
| 11566 | // start catch_reporter_console.cpp |
| 11567 | |
| 11568 | #include <cfloat> |
| 11569 | #include <cstdio> |
| 11570 | |
| 11571 | #if defined(_MSC_VER) |
| 11572 | #pragma warning(push) |
| 11573 | #pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch |
| 11574 | // Note that 4062 (not all labels are handled |
| 11575 | // and default is missing) is enabled |
| 11576 | #endif |
| 11577 | |
| 11578 | namespace Catch { |
| 11579 | |
| 11580 | namespace { |
| 11581 | |
| 11582 | // Formatter impl for ConsoleReporter |
| 11583 | class ConsoleAssertionPrinter { |
| 11584 | public: |
| 11585 | ConsoleAssertionPrinter& operator= (ConsoleAssertionPrinter const&) = delete; |
| 11586 | ConsoleAssertionPrinter(ConsoleAssertionPrinter const&) = delete; |
| 11587 | ConsoleAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) |
| 11588 | : stream(_stream), |
| 11589 | stats(_stats), |
| 11590 | result(_stats.assertionResult), |
| 11591 | colour(Colour::None), |
| 11592 | message(result.getMessage()), |
| 11593 | messages(_stats.infoMessages), |
| 11594 | printInfoMessages(_printInfoMessages) { |
| 11595 | switch (result.getResultType()) { |
| 11596 | case ResultWas::Ok: |
| 11597 | colour = Colour::Success; |
| 11598 | passOrFail = "PASSED"; |
| 11599 | //if( result.hasMessage() ) |
| 11600 | if (_stats.infoMessages.size() == 1) |
| 11601 | messageLabel = "with message"; |
| 11602 | if (_stats.infoMessages.size() > 1) |
| 11603 | messageLabel = "with messages"; |
| 11604 | break; |
| 11605 | case ResultWas::ExpressionFailed: |
| 11606 | if (result.isOk()) { |
| 11607 | colour = Colour::Success; |
| 11608 | passOrFail = "FAILED - but was ok"; |
| 11609 | } else { |
| 11610 | colour = Colour::Error; |
| 11611 | passOrFail = "FAILED"; |
| 11612 | } |
| 11613 | if (_stats.infoMessages.size() == 1) |
| 11614 | messageLabel = "with message"; |
| 11615 | if (_stats.infoMessages.size() > 1) |
| 11616 | messageLabel = "with messages"; |
| 11617 | break; |
| 11618 | case ResultWas::ThrewException: |
| 11619 | colour = Colour::Error; |
| 11620 | passOrFail = "FAILED"; |
| 11621 | messageLabel = "due to unexpected exception with "; |
| 11622 | if (_stats.infoMessages.size() == 1) |
| 11623 | messageLabel += "message"; |
| 11624 | if (_stats.infoMessages.size() > 1) |
| 11625 | messageLabel += "messages"; |
| 11626 | break; |
| 11627 | case ResultWas::FatalErrorCondition: |
| 11628 | colour = Colour::Error; |
| 11629 | passOrFail = "FAILED"; |
| 11630 | messageLabel = "due to a fatal error condition"; |
| 11631 | break; |
| 11632 | case ResultWas::DidntThrowException: |
| 11633 | colour = Colour::Error; |
| 11634 | passOrFail = "FAILED"; |
| 11635 | messageLabel = "because no exception was thrown where one was expected"; |
| 11636 | break; |
| 11637 | case ResultWas::Info: |
| 11638 | messageLabel = "info"; |
| 11639 | break; |
| 11640 | case ResultWas::Warning: |
| 11641 | messageLabel = "warning"; |
| 11642 | break; |
| 11643 | case ResultWas::ExplicitFailure: |
| 11644 | passOrFail = "FAILED"; |
| 11645 | colour = Colour::Error; |
| 11646 | if (_stats.infoMessages.size() == 1) |
| 11647 | messageLabel = "explicitly with message"; |
| 11648 | if (_stats.infoMessages.size() > 1) |
| 11649 | messageLabel = "explicitly with messages"; |
| 11650 | break; |
| 11651 | // These cases are here to prevent compiler warnings |
| 11652 | case ResultWas::Unknown: |
| 11653 | case ResultWas::FailureBit: |
| 11654 | case ResultWas::Exception: |
| 11655 | passOrFail = "** internal error **"; |
| 11656 | colour = Colour::Error; |
| 11657 | break; |
| 11658 | } |
| 11659 | } |
| 11660 | |
| 11661 | void print() const { |
| 11662 | printSourceInfo(); |
| 11663 | if (stats.totals.assertions.total() > 0) { |
| 11664 | if (result.isOk()) |
| 11665 | stream << '\n'; |
| 11666 | printResultType(); |
| 11667 | printOriginalExpression(); |
| 11668 | printReconstructedExpression(); |
| 11669 | } else { |
| 11670 | stream << '\n'; |
| 11671 | } |
| 11672 | printMessage(); |
| 11673 | } |
| 11674 | |
| 11675 | private: |
| 11676 | void printResultType() const { |
| 11677 | if (!passOrFail.empty()) { |
| 11678 | Colour colourGuard(colour); |
| 11679 | stream << passOrFail << ":\n"; |
| 11680 | } |
| 11681 | } |
| 11682 | void printOriginalExpression() const { |
| 11683 | if (result.hasExpression()) { |
| 11684 | Colour colourGuard(Colour::OriginalExpression); |
| 11685 | stream << " "; |
| 11686 | stream << result.getExpressionInMacro(); |
| 11687 | stream << '\n'; |
| 11688 | } |
| 11689 | } |
| 11690 | void printReconstructedExpression() const { |
| 11691 | if (result.hasExpandedExpression()) { |
| 11692 | stream << "with expansion:\n"; |
| 11693 | Colour colourGuard(Colour::ReconstructedExpression); |
| 11694 | stream << Column(result.getExpandedExpression()).indent(2) << '\n'; |
| 11695 | } |
| 11696 | } |
| 11697 | void printMessage() const { |
| 11698 | if (!messageLabel.empty()) |
| 11699 | stream << messageLabel << ':' << '\n'; |
| 11700 | for (auto const& msg : messages) { |
| 11701 | // If this assertion is a warning ignore any INFO messages |
| 11702 | if (printInfoMessages || msg.type != ResultWas::Info) |
| 11703 | stream << Column(msg.message).indent(2) << '\n'; |
| 11704 | } |
| 11705 | } |
| 11706 | void printSourceInfo() const { |
| 11707 | Colour colourGuard(Colour::FileName); |
| 11708 | stream << result.getSourceInfo() << ": "; |
| 11709 | } |
| 11710 | |
| 11711 | std::ostream& stream; |
| 11712 | AssertionStats const& stats; |
| 11713 | AssertionResult const& result; |
| 11714 | Colour::Code colour; |
| 11715 | std::string passOrFail; |
| 11716 | std::string messageLabel; |
| 11717 | std::string message; |
| 11718 | std::vector<MessageInfo> messages; |
| 11719 | bool printInfoMessages; |
| 11720 | }; |
| 11721 | |
| 11722 | std::size_t makeRatio(std::size_t number, std::size_t total) { |
| 11723 | std::size_t ratio = total > 0 ? CATCH_CONFIG_CONSOLE_WIDTH * number / total : 0; |
| 11724 | return (ratio == 0 && number > 0) ? 1 : ratio; |
| 11725 | } |
| 11726 | |
| 11727 | std::size_t& findMax(std::size_t& i, std::size_t& j, std::size_t& k) { |
| 11728 | if (i > j && i > k) |
| 11729 | return i; |
| 11730 | else if (j > k) |
| 11731 | return j; |
| 11732 | else |
| 11733 | return k; |
| 11734 | } |
| 11735 | |
| 11736 | struct ColumnInfo { |
| 11737 | enum Justification { Left, Right }; |
| 11738 | std::string name; |
| 11739 | int width; |
| 11740 | Justification justification; |
| 11741 | }; |
| 11742 | struct ColumnBreak {}; |
| 11743 | struct RowBreak {}; |
| 11744 | |
| 11745 | class Duration { |
| 11746 | enum class Unit { |
| 11747 | Auto, |
| 11748 | Nanoseconds, |
| 11749 | Microseconds, |
| 11750 | Milliseconds, |
| 11751 | Seconds, |
| 11752 | Minutes |
| 11753 | }; |
| 11754 | static const uint64_t s_nanosecondsInAMicrosecond = 1000; |
| 11755 | static const uint64_t s_nanosecondsInAMillisecond = 1000 * s_nanosecondsInAMicrosecond; |
| 11756 | static const uint64_t s_nanosecondsInASecond = 1000 * s_nanosecondsInAMillisecond; |
| 11757 | static const uint64_t s_nanosecondsInAMinute = 60 * s_nanosecondsInASecond; |
| 11758 | |
| 11759 | uint64_t m_inNanoseconds; |
| 11760 | Unit m_units; |
| 11761 | |
| 11762 | public: |
| 11763 | explicit Duration(uint64_t inNanoseconds, Unit units = Unit::Auto) |
| 11764 | : m_inNanoseconds(inNanoseconds), |
| 11765 | m_units(units) { |
| 11766 | if (m_units == Unit::Auto) { |
| 11767 | if (m_inNanoseconds < s_nanosecondsInAMicrosecond) |
| 11768 | m_units = Unit::Nanoseconds; |
| 11769 | else if (m_inNanoseconds < s_nanosecondsInAMillisecond) |
| 11770 | m_units = Unit::Microseconds; |
| 11771 | else if (m_inNanoseconds < s_nanosecondsInASecond) |
| 11772 | m_units = Unit::Milliseconds; |
| 11773 | else if (m_inNanoseconds < s_nanosecondsInAMinute) |
| 11774 | m_units = Unit::Seconds; |
| 11775 | else |
| 11776 | m_units = Unit::Minutes; |
| 11777 | } |
| 11778 | |
| 11779 | } |
| 11780 | |
| 11781 | auto value() const -> double { |
| 11782 | switch (m_units) { |
| 11783 | case Unit::Microseconds: |
| 11784 | return m_inNanoseconds / static_cast<double>(s_nanosecondsInAMicrosecond); |
| 11785 | case Unit::Milliseconds: |
| 11786 | return m_inNanoseconds / static_cast<double>(s_nanosecondsInAMillisecond); |
| 11787 | case Unit::Seconds: |
| 11788 | return m_inNanoseconds / static_cast<double>(s_nanosecondsInASecond); |
| 11789 | case Unit::Minutes: |
| 11790 | return m_inNanoseconds / static_cast<double>(s_nanosecondsInAMinute); |
| 11791 | default: |
| 11792 | return static_cast<double>(m_inNanoseconds); |
| 11793 | } |
| 11794 | } |
| 11795 | auto unitsAsString() const -> std::string { |
| 11796 | switch (m_units) { |
| 11797 | case Unit::Nanoseconds: |
| 11798 | return "ns"; |
| 11799 | case Unit::Microseconds: |
| 11800 | return "µs"; |
| 11801 | case Unit::Milliseconds: |
| 11802 | return "ms"; |
| 11803 | case Unit::Seconds: |
| 11804 | return "s"; |
| 11805 | case Unit::Minutes: |
| 11806 | return "m"; |
| 11807 | default: |
| 11808 | return "** internal error **"; |
| 11809 | } |
| 11810 | |
| 11811 | } |
| 11812 | friend auto operator << (std::ostream& os, Duration const& duration) -> std::ostream& { |
| 11813 | return os << duration.value() << " " << duration.unitsAsString(); |
| 11814 | } |
| 11815 | }; |
| 11816 | } // end anon namespace |
| 11817 | |
| 11818 | class TablePrinter { |
| 11819 | std::ostream& m_os; |
| 11820 | std::vector<ColumnInfo> m_columnInfos; |
| 11821 | std::ostringstream m_oss; |
| 11822 | int m_currentColumn = -1; |
| 11823 | bool m_isOpen = false; |
| 11824 | |
| 11825 | public: |
| 11826 | TablePrinter( std::ostream& os, std::vector<ColumnInfo> columnInfos ) |
| 11827 | : m_os( os ), |
| 11828 | m_columnInfos( std::move( columnInfos ) ) {} |
| 11829 | |
| 11830 | auto columnInfos() const -> std::vector<ColumnInfo> const& { |
| 11831 | return m_columnInfos; |
| 11832 | } |
| 11833 | |
| 11834 | void open() { |
| 11835 | if (!m_isOpen) { |
| 11836 | m_isOpen = true; |
| 11837 | *this << RowBreak(); |
| 11838 | for (auto const& info : m_columnInfos) |
| 11839 | *this << info.name << ColumnBreak(); |
| 11840 | *this << RowBreak(); |
| 11841 | m_os << Catch::getLineOfChars<'-'>() << "\n"; |
| 11842 | } |
| 11843 | } |
| 11844 | void close() { |
| 11845 | if (m_isOpen) { |
| 11846 | *this << RowBreak(); |
| 11847 | m_os << std::endl; |
| 11848 | m_isOpen = false; |
| 11849 | } |
| 11850 | } |
| 11851 | |
| 11852 | template<typename T> |
| 11853 | friend TablePrinter& operator << (TablePrinter& tp, T const& value) { |
| 11854 | tp.m_oss << value; |
| 11855 | return tp; |
| 11856 | } |
| 11857 | |
| 11858 | friend TablePrinter& operator << (TablePrinter& tp, ColumnBreak) { |
| 11859 | auto colStr = tp.m_oss.str(); |
| 11860 | // This takes account of utf8 encodings |
| 11861 | auto strSize = Catch::StringRef(colStr).numberOfCharacters(); |
| 11862 | tp.m_oss.str(""); |
| 11863 | tp.open(); |
| 11864 | if (tp.m_currentColumn == static_cast<int>(tp.m_columnInfos.size() - 1)) { |
| 11865 | tp.m_currentColumn = -1; |
| 11866 | tp.m_os << "\n"; |
| 11867 | } |
| 11868 | tp.m_currentColumn++; |
| 11869 | |
| 11870 | auto colInfo = tp.m_columnInfos[tp.m_currentColumn]; |
| 11871 | auto padding = (strSize + 2 < static_cast<std::size_t>(colInfo.width)) |
| 11872 | ? std::string(colInfo.width - (strSize + 2), ' ') |
| 11873 | : std::string(); |
| 11874 | if (colInfo.justification == ColumnInfo::Left) |
| 11875 | tp.m_os << colStr << padding << " "; |
| 11876 | else |
| 11877 | tp.m_os << padding << colStr << " "; |
| 11878 | return tp; |
| 11879 | } |
| 11880 | |
| 11881 | friend TablePrinter& operator << (TablePrinter& tp, RowBreak) { |
| 11882 | if (tp.m_currentColumn > 0) { |
| 11883 | tp.m_os << "\n"; |
| 11884 | tp.m_currentColumn = -1; |
| 11885 | } |
| 11886 | return tp; |
| 11887 | } |
| 11888 | }; |
| 11889 | |
| 11890 | ConsoleReporter::ConsoleReporter(ReporterConfig const& config) |
| 11891 | : StreamingReporterBase(config), |
| 11892 | m_tablePrinter(new TablePrinter(config.stream(), |
| 11893 | { |
| 11894 | { "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 32, ColumnInfo::Left }, |
| 11895 | { "iters", 8, ColumnInfo::Right }, |
| 11896 | { "elapsed ns", 14, ColumnInfo::Right }, |
| 11897 | { "average", 14, ColumnInfo::Right } |
| 11898 | })) {} |
| 11899 | ConsoleReporter::~ConsoleReporter() = default; |
| 11900 | |
| 11901 | std::string ConsoleReporter::getDescription() { |
| 11902 | return "Reports test results as plain lines of text"; |
| 11903 | } |
| 11904 | |
| 11905 | void ConsoleReporter::noMatchingTestCases(std::string const& spec) { |
| 11906 | stream << "No test cases matched '" << spec << '\'' << std::endl; |
| 11907 | } |
| 11908 | |
| 11909 | void ConsoleReporter::assertionStarting(AssertionInfo const&) {} |
| 11910 | |
| 11911 | bool ConsoleReporter::assertionEnded(AssertionStats const& _assertionStats) { |
| 11912 | AssertionResult const& result = _assertionStats.assertionResult; |
| 11913 | |
| 11914 | bool includeResults = m_config->includeSuccessfulResults() || !result.isOk(); |
| 11915 | |
| 11916 | // Drop out if result was successful but we're not printing them. |
| 11917 | if (!includeResults && result.getResultType() != ResultWas::Warning) |
| 11918 | return false; |
| 11919 | |
| 11920 | lazyPrint(); |
| 11921 | |
| 11922 | ConsoleAssertionPrinter printer(stream, _assertionStats, includeResults); |
| 11923 | printer.print(); |
| 11924 | stream << std::endl; |
| 11925 | return true; |
| 11926 | } |
| 11927 | |
| 11928 | void ConsoleReporter::sectionStarting(SectionInfo const& _sectionInfo) { |
| 11929 | m_headerPrinted = false; |
| 11930 | StreamingReporterBase::sectionStarting(_sectionInfo); |
| 11931 | } |
| 11932 | void ConsoleReporter::sectionEnded(SectionStats const& _sectionStats) { |
| 11933 | m_tablePrinter->close(); |
| 11934 | if (_sectionStats.missingAssertions) { |
| 11935 | lazyPrint(); |
| 11936 | Colour colour(Colour::ResultError); |
| 11937 | if (m_sectionStack.size() > 1) |
| 11938 | stream << "\nNo assertions in section"; |
| 11939 | else |
| 11940 | stream << "\nNo assertions in test case"; |
| 11941 | stream << " '" << _sectionStats.sectionInfo.name << "'\n" << std::endl; |
| 11942 | } |
| 11943 | if (m_config->showDurations() == ShowDurations::Always) { |
| 11944 | stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl; |
| 11945 | } |
| 11946 | if (m_headerPrinted) { |
| 11947 | m_headerPrinted = false; |
| 11948 | } |
| 11949 | StreamingReporterBase::sectionEnded(_sectionStats); |
| 11950 | } |
| 11951 | |
| 11952 | void ConsoleReporter::benchmarkStarting(BenchmarkInfo const& info) { |
| 11953 | lazyPrintWithoutClosingBenchmarkTable(); |
| 11954 | |
| 11955 | auto nameCol = Column( info.name ).width( static_cast<std::size_t>( m_tablePrinter->columnInfos()[0].width - 2 ) ); |
| 11956 | |
| 11957 | bool firstLine = true; |
| 11958 | for (auto line : nameCol) { |
| 11959 | if (!firstLine) |
| 11960 | (*m_tablePrinter) << ColumnBreak() << ColumnBreak() << ColumnBreak(); |
| 11961 | else |
| 11962 | firstLine = false; |
| 11963 | |
| 11964 | (*m_tablePrinter) << line << ColumnBreak(); |
| 11965 | } |
| 11966 | } |
| 11967 | void ConsoleReporter::benchmarkEnded(BenchmarkStats const& stats) { |
| 11968 | Duration average(stats.elapsedTimeInNanoseconds / stats.iterations); |
| 11969 | (*m_tablePrinter) |
| 11970 | << stats.iterations << ColumnBreak() |
| 11971 | << stats.elapsedTimeInNanoseconds << ColumnBreak() |
| 11972 | << average << ColumnBreak(); |
| 11973 | } |
| 11974 | |
| 11975 | void ConsoleReporter::testCaseEnded(TestCaseStats const& _testCaseStats) { |
| 11976 | m_tablePrinter->close(); |
| 11977 | StreamingReporterBase::testCaseEnded(_testCaseStats); |
| 11978 | m_headerPrinted = false; |
| 11979 | } |
| 11980 | void ConsoleReporter::testGroupEnded(TestGroupStats const& _testGroupStats) { |
| 11981 | if (currentGroupInfo.used) { |
| 11982 | printSummaryDivider(); |
| 11983 | stream << "Summary for group '" << _testGroupStats.groupInfo.name << "':\n"; |
| 11984 | printTotals(_testGroupStats.totals); |
| 11985 | stream << '\n' << std::endl; |
| 11986 | } |
| 11987 | StreamingReporterBase::testGroupEnded(_testGroupStats); |
| 11988 | } |
| 11989 | void ConsoleReporter::testRunEnded(TestRunStats const& _testRunStats) { |
| 11990 | printTotalsDivider(_testRunStats.totals); |
| 11991 | printTotals(_testRunStats.totals); |
| 11992 | stream << std::endl; |
| 11993 | StreamingReporterBase::testRunEnded(_testRunStats); |
| 11994 | } |
| 11995 | |
| 11996 | void ConsoleReporter::lazyPrint() { |
| 11997 | |
| 11998 | m_tablePrinter->close(); |
| 11999 | lazyPrintWithoutClosingBenchmarkTable(); |
| 12000 | } |
| 12001 | |
| 12002 | void ConsoleReporter::lazyPrintWithoutClosingBenchmarkTable() { |
| 12003 | |
| 12004 | if (!currentTestRunInfo.used) |
| 12005 | lazyPrintRunInfo(); |
| 12006 | if (!currentGroupInfo.used) |
| 12007 | lazyPrintGroupInfo(); |
| 12008 | |
| 12009 | if (!m_headerPrinted) { |
| 12010 | printTestCaseAndSectionHeader(); |
| 12011 | m_headerPrinted = true; |
| 12012 | } |
| 12013 | } |
| 12014 | void ConsoleReporter::lazyPrintRunInfo() { |
| 12015 | stream << '\n' << getLineOfChars<'~'>() << '\n'; |
| 12016 | Colour colour(Colour::SecondaryText); |
| 12017 | stream << currentTestRunInfo->name |
| 12018 | << " is a Catch v" << libraryVersion() << " host application.\n" |
| 12019 | << "Run with -? for options\n\n"; |
| 12020 | |
| 12021 | if (m_config->rngSeed() != 0) |
| 12022 | stream << "Randomness seeded to: " << m_config->rngSeed() << "\n\n"; |
| 12023 | |
| 12024 | currentTestRunInfo.used = true; |
| 12025 | } |
| 12026 | void ConsoleReporter::lazyPrintGroupInfo() { |
| 12027 | if (!currentGroupInfo->name.empty() && currentGroupInfo->groupsCounts > 1) { |
| 12028 | printClosedHeader("Group: " + currentGroupInfo->name); |
| 12029 | currentGroupInfo.used = true; |
| 12030 | } |
| 12031 | } |
| 12032 | void ConsoleReporter::printTestCaseAndSectionHeader() { |
| 12033 | assert(!m_sectionStack.empty()); |
| 12034 | printOpenHeader(currentTestCaseInfo->name); |
| 12035 | |
| 12036 | if (m_sectionStack.size() > 1) { |
| 12037 | Colour colourGuard(Colour::Headers); |
| 12038 | |
| 12039 | auto |
| 12040 | it = m_sectionStack.begin() + 1, // Skip first section (test case) |
| 12041 | itEnd = m_sectionStack.end(); |
| 12042 | for (; it != itEnd; ++it) |
| 12043 | printHeaderString(it->name, 2); |
| 12044 | } |
| 12045 | |
| 12046 | SourceLineInfo lineInfo = m_sectionStack.back().lineInfo; |
| 12047 | |
| 12048 | if (!lineInfo.empty()) { |
| 12049 | stream << getLineOfChars<'-'>() << '\n'; |
| 12050 | Colour colourGuard(Colour::FileName); |
| 12051 | stream << lineInfo << '\n'; |
| 12052 | } |
| 12053 | stream << getLineOfChars<'.'>() << '\n' << std::endl; |
| 12054 | } |
| 12055 | |
| 12056 | void ConsoleReporter::printClosedHeader(std::string const& _name) { |
| 12057 | printOpenHeader(_name); |
| 12058 | stream << getLineOfChars<'.'>() << '\n'; |
| 12059 | } |
| 12060 | void ConsoleReporter::printOpenHeader(std::string const& _name) { |
| 12061 | stream << getLineOfChars<'-'>() << '\n'; |
| 12062 | { |
| 12063 | Colour colourGuard(Colour::Headers); |
| 12064 | printHeaderString(_name); |
| 12065 | } |
| 12066 | } |
| 12067 | |
| 12068 | // if string has a : in first line will set indent to follow it on |
| 12069 | // subsequent lines |
| 12070 | void ConsoleReporter::printHeaderString(std::string const& _string, std::size_t indent) { |
| 12071 | std::size_t i = _string.find(": "); |
| 12072 | if (i != std::string::npos) |
| 12073 | i += 2; |
| 12074 | else |
| 12075 | i = 0; |
| 12076 | stream << Column(_string).indent(indent + i).initialIndent(indent) << '\n'; |
| 12077 | } |
| 12078 | |
| 12079 | struct SummaryColumn { |
| 12080 | |
| 12081 | SummaryColumn( std::string _label, Colour::Code _colour ) |
| 12082 | : label( std::move( _label ) ), |
| 12083 | colour( _colour ) {} |
| 12084 | SummaryColumn addRow( std::size_t count ) { |
| 12085 | ReusableStringStream rss; |
| 12086 | rss << count; |
| 12087 | std::string row = rss.str(); |
| 12088 | for (auto& oldRow : rows) { |
| 12089 | while (oldRow.size() < row.size()) |
| 12090 | oldRow = ' ' + oldRow; |
| 12091 | while (oldRow.size() > row.size()) |
| 12092 | row = ' ' + row; |
| 12093 | } |
| 12094 | rows.push_back(row); |
| 12095 | return *this; |
| 12096 | } |
| 12097 | |
| 12098 | std::string label; |
| 12099 | Colour::Code colour; |
| 12100 | std::vector<std::string> rows; |
| 12101 | |
| 12102 | }; |
| 12103 | |
| 12104 | void ConsoleReporter::printTotals( Totals const& totals ) { |
| 12105 | if (totals.testCases.total() == 0) { |
| 12106 | stream << Colour(Colour::Warning) << "No tests ran\n"; |
| 12107 | } else if (totals.assertions.total() > 0 && totals.testCases.allPassed()) { |
| 12108 | stream << Colour(Colour::ResultSuccess) << "All tests passed"; |
| 12109 | stream << " (" |
| 12110 | << pluralise(totals.assertions.passed, "assertion") << " in " |
| 12111 | << pluralise(totals.testCases.passed, "test case") << ')' |
| 12112 | << '\n'; |
| 12113 | } else { |
| 12114 | |
| 12115 | std::vector<SummaryColumn> columns; |
| 12116 | columns.push_back(SummaryColumn("", Colour::None) |
| 12117 | .addRow(totals.testCases.total()) |
| 12118 | .addRow(totals.assertions.total())); |
| 12119 | columns.push_back(SummaryColumn("passed", Colour::Success) |
| 12120 | .addRow(totals.testCases.passed) |
| 12121 | .addRow(totals.assertions.passed)); |
| 12122 | columns.push_back(SummaryColumn("failed", Colour::ResultError) |
| 12123 | .addRow(totals.testCases.failed) |
| 12124 | .addRow(totals.assertions.failed)); |
| 12125 | columns.push_back(SummaryColumn("failed as expected", Colour::ResultExpectedFailure) |
| 12126 | .addRow(totals.testCases.failedButOk) |
| 12127 | .addRow(totals.assertions.failedButOk)); |
| 12128 | |
| 12129 | printSummaryRow("test cases", columns, 0); |
| 12130 | printSummaryRow("assertions", columns, 1); |
| 12131 | } |
| 12132 | } |
| 12133 | void ConsoleReporter::printSummaryRow(std::string const& label, std::vector<SummaryColumn> const& cols, std::size_t row) { |
| 12134 | for (auto col : cols) { |
| 12135 | std::string value = col.rows[row]; |
| 12136 | if (col.label.empty()) { |
| 12137 | stream << label << ": "; |
| 12138 | if (value != "0") |
| 12139 | stream << value; |
| 12140 | else |
| 12141 | stream << Colour(Colour::Warning) << "- none -"; |
| 12142 | } else if (value != "0") { |
| 12143 | stream << Colour(Colour::LightGrey) << " | "; |
| 12144 | stream << Colour(col.colour) |
| 12145 | << value << ' ' << col.label; |
| 12146 | } |
| 12147 | } |
| 12148 | stream << '\n'; |
| 12149 | } |
| 12150 | |
| 12151 | void ConsoleReporter::printTotalsDivider(Totals const& totals) { |
| 12152 | if (totals.testCases.total() > 0) { |
| 12153 | std::size_t failedRatio = makeRatio(totals.testCases.failed, totals.testCases.total()); |
| 12154 | std::size_t failedButOkRatio = makeRatio(totals.testCases.failedButOk, totals.testCases.total()); |
| 12155 | std::size_t passedRatio = makeRatio(totals.testCases.passed, totals.testCases.total()); |
| 12156 | while (failedRatio + failedButOkRatio + passedRatio < CATCH_CONFIG_CONSOLE_WIDTH - 1) |
| 12157 | findMax(failedRatio, failedButOkRatio, passedRatio)++; |
| 12158 | while (failedRatio + failedButOkRatio + passedRatio > CATCH_CONFIG_CONSOLE_WIDTH - 1) |
| 12159 | findMax(failedRatio, failedButOkRatio, passedRatio)--; |
| 12160 | |
| 12161 | stream << Colour(Colour::Error) << std::string(failedRatio, '='); |
| 12162 | stream << Colour(Colour::ResultExpectedFailure) << std::string(failedButOkRatio, '='); |
| 12163 | if (totals.testCases.allPassed()) |
| 12164 | stream << Colour(Colour::ResultSuccess) << std::string(passedRatio, '='); |
| 12165 | else |
| 12166 | stream << Colour(Colour::Success) << std::string(passedRatio, '='); |
| 12167 | } else { |
| 12168 | stream << Colour(Colour::Warning) << std::string(CATCH_CONFIG_CONSOLE_WIDTH - 1, '='); |
| 12169 | } |
| 12170 | stream << '\n'; |
| 12171 | } |
| 12172 | void ConsoleReporter::printSummaryDivider() { |
| 12173 | stream << getLineOfChars<'-'>() << '\n'; |
| 12174 | } |
| 12175 | |
| 12176 | CATCH_REGISTER_REPORTER("console", ConsoleReporter) |
| 12177 | |
| 12178 | } // end namespace Catch |
| 12179 | |
| 12180 | #if defined(_MSC_VER) |
| 12181 | #pragma warning(pop) |
| 12182 | #endif |
| 12183 | // end catch_reporter_console.cpp |
| 12184 | // start catch_reporter_junit.cpp |
| 12185 | |
| 12186 | #include <assert.h> |
| 12187 | #include <sstream> |
| 12188 | #include <ctime> |
| 12189 | #include <algorithm> |
| 12190 | |
| 12191 | namespace Catch { |
| 12192 | |
| 12193 | namespace { |
| 12194 | std::string getCurrentTimestamp() { |
| 12195 | // Beware, this is not reentrant because of backward compatibility issues |
| 12196 | // Also, UTC only, again because of backward compatibility (%z is C++11) |
| 12197 | time_t rawtime; |
| 12198 | std::time(&rawtime); |
| 12199 | auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); |
| 12200 | |
| 12201 | #ifdef _MSC_VER |
| 12202 | std::tm timeInfo = {}; |
| 12203 | gmtime_s(&timeInfo, &rawtime); |
| 12204 | #else |
| 12205 | std::tm* timeInfo; |
| 12206 | timeInfo = std::gmtime(&rawtime); |
| 12207 | #endif |
| 12208 | |
| 12209 | char timeStamp[timeStampSize]; |
| 12210 | const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; |
| 12211 | |
| 12212 | #ifdef _MSC_VER |
| 12213 | std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); |
| 12214 | #else |
| 12215 | std::strftime(timeStamp, timeStampSize, fmt, timeInfo); |
| 12216 | #endif |
| 12217 | return std::string(timeStamp); |
| 12218 | } |
| 12219 | |
| 12220 | std::string fileNameTag(const std::vector<std::string> &tags) { |
| 12221 | auto it = std::find_if(begin(tags), |
| 12222 | end(tags), |
| 12223 | [] (std::string const& tag) {return tag.front() == '#'; }); |
| 12224 | if (it != tags.end()) |
| 12225 | return it->substr(1); |
| 12226 | return std::string(); |
| 12227 | } |
| 12228 | } // anonymous namespace |
| 12229 | |
| 12230 | JunitReporter::JunitReporter( ReporterConfig const& _config ) |
| 12231 | : CumulativeReporterBase( _config ), |
| 12232 | xml( _config.stream() ) |
| 12233 | { |
| 12234 | m_reporterPrefs.shouldRedirectStdOut = true; |
| 12235 | } |
| 12236 | |
| 12237 | JunitReporter::~JunitReporter() {} |
| 12238 | |
| 12239 | std::string JunitReporter::getDescription() { |
| 12240 | return "Reports test results in an XML format that looks like Ant's junitreport target"; |
| 12241 | } |
| 12242 | |
| 12243 | void JunitReporter::noMatchingTestCases( std::string const& /*spec*/ ) {} |
| 12244 | |
| 12245 | void JunitReporter::testRunStarting( TestRunInfo const& runInfo ) { |
| 12246 | CumulativeReporterBase::testRunStarting( runInfo ); |
| 12247 | xml.startElement( "testsuites" ); |
| 12248 | } |
| 12249 | |
| 12250 | void JunitReporter::testGroupStarting( GroupInfo const& groupInfo ) { |
| 12251 | suiteTimer.start(); |
| 12252 | stdOutForSuite.clear(); |
| 12253 | stdErrForSuite.clear(); |
| 12254 | unexpectedExceptions = 0; |
| 12255 | CumulativeReporterBase::testGroupStarting( groupInfo ); |
| 12256 | } |
| 12257 | |
| 12258 | void JunitReporter::testCaseStarting( TestCaseInfo const& testCaseInfo ) { |
| 12259 | m_okToFail = testCaseInfo.okToFail(); |
| 12260 | } |
| 12261 | |
| 12262 | bool JunitReporter::assertionEnded( AssertionStats const& assertionStats ) { |
| 12263 | if( assertionStats.assertionResult.getResultType() == ResultWas::ThrewException && !m_okToFail ) |
| 12264 | unexpectedExceptions++; |
| 12265 | return CumulativeReporterBase::assertionEnded( assertionStats ); |
| 12266 | } |
| 12267 | |
| 12268 | void JunitReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { |
| 12269 | stdOutForSuite += testCaseStats.stdOut; |
| 12270 | stdErrForSuite += testCaseStats.stdErr; |
| 12271 | CumulativeReporterBase::testCaseEnded( testCaseStats ); |
| 12272 | } |
| 12273 | |
| 12274 | void JunitReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { |
| 12275 | double suiteTime = suiteTimer.getElapsedSeconds(); |
| 12276 | CumulativeReporterBase::testGroupEnded( testGroupStats ); |
| 12277 | writeGroup( *m_testGroups.back(), suiteTime ); |
| 12278 | } |
| 12279 | |
| 12280 | void JunitReporter::testRunEndedCumulative() { |
| 12281 | xml.endElement(); |
| 12282 | } |
| 12283 | |
| 12284 | void JunitReporter::writeGroup( TestGroupNode const& groupNode, double suiteTime ) { |
| 12285 | XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" ); |
| 12286 | TestGroupStats const& stats = groupNode.value; |
| 12287 | xml.writeAttribute( "name", stats.groupInfo.name ); |
| 12288 | xml.writeAttribute( "errors", unexpectedExceptions ); |
| 12289 | xml.writeAttribute( "failures", stats.totals.assertions.failed-unexpectedExceptions ); |
| 12290 | xml.writeAttribute( "tests", stats.totals.assertions.total() ); |
| 12291 | xml.writeAttribute( "hostname", "tbd" ); // !TBD |
| 12292 | if( m_config->showDurations() == ShowDurations::Never ) |
| 12293 | xml.writeAttribute( "time", "" ); |
| 12294 | else |
| 12295 | xml.writeAttribute( "time", suiteTime ); |
| 12296 | xml.writeAttribute( "timestamp", getCurrentTimestamp() ); |
| 12297 | |
| 12298 | // Write test cases |
| 12299 | for( auto const& child : groupNode.children ) |
| 12300 | writeTestCase( *child ); |
| 12301 | |
| 12302 | xml.scopedElement( "system-out" ).writeText( trim( stdOutForSuite ), false ); |
| 12303 | xml.scopedElement( "system-err" ).writeText( trim( stdErrForSuite ), false ); |
| 12304 | } |
| 12305 | |
| 12306 | void JunitReporter::writeTestCase( TestCaseNode const& testCaseNode ) { |
| 12307 | TestCaseStats const& stats = testCaseNode.value; |
| 12308 | |
| 12309 | // All test cases have exactly one section - which represents the |
| 12310 | // test case itself. That section may have 0-n nested sections |
| 12311 | assert( testCaseNode.children.size() == 1 ); |
| 12312 | SectionNode const& rootSection = *testCaseNode.children.front(); |
| 12313 | |
| 12314 | std::string className = stats.testInfo.className; |
| 12315 | |
| 12316 | if( className.empty() ) { |
| 12317 | className = fileNameTag(stats.testInfo.tags); |
| 12318 | if ( className.empty() ) |
| 12319 | className = "global"; |
| 12320 | } |
| 12321 | |
| 12322 | if ( !m_config->name().empty() ) |
| 12323 | className = m_config->name() + "." + className; |
| 12324 | |
| 12325 | writeSection( className, "", rootSection ); |
| 12326 | } |
| 12327 | |
| 12328 | void JunitReporter::writeSection( std::string const& className, |
| 12329 | std::string const& rootName, |
| 12330 | SectionNode const& sectionNode ) { |
| 12331 | std::string name = trim( sectionNode.stats.sectionInfo.name ); |
| 12332 | if( !rootName.empty() ) |
| 12333 | name = rootName + '/' + name; |
| 12334 | |
| 12335 | if( !sectionNode.assertions.empty() || |
| 12336 | !sectionNode.stdOut.empty() || |
| 12337 | !sectionNode.stdErr.empty() ) { |
| 12338 | XmlWriter::ScopedElement e = xml.scopedElement( "testcase" ); |
| 12339 | if( className.empty() ) { |
| 12340 | xml.writeAttribute( "classname", name ); |
| 12341 | xml.writeAttribute( "name", "root" ); |
| 12342 | } |
| 12343 | else { |
| 12344 | xml.writeAttribute( "classname", className ); |
| 12345 | xml.writeAttribute( "name", name ); |
| 12346 | } |
| 12347 | xml.writeAttribute( "time", ::Catch::Detail::stringify( sectionNode.stats.durationInSeconds ) ); |
| 12348 | |
| 12349 | writeAssertions( sectionNode ); |
| 12350 | |
| 12351 | if( !sectionNode.stdOut.empty() ) |
| 12352 | xml.scopedElement( "system-out" ).writeText( trim( sectionNode.stdOut ), false ); |
| 12353 | if( !sectionNode.stdErr.empty() ) |
| 12354 | xml.scopedElement( "system-err" ).writeText( trim( sectionNode.stdErr ), false ); |
| 12355 | } |
| 12356 | for( auto const& childNode : sectionNode.childSections ) |
| 12357 | if( className.empty() ) |
| 12358 | writeSection( name, "", *childNode ); |
| 12359 | else |
| 12360 | writeSection( className, name, *childNode ); |
| 12361 | } |
| 12362 | |
| 12363 | void JunitReporter::writeAssertions( SectionNode const& sectionNode ) { |
| 12364 | for( auto const& assertion : sectionNode.assertions ) |
| 12365 | writeAssertion( assertion ); |
| 12366 | } |
| 12367 | |
| 12368 | void JunitReporter::writeAssertion( AssertionStats const& stats ) { |
| 12369 | AssertionResult const& result = stats.assertionResult; |
| 12370 | if( !result.isOk() ) { |
| 12371 | std::string elementName; |
| 12372 | switch( result.getResultType() ) { |
| 12373 | case ResultWas::ThrewException: |
| 12374 | case ResultWas::FatalErrorCondition: |
| 12375 | elementName = "error"; |
| 12376 | break; |
| 12377 | case ResultWas::ExplicitFailure: |
| 12378 | elementName = "failure"; |
| 12379 | break; |
| 12380 | case ResultWas::ExpressionFailed: |
| 12381 | elementName = "failure"; |
| 12382 | break; |
| 12383 | case ResultWas::DidntThrowException: |
| 12384 | elementName = "failure"; |
| 12385 | break; |
| 12386 | |
| 12387 | // We should never see these here: |
| 12388 | case ResultWas::Info: |
| 12389 | case ResultWas::Warning: |
| 12390 | case ResultWas::Ok: |
| 12391 | case ResultWas::Unknown: |
| 12392 | case ResultWas::FailureBit: |
| 12393 | case ResultWas::Exception: |
| 12394 | elementName = "internalError"; |
| 12395 | break; |
| 12396 | } |
| 12397 | |
| 12398 | XmlWriter::ScopedElement e = xml.scopedElement( elementName ); |
| 12399 | |
| 12400 | xml.writeAttribute( "message", result.getExpandedExpression() ); |
| 12401 | xml.writeAttribute( "type", result.getTestMacroName() ); |
| 12402 | |
| 12403 | ReusableStringStream rss; |
| 12404 | if( !result.getMessage().empty() ) |
| 12405 | rss << result.getMessage() << '\n'; |
| 12406 | for( auto const& msg : stats.infoMessages ) |
| 12407 | if( msg.type == ResultWas::Info ) |
| 12408 | rss << msg.message << '\n'; |
| 12409 | |
| 12410 | rss << "at " << result.getSourceInfo(); |
| 12411 | xml.writeText( rss.str(), false ); |
| 12412 | } |
| 12413 | } |
| 12414 | |
| 12415 | CATCH_REGISTER_REPORTER( "junit", JunitReporter ) |
| 12416 | |
| 12417 | } // end namespace Catch |
| 12418 | // end catch_reporter_junit.cpp |
| 12419 | // start catch_reporter_multi.cpp |
| 12420 | |
| 12421 | namespace Catch { |
| 12422 | |
| 12423 | void MultipleReporters::add( IStreamingReporterPtr&& reporter ) { |
| 12424 | m_reporters.push_back( std::move( reporter ) ); |
| 12425 | } |
| 12426 | |
| 12427 | ReporterPreferences MultipleReporters::getPreferences() const { |
| 12428 | return m_reporters[0]->getPreferences(); |
| 12429 | } |
| 12430 | |
| 12431 | std::set<Verbosity> MultipleReporters::getSupportedVerbosities() { |
| 12432 | return std::set<Verbosity>{ }; |
| 12433 | } |
| 12434 | |
| 12435 | void MultipleReporters::noMatchingTestCases( std::string const& spec ) { |
| 12436 | for( auto const& reporter : m_reporters ) |
| 12437 | reporter->noMatchingTestCases( spec ); |
| 12438 | } |
| 12439 | |
| 12440 | void MultipleReporters::benchmarkStarting( BenchmarkInfo const& benchmarkInfo ) { |
| 12441 | for( auto const& reporter : m_reporters ) |
| 12442 | reporter->benchmarkStarting( benchmarkInfo ); |
| 12443 | } |
| 12444 | void MultipleReporters::benchmarkEnded( BenchmarkStats const& benchmarkStats ) { |
| 12445 | for( auto const& reporter : m_reporters ) |
| 12446 | reporter->benchmarkEnded( benchmarkStats ); |
| 12447 | } |
| 12448 | |
| 12449 | void MultipleReporters::testRunStarting( TestRunInfo const& testRunInfo ) { |
| 12450 | for( auto const& reporter : m_reporters ) |
| 12451 | reporter->testRunStarting( testRunInfo ); |
| 12452 | } |
| 12453 | |
| 12454 | void MultipleReporters::testGroupStarting( GroupInfo const& groupInfo ) { |
| 12455 | for( auto const& reporter : m_reporters ) |
| 12456 | reporter->testGroupStarting( groupInfo ); |
| 12457 | } |
| 12458 | |
| 12459 | void MultipleReporters::testCaseStarting( TestCaseInfo const& testInfo ) { |
| 12460 | for( auto const& reporter : m_reporters ) |
| 12461 | reporter->testCaseStarting( testInfo ); |
| 12462 | } |
| 12463 | |
| 12464 | void MultipleReporters::sectionStarting( SectionInfo const& sectionInfo ) { |
| 12465 | for( auto const& reporter : m_reporters ) |
| 12466 | reporter->sectionStarting( sectionInfo ); |
| 12467 | } |
| 12468 | |
| 12469 | void MultipleReporters::assertionStarting( AssertionInfo const& assertionInfo ) { |
| 12470 | for( auto const& reporter : m_reporters ) |
| 12471 | reporter->assertionStarting( assertionInfo ); |
| 12472 | } |
| 12473 | |
| 12474 | // The return value indicates if the messages buffer should be cleared: |
| 12475 | bool MultipleReporters::assertionEnded( AssertionStats const& assertionStats ) { |
| 12476 | bool clearBuffer = false; |
| 12477 | for( auto const& reporter : m_reporters ) |
| 12478 | clearBuffer |= reporter->assertionEnded( assertionStats ); |
| 12479 | return clearBuffer; |
| 12480 | } |
| 12481 | |
| 12482 | void MultipleReporters::sectionEnded( SectionStats const& sectionStats ) { |
| 12483 | for( auto const& reporter : m_reporters ) |
| 12484 | reporter->sectionEnded( sectionStats ); |
| 12485 | } |
| 12486 | |
| 12487 | void MultipleReporters::testCaseEnded( TestCaseStats const& testCaseStats ) { |
| 12488 | for( auto const& reporter : m_reporters ) |
| 12489 | reporter->testCaseEnded( testCaseStats ); |
| 12490 | } |
| 12491 | |
| 12492 | void MultipleReporters::testGroupEnded( TestGroupStats const& testGroupStats ) { |
| 12493 | for( auto const& reporter : m_reporters ) |
| 12494 | reporter->testGroupEnded( testGroupStats ); |
| 12495 | } |
| 12496 | |
| 12497 | void MultipleReporters::testRunEnded( TestRunStats const& testRunStats ) { |
| 12498 | for( auto const& reporter : m_reporters ) |
| 12499 | reporter->testRunEnded( testRunStats ); |
| 12500 | } |
| 12501 | |
| 12502 | void MultipleReporters::skipTest( TestCaseInfo const& testInfo ) { |
| 12503 | for( auto const& reporter : m_reporters ) |
| 12504 | reporter->skipTest( testInfo ); |
| 12505 | } |
| 12506 | |
| 12507 | bool MultipleReporters::isMulti() const { |
| 12508 | return true; |
| 12509 | } |
| 12510 | |
| 12511 | } // end namespace Catch |
| 12512 | // end catch_reporter_multi.cpp |
| 12513 | // start catch_reporter_xml.cpp |
| 12514 | |
| 12515 | #if defined(_MSC_VER) |
| 12516 | #pragma warning(push) |
| 12517 | #pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch |
| 12518 | // Note that 4062 (not all labels are handled |
| 12519 | // and default is missing) is enabled |
| 12520 | #endif |
| 12521 | |
| 12522 | namespace Catch { |
| 12523 | XmlReporter::XmlReporter( ReporterConfig const& _config ) |
| 12524 | : StreamingReporterBase( _config ), |
| 12525 | m_xml(_config.stream()) |
| 12526 | { |
| 12527 | m_reporterPrefs.shouldRedirectStdOut = true; |
| 12528 | } |
| 12529 | |
| 12530 | XmlReporter::~XmlReporter() = default; |
| 12531 | |
| 12532 | std::string XmlReporter::getDescription() { |
| 12533 | return "Reports test results as an XML document"; |
| 12534 | } |
| 12535 | |
| 12536 | std::string XmlReporter::getStylesheetRef() const { |
| 12537 | return std::string(); |
| 12538 | } |
| 12539 | |
| 12540 | void XmlReporter::writeSourceInfo( SourceLineInfo const& sourceInfo ) { |
| 12541 | m_xml |
| 12542 | .writeAttribute( "filename", sourceInfo.file ) |
| 12543 | .writeAttribute( "line", sourceInfo.line ); |
| 12544 | } |
| 12545 | |
| 12546 | void XmlReporter::noMatchingTestCases( std::string const& s ) { |
| 12547 | StreamingReporterBase::noMatchingTestCases( s ); |
| 12548 | } |
| 12549 | |
| 12550 | void XmlReporter::testRunStarting( TestRunInfo const& testInfo ) { |
| 12551 | StreamingReporterBase::testRunStarting( testInfo ); |
| 12552 | std::string stylesheetRef = getStylesheetRef(); |
| 12553 | if( !stylesheetRef.empty() ) |
| 12554 | m_xml.writeStylesheetRef( stylesheetRef ); |
| 12555 | m_xml.startElement( "Catch" ); |
| 12556 | if( !m_config->name().empty() ) |
| 12557 | m_xml.writeAttribute( "name", m_config->name() ); |
| 12558 | } |
| 12559 | |
| 12560 | void XmlReporter::testGroupStarting( GroupInfo const& groupInfo ) { |
| 12561 | StreamingReporterBase::testGroupStarting( groupInfo ); |
| 12562 | m_xml.startElement( "Group" ) |
| 12563 | .writeAttribute( "name", groupInfo.name ); |
| 12564 | } |
| 12565 | |
| 12566 | void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) { |
| 12567 | StreamingReporterBase::testCaseStarting(testInfo); |
| 12568 | m_xml.startElement( "TestCase" ) |
| 12569 | .writeAttribute( "name", trim( testInfo.name ) ) |
| 12570 | .writeAttribute( "description", testInfo.description ) |
| 12571 | .writeAttribute( "tags", testInfo.tagsAsString() ); |
| 12572 | |
| 12573 | writeSourceInfo( testInfo.lineInfo ); |
| 12574 | |
| 12575 | if ( m_config->showDurations() == ShowDurations::Always ) |
| 12576 | m_testCaseTimer.start(); |
| 12577 | m_xml.ensureTagClosed(); |
| 12578 | } |
| 12579 | |
| 12580 | void XmlReporter::sectionStarting( SectionInfo const& sectionInfo ) { |
| 12581 | StreamingReporterBase::sectionStarting( sectionInfo ); |
| 12582 | if( m_sectionDepth++ > 0 ) { |
| 12583 | m_xml.startElement( "Section" ) |
| 12584 | .writeAttribute( "name", trim( sectionInfo.name ) ) |
| 12585 | .writeAttribute( "description", sectionInfo.description ); |
| 12586 | writeSourceInfo( sectionInfo.lineInfo ); |
| 12587 | m_xml.ensureTagClosed(); |
| 12588 | } |
| 12589 | } |
| 12590 | |
| 12591 | void XmlReporter::assertionStarting( AssertionInfo const& ) { } |
| 12592 | |
| 12593 | bool XmlReporter::assertionEnded( AssertionStats const& assertionStats ) { |
| 12594 | |
| 12595 | AssertionResult const& result = assertionStats.assertionResult; |
| 12596 | |
| 12597 | bool includeResults = m_config->includeSuccessfulResults() || !result.isOk(); |
| 12598 | |
| 12599 | if( includeResults || result.getResultType() == ResultWas::Warning ) { |
| 12600 | // Print any info messages in <Info> tags. |
| 12601 | for( auto const& msg : assertionStats.infoMessages ) { |
| 12602 | if( msg.type == ResultWas::Info && includeResults ) { |
| 12603 | m_xml.scopedElement( "Info" ) |
| 12604 | .writeText( msg.message ); |
| 12605 | } else if ( msg.type == ResultWas::Warning ) { |
| 12606 | m_xml.scopedElement( "Warning" ) |
| 12607 | .writeText( msg.message ); |
| 12608 | } |
| 12609 | } |
| 12610 | } |
| 12611 | |
| 12612 | // Drop out if result was successful but we're not printing them. |
| 12613 | if( !includeResults && result.getResultType() != ResultWas::Warning ) |
| 12614 | return true; |
| 12615 | |
| 12616 | // Print the expression if there is one. |
| 12617 | if( result.hasExpression() ) { |
| 12618 | m_xml.startElement( "Expression" ) |
| 12619 | .writeAttribute( "success", result.succeeded() ) |
| 12620 | .writeAttribute( "type", result.getTestMacroName() ); |
| 12621 | |
| 12622 | writeSourceInfo( result.getSourceInfo() ); |
| 12623 | |
| 12624 | m_xml.scopedElement( "Original" ) |
| 12625 | .writeText( result.getExpression() ); |
| 12626 | m_xml.scopedElement( "Expanded" ) |
| 12627 | .writeText( result.getExpandedExpression() ); |
| 12628 | } |
| 12629 | |
| 12630 | // And... Print a result applicable to each result type. |
| 12631 | switch( result.getResultType() ) { |
| 12632 | case ResultWas::ThrewException: |
| 12633 | m_xml.startElement( "Exception" ); |
| 12634 | writeSourceInfo( result.getSourceInfo() ); |
| 12635 | m_xml.writeText( result.getMessage() ); |
| 12636 | m_xml.endElement(); |
| 12637 | break; |
| 12638 | case ResultWas::FatalErrorCondition: |
| 12639 | m_xml.startElement( "FatalErrorCondition" ); |
| 12640 | writeSourceInfo( result.getSourceInfo() ); |
| 12641 | m_xml.writeText( result.getMessage() ); |
| 12642 | m_xml.endElement(); |
| 12643 | break; |
| 12644 | case ResultWas::Info: |
| 12645 | m_xml.scopedElement( "Info" ) |
| 12646 | .writeText( result.getMessage() ); |
| 12647 | break; |
| 12648 | case ResultWas::Warning: |
| 12649 | // Warning will already have been written |
| 12650 | break; |
| 12651 | case ResultWas::ExplicitFailure: |
| 12652 | m_xml.startElement( "Failure" ); |
| 12653 | writeSourceInfo( result.getSourceInfo() ); |
| 12654 | m_xml.writeText( result.getMessage() ); |
| 12655 | m_xml.endElement(); |
| 12656 | break; |
| 12657 | default: |
| 12658 | break; |
| 12659 | } |
| 12660 | |
| 12661 | if( result.hasExpression() ) |
| 12662 | m_xml.endElement(); |
| 12663 | |
| 12664 | return true; |
| 12665 | } |
| 12666 | |
| 12667 | void XmlReporter::sectionEnded( SectionStats const& sectionStats ) { |
| 12668 | StreamingReporterBase::sectionEnded( sectionStats ); |
| 12669 | if( --m_sectionDepth > 0 ) { |
| 12670 | XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" ); |
| 12671 | e.writeAttribute( "successes", sectionStats.assertions.passed ); |
| 12672 | e.writeAttribute( "failures", sectionStats.assertions.failed ); |
| 12673 | e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk ); |
| 12674 | |
| 12675 | if ( m_config->showDurations() == ShowDurations::Always ) |
| 12676 | e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds ); |
| 12677 | |
| 12678 | m_xml.endElement(); |
| 12679 | } |
| 12680 | } |
| 12681 | |
| 12682 | void XmlReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { |
| 12683 | StreamingReporterBase::testCaseEnded( testCaseStats ); |
| 12684 | XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" ); |
| 12685 | e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() ); |
| 12686 | |
| 12687 | if ( m_config->showDurations() == ShowDurations::Always ) |
| 12688 | e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() ); |
| 12689 | |
| 12690 | if( !testCaseStats.stdOut.empty() ) |
| 12691 | m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), false ); |
| 12692 | if( !testCaseStats.stdErr.empty() ) |
| 12693 | m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), false ); |
| 12694 | |
| 12695 | m_xml.endElement(); |
| 12696 | } |
| 12697 | |
| 12698 | void XmlReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { |
| 12699 | StreamingReporterBase::testGroupEnded( testGroupStats ); |
| 12700 | // TODO: Check testGroupStats.aborting and act accordingly. |
| 12701 | m_xml.scopedElement( "OverallResults" ) |
| 12702 | .writeAttribute( "successes", testGroupStats.totals.assertions.passed ) |
| 12703 | .writeAttribute( "failures", testGroupStats.totals.assertions.failed ) |
| 12704 | .writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk ); |
| 12705 | m_xml.endElement(); |
| 12706 | } |
| 12707 | |
| 12708 | void XmlReporter::testRunEnded( TestRunStats const& testRunStats ) { |
| 12709 | StreamingReporterBase::testRunEnded( testRunStats ); |
| 12710 | m_xml.scopedElement( "OverallResults" ) |
| 12711 | .writeAttribute( "successes", testRunStats.totals.assertions.passed ) |
| 12712 | .writeAttribute( "failures", testRunStats.totals.assertions.failed ) |
| 12713 | .writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk ); |
| 12714 | m_xml.endElement(); |
| 12715 | } |
| 12716 | |
| 12717 | CATCH_REGISTER_REPORTER( "xml", XmlReporter ) |
| 12718 | |
| 12719 | } // end namespace Catch |
| 12720 | |
| 12721 | #if defined(_MSC_VER) |
| 12722 | #pragma warning(pop) |
| 12723 | #endif |
| 12724 | // end catch_reporter_xml.cpp |
| 12725 | |
| 12726 | namespace Catch { |
| 12727 | LeakDetector leakDetector; |
| 12728 | } |
| 12729 | |
| 12730 | #ifdef __clang__ |
| 12731 | #pragma clang diagnostic pop |
| 12732 | #endif |
| 12733 | |
| 12734 | // end catch_impl.hpp |
| 12735 | #endif |
| 12736 | |
| 12737 | #ifdef CATCH_CONFIG_MAIN |
| 12738 | // start catch_default_main.hpp |
| 12739 | |
| 12740 | #ifndef __OBJC__ |
| 12741 | |
| 12742 | #if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(_UNICODE) && !defined(DO_NOT_USE_WMAIN) |
| 12743 | // Standard C/C++ Win32 Unicode wmain entry point |
| 12744 | extern "C" int wmain (int argc, wchar_t * argv[], wchar_t * []) { |
| 12745 | #else |
| 12746 | // Standard C/C++ main entry point |
| 12747 | int main (int argc, char * argv[]) { |
| 12748 | #endif |
| 12749 | |
| 12750 | return Catch::Session().run( argc, argv ); |
| 12751 | } |
| 12752 | |
| 12753 | #else // __OBJC__ |
| 12754 | |
| 12755 | // Objective-C entry point |
| 12756 | int main (int argc, char * const argv[]) { |
| 12757 | #if !CATCH_ARC_ENABLED |
| 12758 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 12759 | #endif |
| 12760 | |
| 12761 | Catch::registerTestMethods(); |
| 12762 | int result = Catch::Session().run( argc, (char**)argv ); |
| 12763 | |
| 12764 | #if !CATCH_ARC_ENABLED |
| 12765 | [pool drain]; |
| 12766 | #endif |
| 12767 | |
| 12768 | return result; |
| 12769 | } |
| 12770 | |
| 12771 | #endif // __OBJC__ |
| 12772 | |
| 12773 | // end catch_default_main.hpp |
| 12774 | #endif |
| 12775 | |
| 12776 | #if !defined(CATCH_CONFIG_IMPL_ONLY) |
| 12777 | |
| 12778 | #ifdef CLARA_CONFIG_MAIN_NOT_DEFINED |
| 12779 | # undef CLARA_CONFIG_MAIN |
| 12780 | #endif |
| 12781 | |
| 12782 | #if !defined(CATCH_CONFIG_DISABLE) |
| 12783 | ////// |
| 12784 | // If this config identifier is defined then all CATCH macros are prefixed with CATCH_ |
| 12785 | #ifdef CATCH_CONFIG_PREFIX_ALL |
| 12786 | |
| 12787 | #define CATCH_REQUIRE( ... ) INTERNAL_CATCH_TEST( "CATCH_REQUIRE", Catch::ResultDisposition::Normal, __VA_ARGS__ ) |
| 12788 | #define CATCH_REQUIRE_FALSE( ... ) INTERNAL_CATCH_TEST( "CATCH_REQUIRE_FALSE", Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) |
| 12789 | |
| 12790 | #define CATCH_REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "CATCH_REQUIRE_THROWS", Catch::ResultDisposition::Normal, "", __VA_ARGS__ ) |
| 12791 | #define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr ) |
| 12792 | #define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr ) |
| 12793 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12794 | #define CATCH_REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr ) |
| 12795 | #endif// CATCH_CONFIG_DISABLE_MATCHERS |
| 12796 | #define CATCH_REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ ) |
| 12797 | |
| 12798 | #define CATCH_CHECK( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12799 | #define CATCH_CHECK_FALSE( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK_FALSE", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) |
| 12800 | #define CATCH_CHECKED_IF( ... ) INTERNAL_CATCH_IF( "CATCH_CHECKED_IF", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12801 | #define CATCH_CHECKED_ELSE( ... ) INTERNAL_CATCH_ELSE( "CATCH_CHECKED_ELSE", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12802 | #define CATCH_CHECK_NOFAIL( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK_NOFAIL", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, __VA_ARGS__ ) |
| 12803 | |
| 12804 | #define CATCH_CHECK_THROWS( ... ) INTERNAL_CATCH_THROWS( "CATCH_CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, "", __VA_ARGS__ ) |
| 12805 | #define CATCH_CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr ) |
| 12806 | #define CATCH_CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) |
| 12807 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12808 | #define CATCH_CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) |
| 12809 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12810 | #define CATCH_CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12811 | |
| 12812 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12813 | #define CATCH_CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg ) |
| 12814 | |
| 12815 | #define CATCH_REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg ) |
| 12816 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12817 | |
| 12818 | #define CATCH_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg ) |
| 12819 | #define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( "CATCH_WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) |
| 12820 | #define CATCH_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CATCH_CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) ) |
| 12821 | |
| 12822 | #define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) |
| 12823 | #define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) |
| 12824 | #define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) |
| 12825 | #define CATCH_REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) |
| 12826 | #define CATCH_SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) |
| 12827 | #define CATCH_FAIL( ... ) INTERNAL_CATCH_MSG( "CATCH_FAIL", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, __VA_ARGS__ ) |
| 12828 | #define CATCH_FAIL_CHECK( ... ) INTERNAL_CATCH_MSG( "CATCH_FAIL_CHECK", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12829 | #define CATCH_SUCCEED( ... ) INTERNAL_CATCH_MSG( "CATCH_SUCCEED", Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12830 | |
| 12831 | #define CATCH_ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE() |
| 12832 | |
| 12833 | // "BDD-style" convenience wrappers |
| 12834 | #define CATCH_SCENARIO( ... ) CATCH_TEST_CASE( "Scenario: " __VA_ARGS__ ) |
| 12835 | #define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) |
| 12836 | #define CATCH_GIVEN( desc ) CATCH_SECTION( std::string( "Given: ") + desc ) |
| 12837 | #define CATCH_WHEN( desc ) CATCH_SECTION( std::string( " When: ") + desc ) |
| 12838 | #define CATCH_AND_WHEN( desc ) CATCH_SECTION( std::string( " And: ") + desc ) |
| 12839 | #define CATCH_THEN( desc ) CATCH_SECTION( std::string( " Then: ") + desc ) |
| 12840 | #define CATCH_AND_THEN( desc ) CATCH_SECTION( std::string( " And: ") + desc ) |
| 12841 | |
| 12842 | // If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required |
| 12843 | #else |
| 12844 | |
| 12845 | #define REQUIRE( ... ) INTERNAL_CATCH_TEST( "REQUIRE", Catch::ResultDisposition::Normal, __VA_ARGS__ ) |
| 12846 | #define REQUIRE_FALSE( ... ) INTERNAL_CATCH_TEST( "REQUIRE_FALSE", Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) |
| 12847 | |
| 12848 | #define REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "REQUIRE_THROWS", Catch::ResultDisposition::Normal, __VA_ARGS__ ) |
| 12849 | #define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr ) |
| 12850 | #define REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr ) |
| 12851 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12852 | #define REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr ) |
| 12853 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12854 | #define REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ ) |
| 12855 | |
| 12856 | #define CHECK( ... ) INTERNAL_CATCH_TEST( "CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12857 | #define CHECK_FALSE( ... ) INTERNAL_CATCH_TEST( "CHECK_FALSE", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) |
| 12858 | #define CHECKED_IF( ... ) INTERNAL_CATCH_IF( "CHECKED_IF", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12859 | #define CHECKED_ELSE( ... ) INTERNAL_CATCH_ELSE( "CHECKED_ELSE", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12860 | #define CHECK_NOFAIL( ... ) INTERNAL_CATCH_TEST( "CHECK_NOFAIL", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, __VA_ARGS__ ) |
| 12861 | |
| 12862 | #define CHECK_THROWS( ... ) INTERNAL_CATCH_THROWS( "CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12863 | #define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr ) |
| 12864 | #define CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) |
| 12865 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12866 | #define CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) |
| 12867 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12868 | #define CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12869 | |
| 12870 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12871 | #define CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg ) |
| 12872 | |
| 12873 | #define REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg ) |
| 12874 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12875 | |
| 12876 | #define INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg ) |
| 12877 | #define WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) |
| 12878 | #define CAPTURE( msg ) INTERNAL_CATCH_INFO( "CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) ) |
| 12879 | |
| 12880 | #define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) |
| 12881 | #define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) |
| 12882 | #define METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) |
| 12883 | #define REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) |
| 12884 | #define SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) |
| 12885 | #define FAIL( ... ) INTERNAL_CATCH_MSG( "FAIL", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, __VA_ARGS__ ) |
| 12886 | #define FAIL_CHECK( ... ) INTERNAL_CATCH_MSG( "FAIL_CHECK", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12887 | #define SUCCEED( ... ) INTERNAL_CATCH_MSG( "SUCCEED", Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) |
| 12888 | #define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE() |
| 12889 | |
| 12890 | #endif |
| 12891 | |
| 12892 | #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) |
| 12893 | |
| 12894 | // "BDD-style" convenience wrappers |
| 12895 | #define SCENARIO( ... ) TEST_CASE( "Scenario: " __VA_ARGS__ ) |
| 12896 | #define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) |
| 12897 | |
| 12898 | #define GIVEN( desc ) SECTION( std::string(" Given: ") + desc ) |
| 12899 | #define WHEN( desc ) SECTION( std::string(" When: ") + desc ) |
| 12900 | #define AND_WHEN( desc ) SECTION( std::string("And when: ") + desc ) |
| 12901 | #define THEN( desc ) SECTION( std::string(" Then: ") + desc ) |
| 12902 | #define AND_THEN( desc ) SECTION( std::string(" And: ") + desc ) |
| 12903 | |
| 12904 | using Catch::Detail::Approx; |
| 12905 | |
| 12906 | #else |
| 12907 | ////// |
| 12908 | // If this config identifier is defined then all CATCH macros are prefixed with CATCH_ |
| 12909 | #ifdef CATCH_CONFIG_PREFIX_ALL |
| 12910 | |
| 12911 | #define CATCH_REQUIRE( ... ) (void)(0) |
| 12912 | #define CATCH_REQUIRE_FALSE( ... ) (void)(0) |
| 12913 | |
| 12914 | #define CATCH_REQUIRE_THROWS( ... ) (void)(0) |
| 12915 | #define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) (void)(0) |
| 12916 | #define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) (void)(0) |
| 12917 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12918 | #define CATCH_REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) |
| 12919 | #endif// CATCH_CONFIG_DISABLE_MATCHERS |
| 12920 | #define CATCH_REQUIRE_NOTHROW( ... ) (void)(0) |
| 12921 | |
| 12922 | #define CATCH_CHECK( ... ) (void)(0) |
| 12923 | #define CATCH_CHECK_FALSE( ... ) (void)(0) |
| 12924 | #define CATCH_CHECKED_IF( ... ) if (__VA_ARGS__) |
| 12925 | #define CATCH_CHECKED_ELSE( ... ) if (!(__VA_ARGS__)) |
| 12926 | #define CATCH_CHECK_NOFAIL( ... ) (void)(0) |
| 12927 | |
| 12928 | #define CATCH_CHECK_THROWS( ... ) (void)(0) |
| 12929 | #define CATCH_CHECK_THROWS_AS( expr, exceptionType ) (void)(0) |
| 12930 | #define CATCH_CHECK_THROWS_WITH( expr, matcher ) (void)(0) |
| 12931 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12932 | #define CATCH_CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) |
| 12933 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12934 | #define CATCH_CHECK_NOTHROW( ... ) (void)(0) |
| 12935 | |
| 12936 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12937 | #define CATCH_CHECK_THAT( arg, matcher ) (void)(0) |
| 12938 | |
| 12939 | #define CATCH_REQUIRE_THAT( arg, matcher ) (void)(0) |
| 12940 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12941 | |
| 12942 | #define CATCH_INFO( msg ) (void)(0) |
| 12943 | #define CATCH_WARN( msg ) (void)(0) |
| 12944 | #define CATCH_CAPTURE( msg ) (void)(0) |
| 12945 | |
| 12946 | #define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) |
| 12947 | #define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) |
| 12948 | #define CATCH_METHOD_AS_TEST_CASE( method, ... ) |
| 12949 | #define CATCH_REGISTER_TEST_CASE( Function, ... ) (void)(0) |
| 12950 | #define CATCH_SECTION( ... ) |
| 12951 | #define CATCH_FAIL( ... ) (void)(0) |
| 12952 | #define CATCH_FAIL_CHECK( ... ) (void)(0) |
| 12953 | #define CATCH_SUCCEED( ... ) (void)(0) |
| 12954 | |
| 12955 | #define CATCH_ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) |
| 12956 | |
| 12957 | // "BDD-style" convenience wrappers |
| 12958 | #define CATCH_SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) |
| 12959 | #define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) |
| 12960 | #define CATCH_GIVEN( desc ) |
| 12961 | #define CATCH_WHEN( desc ) |
| 12962 | #define CATCH_AND_WHEN( desc ) |
| 12963 | #define CATCH_THEN( desc ) |
| 12964 | #define CATCH_AND_THEN( desc ) |
| 12965 | |
| 12966 | // If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required |
| 12967 | #else |
| 12968 | |
| 12969 | #define REQUIRE( ... ) (void)(0) |
| 12970 | #define REQUIRE_FALSE( ... ) (void)(0) |
| 12971 | |
| 12972 | #define REQUIRE_THROWS( ... ) (void)(0) |
| 12973 | #define REQUIRE_THROWS_AS( expr, exceptionType ) (void)(0) |
| 12974 | #define REQUIRE_THROWS_WITH( expr, matcher ) (void)(0) |
| 12975 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12976 | #define REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) |
| 12977 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12978 | #define REQUIRE_NOTHROW( ... ) (void)(0) |
| 12979 | |
| 12980 | #define CHECK( ... ) (void)(0) |
| 12981 | #define CHECK_FALSE( ... ) (void)(0) |
| 12982 | #define CHECKED_IF( ... ) if (__VA_ARGS__) |
| 12983 | #define CHECKED_ELSE( ... ) if (!(__VA_ARGS__)) |
| 12984 | #define CHECK_NOFAIL( ... ) (void)(0) |
| 12985 | |
| 12986 | #define CHECK_THROWS( ... ) (void)(0) |
| 12987 | #define CHECK_THROWS_AS( expr, exceptionType ) (void)(0) |
| 12988 | #define CHECK_THROWS_WITH( expr, matcher ) (void)(0) |
| 12989 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12990 | #define CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) |
| 12991 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12992 | #define CHECK_NOTHROW( ... ) (void)(0) |
| 12993 | |
| 12994 | #if !defined(CATCH_CONFIG_DISABLE_MATCHERS) |
| 12995 | #define CHECK_THAT( arg, matcher ) (void)(0) |
| 12996 | |
| 12997 | #define REQUIRE_THAT( arg, matcher ) (void)(0) |
| 12998 | #endif // CATCH_CONFIG_DISABLE_MATCHERS |
| 12999 | |
| 13000 | #define INFO( msg ) (void)(0) |
| 13001 | #define WARN( msg ) (void)(0) |
| 13002 | #define CAPTURE( msg ) (void)(0) |
| 13003 | |
| 13004 | #define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) |
| 13005 | #define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) |
| 13006 | #define METHOD_AS_TEST_CASE( method, ... ) |
| 13007 | #define REGISTER_TEST_CASE( Function, ... ) (void)(0) |
| 13008 | #define SECTION( ... ) |
| 13009 | #define FAIL( ... ) (void)(0) |
| 13010 | #define FAIL_CHECK( ... ) (void)(0) |
| 13011 | #define SUCCEED( ... ) (void)(0) |
| 13012 | #define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) |
| 13013 | |
| 13014 | #endif |
| 13015 | |
| 13016 | #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) |
| 13017 | |
| 13018 | // "BDD-style" convenience wrappers |
| 13019 | #define SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ) ) |
| 13020 | #define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) |
| 13021 | |
| 13022 | #define GIVEN( desc ) |
| 13023 | #define WHEN( desc ) |
| 13024 | #define AND_WHEN( desc ) |
| 13025 | #define THEN( desc ) |
| 13026 | #define AND_THEN( desc ) |
| 13027 | |
| 13028 | using Catch::Detail::Approx; |
| 13029 | |
| 13030 | #endif |
| 13031 | |
| 13032 | #endif // ! CATCH_CONFIG_IMPL_ONLY |
| 13033 | |
| 13034 | // start catch_reenable_warnings.h |
| 13035 | |
| 13036 | |
| 13037 | #ifdef __clang__ |
| 13038 | # ifdef __ICC // icpc defines the __clang__ macro |
| 13039 | # pragma warning(pop) |
| 13040 | # else |
| 13041 | # pragma clang diagnostic pop |
| 13042 | # endif |
| 13043 | #elif defined __GNUC__ |
| 13044 | # pragma GCC diagnostic pop |
| 13045 | #endif |
| 13046 | |
| 13047 | // end catch_reenable_warnings.h |
| 13048 | // end catch.hpp |
| 13049 | #endif // TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED |
| 13050 | |