spec/integration/transition_spec.rb in state-fu-0.13.4 vs spec/integration/transition_spec.rb in state-fu-0.13.5
- old
+ new
@@ -523,46 +523,41 @@
end
end # a transition ..
describe "fire! calling hooks" do
before do
- @t = @obj.state_fu.transition( :go )
+ @t = @obj.state_fu.transition(:go)
end
- it "should update the object's state after state:entering and before event:after" do
- @binding = @obj.state_fu
- pending
+ it "should change state between state:entering and event:after" do
+ @binding = @obj.state_fu
+ @obj.should_receive(:entering_b).and_return do
+ @obj.current_state.name.should == :a
+ end
+ @obj.should_receive(:after_go).and_return do
+ @obj.current_state.name.should == :b
+ end
@t.fire!
end
- it "should be accepted after state:entering and before event:after" do
- pending
- mock( @obj ).entering_b( @t ) { @t.should_not be_accepted }
- mock( @obj ).after_go(@t) { @t.should be_accepted }
- mock( @obj ).accepted_b(@t) { @t.should be_accepted }
- @t.fire!
- end
-
it "should call the method for each hook on @obj in order, with the transition" do
- pending
- mock( @obj ).before_go(@t) { @called << :before_go }
- mock( @obj ).exiting_a(@t) { @called << :exiting_a }
- mock( @obj ).execute_go(@t) { @called << :execute_go }
- mock( @obj ).entering_b(@t) { @called << :entering_b }
- mock( @obj ).after_go(@t) { @called << :after_go }
- mock( @obj ).accepted_b(@t) { @called << :accepted_b }
-
+ hooks = [:before_go, :exiting_a, :execute_go, :entering_b, :after_go, :accepted_b]
+ hooks.each do |hook|
+ @obj.should_receive(hook).and_return do
+ @obj.called hook
+ end
+ end
@t.fire!()
+ @obj.calls.should == hooks
end
describe "adding an anonymous hook for event.hooks[:execute]" do
before do
- called = @called # get us a ref for the closure
Klass.state_fu_machine do
event( :go ) do
execute do |ctx|
- called( :execute_proc )
+ called :execute_proc
end
end
end
end
@@ -579,80 +574,67 @@
:after_go,
:accepted_b ]
end
it "should be replace the previous proc for a slot if redefined" do
- pending
called = @called # get us a ref for the closure
Klass.state_fu_machine do
event( :go ) do
execute do |ctx|
- called << :execute_proc_2
+ called(:updated)
end
end
end
-
- @event.hooks[:execute].length.should == 2
- @event.hooks[:execute].first.class.should == Symbol
- @event.hooks[:execute].last.class.should == Proc
-
- @t.fire!()
- @called.should == [ :before_go,
- :exiting_a,
- :execute_go,
- :execute_proc_2,
- :entering_b,
- :after_go,
- :accepted_b ]
+ @t.fire!
+ @obj.calls.should == [:before_go,
+ :exiting_a,
+ :execute_go,
+ :updated,
+ :entering_b,
+ :after_go,
+ :accepted_b]
end
end # anonymous hook
describe "adding a named hook with a block" do
describe "with arity of -1/0" do
it "should call the block in the context of the transition" do
- pending
- called = @called # get us a ref for the closure
Klass.state_fu_machine do
event( :go ) do
execute(:named_execute) do
- raise self.class.inspect unless self.is_a?( StateFu::Transition )
- called << :execute_named_proc
+ called :execute_named_proc
end
end
end
@t.fire!()
- @called.should == [ :before_go,
- :exiting_a,
- :execute_go,
- :execute_named_proc,
- :entering_b,
- :after_go,
- :accepted_b ]
+ @obj.calls.should == [ :before_go,
+ :exiting_a,
+ :execute_go,
+ :execute_named_proc,
+ :entering_b,
+ :after_go,
+ :accepted_b ]
end
end # arity 0
describe "with arity of 1" do
it "should call the proc in the context of the object, passing the transition as the argument" do
- pending
- called = @called # get us a ref for the closure
Klass.state_fu_machine do
event( :go ) do
- execute(:named_execute) do |ctx|
- raise ctx.class.inspect unless ctx.is_a?( StateFu::Transition )
- raise self.class.inspect unless self.is_a?( Klass )
- called << :execute_named_proc
+ execute(:named_execute) do |t|
+ called [:execute_named_proc, t]
end
end
end
@t.fire!()
- @called.should == [ :before_go,
- :exiting_a,
- :execute_go,
- :execute_named_proc,
- :entering_b,
- :after_go,
- :accepted_b ]
+ @obj.calls.should == [:before_go,
+ :exiting_a,
+ :execute_go,
+ [:execute_named_proc, @t],
+ :entering_b,
+ :after_go,
+ :accepted_b]
end
end # arity 1
end # named proc
describe "halting the transition during the execute hook" do
@@ -712,11 +694,10 @@
@obj = Klass.new
@binding = @obj.state_fu
@event = @machine.events[:go]
@a = @machine.states[:a]
@b = @machine.states[:b]
- # stub(@obj).ok? { true }
end
describe "when no block is supplied for the requirement" do
it "should have an event named :go" do
@@ -733,27 +714,23 @@
@binding.events.should_not be_empty
end
it "should contain the event in @binding.valid_events if @obj.ok? is true" do
- # stub( @binding ).ok?() { true }
-# set_method_arity(@binding,:ok, 0)
@obj.ok = true
@binding.current_state.should == @machine.initial_state
@binding.events.should == @machine.events
@binding.valid_events.should == [@event]
end
it "should not contain :go in @binding.valid_events if !@obj.ok?" do
- # stub( @binding ).ok?() { false }
@obj.ok = false
@binding.events.should == @machine.events
@binding.valid_events.should == []
end
it "should raise a RequirementError if requirements are not satisfied" do
- #stub( @binding ).ok? { false }
@obj.ok = false
lambda do
@obj.state_fu.fire_transition!( :go )
end.should raise_error( StateFu::RequirementError )
end
@@ -812,18 +789,16 @@
t = @binding.transition([@event, @b])
@binding.valid_next_states.should == [@b]
end
it "should be invalid if @obj.entry_ok? is false" do
- #mock( @obj ).entry_ok? { false }
@obj.entry_ok = false
@b.entry_requirements.should == [:entry_ok?]
@binding.valid_next_states.should == []
end
it "should be valid if @obj.entry_ok? is true" do
- # mock( @obj ).entry_ok? { true }
@obj.entry_ok = true
@binding.valid_next_states.should == [@b]
end
end # no block
@@ -861,22 +836,10 @@
@obj = Klass.new()
end # before
describe "a method defined on the stateful object" do
- it "should be able to conditionally execute code based on whether the transition is a test" do
- pending
- testing = nil
- @obj.__define_singleton_method(:run_exec) do
- testing = t.testing?
- end
- @obj.state_fu.fire! :run do |t|
- t.test_only = true
- end
- testing.should == true
- end
-
it "should be able to call methods on the transition mixed in via machine.helper" do
t1 = @obj.state_fu.transition( :run)
t1.should_not respond_to(:my_rad_method)
@machine.helper :my_rad_helper
@@ -899,44 +862,37 @@
t2.should respond_to(:my_rad_method)
t3.should_not respond_to(:my_rad_method)
end
it "should be able to access the args / options passed to fire! via transition.args" do
- pending
# NOTE a trailing hash gets munged into options - not args
args = [:a, :b, { 'c' => :d }]
- @obj.__define_singleton_method(:run_exec) do
- t.args.should == [:a, :b,{'c' => :d}]
- t.options.should == {}
+ Klass.state_fu_machine do
+ event(:run, :from => :start, :to => :finish ) do
+ execute( :run_exec ) do |t|
+ t.args.should == [:a, :b, {'c' => :d}]
+ t.options.should == {:c => :d} # options are symbolized
+ end
+ end
end
- trans = @obj.state_fu.fire!( :run, *args )
+ trans = @obj.state_fu.run!( *args )
trans.should be_accepted
end
end # method defined on object
describe "a block passed to binding.transition" do
- it "should execute in the context of the transition initializer after it's set up" do
- pending
- @obj.__define_singleton_method(:run_exec) do
- t.args.should == ['who','yo','daddy?']
- t.options.should == {:hi => :mum}
- end
- trans = @obj.state_fu.transition( :run ) do
- @args = %w/ who yo daddy? /
- @options = {:hi => :mum}
-
- end
- trans.fire!()
+
+ it "should not be possible unless it's made less confusing" do
+ pending "wtf is the use case?"
end
end
end # args with fire!
describe "next_transition" do
describe "when there are multiple events but only one is fireable?" do
before do
- pending
reset!
make_pristine_class("Klass")
@machine = Klass.state_fu_machine do
initial_state :alive do
event :impossibility do
@@ -952,12 +908,10 @@
end
end
@obj = Klass.new()
@binding = @obj.state_fu
@binding.events.length.should == 2
- #@machine.events[:impossibility].fireable_by?( @binding ).should == false
- #@machine.events[:inevitability].fireable_by?( @binding ).should == true
end
describe "when the fireable? event has only one target" do
it "should return a transition for the fireable event & its target" do
@machine.events[:inevitability].targets.length.should == 1
@@ -1020,11 +974,11 @@
it "should not return a transition" do
t = @binding.next_transition
t.should be_nil
end
- it "should raise an IllegalTransition if next! is called" do
- lambda { @binding.next! }.should raise_error( StateFu::IllegalTransition )
+ it "should raise TransitionNotFound if next! is called" do
+ lambda { @binding.next! }.should raise_error( StateFu::TransitionNotFound )
end
end
end
end