1 #pragma once 2 3 #include "rapidcheck/detail/Results.h" 4 #include "rapidcheck/detail/Capture.h" 5 6 #define RC_INTERNAL_CONDITIONAL_RESULT( \ 7 ResultType, expression, invert, name, ...) \ 8 ::rc::detail::doAssert(RC_INTERNAL_CAPTURE(expression), \ 9 (invert), \ 10 ::rc::detail::CaseResult::Type::ResultType, \ 11 __FILE__, \ 12 __LINE__, \ 13 name "(" #expression ")") 14 15 16 #define RC_INTERNAL_STRINGIFY(x) #x 17 18 #define RC_INTERNAL_UNCONDITIONAL_RESULT(ResultType, name, expression) \ 19 do { \ 20 throw ::rc::detail::CaseResult( \ 21 ::rc::detail::CaseResult::Type::ResultType, \ 22 ::rc::detail::makeMessage( \ 23 __FILE__, __LINE__, name "(" #expression ")", {expression})); \ 24 } while (false) 25 26 /// Fails the current test case unless the given condition is `true`. 27 #define RC_ASSERT(expression) \ 28 RC_INTERNAL_CONDITIONAL_RESULT(Failure, expression, true, "RC_ASSERT", ) 29 30 /// Fails the current test case unless the given condition is `false`. 31 #define RC_ASSERT_FALSE(expression) \ 32 RC_INTERNAL_CONDITIONAL_RESULT(Failure, \ 33 expression, \ 34 false, \ 35 "RC_ASSERT_FALSE", ) 36 37 /// Fails the current test case unless the provided expression throws an 38 /// exception of any type 39 #define RC_ASSERT_THROWS(expression) \ 40 do { \ 41 try { \ 42 expression; \ 43 } catch (...) { \ 44 break; \ 45 } \ 46 throw ::rc::detail::CaseResult( \ 47 ::rc::detail::CaseResult::Type::Failure, \ 48 ::rc::detail::makeUnthrownExceptionMessage( \ 49 __FILE__, __LINE__, "RC_ASSERT_THROWS(" #expression ")")); \ 50 } while (false) 51 52 /// Fails the current test case unless the given expression throws an 53 /// exception that matches the given exception type 54 #define RC_ASSERT_THROWS_AS(expression, ExceptionType) \ 55 do { \ 56 try { \ 57 expression; \ 58 } catch (const ExceptionType &) { \ 59 break; \ 60 } catch (...) { \ 61 throw ::rc::detail::CaseResult(::rc::detail::CaseResult::Type::Failure, \ 62 ::rc::detail::makeWrongExceptionMessage( \ 63 __FILE__, \ 64 __LINE__, \ 65 "RC_ASSERT_THROWS_AS(" #expression \ 66 ", " #ExceptionType ")", \ 67 #ExceptionType)); \ 68 } \ 69 throw ::rc::detail::CaseResult(::rc::detail::CaseResult::Type::Failure, \ 70 ::rc::detail::makeUnthrownExceptionMessage( \ 71 __FILE__, \ 72 __LINE__, \ 73 "RC_ASSERT_THROWS_AS(" #expression \ 74 ", " #ExceptionType ")")); \ 75 } while (false) 76 77 /// Unconditionally fails the current test case with the given message. 78 #define RC_FAIL(...) \ 79 RC_INTERNAL_UNCONDITIONAL_RESULT(Failure, "RC_FAIL", __VA_ARGS__) 80 81 /// Succeed if the given condition is true. 82 #define RC_SUCCEED_IF(expression) \ 83 RC_INTERNAL_CONDITIONAL_RESULT(Success, expression, false, "RC_SUCCEED_IF", ) 84 85 /// Unconditionally succeed with the given message. 86 #define RC_SUCCEED(...) \ 87 RC_INTERNAL_UNCONDITIONAL_RESULT(Success, "RC_SUCCEED", __VA_ARGS__) 88 89 /// Discards the current test case if the given condition is false. 90 #define RC_PRE(expression) \ 91 RC_INTERNAL_CONDITIONAL_RESULT(Discard, expression, true, "RC_PRE", !) 92 93 /// Discards the current test case with the given description. 94 #define RC_DISCARD(...) \ 95 RC_INTERNAL_UNCONDITIONAL_RESULT(Discard, "RC_DISCARD", __VA_ARGS__) 96 97 #include "Assertions.hpp" 98