Hegel 0.17.5
Property-based testing for C++
Loading...
Searching...
No Matches
hegel Namespace Reference

Main namespace. More...

Namespaces

namespace  generators
 Hegel generators.
namespace  stateful
 Stateful (model-based) property testing.

Classes

class  RunError
 The engine stopped the run without a verdict on the property. More...
class  FailedHealthCheck
 A health check failed. More...
class  Flaky
 The test behaved nondeterministically under NondeterminismStrictness::Error. More...
class  Unsatisfiable
 The engine could not find inputs that satisfy the test's assumptions and filters. More...
class  DidNotReproduce
 No failure blob given to hegel::test() or HEGEL_REPRODUCE_FAILURE made the test fail. More...
class  MultipleFailures
 The run found more than one distinct failure. More...
struct  NamedValue
 A value of an ExplicitTestCase that a named draw takes. More...
class  ExplicitTestCase
 Values to run a test with before Hegel generates any. More...
class  GTestFailure
 The exception the GoogleTest integration raises for a test case that fails an assertion. More...
class  FailureOrigin
 Interface for an exception that names its own failure origin. More...
struct  TestLocation
 Where a test is defined. More...
class  Printer
 Writes to the document that a failure report shows. More...
class  Speculation
 An open speculative region; see Printer::speculate(). More...
class  Document
 A document of its own, outside a test case. More...
class  Database
 Configure the Hegel database. More...
struct  Settings
 Configuration options for hegel::test(). More...
class  TestCase
 Handle to the currently-executing test case. More...
class  Worker
 Handle to a worker started by TestCase::spawn(). More...

Enumerations

enum class  Verbosity { Quiet , Normal , Verbose , Debug }
 Verbosity levels. More...
enum class  HealthCheck { FilterTooMuch , TooSlow , TestCasesTooLarge , LargeInitialTestCase }
 Health checks. More...
enum class  Phase {
  Explicit , Reuse , Generate , Target ,
  Shrink
}
 Phases of a property-test run. More...
enum class  Backend { Auto , Default , Urandom }
 Source of randomness the engine draws from. More...
enum class  NondeterminismStrictness { Quiet , Warn , Error }
 How a run reacts when it detects nondeterministic test behavior: a test whose structure or outcome changes when the same choices are replayed. More...

Functions

template<typename T>
NamedValue< std::decay_t< T > > named (std::string name, T &&value)
 Give a value of an ExplicitTestCase to the draw named name.
void test (const std::function< void(TestCase &)> &test_fn, const Settings &settings={}, const std::vector< std::string > &failure_blobs={}, const std::vector< ExplicitTestCase > &explicit_cases={}, const char *caller_file=HEGEL_CALLER_FILE, const char *caller_function=HEGEL_CALLER_FUNCTION, int caller_line=HEGEL_CALLER_LINE)
 Run a Hegel test.
void test (const std::function< void(TestCase &)> &test_fn, const TestLocation &location, const Settings &settings={}, const std::vector< std::string > &failure_blobs={}, const std::vector< ExplicitTestCase > &explicit_cases={})
 Run a Hegel test at a location you name.
std::vector< HealthCheck > all_health_checks ()
 All health checks, suitable for full suppression.
std::vector< Phase > all_phases ()
 All phases, the base value of Settings::phases.
void register_profile (const std::string &name, const Settings &settings)
 Register settings as a named settings profile.
void set_default_profile (const std::string &name)
 Set the default settings profile for the whole process.
std::string engine_version ()
 The version of libhegel, the engine that Hegel for C++ drives.

Variables

template<typename T>
constexpr bool is_representable_v
 Whether Printer::repr() has a C++ representation for T.

Detailed Description

Main namespace.

Enumeration Type Documentation

◆ Backend

enum class hegel::Backend
strong

Source of randomness the engine draws from.

Enumerator
Auto 

Leave the choice to the engine's settings profile (the default): Urandom inside Antithesis, Default otherwise.

Default 

Expand a single seeded PRNG. Runs are reproducible from the seed and shrinking / replay work as usual.

Urandom 

Read fresh entropy from /dev/urandom on every draw. Intended for running under Antithesis; you almost certainly don't want it otherwise.

◆ HealthCheck

enum class hegel::HealthCheck
strong

