Sha256: 053adf83174298bc805d5f3b3308512fc74498c22e5b6ccfdbb6cf851ce6d97c

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# encoding: utf-8

require 'spec_helper'

RSpec.describe FiniteMachine, 'define' do

  context 'with block' do
    it "creates system state machine" do
      fsm = FiniteMachine.define do
        initial :green

        events {
          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.define
      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

  xit "creates multiple machines"
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
finite_machine-0.11.1 spec/unit/define_spec.rb
finite_machine-0.11.0 spec/unit/define_spec.rb
finite_machine-0.10.2 spec/unit/define_spec.rb
finite_machine-0.10.1 spec/unit/define_spec.rb