Sha256: 2f3abcaafb3cefec51024973563a1204697637984bc688c497103cb1a922a162

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

RSpec.describe FiniteMachine, '.new' do
  context 'with block' do
    it "creates system state machine" do
      fsm = FiniteMachine.new do
        initial :green

        event :slow,  :green  => :yellow
        event :stop,  :yellow => :red
        event :ready, :red    => :yellow
        event :go,    :yellow => :green
      end

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

      fsm.slow
      expect(fsm.current).to eql(:yellow)
      fsm.stop
      expect(fsm.current).to eql(:red)
      fsm.ready
      expect(fsm.current).to eql(:yellow)
      fsm.go
      expect(fsm.current).to eql(:green)
    end
  end

  context 'without block' do
    it "creates state machine" do
      called = []
      fsm = FiniteMachine.new
      fsm.initial(:green)
      fsm.event(:slow, :green => :yellow)
      fsm.event(:stop, :yellow => :red)
      fsm.event(:ready,:red    => :yellow)
      fsm.event(:go,   :yellow => :green)
      fsm.on_enter(:yellow) { |event| called << 'on_enter_yellow' }
      fsm.handle(FiniteMachine::InvalidStateError) { |exception|
        called << 'error_handler'
      }
      fsm.init
      expect(fsm.current).to eql(:green)
      fsm.slow
      expect(fsm.current).to eql(:yellow)
      fsm.ready
      expect(fsm.current).to eql(:yellow)
      expect(called).to match_array(['on_enter_yellow', 'error_handler'])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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