Hegel 0.11.4
Property-based testing for C++
Loading...
Searching...
No Matches
hegel::stateful::StateMachine< Derived, State > Class Template Reference

Base class for a state machine. Holds the state and declares the rules and the invariants that act on it. More...

#include <stateful.h>

Public Types

using state_type = State
 The type of the state the rules act on.

Public Member Functions

 StateMachine (StateMachineParams< State > params)
 Builds a machine holding the given initial state.
std::vector< Invariant< Derived > > invariants ()
 Invariants checked before the first step and after every valid step. Override to add them. Defaults to none.

Public Attributes

State state

Detailed Description

template<typename Derived, typename State>
class hegel::stateful::StateMachine< Derived, State >

Base class for a state machine. Holds the state and declares the rules and the invariants that act on it.

Derive from it with the deriving type as Derived and the state's type as State, pass the initial state to this constructor, and define

std::vector<Rule<Derived>> rules();

returning the actions the test may apply. Optionally override invariants to add predicates checked before the first step and after every valid step. Rules mutate state in place. Pass an instance to run.

struct Counter : hegel::stateful::StateMachine<Counter, int> {
Counter() : StateMachine({.initial_state = 0}) {}
std::vector<hegel::stateful::Rule<Counter>> rules() {
"inc", [](hegel::TestCase&, Counter& m) { m.state++; })};
}
};
Handle to the currently-executing test case.
Definition test_case.h:40
A rule is one possible action in a stateful test.
Definition stateful.h:289
Base class for a state machine. Holds the state and declares the rules and the invariants that act on...
Definition stateful.h:406
StateMachine(StateMachineParams< State > params)
Builds a machine holding the given initial state.
Definition stateful.h:419

A machine may hold members besides the state, for anything the state's type cannot carry. A Pool, for instance, is tied to a test case and is neither copyable nor movable.

Template Parameters
DerivedThe deriving state-machine type
StateThe type of the state the rules act on

Constructor & Destructor Documentation

◆ StateMachine()

template<typename Derived, typename State>
hegel::stateful::StateMachine< Derived, State >::StateMachine ( StateMachineParams< State > params)
inlineexplicit

Builds a machine holding the given initial state.

Counter() : StateMachine({.initial_state = 0}) {}
Parameters
paramsThe initial state. See StateMachineParams.

Member Function Documentation

◆ invariants()

template<typename Derived, typename State>
std::vector< Invariant< Derived > > hegel::stateful::StateMachine< Derived, State >::invariants ( )
inline

Invariants checked before the first step and after every valid step. Override to add them. Defaults to none.

Returns
const std::vector<Invariant<Derived>>&

Member Data Documentation

◆ state

template<typename Derived, typename State>
State hegel::stateful::StateMachine< Derived, State >::state

The state the rules act on. Rules mutate it in place, and a failing run prints it. See RunParams::print_state.


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