spec/unit/target_spec.rb in finite_machine-0.11.3 vs spec/unit/target_spec.rb in finite_machine-0.12.0
- old
+ new
@@ -1,9 +1,7 @@
-# encoding: utf-8
+# frozen_string_literal: true
-require 'spec_helper'
-
RSpec.describe FiniteMachine, '#target' do
it "allows to target external object" do
stub_const("Car", Class.new do
attr_accessor :reverse_lights
@@ -18,32 +16,25 @@
def reverse_lights?
@reverse_lights ||= false
end
def engine
- context = self
- @engine ||= FiniteMachine.define do
+ @engine ||= FiniteMachine.new(self) do
initial :neutral
- target context
+ event :forward, [:reverse, :neutral] => :one
+ event :shift, :one => :two
+ event :shift, :two => :one
+ event :back, [:neutral, :one] => :reverse
- events {
- event :forward, [:reverse, :neutral] => :one
- event :shift, :one => :two
- event :shift, :two => :one
- event :back, [:neutral, :one] => :reverse
- }
+ on_enter :reverse do |event|
+ target.turn_reverse_lights_on
+ end
- callbacks {
- on_enter :reverse do |event|
- target.turn_reverse_lights_on
- end
-
- on_exit :reverse do |event|
- target.turn_reverse_lights_off
- end
- }
+ on_exit :reverse do |event|
+ target.turn_reverse_lights_off
+ end
end
end
end)
car = Car.new
expect(car.reverse_lights?).to be(false)
@@ -55,47 +46,40 @@
expect(car.engine.current).to eql(:one)
expect(car.reverse_lights?).to be(false)
end
it "propagates method call" do
- fsm = FiniteMachine.define do
+ fsm = FiniteMachine.new do
initial :green
- events {
- event :slow, :green => :yellow
- }
- callbacks {
- on_enter_yellow do |event|
- uknown_method
- end
- }
+ event :slow, :green => :yellow
+
+ on_enter_yellow do |event|
+ uknown_method
+ end
end
expect(fsm.current).to eql(:green)
expect { fsm.slow }.to raise_error(StandardError)
end
it "references machine methods inside callback" do
called = []
- fsm = FiniteMachine.define do
+ fsm = FiniteMachine.new do
initial :green
- events {
- event :slow, :green => :yellow
- event :stop, :yellow => :red
- event :ready, :red => :yellow
- event :go, :yellow => :green
- }
+ event :slow, :green => :yellow
+ event :stop, :yellow => :red
+ event :ready, :red => :yellow
+ event :go, :yellow => :green
- callbacks {
- on_enter_yellow do |event|
- stop(:now)
- end
+ on_enter_yellow do |event|
+ stop(:now)
+ end
- on_enter_red do |event, param|
- called << "#{event.from} #{param}"
- end
- }
+ on_enter_red do |event, param|
+ called << "#{event.from} #{param}"
+ end
end
expect(fsm.current).to eql(:green)
fsm.slow
expect(fsm.current).to eql(:red)
@@ -119,33 +103,27 @@
@reverse_lights ||= false
end
def engine
self.called ||= []
- context ||= self
- @engine ||= FiniteMachine.define do
+
+ @engine ||= FiniteMachine.new(self) do
initial :neutral
- target context
+ event :forward, [:reverse, :neutral] => :one
+ event :shift, :one => :two
+ event :shift, :two => :one
+ event :back, [:neutral, :one] => :reverse
- events {
- event :forward, [:reverse, :neutral] => :one
- event :shift, :one => :two
- event :shift, :two => :one
- event :back, [:neutral, :one] => :reverse
- }
-
- callbacks {
- on_enter :reverse do |event|
- target.called << 'on_enter_reverse'
- target.turn_reverse_lights_on
- forward('Piotr!')
- end
- on_before :forward do |event, name|
- target.called << "on_enter_forward with #{name}"
- end
- }
+ on_enter :reverse do |event|
+ target.called << 'on_enter_reverse'
+ target.turn_reverse_lights_on
+ forward('Piotr!')
+ end
+ on_before :forward do |event, name|
+ target.called << "on_enter_forward with #{name}"
+ end
end
end
end)
car = Car.new
@@ -160,24 +138,19 @@
end
it "allows to access target inside the callback" do
context = double(:context)
called = nil
- fsm = FiniteMachine.define do
+ fsm = FiniteMachine.new(context) do
initial :green
- target context
+ event :slow, :green => :yellow
+ event :stop, :yellow => :red
- events {
- event :slow, :green => :yellow
- event :stop, :yellow => :red
- }
- callbacks {
- on_enter_yellow do |event|
- called = target
- end
- }
+ on_enter_yellow do |event|
+ called = target
+ end
end
expect(fsm.current).to eql(:green)
fsm.slow
expect(called).to eq(context)
end
@@ -192,28 +165,22 @@
@called << 'car save called'
end
end)
car = Car.new(called)
- fsm = FiniteMachine.define do
+ fsm = FiniteMachine.new(car) do
initial :unsaved
- target car
+ event :validate, :unsaved => :valid
+ event :save, :valid => :saved
- events {
- event :validate, :unsaved => :valid
- event :save, :valid => :saved
- }
-
- callbacks {
- on_enter :valid do |event|
- target.save
- save
- end
- on_after :save do |event|
- called << 'event save called'
- end
- }
+ on_enter :valid do |event|
+ target.save
+ save
+ end
+ on_after :save do |event|
+ called << 'event save called'
+ end
end
expect(fsm.current).to eql(:unsaved)
fsm.validate
expect(fsm.current).to eql(:saved)
expect(called).to eq([