Health checks.

Enumerator
FilterTooMuch 

Test filters out too many generated examples.

TooSlow 

Test cases are running too slowly.

TestCasesTooLarge 

Generated test cases are too large.

LargeInitialTestCase 

First generated test case is unusually large.

◆ NondeterminismStrictness

How a run reacts when it detects nondeterministic test behavior: a test whose structure or outcome changes when the same choices are replayed.

Enumerator
Quiet 

Switch to nondeterministic handling silently: failures are confirmed by repeated replay before they are shrunk or reported, and the report says how reliably each one reproduced. The default.

Warn 

Switch as under Quiet, printing a one-line notice once per run.

Error 

Fail the run with a flaky-test error, for suites that use determinism as a lint.

◆ Phase

enum class hegel::Phase
strong

Phases of a property-test run.

Enumerator
Explicit 

Run hard-coded explicit examples.

Reuse 

Replay counterexamples persisted in the database.

Generate 

Randomly generate fresh test cases.

Target 

Hill-climb toward observed target scores.

Shrink 

Shrink failing examples toward minimal counterexamples.

◆ Verbosity

enum class hegel::Verbosity
strong

Verbosity levels.

Enumerator
Quiet 

Minimal output (used by TUI).

Normal 

Default - standard test output.

Verbose 

More detailed output.

Debug 

Maximum verbosity + engine-side shrinker tracing.

Function Documentation

◆ all_health_checks()

std::vector< HealthCheck > hegel::all_health_checks ( )
inline

All health checks, suitable for full suppression.

Returns
A vector containing every HealthCheck variant.

◆ all_phases()

std::vector< Phase > hegel::all_phases ( )
inline

All phases, the base value of Settings::phases.

Returns
A vector containing every Phase variant.

◆ engine_version()

std::string hegel::engine_version ( )

The version of libhegel, the engine that Hegel for C++ drives.

Returns
The version, e.g. "0.48.0".

◆ named()

template<typename T>
NamedValue< std::decay_t< T > > hegel::named ( std::string name,
T && value )

Give a value of an ExplicitTestCase to the draw named name.

Template Parameters
TThe value type. It must be the type the draw produces.
Parameters
nameThe name the draw prints under: x_1 for the first tc.draw("x", ...), x_2 for the second, draw_1 for the first unnamed draw.
valueThe value the draw returns.
Returns
The named value.

◆ register_profile()

void hegel::register_profile ( const std::string & name,
const Settings & settings )

Register settings as a named settings profile.

The profile holds a complete snapshot of settings. Settings::database_key is not part of the snapshot. Registration is process-wide and replaces an earlier registration of the same name. It is not retroactive: a Settings created before it keeps its values, so register a profile before creating the Settings that use it. A hegel.toml section for name still applies over the snapshot.

nightly.test_cases = 10000;
hegel::register_profile("nightly", nightly);
void register_profile(const std::string &name, const Settings &settings)
Register settings as a named settings profile.
void test(const std::function< void(TestCase &)> &test_fn, const Settings &settings={}, const std::vector< std::string > &failure_blobs={}, const std::vector< ExplicitTestCase > &explicit_cases={}, const char *caller_file=HEGEL_CALLER_FILE, const char *caller_function=HEGEL_CALLER_FUNCTION, int caller_line=HEGEL_CALLER_LINE)
Run a Hegel test.
Configuration options for hegel::test().
Definition settings.h:180
static Settings from_profile(const std::string &name)
uint64_t test_cases
Number of test cases to run. The base value is 100.
Definition settings.h:197
Parameters
nameThe profile name: ASCII letters, digits, - and _. The reserved names base and default are not permitted.
settingsThe settings to register.
Exceptions
std::invalid_argumentfor an invalid or reserved name, or when settings name a profile that does not exist.

◆ set_default_profile()

void hegel::set_default_profile ( const std::string & name)

Set the default settings profile for the whole process.

The default profile then resolves to name. This takes precedence over the HEGEL_DEFAULT_PROFILE environment variable, the default entry in hegel.toml, and the detected environment. It is not retroactive: a Settings created before it keeps its values. The profile does not have to exist yet.

Parameters
nameThe profile name, or an empty string to remove an earlier call's selection.
Exceptions
std::invalid_argumentfor an invalid name.

