state_machine 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -307,6 +307,35 @@ begin
307
307
  assert_equal [1, 2, 3], @record.callback_result
308
308
  end
309
309
  end
310
+
311
+ class MachineWithStateDrivenValidationsTest < BaseTestCase
312
+ def setup
313
+ @model = new_model do
314
+ attr_accessor :seatbelt
315
+ end
316
+
317
+ @machine = StateMachine::Machine.new(@model)
318
+ @machine.state :first_gear do
319
+ validates_presence_of :seatbelt
320
+ end
321
+ @machine.other_states :parked
322
+ end
323
+
324
+ def test_should_be_valid_if_validation_fails_outside_state_scope
325
+ record = @model.new(:state => 'parked', :seatbelt => nil)
326
+ assert record.valid?
327
+ end
328
+
329
+ def test_should_be_invalid_if_validation_fails_within_state_scope
330
+ record = @model.new(:state => 'first_gear', :seatbelt => nil)
331
+ assert !record.valid?
332
+ end
333
+
334
+ def test_should_be_valid_if_validation_succeeds_within_state_scope
335
+ record = @model.new(:state => 'first_gear', :seatbelt => true)
336
+ assert record.valid?
337
+ end
338
+ end
310
339
  end
311
340
  rescue LoadError
312
341
  $stderr.puts 'Skipping Sequel tests. `gem install sequel` and try again.'
@@ -373,6 +373,16 @@ class MachineTest < Test::Unit::TestCase
373
373
 
374
374
  assert called
375
375
  end
376
+
377
+ def test_should_provide_matcher_helpers_during_initialization
378
+ matchers = []
379
+
380
+ StateMachine::Machine.new(Class.new) do
381
+ matchers = [all, any, same]
382
+ end
383
+
384
+ assert_equal [StateMachine::AllMatcher.instance, StateMachine::AllMatcher.instance, StateMachine::LoopbackMatcher.instance], matchers
385
+ end
376
386
  end
377
387
 
378
388
  class MachineAfterBeingCopiedTest < Test::Unit::TestCase
@@ -927,6 +937,11 @@ class MachineWithEventsTest < Test::Unit::TestCase
927
937
  assert responded
928
938
  end
929
939
 
940
+ def test_should_be_aliased_as_on
941
+ event = @machine.on(:ignite) {}
942
+ assert_equal :ignite, event.name
943
+ end
944
+
930
945
  def test_should_have_events
931
946
  event = @machine.event(:ignite)
932
947
  assert_equal [event], @machine.events.to_a
@@ -1029,9 +1044,8 @@ class MachineWithTransitionCallbacksTest < Test::Unit::TestCase
1029
1044
  @object.callbacks = []
1030
1045
  end
1031
1046
 
1032
- def test_should_raise_exception_if_invalid_option_specified
1033
- exception = assert_raise(ArgumentError) {@machine.before_transition :invalid => true, :do => lambda {}}
1034
- assert_equal 'Invalid key(s): invalid', exception.message
1047
+ def test_should_not_raise_exception_if_implicit_option_specified
1048
+ assert_nothing_raised {@machine.before_transition :invalid => true, :do => lambda {}}
1035
1049
  end
1036
1050
 
1037
1051
  def test_should_raise_exception_if_do_option_not_specified
@@ -1095,6 +1109,14 @@ class MachineWithTransitionCallbacksTest < Test::Unit::TestCase
1095
1109
  assert_equal [:park], @object.callbacks
1096
1110
  end
1097
1111
 
1112
+ def test_should_support_implicit_requirement
1113
+ @machine.before_transition :parked => :idling, :do => lambda {|object| object.callbacks << :parked}
1114
+ @machine.before_transition :idling => :parked, :do => lambda {|object| object.callbacks << :idling}
1115
+
1116
+ @event.fire(@object)
1117
+ assert_equal [:parked], @object.callbacks
1118
+ end
1119
+
1098
1120
  def test_should_track_states_defined_in_transition_callbacks
1099
1121
  @machine.before_transition :from => :parked, :to => :idling, :do => lambda {}
