state_machine 0.7.5 → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,10 +11,18 @@ class MachineByDefaultTest < Test::Unit::TestCase
11
11
  assert_equal @klass, @machine.owner_class
12
12
  end
13
13
 
14
+ def test_should_have_a_name
15
+ assert_equal :state, @machine.name
16
+ end
17
+
14
18
  def test_should_have_an_attribute
15
19
  assert_equal :state, @machine.attribute
16
20
  end
17
21
 
22
+ def test_should_prefix_custom_attributes_with_attribute
23
+ assert_equal :state_event, @machine.attribute(:event)
24
+ end
25
+
18
26
  def test_should_have_an_initial_state
19
27
  assert_not_nil @machine.initial_state(@object)
20
28
  end
@@ -143,17 +151,25 @@ class MachineByDefaultTest < Test::Unit::TestCase
143
151
  end
144
152
  end
145
153
 
146
- class MachineWithCustomAttributeTest < Test::Unit::TestCase
154
+ class MachineWithCustomNameTest < Test::Unit::TestCase
147
155
  def setup
148
156
  @klass = Class.new
149
157
  @machine = StateMachine::Machine.new(@klass, :status)
150
158
  @object = @klass.new
151
159
  end
152
160
 
153
- def test_should_use_custom_attribute
161
+ def test_should_use_custom_name
162
+ assert_equal :status, @machine.name
163
+ end
164
+
165
+ def test_should_use_custom_name_for_attribute
154
166
  assert_equal :status, @machine.attribute
155
167
  end
156
168
 
169
+ def test_should_prefix_custom_attributes_with_custom_name
170
+ assert_equal :status_event, @machine.attribute(:event)
171
+ end
172
+
157
173
  def test_should_define_a_reader_attribute_for_the_attribute
158
174
  assert @object.respond_to?(:status)
159
175
  end
@@ -998,21 +1014,46 @@ end
998
1014
 
999
1015
  class MachinePersistenceTest < Test::Unit::TestCase
1000
1016
  def setup
1001
- @klass = Class.new
1017
+ @klass = Class.new do
1018
+ attr_accessor :state_event
1019
+ end
1002
1020
  @machine = StateMachine::Machine.new(@klass, :initial => :parked)
1003
1021
  @object = @klass.new
1004
1022
  end
1005
1023
 
1006
1024
  def test_should_allow_reading_state
1007
- assert_equal 'parked', @machine.read(@object)
1025
+ assert_equal 'parked', @machine.read(@object, :state)
1026
+ end
1027
+
1028
+ def test_should_allow_reading_custom_attributes
1029
+ assert_nil @machine.read(@object, :event)
1030
+
1031
+ @object.state_event = 'ignite'
1032
+ assert_equal 'ignite', @machine.read(@object, :event)
1033
+ end
1034
+
1035
+ def test_should_allow_reading_custom_instance_variables
1036
+ @klass.class_eval do
1037
+ attr_writer :state_value
1038
+ end
1039
+
1040
+ @object.state_value = 1
1041
+ assert_raise(NoMethodError) { @machine.read(@object, :value) }
1042
+ assert_equal 1, @machine.read(@object, :value, true)
1008
1043
  end
1009
1044
 
1010
1045
  def test_should_allow_writing_state
1011
- @machine.write(@object, 'idling')
1046
+ @machine.write(@object, :state, 'idling')
1012
1047
  assert_equal 'idling', @object.state
1013
1048
  end
1049
+
1050
+ def test_should_allow_writing_custom_attributes
1051
+ @machine.write(@object, :event, 'ignite')
1052
+ assert_equal 'ignite', @object.state_event
1053
+ end
1014
1054
  end
1015
1055
 
1056
+
1016
1057
  class MachineWithStatesTest < Test::Unit::TestCase
1017
1058
  def setup
1018
1059
  @klass = Class.new
@@ -1445,6 +1486,40 @@ class MachineWithExistingMachinesOnOwnerClassTest < Test::Unit::TestCase
1445
1486
  end
1446
1487
  end
