From e0d8cf553a83bc4cd6a41e3a12f8e3019500affe Mon Sep 17 00:00:00 2001 From: Florian Hanke Date: Thu, 2 Jun 2011 11:44:40 +1000 Subject: [PATCH] + checking if into or exit got a string or block --- lib/james/state_api.rb | 12 +++-- test/unit/lib/james/state_test.rb | 70 ++++++++++++++++++++++++++++- test/unit/lib/james/visitor_test.rb | 6 --- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/lib/james/state_api.rb b/lib/james/state_api.rb index 6599faf..8258df7 100644 --- a/lib/james/state_api.rb +++ b/lib/james/state_api.rb @@ -48,14 +48,18 @@ def hear transitions # Execute this block when entering this state. # - def into &block - @into_block = block + def into text = nil, &block + @into_block = block || + text && lambda { text } || + raise(ArgumentError.new("Neither block nor text given to #{self.class}##{__method__} call of #{caller}.")) end # Execute this block when exiting this state. # - def exit &block - @exit_block = block + def exit text = nil, &block + @exit_block = block || + text && lambda { text } || + raise(ArgumentError.new("Neither block nor text given to #{self.class}##{__method__} call of #{caller}.")) end # By default, a state is not chainable. diff --git a/test/unit/lib/james/state_test.rb b/test/unit/lib/james/state_test.rb index fbdeac8..e560eb7 100644 --- a/test/unit/lib/james/state_test.rb +++ b/test/unit/lib/james/state_test.rb @@ -3,6 +3,9 @@ require File.expand_path '../../../../../lib/james/state_api', __FILE__ require File.expand_path '../../../../../lib/james/state_internals', __FILE__ +require 'minitest/autorun' +require 'minitest/unit' + describe James::State do before do @@ -50,10 +53,12 @@ def state_for name end describe 'expand' do it '' do - assert_equal state.expand([:a, :b] => 1), :a => 1, :b => 1 + expected = { :a => 1, :b => 1 } + assert_equal expected, state.expand([:a, :b] => 1) end it '' do - assert_equal state.expand(:a => 1), :a => 1 + expected = { :a => 1 } + assert_equal expected, state.expand(:a => 1) end end describe 'method __into__' do @@ -139,6 +144,67 @@ def state_for name end end + describe 'raise on into/exit ArgumentError' do + describe 'into' do + it 'raises' do + assert_raises ArgumentError do + James::State.new :some_name, @context do + hear 'transition one' => :next_state1 + into + end + end + end + end + describe 'exit' do + it 'raises' do + assert_raises ArgumentError do + James::State.new :some_name, @context do + hear 'transition one' => :next_state1 + exit + end + end + end + end + end + + describe 'with 1 transition and into and exit (both in text form)' do + before do + @state ||= James::State.new :some_name, @context do + hear 'transition one' => :next_state1 + into "hi there" + exit "good bye" + end + end + describe 'phrases' do + it '' do + assert_equal ['transition one'], state.phrases + end + end + describe 'to_s' do + it '' do + assert_equal 'James::State(some_name, some_context, {"transition one"=>:next_state1})', state.to_s + end + end + describe 'next_for' do + it '' do + assert_equal :some_state_object1, state.next_for('transition one') + end + it '' do + assert_nil state.next_for('non-existent') + end + end + describe 'method __into__' do + it 'is called' do + assert_equal 'hi there', state.__into__ + end + end + describe 'method __exit__' do + it 'is conditionally called' do + assert_equal 'good bye', state.__exit__ + end + end + end + describe 'with multiple transition and separate hears' do before do @state ||= James::State.new :some_name, @context do diff --git a/test/unit/lib/james/visitor_test.rb b/test/unit/lib/james/visitor_test.rb index 292ac7d..4313974 100644 --- a/test/unit/lib/james/visitor_test.rb +++ b/test/unit/lib/james/visitor_test.rb @@ -15,12 +15,6 @@ it 'works' do visitor.reset end - # it 'calls methods in order' do - # timer.should_receive(:stop).once.with - # visitor.should_receive(:current=).once.with initial - # - # visitor.reset - # end it 'survives a functional test' do next_state = MiniTest::Mock.new initial.expect :next_for, next_state, ['some phrase']