test/unit/guard_test.rb in state_machine-0.5.1 vs test/unit/guard_test.rb in state_machine-0.5.2

- old
+ new

@@ -8,22 +8,37 @@ def test_should_raise_exception_if_invalid_option_specified exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:invalid => true) } assert_equal 'Invalid key(s): invalid', exception.message end - def test_should_have_requirements - expected = {:to => [:idling], :from => [:parked]} - assert_equal expected, @guard.requirements + def test_should_not_have_an_if_condition + assert_nil @guard.if_condition end + + def test_should_not_have_an_unless_condition + assert_nil @guard.unless_condition + end end class GuardWithNoRequirementsTest < Test::Unit::TestCase def setup @object = Object.new @guard = StateMachine::Guard.new end + def test_should_use_all_matcher_for_event_requirement + assert_equal StateMachine::AllMatcher.instance, @guard.event_requirement + end + + def test_should_use_all_matcher_for_from_state_requirement + assert_equal StateMachine::AllMatcher.instance, @guard.state_requirement[:from] + end + + def test_should_use_all_matcher_for_to_state_requirement + assert_equal StateMachine::AllMatcher.instance, @guard.state_requirement[:to] + end + def test_should_match_nil_query assert @guard.matches?(@object, nil) end def test_should_match_empty_query @@ -39,10 +54,14 @@ def setup @object = Object.new @guard = StateMachine::Guard.new(:from => :parked) end + def test_should_use_a_whitelist_matcher + assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirement[:from] + end + def test_should_match_if_not_specified assert @guard.matches?(@object, :to => :idling) end def test_should_match_if_included @@ -93,10 +112,14 @@ def setup @object = Object.new @guard = StateMachine::Guard.new(:to => :idling) end + def test_should_use_a_whitelist_matcher + assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirement[:to] + end + def test_should_match_if_not_specified assert @guard.matches?(@object, :from => :parked) end def test_should_match_if_included @@ -147,10 +170,14 @@ def setup @object = Object.new @guard = StateMachine::Guard.new(:on => :ignite) end + def test_should_use_a_whitelist_matcher + assert_instance_of StateMachine::WhitelistMatcher, @guard.event_requirement + end + def test_should_match_if_not_specified assert @guard.matches?(@object, :from => :parked) end def test_should_match_if_included @@ -197,10 +224,14 @@ def setup @object = Object.new @guard = StateMachine::Guard.new(:except_from => :parked) end + def test_should_use_a_blacklist_matcher + assert_instance_of StateMachine::BlacklistMatcher, @guard.state_requirement[:from] + end + def test_should_match_if_not_included assert @guard.matches?(@object, :from => :idling) end def test_should_not_match_if_included @@ -247,10 +278,14 @@ def setup @object = Object.new @guard = StateMachine::Guard.new(:except_to => :idling) end + def test_should_use_a_blacklist_matcher + assert_instance_of StateMachine::BlacklistMatcher, @guard.state_requirement[:to] + end + def test_should_match_if_not_included assert @guard.matches?(@object, :to => :parked) end def test_should_not_match_if_included @@ -297,10 +332,14 @@ def setup @object = Object.new @guard = StateMachine::Guard.new(:except_on => :ignite) end + def test_should_use_a_blacklist_matcher + assert_instance_of StateMachine::BlacklistMatcher, @guard.event_requirement + end + def test_should_match_if_not_included assert @guard.matches?(@object, :on => :park) end def test_should_not_match_if_included @@ -338,40 +377,28 @@ assert !@guard.matches?(@object, :on => :ignite) end end class GuardWithConflictingFromRequirementsTest < Test::Unit::TestCase - def setup - @object = Object.new - @guard = StateMachine::Guard.new(:from => :parked, :except_from => :parked) + def test_should_raise_an_exception + exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:from => :parked, :except_from => :parked) } + assert_equal 'Conflicting keys: from, except_from', exception.message end - - def test_should_ignore_except_requirement - assert @guard.matches?(@object, :from => :parked) - end end class GuardWithConflictingToRequirementsTest < Test::Unit::TestCase - def setup - @object = Object.new - @guard = StateMachine::Guard.new(:to => :idling, :except_to => :idling) + def test_should_raise_an_exception + exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:to => :idling, :except_to => :idling) } + assert_equal 'Conflicting keys: to, except_to', exception.message end - - def test_should_ignore_except_requirement - assert @guard.matches?(@object, :to => :idling) - end end class GuardWithConflictingOnRequirementsTest < Test::Unit::TestCase - def setup - @object = Object.new - @guard = StateMachine::Guard.new(:on => :ignite, :except_on => :ignite) + def test_should_raise_an_exception + exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:on => :ignite, :except_on => :ignite) } + assert_equal 'Conflicting keys: on, except_on', exception.message end - - def test_should_ignore_except_requirement - assert @guard.matches?(@object, :on => :ignite) - end end class GuardWithDifferentRequirementsTest < Test::Unit::TestCase def setup @object = Object.new @@ -438,10 +465,15 @@ class GuardWithIfConditionalTest < Test::Unit::TestCase def setup @object = Object.new end + def test_should_have_an_if_condition + guard = StateMachine::Guard.new(:if => lambda {true}) + assert_not_nil guard.if_condition + end + def test_should_match_if_true guard = StateMachine::Guard.new(:if => lambda {true}) assert guard.matches?(@object) end @@ -454,10 +486,15 @@ class GuardWithUnlessConditionalTest < Test::Unit::TestCase def setup @object = Object.new end + def test_should_have_an_unless_condition + guard = StateMachine::Guard.new(:unless => lambda {true}) + assert_not_nil guard.unless_condition + end + def test_should_match_if_false guard = StateMachine::Guard.new(:unless => lambda {false}) assert guard.matches?(@object) end @@ -466,21 +503,12 @@ assert !guard.matches?(@object) end end class GuardWithConflictingConditionalsTest < Test::Unit::TestCase - def setup - @object = Object.new - end - - def test_should_match_if_true - guard = StateMachine::Guard.new(:if => lambda {true}, :unless => lambda {true}) - assert guard.matches?(@object) - end - - def test_should_not_match_if_false - guard = StateMachine::Guard.new(:if => lambda {false}, :unless => lambda {false}) - assert !guard.matches?(@object) + def test_should_raise_an_exception + exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:if => lambda {true}, :unless => lambda {true}) } + assert_equal 'Conflicting keys: if, unless', exception.message end end begin # Load library