1447
1488
 
1489
+ class MachineWithExistingMachinesWithSameAttributesOnOwnerClassTest < Test::Unit::TestCase
1490
+ def setup
1491
+ @klass = Class.new
1492
+ @machine = StateMachine::Machine.new(@klass, :initial => :parked)
1493
+ @second_machine = StateMachine::Machine.new(@klass, :public_state, :attribute => :state)
1494
+ @object = @klass.new
1495
+ end
1496
+
1497
+ def test_should_track_each_state_machine
1498
+ expected = {:state => @machine, :public_state => @second_machine}
1499
+ assert_equal expected, @klass.state_machines
1500
+ end
1501
+
1502
+ def test_should_initialize_based_on_first_available_initial_state
1503
+ assert_equal 'parked', @object.state
1504
+ end
1505
+
1506
+ def test_should_allow_transitions_on_both_machines
1507
+ @machine.event :ignite do
1508
+ transition :parked => :idling
1509
+ end
1510
+
1511
+ @second_machine.event :park do
1512
+ transition :idling => :parked
1513
+ end
1514
+
1515
+ @object.ignite
1516
+ assert_equal 'idling', @object.state
1517
+
1518
+ @object.park
1519
+ assert_equal 'parked', @object.state
1520
+ end
1521
+ end
1522
+
1448
1523
  class MachineWithNamespaceTest < Test::Unit::TestCase
1449
1524
  def setup
1450
1525
  @klass = Class.new
@@ -1491,6 +1566,80 @@ class MachineWithNamespaceTest < Test::Unit::TestCase
1491
1566
  end
1492
1567
  end
1493
1568
 
1569
+ class MachineWithCustomAttributeTest < Test::Unit::TestCase
1570
+ def setup
1571
+ StateMachine::Integrations.const_set('Custom', Module.new do
1572
+ class << self; attr_reader :defaults; end
1573
+ @defaults = {:action => :save, :use_transactions => false}
1574
+
1575
+ def create_with_scope(name)
1576
+ lambda {}
1577
+ end
1578
+
1579
+ def create_without_scope(name)
1580
+ lambda {}
1581
+ end
1582
+ end)
1583
+
1584
+ @klass = Class.new
1585
+ @machine = StateMachine::Machine.new(@klass, :state, :attribute => :state_id, :initial => :active, :integration => :custom) do
1586
+ event :ignite do
1587
+ transition :parked => :idling
1588
+ end
1589
+ end
1590
+ @object = @klass.new
1591
+ end
1592
+
1593
+ def test_should_define_a_reader_attribute_for_the_attribute
1594
+ assert @object.respond_to?(:state_id)
1595
+ end
1596
+
1597
+ def test_should_define_a_writer_attribute_for_the_attribute
1598
+ assert @object.respond_to?(:state_id=)
1599
+ end
1600
+
1601
+ def test_should_define_a_predicate_for_the_attribute
1602
+ assert @object.respond_to?(:state?)
1603
+ end
1604
+
1605
+ def test_should_define_a_name_reader_for_the_attribute
1606
+ assert @object.respond_to?(:state_name)
1607
+ end
1608
+
1609
+ def test_should_define_an_event_reader_for_the_attribute
1610
+ assert @object.respond_to?(:state_events)
1611
+ end
1612
+
1613
+ def test_should_define_a_transition_reader_for_the_attribute
1614
+ assert @object.respond_to?(:state_transitions)
1615
+ end
1616
+
1617
+ def test_should_define_singular_with_scope
1618
+ assert @klass.respond_to?(:with_state)
1619
+ end
1620
+
1621
+ def test_should_define_singular_without_scope
1622
+ assert @klass.respond_to?(:without_state)
1623
+ end
1624
+
1625
+ def test_should_define_plural_with_scope
1626
+ assert @klass.respond_to?(:with_states)
1627
+ end
1628
+
1629
+ def test_should_define_plural_without_scope
1630
+ assert @klass.respond_to?(:without_states)
1631
+ end
1632
+
1633
+ def test_should_define_state_machines_reader
1634
+ expected = {:state => @machine}
1635
+ assert_equal expected, @klass.state_machines
1636
+ end
1637
+
1638
+ def teardown
1639
+ StateMachine::Integrations.send(:remove_const, 'Custom')
1640
+ end
1641
+ end
1642
+
1494
1643
  class MachineFinderWithoutExistingMachineTest < Test::Unit::TestCase
