Class: Pushdown::SpecHelpers::StateTransitionMatcher

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Includes:
RSpec::Matchers
Defined in:
lib/pushdown/spec_helpers.rb

Overview

RSpec matcher for matching transitions of Pushdown::States

Constant Summary collapse

DEFAULT_CALLBACK =
[ :update ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStateTransitionMatcher

Create a new matcher that expects a transition to occur.



26
27
28
29
30
31
32
33
34
# File 'lib/pushdown/spec_helpers.rb', line 26

def initialize
  @expected_type = nil
  @target_state = nil
  @callback = nil
  @additional_expectations = []
  @state = nil
  @result = nil
  @failure_description = nil
end

Instance Attribute Details

#additional_expectationsObject (readonly)

Returns the value of attribute additional_expectations.



37
38
39
# File 'lib/pushdown/spec_helpers.rb', line 37

def additional_expectations
  @additional_expectations
end

#callbackObject (readonly)

Returns the value of attribute callback.



37
38
39
# File 'lib/pushdown/spec_helpers.rb', line 37

def callback
  @callback
end

#expected_typeObject (readonly)

Returns the value of attribute expected_type.



37
38
39
# File 'lib/pushdown/spec_helpers.rb', line 37

def expected_type
  @expected_type
end

#failure_descriptionObject (readonly)

Returns the value of attribute failure_description.



37
38
39
# File 'lib/pushdown/spec_helpers.rb', line 37

def failure_description
  @failure_description
end

#resultObject (readonly)

Returns the value of attribute result.



37
38
39
# File 'lib/pushdown/spec_helpers.rb', line 37

def result
  @result
end

#stateObject (readonly)

Returns the value of attribute state.



37
38
39
# File 'lib/pushdown/spec_helpers.rb', line 37

def state
  @state
end

#target_stateObject (readonly)

Returns the value of attribute target_state.



37
38
39
# File 'lib/pushdown/spec_helpers.rb', line 37

def target_state
  @target_state
end

Instance Method Details

#failure_messageObject

RSpec matcher API – return a message describing an expectation failure.



60
61
62
# File 'lib/pushdown/spec_helpers.rb', line 60

def failure_message
  return "%p: %s" % [ self.state, self.describe_failure ]
end

#failure_message_when_negatedObject

RSpec matcher API – return a message describing an expectation being met when the matcher was used in a negated context.



67
68
69
# File 'lib/pushdown/spec_helpers.rb', line 67

def failure_message_when_negated
  return "%p: %s" % [ self.state, self.describe_negated_failure ]
end

#matches?(state) ⇒ Boolean

RSpec matcher API – returns true if all expectations are met after calling #update on the specified state.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/pushdown/spec_helpers.rb', line 48

def matches?( state )
  @state = state

  return self.update_ran_without_error? &&
    self.update_returned_transition? &&
    self.correct_transition_type? &&
    self.correct_target_state? &&
    self.matches_additional_expectations?
end

#on_an_event(event, *args) ⇒ Object Also known as: on_event

Specify that the operation that should cause the transition is the #on_event callback.

Raises:

  • (ScriptError)


106
107
108
109
110
# File 'lib/pushdown/spec_helpers.rb', line 106

def on_an_event( event, *args )
  raise ScriptError, "can't specify more than one callback" if self.callback
  @callback = [ :on_event, event, *args ]
  return self
end

#on_updateObject

Specify that the operation that should cause the transition is the #update callback. This is the default.

Raises:

  • (ScriptError)


98
99
100
101
102
# File 'lib/pushdown/spec_helpers.rb', line 98

def on_update
  raise ScriptError, "can't specify more than one callback" if self.callback
  @callback = [ :update ]
  return self
end

#to_state(state_name) ⇒ Object Also known as: to

Add an additional expectation that the state that is transitioned to be of the given state_name. This only applies to transitions that take a target state type. Expecting a particular state_name in transitions which do not take a state is undefined behavior.



89
90
91
92
# File 'lib/pushdown/spec_helpers.rb', line 89

def to_state( state_name )
  @target_state = state_name
  return self
end

#via_transition_type(transition_type) ⇒ Object Also known as: via

Add an additional expectation that the transition that occurs be of the specified transition_type.



78
79
80
81
# File 'lib/pushdown/spec_helpers.rb', line 78

def via_transition_type( transition_type )
  @expected_type = transition_type
  return self
end