Hegel 0.17.5
Property-based testing for C++
Loading...
Searching...
No Matches
Test definition macros

Macros for defining Hegel property tests. More...

Macros

#define HEGEL_TEST(name, ...)
 Define a Hegel property test.
#define HEGEL_MAIN(name, ...)
 Define a standalone test binary that runs one test case.
#define HEGEL_REPRODUCE_FAILURE(name, blob, ...)
 Replay a failing example for a HEGEL_TEST from its blob.
#define HEGEL_EXPLICIT_TEST_CASE(name, ...)
 Add an explicit test case to a HEGEL_TEST.

Detailed Description

Macros for defining Hegel property tests.

Macro Definition Documentation

◆ HEGEL_EXPLICIT_TEST_CASE

#define HEGEL_EXPLICIT_TEST_CASE ( name,
... )
Value:
[[maybe_unused]] static const bool HEGEL_CONCAT( \
hegel_explicit_registered_##name##_, __LINE__) = \
::hegel::internal::register_explicit_case( \
__FILE__ "::" #name, ::hegel::ExplicitTestCase(__VA_ARGS__));
Values to run a test with before Hegel generates any.
Definition explicit.h:87

Add an explicit test case to a HEGEL_TEST.

Place it above the matching HEGEL_TEST, keyed by the same name. The arguments are the values of a hegel::ExplicitTestCase: positional values, taken by the test's draws in order, and hegel::named() values, taken by the draw of that name. Each explicit test case runs once, in the order they appear, before any generated test case.

HEGEL_EXPLICIT_TEST_CASE(sorted_is_idempotent, std::vector<int>{})
HEGEL_EXPLICIT_TEST_CASE(sorted_is_idempotent,
hegel::named("xs", std::vector<int>{1, 1}))
HEGEL_TEST(sorted_is_idempotent)(hegel::TestCase& tc) {
auto xs = tc.draw("xs", gs::vectors(gs::integers<int>()));
// ...
}
Handle to the currently-executing test case.
Definition test_case.h:46
#define HEGEL_TEST(name,...)
Define a Hegel property test.
Definition hegel.h:667
#define HEGEL_EXPLICIT_TEST_CASE(name,...)
Add an explicit test case to a HEGEL_TEST.
Definition hegel.h:784
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::ExplicitTestCase

◆ HEGEL_MAIN

#define HEGEL_MAIN ( name,
... )
Value:
static void hegel_test_body_##name(::hegel::TestCase&); \
int main(int argc, char** argv) { \
return ::hegel::internal::main_from_macro( \
argc, argv, hegel_test_body_##name, \
::hegel::TestLocation{#name, __FILE__, __LINE__}, \
[] { return ::hegel::Settings(__VA_ARGS__); }, \
::hegel::internal::reproduce_blobs_for(__FILE__ "::" #name), \
::hegel::internal::explicit_cases_for(__FILE__ "::" #name)); \
} \
static void hegel_test_body_##name
Where a test is defined.
Definition hegel.h:472

Define a standalone test binary that runs one test case.

HEGEL_MAIN(workload)(hegel::TestCase& tc) {
namespace gs = hegel::generators;
auto n = tc.draw("n", gs::integers<int>());
if (n + 0 != n) {
throw std::runtime_error("n + 0 != n");
}
}
#define HEGEL_MAIN(name,...)
Define a standalone test binary that runs one test case.
Definition hegel.h:715
Hegel generators.
Definition core.h:25

Settings::test_cases has no effect, and the TooSlow and TestCasesTooLarge health checks are always suppressed.

Each setting is a command-line option named after its hegel.toml key (e.g. --seed 42, --phases generate,shrink, ...). Each option also takes the form --option=VALUE. Options override settings in the test code, and --profile selects the settings profile those settings are created from.

A failure report ends with the option that replays it, then the test case's exception:

To reproduce this failure, run the program with:
--failure-blob AAEAAAAACgEAAAAy
Exception: std::runtime_error: n + 0 != n

The program exits with status 0 if the test case passes, 1 if it fails, and 2 for a usage error. HEGEL_REPRODUCE_FAILURE and HEGEL_EXPLICIT_TEST_CASE work with the same name.

See also
HEGEL_TEST

◆ HEGEL_REPRODUCE_FAILURE

#define HEGEL_REPRODUCE_FAILURE ( name,
blob,
... )
Value:
[[maybe_unused]] static const bool hegel_reproduce_registered_##name = \
::hegel::internal::register_blob(__FILE__ "::" #name, \
{blob, __VA_ARGS__});

Replay a failing example for a HEGEL_TEST from its blob.

To reproduce this failure, add this line above HEGEL_TEST(addition_commutes):
HEGEL_REPRODUCE_FAILURE(addition_commutes, "AAEAAAAACgEAAAAA")
#define HEGEL_REPRODUCE_FAILURE(name, blob,...)
Replay a failing example for a HEGEL_TEST from its blob.
Definition hegel.h:753

Place it above the matching HEGEL_TEST, keyed by the same name. The test then replays that example instead of generating new cases. Delete the annotation to return to a normal run. Settings::print_blob controls whether the report prints the line.

At least one blob is required. Each blob is replayed. If more than one of them makes the test fail, the test throws hegel::MultipleFailures. If none does, it throws hegel::DidNotReproduce.

HEGEL_REPRODUCE_FAILURE(addition_commutes, "AAEAAAAACgEAAAAA")
HEGEL_TEST(addition_commutes)(hegel::TestCase& tc) {
// ...
}
Main namespace.
Definition config.h:29
See also
HEGEL_TEST

◆ HEGEL_TEST

#define HEGEL_TEST ( name,
... )
Value:
static void hegel_test_body_##name(::hegel::TestCase&); \
static void name(const ::hegel::Settings& settings = \
::hegel::Settings(__VA_ARGS__)) { \
::hegel::internal::test_from_macro( \
hegel_test_body_##name, \
::hegel::TestLocation{#name, __FILE__, __LINE__}, settings, \
::hegel::internal::reproduce_blobs_for(__FILE__ "::" #name), \
::hegel::internal::explicit_cases_for(__FILE__ "::" #name)); \
} \
static void hegel_test_body_##name
Configuration options for hegel::test().
Definition settings.h:180

Define a Hegel property test.

It expands to a function name(hegel::Settings = ...) that runs that body via hegel::test() with Settings::database_key defaulted to "file.cpp::name" so counterexamples persisted to the database are scoped to this test and replayed on later runs. A database_key set explicitly in the Settings overrides the derived one.

Settings for the test are written inline after the name. They become the function's default argument, and settings passed at the call site replace them entirely.

With a test framework, write the property inside one of its tests and call hegel::test() instead. If you use HEGEL_TEST, it must be used outside of the test framework's test macro/function then call the named Hegel test.

HEGEL_TEST(addition_commutes, {.test_cases = 500})(hegel::TestCase& tc) {
namespace gs = hegel::generators;
int x = tc.draw(gs::integers<int>());
int y = tc.draw(gs::integers<int>());
if (x + y != y + x) {
throw std::runtime_error("addition is not commutative");
}
}
int main() {
addition_commutes(); // runs with .test_cases = 500
addition_commutes({.test_cases = 5}); // call-site Settings take over
return 0;
}
T draw(const generators::Generator< T > &gen) const
Draw a random value from a printable generator.
Definition core.h:991
See also
HEGEL_REPRODUCE_FAILURE to replay a specific failing example.