Class: Hegel::StateMachine

Inherits:
Object
  • Object
show all
Includes:
Hegel::Syntax::Methods
Defined in:
lib/hegel/state_machine.rb,
sig/hegel.rbs

Overview

Base class for a stateful (model-based) test's machine: a subclass declares its actions with the class-level #rule and #invariant macros, then Hegel::Stateful.run drives one instance of it inside an ordinary Hegel.test block. See docs/adr/0010 for the declared shape (a class with macros, not a method-naming convention or an instance-built list) and the reasons behind it.

Only declaration lives here. Hegel::Stateful.run owns the loop that calls the declared blocks, the same split hegel-rust draws between its StateMachine trait (rules()/invariants()) and its own free function run.

Class Method Summary collapse

Methods included from Hegel::Syntax::Methods

#arrays, #binary, #booleans, #characters, #composite, #dates, #datetimes, #deferred, #domains, #emails, #floats, #from_regex, #hashes, #integers, #ip_addresses, #just, #one_of, #optional, #sampled_from, #sets, #text, #times, #tuples, #urls, #uuids

Class Method Details

.invariant(name) {|arg0| ... } ⇒ void

This method returns an undefined value.

Declares an invariant named name, checked once before the first rule runs and again after every rule that completes without its own assumption failing. Same block/argument contract as #rule.

Parameters:

  • name (Symbol, String)

Yields:

Yield Parameters:

Yield Returns:

  • (Object)


39
40
41
# File 'lib/hegel/state_machine.rb', line 39

def invariant(name, &block)
  declare(:@invariants, "invariant", name, block)
end

.invariant_definitionsHash[String, Proc]

The invariant analogue of #rule_definitions.

Returns:

  • (Hash[String, Proc])


51
52
53
# File 'lib/hegel/state_machine.rb', line 51

def invariant_definitions
  merged_definitions(:@invariants, :invariant_definitions)
end

.rule(name) {|arg0| ... } ⇒ void

This method returns an undefined value.

Declares a rule named name: an action the engine may pick to run at any step. block runs via #instance_exec against the machine instance being tested, and is handed the running Hegel::TestCase as its one argument -- ignored if the block declares no parameter, the ordinary Ruby block rule.

Parameters:

  • name (Symbol, String)

Yields:

Yield Parameters:

Yield Returns:

  • (Object)


32
33
34
# File 'lib/hegel/state_machine.rb', line 32

def rule(name, &block)
  declare(:@rules, "rule", name, block)
end

.rule_definitionsHash[String, Proc]

name => block, in declaration order, this class's own declarations merged over its ancestors'. Hegel::Stateful.run reads this directly to build the ordered rule-name list libhegel indexes by position.

Returns:

  • (Hash[String, Proc])


46
47
48
# File 'lib/hegel/state_machine.rb', line 46

def rule_definitions
  merged_definitions(:@rules, :rule_definitions)
end