|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <sstream> |
| 4 | +#include <source_location> |
| 5 | + |
| 6 | +#include <rapidcheck.h> |
| 7 | + |
| 8 | +namespace rc::doctest { |
| 9 | + |
| 10 | +/** |
| 11 | + * Checks the given predicate by applying it to randomly generated arguments. |
| 12 | + * |
| 13 | + * Quotes the given description string if the predicate can be falsified. |
| 14 | + * |
| 15 | + * Traces a progress message to 'stdout' if the flag 'v' is true. |
| 16 | + * |
| 17 | + * Like the function 'rc::check', but integrates with 'doctest' to include its |
| 18 | + * result in the statistics that are gathered for a test run. |
| 19 | + * |
| 20 | + * For example: |
| 21 | + * |
| 22 | + * TEST_CASE("addition is commutative") |
| 23 | + * { |
| 24 | + * wol::test::check("a+b == b+a", [](int a, int b) { return a+b == b+a; }); |
| 25 | + * } |
| 26 | + * |
| 27 | + * @param d A description of the predicate being checked. |
| 28 | + * @param t A predicate to check. |
| 29 | + * @param v A flag requesting verbose output. |
| 30 | + * |
| 31 | + * @see https://github.com/emil-e/rapidcheck/blob/master/doc/properties.md |
| 32 | + * for more on 'rc::check', on which this function is modeled. |
| 33 | + * |
| 34 | + * @see https://github.com/emil-e/rapidcheck/blob/master/doc/catch.md |
| 35 | + * for more on the integration of 'rapidcheck' and 'catch', on which |
| 36 | + * this implementation is based. |
| 37 | + */ |
| 38 | +template <class testable> |
| 39 | +void check(const char* d, |
| 40 | + testable&& t, |
| 41 | + bool v = false, |
| 42 | + std::source_location s = std::source_location::current()) |
| 43 | +{ |
| 44 | + using namespace rc::detail; |
| 45 | + using namespace doctest::detail; |
| 46 | + |
| 47 | + DOCTEST_SUBCASE(d) |
| 48 | + { |
| 49 | + auto r = checkTestable(std::forward<testable>(t)); |
| 50 | + |
| 51 | + if (r.template is<SuccessResult>()) |
| 52 | + { |
| 53 | + if (!r.template get<SuccessResult>().distribution.empty() || v) |
| 54 | + { |
| 55 | + std::cout << "- " << d << std::endl; |
| 56 | + printResultMessage(r, std::cout); |
| 57 | + std::cout << std::endl; |
| 58 | + } |
| 59 | + |
| 60 | + REQUIRE(true); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + std::ostringstream o; |
| 65 | + printResultMessage(r, o << '\n'); |
| 66 | + DOCTEST_INFO(o.str()); |
| 67 | + ResultBuilder b(doctest::assertType::DT_CHECK, s.file_name(), s.line(), s.function_name()); |
| 68 | + DOCTEST_ASSERT_LOG_REACT_RETURN(b); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Checks the given predicate by applying it to randomly generated arguments. |
| 75 | + * |
| 76 | + * Quotes the given description string if the predicate can be falsified. |
| 77 | + * |
| 78 | + * Traces a progress message to 'stdout' if the flag 'v' is true. |
| 79 | + * |
| 80 | + * Like the function 'rc::check', but integrates with 'doctest' to include its |
| 81 | + * result in the statitics that are gathered for a test run. |
| 82 | + * |
| 83 | + * For example: |
| 84 | + * |
| 85 | + * TEST_CASE("addition is commutative") |
| 86 | + * { |
| 87 | + * wol::test::check("a+b == b+a", [](int a, int b) { return a+b == b+a; }); |
| 88 | + * } |
| 89 | + * |
| 90 | + * @param t A predicate to check. |
| 91 | + * @param v A flag requesting verbose output. |
| 92 | + * |
| 93 | + * @see https://github.com/emil-e/rapidcheck/blob/master/doc/properties.md |
| 94 | + * for more on 'rc::check', on which this function is modeled. |
| 95 | + * |
| 96 | + * @see https://github.com/emil-e/rapidcheck/blob/master/doc/catch.md |
| 97 | + * for more on the integration of 'rapidcheck' and 'catch', on which |
| 98 | + * this implementation is based. |
| 99 | + */ |
| 100 | +template <class testable> |
| 101 | +inline |
| 102 | +void check(testable&& t, |
| 103 | + bool v = false, |
| 104 | + std::source_location s = std::source_location::current()) |
| 105 | +{ |
| 106 | + check("", t, v ,s); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace rc::doctest |
0 commit comments