test/unit/machine_test.rb in pluginaweek-state_machine-0.7.6 vs test/unit/machine_test.rb in pluginaweek-state_machine-0.8.0

- old
+ new

@@ -149,26 +149,26 @@ expected = {:state => @machine} assert_equal expected, @klass.state_machines end end -class MachineWithCustomAttributeTest < Test::Unit::TestCase +class MachineWithCustomNameTest < Test::Unit::TestCase def setup @klass = Class.new @machine = StateMachine::Machine.new(@klass, :status) @object = @klass.new end - def test_should_use_custom_attribute_for_name + def test_should_use_custom_name assert_equal :status, @machine.name end - def test_should_use_custom_attribute + def test_should_use_custom_name_for_attribute assert_equal :status, @machine.attribute end - def test_should_prefix_custom_attributes_with_custom_attribute + def test_should_prefix_custom_attributes_with_custom_name assert_equal :status_event, @machine.attribute(:event) end def test_should_define_a_reader_attribute_for_the_attribute assert @object.respond_to?(:status) @@ -205,10 +205,14 @@ end @machine = StateMachine::Machine.new(@klass, :initial => :parked) end + def test_should_not_have_dynamic_initial_state + assert !@machine.dynamic_initial_state? + end + def test_should_have_an_initial_state object = @klass.new assert_equal 'parked', @machine.initial_state(object).value end @@ -229,10 +233,24 @@ def test_should_not_set_initial_state_if_existing_is_not_empty object = @klass.new(:state => 'idling') assert_equal 'idling', object.state end + def test_should_set_initial_state_prior_to_initialization + base = Class.new do + attr_accessor :state_on_init + + def initialize + self.state_on_init = state + end + end + klass = Class.new(base) + machine = StateMachine::Machine.new(klass, :initial => :parked) + + assert_equal 'parked', klass.new.state_on_init + end + def test_should_be_included_in_known_states assert_equal [:parked], @machine.states.keys end end @@ -244,10 +262,14 @@ @machine = StateMachine::Machine.new(@klass, :initial => lambda {|object| object.initial_state || :default}) @machine.state :parked, :idling, :default @object = @klass.new end + def test_should_have_dynamic_initial_state + assert @machine.dynamic_initial_state? + end + def test_should_use_the_record_for_determining_the_initial_state @object.initial_state = :parked assert_equal :parked, @machine.initial_state(@object).name @object.initial_state = :idling @@ -256,10 +278,25 @@ def test_should_set_initial_state_on_created_object assert_equal 'default', @object.state end + def test_should_set_initial_state_after_initialization + base = Class.new do + attr_accessor :state_on_init + + def initialize + self.state_on_init = state + end + end + klass = Class.new(base) + machine = StateMachine::Machine.new(klass, :initial => lambda {|object| :parked}) + machine.state :parked + + assert_nil klass.new.state_on_init + end + def test_should_not_be_included_in_known_states assert_equal [:parked, :idling, :default], @machine.states.map {|state| state.name} end end @@ -1373,10 +1410,28 @@ @event.fire(@object) assert_equal %w(before after), @object.callbacks end + def test_should_allow_multiple_callbacks + @machine.before_transition lambda {|object| object.callbacks << 'before1'}, lambda {|object| object.callbacks << 'before2'} + @machine.after_transition lambda {|object| object.callbacks << 'after1'}, lambda {|object| object.callbacks << 'after2'} + + @event.fire(@object) + assert_equal %w(before1 before2 after1 after2), @object.callbacks + end + + def test_should_allow_multiple_callbacks_with_requirements + @machine.before_transition lambda {|object| object.callbacks << 'before_parked1'}, lambda {|object| object.callbacks << 'before_parked2'}, :from => :parked + @machine.before_transition lambda {|object| object.callbacks << 'before_idling1'}, lambda {|object| object.callbacks << 'before_idling2'}, :from => :idling + @machine.after_transition lambda {|object| object.callbacks << 'after_parked1'}, lambda {|object| object.callbacks << 'after_parked2'}, :from => :parked + @machine.after_transition lambda {|object| object.callbacks << 'after_idling1'}, lambda {|object| object.callbacks << 'after_idling2'}, :from => :idling + + @event.fire(@object) + assert_equal %w(before_parked1 before_parked2 after_parked1 after_parked2), @object.callbacks + end + def test_should_support_from_requirement @machine.before_transition :from => :parked, :do => lambda {|object| object.callbacks << :parked} @machine.before_transition :from => :idling, :do => lambda {|object| object.callbacks << :idling} @event.fire(@object) @@ -1484,10 +1539,44 @@ assert_equal 'parked', @object.state assert_equal 'idling', @object.status end end +class MachineWithExistingMachinesWithSameAttributesOnOwnerClassTest < Test::Unit::TestCase + def setup + @klass = Class.new + @machine = StateMachine::Machine.new(@klass, :initial => :parked) + @second_machine = StateMachine::Machine.new(@klass, :public_state, :attribute => :state) + @object = @klass.new + end + + def test_should_track_each_state_machine + expected = {:state => @machine, :public_state => @second_machine} + assert_equal expected, @klass.state_machines + end + + def test_should_initialize_based_on_first_available_initial_state + assert_equal 'parked', @object.state + end + + def test_should_allow_transitions_on_both_machines + @machine.event :ignite do + transition :parked => :idling + end + + @second_machine.event :park do + transition :idling => :parked + end + + @object.ignite + assert_equal 'idling', @object.state + + @object.park + assert_equal 'parked', @object.state + end +end + class MachineWithNamespaceTest < Test::Unit::TestCase def setup @klass = Class.new @machine = StateMachine::Machine.new(@klass, :namespace => 'alarm', :initial => :active) do event :enable do @@ -1530,11 +1619,11 @@ assert @object.respond_to?(name) end end end -class MachineWithCustomNameTest < Test::Unit::TestCase +class MachineWithCustomAttributeTest < Test::Unit::TestCase def setup StateMachine::Integrations.const_set('Custom', Module.new do class << self; attr_reader :defaults; end @defaults = {:action => :save, :use_transactions => false} @@ -1546,24 +1635,24 @@ lambda {} end end) @klass = Class.new - @machine = StateMachine::Machine.new(@klass, :state_id, :as => 'state', :initial => :active, :integration => :custom) do + @machine = StateMachine::Machine.new(@klass, :state, :attribute => :state_id, :initial => :active, :integration => :custom) do event :ignite do transition :parked => :idling end end @object = @klass.new end - def test_should_not_define_a_reader_attribute_for_the_attribute - assert !@object.respond_to?(:state) + def test_should_define_a_reader_attribute_for_the_attribute + assert @object.respond_to?(:state_id) end - def test_should_not_define_a_writer_attribute_for_the_attribute - assert !@object.respond_to?(:state=) + def test_should_define_a_writer_attribute_for_the_attribute + assert @object.respond_to?(:state_id=) end def test_should_define_a_predicate_for_the_attribute assert @object.respond_to?(:state?) end @@ -1592,9 +1681,14 @@ assert @klass.respond_to?(:with_states) end def test_should_define_plural_without_scope assert @klass.respond_to?(:without_states) + end + + def test_should_define_state_machines_reader + expected = {:state => @machine} + assert_equal expected, @klass.state_machines end def teardown StateMachine::Integrations.send(:remove_const, 'Custom') end