1495
1644
  def setup
1496
1645
  @klass = Class.new
@@ -258,7 +258,8 @@ class StateWithCachedLambdaValueTest < Test::Unit::TestCase
258
258
  def setup
259
259
  @klass = Class.new
260
260
  @machine = StateMachine::Machine.new(@klass)
261
- @state = StateMachine::State.new(@machine, :parked, :value => lambda {Object.new}, :cache => true)
261
+ @dynamic_value = lambda {'value'}
262
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => @dynamic_value, :cache => true)
262
263
  end
263
264
 
264
265
  def test_should_be_caching
@@ -266,20 +267,27 @@ class StateWithCachedLambdaValueTest < Test::Unit::TestCase
266
267
  end
267
268
 
268
269
  def test_should_evaluate_value
269
- assert_instance_of Object, @state.value
270
+ assert_equal 'value', @state.value
270
271
  end
271
272
 
272
273
  def test_should_only_evaluate_value_once
273
274
  value = @state.value
274
275
  assert_same value, @state.value
275
276
  end
277
+
278
+ def test_should_update_value_index_for_state_collection
279
+ @state.value
280
+ assert_equal @state, @machine.states['value', :value]
281
+ assert_nil @machine.states[@dynamic_value, :value]
282
+ end
276
283
  end
277
284
 
278
285
  class StateWithoutCachedLambdaValueTest < Test::Unit::TestCase
279
286
  def setup
280
287
  @klass = Class.new
281
288
  @machine = StateMachine::Machine.new(@klass)
282
- @state = StateMachine::State.new(@machine, :parked, :value => lambda {Object.new})
289
+ @dynamic_value = lambda {'value'}
290
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => @dynamic_value)
283
291
  end
284
292
 
285
293
  def test_should_not_be_caching
@@ -290,6 +298,12 @@ class StateWithoutCachedLambdaValueTest < Test::Unit::TestCase
290
298
  value = @state.value
291
299
  assert_not_same value, @state.value
292
300
  end
301
+
302
+ def test_should_not_update_value_index_for_state_collection
303
+ @state.value
304
+ assert_nil @machine.states['value', :value]
305
+ assert_equal @state, @machine.states[@dynamic_value, :value]
306
+ end
293
307
  end
294
308
 
295
309
  class StateWithMatcherTest < Test::Unit::TestCase
@@ -197,6 +197,33 @@ class TransitionWithNamespaceTest < Test::Unit::TestCase
197
197
  end
198
198
  end
199
199
 
200
+ class TransitionWithCustomMachineAttributeTest < Test::Unit::TestCase
201
+ def setup
202
+ @klass = Class.new
203
+ @machine = StateMachine::Machine.new(@klass, :state, :attribute => :state_id)
204
+ @machine.state :off, :value => 1
205
+ @machine.state :active, :value => 2
206
+ @machine.event :activate
207
+
208
+ @object = @klass.new
209
+ @object.state_id = 1
210
+
211
+ @transition = StateMachine::Transition.new(@object, @machine, :activate, :off, :active)
212
+ end
213
+
214
+ def test_should_persist
215
+ @transition.persist
216
+ assert_equal 2, @object.state_id
217
+ end
218
+
219
+ def test_should_rollback
220
+ @object.state_id = 2
221
+ @transition.rollback
222
+
223
+ assert_equal 1, @object.state_id
224
+ end
225
+ end
226
+
200
227
  class TransitionWithActionTest < Test::Unit::TestCase
201
228
  def setup
202
229
  @klass = Class.new do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-25 00:00:00 -04:00
12
+ date: 2009-06-17 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15