◆ test() [1/2]

void hegel::test ( const std::function< void(TestCase &)> & test_fn,
const Settings & settings = {},
const std::vector< std::string > & failure_blobs = {},
const std::vector< ExplicitTestCase > & explicit_cases = {},
const char * caller_file = HEGEL_CALLER_FILE,
const char * caller_function = HEGEL_CALLER_FUNCTION,
int caller_line = HEGEL_CALLER_LINE )

Run a Hegel test.

This is the underlying entry point that HEGEL_TEST expands to.

Settings::database_key defaults to "<file>::<name>", where the file is the one holding the call and the name is the enclosing GoogleTest test (Suite.Name) or, outside a test framework, the enclosing function. The key is derived also when the database is disabled, because a derandomized run derives its seed from it.

Inside a GoogleTest test, the test location is that test and the line of the call. Pass a TestLocation to the overload below to name it yourself.

#include "hegel/hegel.h"
int main() {
namespace gs = hegel::generators;
auto x = tc.draw(gs::integers<int>({.min_value = 0, .max_value =
100})); auto y = tc.draw(gs::integers<int>({.min_value = 0, .max_value =
100}));
// Property: x + y >= x (true for non-negative integers)
if (x + y < x) {
throw std::runtime_error("Addition underflow!");
}
}, {.test_cases = 1000});
return 0;
}
Handle to the currently-executing test case.
Definition test_case.h:46
T draw(const generators::Generator< T > &gen) const
Draw a random value from a printable generator.
Definition core.h:991
Hegel generators.
Definition core.h:25
Parameters
test_fnThe test function to run repeatedly. Receives a TestCase which it uses to draw values, make assumptions, and record notes.
settingsConfiguration settings (test count, debug mode, etc.)
failure_blobsReproduce blobs from earlier failure reports. If any are given, the test replays each blob instead of generating test cases.
explicit_casesTest cases to run with the values they give, before any generated test case, when the Explicit phase is enabled. See ExplicitTestCase.
caller_fileFilled in by the compiler with the file of the call.
caller_functionFilled in by the compiler with the function holding the call.
caller_lineFilled in by the compiler with the line of the call.
Exceptions
MultipleFailuresif the run reports more than one failure. For a single failure, the test's own exception propagates.
RunError(or a subclass) if the run stops without a verdict, for example because a health check fails.
DidNotReproduceif no blob in failure_blobs makes the test fail.
std::invalid_argumentfor invalid settings, an unknown Settings::from_profile(), or a blob that is not valid.
See also
Settings for configuration settings

◆ test() [2/2]

void hegel::test ( const std::function< void(TestCase &)> & test_fn,
const TestLocation & location,
const Settings & settings = {},
const std::vector< std::string > & failure_blobs = {},
const std::vector< ExplicitTestCase > & explicit_cases = {} )

Run a Hegel test at a location you name.

Behaves like the overload above, except that the test location is location and Settings::database_key defaults to the location's file and name.

hegel::test(my_body, {"my_property", __FILE__, __LINE__},
{.test_cases = 500});
Parameters
test_fnThe test function to run repeatedly.
locationWhere the test is defined.
settingsConfiguration settings (test count, debug mode, etc.)
failure_blobsReproduce blobs from earlier failure reports. If any are given, the test replays each blob instead of generating test cases.
explicit_casesTest cases to run with the values they give, before any generated test case, when the Explicit phase is enabled. See ExplicitTestCase.
Exceptions
MultipleFailures,RunError,DidNotReproduce,std::invalid_argumentas the overload above.

Variable Documentation

◆ is_representable_v

template<typename T>
bool hegel::is_representable_v
inlineconstexpr
Initial value:
=
internal::repr_detail::is_representable<T>::value

Whether Printer::repr() has a C++ representation for T.

True for arithmetic types, enums, std::string, std::monostate, a type with a void print_to(hegel::Printer&) const member, the standard pairs, tuples, variants, optionals, vectors, sets, arrays and maps of such types, a reflectable aggregate (C++20), and a type with an operator<<. For a reflectable aggregate it does not look at the fields: a field with no representation prints as <unprintable ...>.

A generator that prints the value it produced (map, just, sampled_from, compose) is printable only for such a type.

Template Parameters
TThe type to check