Hegel 0.11.4
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_REPRODUCE_FAILURE(name, blob, ...)
 Replay a failing example for a HEGEL_TEST from its blob.

Detailed Description

Macros for defining Hegel property tests.

Macro Definition Documentation

◆ 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.

rerun with: 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:613

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. Additional blobs are accepted for bookkeeping, but only the first is replayed.

HEGEL_REPRODUCE_FAILURE(addition_commutes, "AAEAAAAACgEAAAAA")
HEGEL_TEST(addition_commutes)(hegel::TestCase& tc) {
// ...
}
#define HEGEL_TEST(name,...)
Define a Hegel property test.
Definition hegel.h:575
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(::hegel::Settings settings = \
::hegel::Settings(__VA_ARGS__)) { \
if (!settings.database_key.has_value()) { \
settings.database_key = __FILE__ "::" #name; \
} \
::hegel::internal::test_from_macro( \
hegel_test_body_##name, \
::hegel::TestLocation{#name, __FILE__, __LINE__}, settings, \
::hegel::internal::reproduce_blobs_for(__FILE__ "::" #name)); \
} \
static void hegel_test_body_##name
Handle to the currently-executing test case.
Definition test_case.h:40
Configuration options for hegel::test().
Definition settings.h:198
Where a test is defined.
Definition hegel.h:415

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 generator.
Definition core.h:264
Hegel generators.
Definition core.h:17
See also
HEGEL_REPRODUCE_FAILURE to replay a specific failing example.