Sha256: ab0171ef1f9c8a1c0cb6c16403b91e093010872f88be5c7d0ca3bf0a3c0424eb

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

RSpec.describe FiniteMachine, '#cancel_event' do
  it "cancels transition on event callback" do
    fsm = FiniteMachine.new do
      initial :green

      event :slow, :green  => :yellow
      event :go,   :yellow => :green

      on_exit :green do |event|
        cancel_event(event)
      end
    end

    expect(fsm.current).to eql(:green)
    fsm.slow
    expect(fsm.current).to eql(:green)
  end

  it "stops executing callbacks when cancelled" do
    called = []

    fsm = FiniteMachine.new do
      initial :initial

      event :bump, initial: :low

      on_before do |event|
        called << "enter_#{event.name}_#{event.from}_#{event.to}"

        cancel_event(event)
      end

      on_exit :initial do |event| called << "exit_initial" end
      on_exit          do |event| called << "exit_any" end
      on_enter :low    do |event| called << "enter_low" end
      on_after :bump   do |event| called << "after_#{event.name}" end
      on_after         do |event| called << "after_any" end
    end

    fsm.bump

    expect(called).to eq(['enter_bump_initial_low'])
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
finite_machine-0.12.1 spec/unit/cancel_callbacks_spec.rb
finite_machine-0.12.0 spec/unit/cancel_callbacks_spec.rb