Skip to content

Commit

Permalink
+ checking if into or exit got a string or block
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Jun 2, 2011
1 parent b9551b1 commit e0d8cf5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
12 changes: 8 additions & 4 deletions lib/james/state_api.rb
Expand Up @@ -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.
Expand Down
70 changes: 68 additions & 2 deletions test/unit/lib/james/state_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions test/unit/lib/james/visitor_test.rb
Expand Up @@ -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']
Expand Down

0 comments on commit e0d8cf5

Please sign in to comment.