state_machine 0.7.2 → 0.7.3
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.
- data/CHANGELOG.rdoc +8 -0
- data/README.rdoc +2 -2
- data/Rakefile +1 -1
- data/lib/state_machine/event_collection.rb +2 -2
- data/lib/state_machine/integrations/active_record.rb +4 -0
- data/lib/state_machine/integrations/data_mapper.rb +15 -2
- data/lib/state_machine/integrations/sequel.rb +9 -0
- data/lib/state_machine/machine.rb +7 -7
- data/lib/state_machine/state.rb +1 -1
- data/lib/state_machine/state_collection.rb +26 -7
- data/test/active_record.log +578 -198416
- data/test/sequel.log +1 -53150
- data/test/unit/callback_test.rb +1 -1
- data/test/unit/event_collection_test.rb +35 -0
- data/test/unit/event_test.rb +1 -1
- data/test/unit/guard_test.rb +1 -1
- data/test/unit/integrations/active_record_test.rb +23 -0
- data/test/unit/integrations/data_mapper_test.rb +25 -13
- data/test/unit/integrations/sequel_test.rb +23 -0
- data/test/unit/machine_test.rb +11 -1
- data/test/unit/state_collection_test.rb +12 -2
- metadata +2 -2
data/test/unit/callback_test.rb
CHANGED
|
@@ -19,7 +19,7 @@ class CallbackTest < Test::Unit::TestCase
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def test_should_not_raise_exception_if_implicit_option_specified
|
|
22
|
-
assert_nothing_raised { StateMachine::Callback.new(:do => :run, :invalid =>
|
|
22
|
+
assert_nothing_raised { StateMachine::Callback.new(:do => :run, :invalid => :valid) }
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def test_should_not_bind_to_objects
|
|
@@ -168,6 +168,41 @@ class EventCollectionAttributeWithMachineActionTest < Test::Unit::TestCase
|
|
|
168
168
|
end
|
|
169
169
|
end
|
|
170
170
|
|
|
171
|
+
class EventCollectionAttributeWithNamespacedMachineTest < Test::Unit::TestCase
|
|
172
|
+
def setup
|
|
173
|
+
@klass = Class.new do
|
|
174
|
+
def save
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
@machine = StateMachine::Machine.new(@klass, :namespace => 'alarm', :initial => :active, :action => :save)
|
|
179
|
+
@events = StateMachine::EventCollection.new(@machine)
|
|
180
|
+
|
|
181
|
+
@machine.event :disable
|
|
182
|
+
@machine.state :active, :off
|
|
183
|
+
@events << @disable = StateMachine::Event.new(@machine, :disable)
|
|
184
|
+
|
|
185
|
+
@object = @klass.new
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def test_should_not_have_transition_if_nil
|
|
189
|
+
@object.state_event = nil
|
|
190
|
+
assert_nil @events.attribute_transition_for(@object)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def test_should_have_invalid_transition_if_event_cannot_be_fired
|
|
194
|
+
@object.state_event = 'disable'
|
|
195
|
+
assert_equal false, @events.attribute_transition_for(@object)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def test_should_have_valid_transition_if_event_can_be_fired
|
|
199
|
+
@disable.transition :active => :off
|
|
200
|
+
@object.state_event = 'disable'
|
|
201
|
+
|
|
202
|
+
assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
171
206
|
class EventCollectionWithValidationsTest < Test::Unit::TestCase
|
|
172
207
|
def setup
|
|
173
208
|
StateMachine::Integrations.const_set('Custom', Module.new do
|
data/test/unit/event_test.rb
CHANGED
|
@@ -193,7 +193,7 @@ class EventTransitionsTest < Test::Unit::TestCase
|
|
|
193
193
|
end
|
|
194
194
|
|
|
195
195
|
def test_should_not_raise_exception_if_implicit_option_specified
|
|
196
|
-
assert_nothing_raised {@event.transition(:invalid =>
|
|
196
|
+
assert_nothing_raised {@event.transition(:invalid => :valid)}
|
|
197
197
|
end
|
|
198
198
|
|
|
199
199
|
def test_should_not_allow_on_option
|
data/test/unit/guard_test.rb
CHANGED
|
@@ -6,7 +6,7 @@ class GuardTest < Test::Unit::TestCase
|
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
def test_should_not_raise_exception_if_implicit_option_specified
|
|
9
|
-
assert_nothing_raised { StateMachine::Guard.new(:invalid =>
|
|
9
|
+
assert_nothing_raised { StateMachine::Guard.new(:invalid => :valid) }
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def test_should_not_have_an_if_condition
|
|
@@ -526,6 +526,29 @@ begin
|
|
|
526
526
|
end
|
|
527
527
|
end
|
|
528
528
|
|
|
529
|
+
class MachineWithValidationsTest < ActiveRecord::TestCase
|
|
530
|
+
def setup
|
|
531
|
+
@model = new_model
|
|
532
|
+
@machine = StateMachine::Machine.new(@model)
|
|
533
|
+
@machine.state :parked
|
|
534
|
+
|
|
535
|
+
@record = @model.new
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
def test_should_be_valid_if_state_is_known
|
|
539
|
+
@record.state = 'parked'
|
|
540
|
+
|
|
541
|
+
assert @record.valid?
|
|
542
|
+
end
|
|
543
|
+
|
|
544
|
+
def test_should_not_be_valid_if_state_is_unknown
|
|
545
|
+
@record.state = 'invalid'
|
|
546
|
+
|
|
547
|
+
assert !@record.valid?
|
|
548
|
+
assert_equal ['State is invalid'], @record.errors.full_messages
|
|
549
|
+
end
|
|
550
|
+
end
|
|
551
|
+
|
|
529
552
|
class MachineWithStateDrivenValidationsTest < ActiveRecord::TestCase
|
|
530
553
|
def setup
|
|
531
554
|
@model = new_model do
|
|
@@ -4,7 +4,7 @@ begin
|
|
|
4
4
|
# Load library
|
|
5
5
|
require 'rubygems'
|
|
6
6
|
|
|
7
|
-
gem 'dm-core', ENV['DM_VERSION'] ? "=#{ENV['DM_VERSION']}" : '>=0.9.
|
|
7
|
+
gem 'dm-core', ENV['DM_VERSION'] ? "=#{ENV['DM_VERSION']}" : '>=0.9.4'
|
|
8
8
|
require 'dm-core'
|
|
9
9
|
|
|
10
10
|
# Establish database connection
|
|
@@ -324,7 +324,7 @@ begin
|
|
|
324
324
|
end
|
|
325
325
|
|
|
326
326
|
begin
|
|
327
|
-
gem 'dm-observer', ENV['DM_VERSION'] ? "=#{ENV['DM_VERSION']}" : '>=0.9.
|
|
327
|
+
gem 'dm-observer', ENV['DM_VERSION'] ? "=#{ENV['DM_VERSION']}" : '>=0.9.4'
|
|
328
328
|
require 'dm-observer'
|
|
329
329
|
|
|
330
330
|
class MachineWithObserversTest < BaseTestCase
|
|
@@ -524,7 +524,7 @@ begin
|
|
|
524
524
|
end
|
|
525
525
|
|
|
526
526
|
begin
|
|
527
|
-
gem 'dm-validations', ENV['DM_VERSION'] ? "=#{ENV['DM_VERSION']}" : '>=0.9.
|
|
527
|
+
gem 'dm-validations', ENV['DM_VERSION'] ? "=#{ENV['DM_VERSION']}" : '>=0.9.4'
|
|
528
528
|
require 'dm-validations'
|
|
529
529
|
|
|
530
530
|
class MachineWithValidationsTest < BaseTestCase
|
|
@@ -532,24 +532,36 @@ begin
|
|
|
532
532
|
@resource = new_resource
|
|
533
533
|
@machine = StateMachine::Machine.new(@resource)
|
|
534
534
|
@machine.state :parked
|
|
535
|
+
|
|
536
|
+
@record = @resource.new
|
|
535
537
|
end
|
|
536
538
|
|
|
537
539
|
def test_should_invalidate_using_errors
|
|
538
|
-
record =
|
|
539
|
-
record.state = 'parked'
|
|
540
|
-
|
|
541
|
-
@machine.invalidate(record, :state, :invalid_transition, [[:event, :park]])
|
|
540
|
+
@record.state = 'parked'
|
|
542
541
|
|
|
543
|
-
|
|
542
|
+
@machine.invalidate(@record, :state, :invalid_transition, [[:event, :park]])
|
|
543
|
+
assert_equal ['cannot transition via "park"'], @record.errors.on(:state)
|
|
544
544
|
end
|
|
545
545
|
|
|
546
546
|
def test_should_clear_errors_on_reset
|
|
547
|
-
record =
|
|
548
|
-
record.state
|
|
549
|
-
|
|
547
|
+
@record.state = 'parked'
|
|
548
|
+
@record.errors.add(:state, 'is invalid')
|
|
549
|
+
|
|
550
|
+
@machine.reset(@record)
|
|
551
|
+
assert_nil @record.errors.on(:id)
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
def test_should_be_valid_if_state_is_known
|
|
555
|
+
@record.state = 'parked'
|
|
550
556
|
|
|
551
|
-
@
|
|
552
|
-
|
|
557
|
+
assert @record.valid?
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
def test_should_not_be_valid_if_state_is_unknown
|
|
561
|
+
@record.state = 'invalid'
|
|
562
|
+
|
|
563
|
+
assert !@record.valid?
|
|
564
|
+
assert_equal ['is invalid'], @record.errors.on(:state)
|
|
553
565
|
end
|
|
554
566
|
end
|
|
555
567
|
|
|
@@ -336,6 +336,29 @@ begin
|
|
|
336
336
|
end
|
|
337
337
|
end
|
|
338
338
|
|
|
339
|
+
class MachineWithValidationsTest < BaseTestCase
|
|
340
|
+
def setup
|
|
341
|
+
@model = new_model
|
|
342
|
+
@machine = StateMachine::Machine.new(@model)
|
|
343
|
+
@machine.state :parked
|
|
344
|
+
|
|
345
|
+
@record = @model.new
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def test_should_be_valid_if_state_is_known
|
|
349
|
+
@record.state = 'parked'
|
|
350
|
+
|
|
351
|
+
assert @record.valid?
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def test_should_not_be_valid_if_state_is_unknown
|
|
355
|
+
@record.state = 'invalid'
|
|
356
|
+
|
|
357
|
+
assert !@record.valid?
|
|
358
|
+
assert_equal ['state is invalid'], @record.errors.full_messages
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
|
|
339
362
|
class MachineWithStateDrivenValidationsTest < BaseTestCase
|
|
340
363
|
def setup
|
|
341
364
|
@model = new_model do
|
data/test/unit/machine_test.rb
CHANGED
|
@@ -55,6 +55,12 @@ class MachineByDefaultTest < Test::Unit::TestCase
|
|
|
55
55
|
assert @machine.state(nil).initial
|
|
56
56
|
end
|
|
57
57
|
|
|
58
|
+
def test_should_generate_default_messages
|
|
59
|
+
assert_equal 'is invalid', @machine.generate_message(:invalid)
|
|
60
|
+
assert_equal 'cannot transition when parked', @machine.generate_message(:invalid_event, [[:state, :parked]])
|
|
61
|
+
assert_equal 'cannot transition via "park"', @machine.generate_message(:invalid_transition, [[:event, :park]])
|
|
62
|
+
end
|
|
63
|
+
|
|
58
64
|
def test_should_not_be_extended_by_the_active_record_integration
|
|
59
65
|
assert !(class << @machine; ancestors; end).include?(StateMachine::Integrations::ActiveRecord)
|
|
60
66
|
end
|
|
@@ -487,6 +493,10 @@ class MachineWithCustomInvalidationTest < Test::Unit::TestCase
|
|
|
487
493
|
@object.state = 'parked'
|
|
488
494
|
end
|
|
489
495
|
|
|
496
|
+
def test_generate_custom_message
|
|
497
|
+
assert_equal 'cannot park', @machine.generate_message(:invalid_transition, [[:event, :park]])
|
|
498
|
+
end
|
|
499
|
+
|
|
490
500
|
def test_use_custom_message
|
|
491
501
|
@machine.invalidate(@object, :state, :invalid_transition, [[:event, :park]])
|
|
492
502
|
assert_equal 'cannot park', @object.error
|
|
@@ -1273,7 +1283,7 @@ class MachineWithTransitionCallbacksTest < Test::Unit::TestCase
|
|
|
1273
1283
|
end
|
|
1274
1284
|
|
|
1275
1285
|
def test_should_not_raise_exception_if_implicit_option_specified
|
|
1276
|
-
assert_nothing_raised {@machine.before_transition :invalid =>
|
|
1286
|
+
assert_nothing_raised {@machine.before_transition :invalid => :valid, :do => lambda {}}
|
|
1277
1287
|
end
|
|
1278
1288
|
|
|
1279
1289
|
def test_should_raise_exception_if_method_not_specified
|
|
@@ -62,9 +62,19 @@ class StateCollectionTest < Test::Unit::TestCase
|
|
|
62
62
|
assert_equal @parked, @states.match(@object)
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
def
|
|
65
|
+
def test_should_find_bang_state_for_object_if_value_is_known
|
|
66
|
+
@object.state = 'parked'
|
|
67
|
+
assert_equal @parked, @states.match!(@object)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_should_not_find_state_for_object_with_unknown_value
|
|
71
|
+
@object.state = 'invalid'
|
|
72
|
+
assert_nil @states.match(@object)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_should_raise_exception_if_finding_bang_state_for_object_with_unknown_value
|
|
66
76
|
@object.state = 'invalid'
|
|
67
|
-
exception = assert_raise(ArgumentError) { @states.match(@object) }
|
|
77
|
+
exception = assert_raise(ArgumentError) { @states.match!(@object) }
|
|
68
78
|
assert_equal '"invalid" is not a known state value', exception.message
|
|
69
79
|
end
|
|
70
80
|
end
|
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.
|
|
4
|
+
version: 0.7.3
|
|
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-04-
|
|
12
|
+
date: 2009-04-25 00:00:00 -04:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|