1100
1122
  @machine.after_transition :from => :first_gear, :to => :second_gear, :do => lambda {}
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class MatcherHelpersAllTest < Test::Unit::TestCase
4
+ include StateMachine::MatcherHelpers
5
+
6
+ def setup
7
+ @matcher = all
8
+ end
9
+
10
+ def test_should_build_an_all_matcher
11
+ assert_equal StateMachine::AllMatcher.instance, @matcher
12
+ end
13
+ end
14
+
15
+ class MatcherHelpersAnyTest < Test::Unit::TestCase
16
+ include StateMachine::MatcherHelpers
17
+
18
+ def setup
19
+ @matcher = any
20
+ end
21
+
22
+ def test_should_build_an_all_matcher
23
+ assert_equal StateMachine::AllMatcher.instance, @matcher
24
+ end
25
+ end
26
+
27
+ class MatcherHelpersSameTest < Test::Unit::TestCase
28
+ include StateMachine::MatcherHelpers
29
+
30
+ def setup
31
+ @matcher = same
32
+ end
33
+
34
+ def test_should_build_a_loopback_matcher
35
+ assert_equal StateMachine::LoopbackMatcher.instance, @matcher
36
+ end
37
+ end
@@ -6,10 +6,6 @@ class NodeCollectionByDefaultTest < Test::Unit::TestCase
6
6
  end
7
7
 
8
8
  def test_should_not_have_any_nodes
9
- assert !@collection.any?
10
- end
11
-
12
- def test_should_have_a_zero_length
13
9
  assert_equal 0, @collection.length
14
10
  end
15
11
 
@@ -6,10 +6,6 @@ class StateCollectionByDefaultTest < Test::Unit::TestCase
6
6
  end
7
7
 
8
8
  def test_should_not_have_any_nodes
9
- assert !@states.any?
10
- end
11
-
12
- def test_should_have_a_zero_length
13
9
  assert_equal 0, @states.length
14
10
  end
15
11
 
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.5.2
4
+ version: 0.6.0
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-02-17 00:00:00 -05:00
12
+ date: 2009-03-03 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -44,6 +44,7 @@ files:
44
44
  - lib/state_machine/integrations/active_record.rb
45
45
  - lib/state_machine/integrations/data_mapper.rb
46
46
  - lib/state_machine/eval_helpers.rb
47
+ - lib/state_machine/matcher_helpers.rb
47
48
  - lib/state_machine/transition.rb
48
49
  - lib/state_machine/callback.rb
49
50
  - lib/state_machine/extensions.rb
@@ -51,6 +52,7 @@ files:
51
52
  - lib/state_machine/state_collection.rb
52
53
  - lib/state_machine/assertions.rb
53
54
  - lib/state_machine/matcher.rb
55
+ - lib/state_machine/condition_proxy.rb
54
56
  - lib/state_machine/machine.rb
55
57
  - lib/state_machine.rb
56
58
  - test/sequel.log
@@ -61,6 +63,7 @@ files:
61
63
  - test/classes
62
64
  - test/classes/switch.rb
63
65
  - test/unit
66
+ - test/unit/matcher_helpers_test.rb
64
67
  - test/unit/node_collection_test.rb
65
68
  - test/unit/matcher_test.rb
66
69
  - test/unit/integrations
@@ -77,6 +80,7 @@ files:
77
80
  - test/unit/state_test.rb
78
81
  - test/unit/guard_test.rb
79
82
  - test/unit/assertions_test.rb
83
+ - test/unit/condition_proxy_test.rb
80
84
  - test/unit/machine_test.rb
81
85
  - test/unit/integrations_test.rb
82
86
  - CHANGELOG.rdoc
@@ -112,6 +116,7 @@ specification_version: 2
112
116
  summary: Adds support for creating state machines for attributes on any Ruby class
113
117
  test_files:
114
118
  - test/functional/state_machine_test.rb
119
+ - test/unit/matcher_helpers_test.rb
115
120
  - test/unit/node_collection_test.rb
116
121
  - test/unit/matcher_test.rb
117
122
  - test/unit/integrations/data_mapper_test.rb
@@ -127,5 +132,6 @@ test_files:
127
132
  - test/unit/state_test.rb
128
133
  - test/unit/guard_test.rb
129
134
  - test/unit/assertions_test.rb
135
+ - test/unit/condition_proxy_test.rb
130
136
  - test/unit/machine_test.rb
131
137
  - test/unit/integrations_test.rb