Skip to content

Commit

Permalink
Merge pull request svenfuchs#6 from j-wilkins/from_array
Browse files Browse the repository at this point in the history
allow passing an array as a :from option
  • Loading branch information
Sven Fuchs committed Apr 22, 2012
2 parents c564e95 + 7f17581 commit ed05a3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/simple_states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def add_states(*states)
end

def event(name, options = {})
add_states(options[:from], options[:to])
add_states(*options[:from], options[:to])
self.events += [Event.new(name, options)]
end
end
Expand Down
30 changes: 28 additions & 2 deletions test/assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AssertionsTest < Test::Unit::TestCase
end
end

test "does not raise an exception if an event is received when the object is in one of the expected states (multiple :from states)" do
test "does not raise an exception if an event is received when the object is in one of the expected states (multiple :from states using :all)" do
klass = create_class do
event :error, :from => :started, :to => :errored
event :all, :from => :warning
Expand All @@ -30,6 +30,19 @@ class AssertionsTest < Test::Unit::TestCase
end
end

test "does not raise an exception if an event is received when the object is in one of the expected states (multiple :from states using from: [])" do
klass = create_class do
event :error, :from => [:started, :warning], :to => :errored
end

object = klass.new
object.state = :warning

assert_nothing_raised(SimpleStates::TransitionException) do
object.error
end
end

test "raises an exception if an event is received when the object is not in the expected state (single :from state)" do
klass = create_class do
event :error, :from => :started, :to => :errored
Expand All @@ -43,7 +56,7 @@ class AssertionsTest < Test::Unit::TestCase
end
end

test "raises an exception if an event is received when the object is not in any of the expected states (multiple :from states)" do
test "raises an exception if an event is received when the object is not in any of the expected states (multiple :from states using :all)" do
klass = create_class do
event :error, :from => :started, :to => :errored
event :all, :from => :warning
Expand All @@ -56,5 +69,18 @@ class AssertionsTest < Test::Unit::TestCase
object.error
end
end

test "raises an exception if an event is received when the object is not in any of the expected states (multiple :from states using from: [])" do
klass = create_class do
event :error, :from => [:started, :warning], :to => :errored
end

object = klass.new
object.state = :initialized

assert_raises(SimpleStates::TransitionException) do
object.error
end
end
end

0 comments on commit ed05a3e

Please sign in to comment.