Hegel 0.18.2
Property-based testing for C++
Loading...
Searching...
No Matches
hegel::ExplicitTestCase Class Reference

Values to run a test with before Hegel generates any. More...

#include <explicit.h>

Public Member Functions

template<typename... Ts, typename = std::enable_if_t< !(std::is_same_v<std::decay_t<Ts>, ExplicitTestCase> || ...)>>
 ExplicitTestCase (Ts &&... values)
 An explicit test case with the given values.

Detailed Description

Values to run a test with before Hegel generates any.

An explicit test case runs the test body once, in the Explicit phase (Phase::Explicit), before any generated test case. Each draw of the body returns a value of the explicit test case instead of a generated one: the value named for the draw (see hegel::named()), else the next positional value. Each value must have the exact type the draw produces, so write int64_t{5} for a draw of integers<int64_t>(). A string literal is a std::string.

Every value must be drawn. TestCase::target() and TestCase::event() do nothing, TestCase::repeat() runs its body once and then ends the test case, a TestCase::clone() draws the same values and names its draws in the same sequence as the test case it was cloned from, and TestCase::set_worker() does nothing. Anything else is a usage error, which makes hegel::test() throw std::invalid_argument without reporting a failure: a value that no draw takes, a draw with no value or a value of another type, a rejection (TestCase::assume() or TestCase::reject(), whose message begins "Explicit test cases must not be rejected"), and anything that needs generated data other than a draw (TestCase::draw_silent(), stateful testing).

If an explicit test case fails, Hegel prints what it drew and noted, and the test's exception propagates; no test case is generated. With Settings::report_multiple_failures, every explicit test case runs, the generated test cases follow, and every distinct failure is reported together, the explicit test cases' first.

HEGEL_EXPLICIT_TEST_CASE(division, 7, 0)
HEGEL_EXPLICIT_TEST_CASE(division, hegel::named("d", 1), 0)
HEGEL_TEST(division)(hegel::TestCase& tc) {
auto n = tc.draw(gs::integers<int>());
auto d = tc.draw("d", gs::integers<int>());
if (d != 0 && n / d * d + n % d != n) {
throw std::runtime_error("division is wrong");
}
}
Handle to the currently-executing test case.
Definition test_case.h:46
#define HEGEL_TEST(name,...)
Define a Hegel property test.
Definition hegel.h:674
#define HEGEL_EXPLICIT_TEST_CASE(name,...)
Add an explicit test case to a HEGEL_TEST.
Definition hegel.h:791
Main namespace.
Definition config.h:29
NamedValue< std::decay_t< T > > named(std::string name, T &&value)
Give a value of an ExplicitTestCase to the draw named name.
Definition explicit.h:39
See also
HEGEL_EXPLICIT_TEST_CASE, hegel::test()

Constructor & Destructor Documentation

◆ ExplicitTestCase()

template<typename... Ts, typename = std::enable_if_t< !(std::is_same_v<std::decay_t<Ts>, ExplicitTestCase> || ...)>>
hegel::ExplicitTestCase::ExplicitTestCase ( Ts &&... values)
inlineexplicit

An explicit test case with the given values.

Template Parameters
TsThe value types. A hegel::named() value is named, and every other value is positional.
Parameters
valuesThe values, in the order the draws take the positional ones.

The documentation for this class was generated from the following file: