File: event_collection_test.rb

package info (click to toggle)
ruby-state-machine 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,764 kB
  • ctags: 6,623
  • sloc: ruby: 26,008; makefile: 11
file content (398 lines) | stat: -rw-r--r-- 12,572 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

class EventCollectionByDefaultTest < Test::Unit::TestCase
  def setup
    @klass = Class.new
    @machine = StateMachine::Machine.new(@klass)
    @events = StateMachine::EventCollection.new(@machine)
    @object = @klass.new
  end
  
  def test_should_not_have_any_nodes
    assert_equal 0, @events.length
  end
  
  def test_should_have_a_machine
    assert_equal @machine, @events.machine
  end
  
  def test_should_not_have_any_valid_events_for_an_object
    assert @events.valid_for(@object).empty?
  end
  
  def test_should_not_have_any_transitions_for_an_object
    assert @events.transitions_for(@object).empty?
  end
end

class EventCollectionTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new, :namespace => 'alarm')
    @events = StateMachine::EventCollection.new(machine)
    
    @events << @open = StateMachine::Event.new(machine, :enable)
    machine.events.concat(@events)
  end
  
  def test_should_index_by_name
    assert_equal @open, @events[:enable, :name]
  end
  
  def test_should_index_by_name_by_default
    assert_equal @open, @events[:enable]
  end
  
  def test_should_index_by_string_name
    assert_equal @open, @events['enable']
  end
  
  def test_should_index_by_qualified_name
    assert_equal @open, @events[:enable_alarm, :qualified_name]
  end
  
  def test_should_index_by_string_qualified_name
    assert_equal @open, @events['enable_alarm', :qualified_name]
  end
end

class EventStringCollectionTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new, :namespace => 'alarm')
    @events = StateMachine::EventCollection.new(machine)
    
    @events << @open = StateMachine::Event.new(machine, 'enable')
    machine.events.concat(@events)
  end
  
  def test_should_index_by_name
    assert_equal @open, @events['enable', :name]
  end
  
  def test_should_index_by_name_by_default
    assert_equal @open, @events['enable']
  end
  
  def test_should_index_by_symbol_name
    assert_equal @open, @events[:enable]
  end
  
  def test_should_index_by_qualified_name
    assert_equal @open, @events['enable_alarm', :qualified_name]
  end
  
  def test_should_index_by_symbol_qualified_name
    assert_equal @open, @events[:enable_alarm, :qualified_name]
  end
end

class EventCollectionWithEventsWithTransitionsTest < Test::Unit::TestCase
  def setup
    @klass = Class.new
    @machine = StateMachine::Machine.new(@klass, :initial => :parked)
    @events = StateMachine::EventCollection.new(@machine)
    
    @machine.state :idling, :first_gear
    
    @events << @ignite = StateMachine::Event.new(@machine, :ignite)
    @ignite.transition :parked => :idling
    
    @events << @park = StateMachine::Event.new(@machine, :park)
    @park.transition :idling => :parked
    
    @events << @shift_up = StateMachine::Event.new(@machine, :shift_up)
    @shift_up.transition :parked => :first_gear
    @shift_up.transition :idling => :first_gear, :if => lambda{false}
    
    @machine.events.concat(@events)
    
    @object = @klass.new
  end
  
  def test_should_find_valid_events_based_on_current_state
    assert_equal [@ignite, @shift_up], @events.valid_for(@object)
  end
  
  def test_should_filter_valid_events_by_from_state
    assert_equal [@park], @events.valid_for(@object, :from => :idling)
  end
  
  def test_should_filter_valid_events_by_to_state
    assert_equal [@shift_up], @events.valid_for(@object, :to => :first_gear)
  end
  
  def test_should_filter_valid_events_by_event
    assert_equal [@ignite], @events.valid_for(@object, :on => :ignite)
  end
  
  def test_should_filter_valid_events_by_multiple_requirements
    assert_equal [], @events.valid_for(@object, :from => :idling, :to => :first_gear)
  end
  
  def test_should_allow_finding_valid_events_without_guards
    assert_equal [@shift_up], @events.valid_for(@object, :from => :idling, :to => :first_gear, :guard => false)
  end
  
  def test_should_find_valid_transitions_based_on_current_state
    assert_equal [
      StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
      StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)
    ], @events.transitions_for(@object)
  end
  
  def test_should_filter_valid_transitions_by_from_state
    assert_equal [StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)], @events.transitions_for(@object, :from => :idling)
  end
  
  def test_should_filter_valid_transitions_by_to_state
    assert_equal [StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)], @events.transitions_for(@object, :to => :first_gear)
  end
  
  def test_should_filter_valid_transitions_by_event
    assert_equal [StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)], @events.transitions_for(@object, :on => :ignite)
  end
  
  def test_should_filter_valid_transitions_by_multiple_requirements
    assert_equal [], @events.transitions_for(@object, :from => :idling, :to => :first_gear)
  end
  
  def test_should_allow_finding_valid_transitions_without_guards
    assert_equal [StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)], @events.transitions_for(@object, :from => :idling, :to => :first_gear, :guard => false)
  end
end

class EventCollectionWithMultipleEventsTest < Test::Unit::TestCase
  def setup
    @klass = Class.new
    @machine = StateMachine::Machine.new(@klass, :initial => :parked)
    @events = StateMachine::EventCollection.new(@machine)
    
    @machine.state :first_gear
    @park, @shift_down = @machine.event :park, :shift_down
    
    @events << @park
    @park.transition :first_gear => :parked
    
    @events << @shift_down
    @shift_down.transition :first_gear => :parked
    
    @machine.events.concat(@events)
  end
  
  def test_should_only_include_all_valid_events_for_an_object
    object = @klass.new
    object.state = 'first_gear'
    assert_equal [@park, @shift_down], @events.valid_for(object)
  end
end

class EventCollectionWithoutMachineActionTest < Test::Unit::TestCase
  def setup
    @klass = Class.new
    @machine = StateMachine::Machine.new(@klass, :initial => :parked)
    @events = StateMachine::EventCollection.new(@machine)
    @events << StateMachine::Event.new(@machine, :ignite)
    @machine.events.concat(@events)
    
    @object = @klass.new
  end
  
  def test_should_not_have_an_attribute_transition
    assert_nil @events.attribute_transition_for(@object)
  end
end

class EventCollectionAttributeWithMachineActionTest < Test::Unit::TestCase
  def setup
    @klass = Class.new do
      def save
      end
    end
    
    @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
    @events = StateMachine::EventCollection.new(@machine)
    
    @machine.state :parked, :idling
    @events << @ignite = StateMachine::Event.new(@machine, :ignite)
    @machine.events.concat(@events)
    
    @object = @klass.new
  end
  
  def test_should_not_have_transition_if_nil
    @object.state_event = nil
    assert_nil @events.attribute_transition_for(@object)
  end
  
  def test_should_not_have_transition_if_empty
    @object.state_event = ''
    assert_nil @events.attribute_transition_for(@object)
  end
  
  def test_should_have_invalid_transition_if_invalid_event_specified
    @object.state_event = 'invalid'
    assert_equal false, @events.attribute_transition_for(@object)
  end
  
  def test_should_have_invalid_transition_if_event_cannot_be_fired
    @object.state_event = 'ignite'
    assert_equal false, @events.attribute_transition_for(@object)
  end
  
  def test_should_have_valid_transition_if_event_can_be_fired
    @ignite.transition :parked => :idling
    @object.state_event = 'ignite'
    
    assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  end
  
  def test_should_have_valid_transition_if_already_defined_in_transition_cache
    @ignite.transition :parked => :idling
    @object.state_event = nil
    @object.send(:state_event_transition=, transition = @ignite.transition_for(@object))
    
    assert_equal transition, @events.attribute_transition_for(@object)
  end
  
  def test_should_use_transition_cache_if_both_event_and_transition_are_present
    @ignite.transition :parked => :idling
    @object.state_event = 'ignite'
    @object.send(:state_event_transition=, transition = @ignite.transition_for(@object))
    
    assert_equal transition, @events.attribute_transition_for(@object)
  end
end

class EventCollectionAttributeWithNamespacedMachineTest < Test::Unit::TestCase
  def setup
    @klass = Class.new do
      def save
      end
    end
    
    @machine = StateMachine::Machine.new(@klass, :namespace => 'alarm', :initial => :active, :action => :save)
    @events = StateMachine::EventCollection.new(@machine)
    
    @machine.state :active, :off
    @events << @disable = StateMachine::Event.new(@machine, :disable)
    @machine.events.concat(@events)
    
    @object = @klass.new
  end
  
  def test_should_not_have_transition_if_nil
    @object.state_event = nil
    assert_nil @events.attribute_transition_for(@object)
  end
  
  def test_should_have_invalid_transition_if_event_cannot_be_fired
    @object.state_event = 'disable'
    assert_equal false, @events.attribute_transition_for(@object)
  end
  
  def test_should_have_valid_transition_if_event_can_be_fired
    @disable.transition :active => :off
    @object.state_event = 'disable'
    
    assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  end
end

class EventCollectionWithValidationsTest < Test::Unit::TestCase
  def setup
    StateMachine::Integrations.const_set('Custom', Module.new do
      include StateMachine::Integrations::Base
      
      def invalidate(object, attribute, message, values = [])
        (object.errors ||= []) << generate_message(message, values)
      end
      
      def reset(object)
        object.errors = []
      end
    end)
    
    @klass = Class.new do
      attr_accessor :errors
      
      def initialize
        @errors = []
        super
      end
    end
    
    @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save, :integration => :custom)
    @events = StateMachine::EventCollection.new(@machine)
    
    @parked, @idling = @machine.state :parked, :idling
    @events << @ignite = StateMachine::Event.new(@machine, :ignite)
    @machine.events.concat(@events)
    
    @object = @klass.new
  end
  
  def test_should_invalidate_if_invalid_event_specified
    @object.state_event = 'invalid'
    @events.attribute_transition_for(@object, true)
    
    assert_equal ['is invalid'], @object.errors
  end
  
  def test_should_invalidate_if_event_cannot_be_fired
    @object.state = 'idling'
    @object.state_event = 'ignite'
    @events.attribute_transition_for(@object, true)
    
    assert_equal ['cannot transition when idling'], @object.errors
  end
  
  def test_should_invalidate_with_human_name_if_invalid_event_specified
    @idling.human_name = 'waiting'
    @object.state = 'idling'
    @object.state_event = 'ignite'
    @events.attribute_transition_for(@object, true)
    
    assert_equal ['cannot transition when waiting'], @object.errors
  end
  
  def test_should_not_invalidate_event_can_be_fired
    @ignite.transition :parked => :idling
    @object.state_event = 'ignite'
    @events.attribute_transition_for(@object, true)
    
    assert_equal [], @object.errors
  end
  
  def teardown
    StateMachine::Integrations.send(:remove_const, 'Custom')
  end
end

class EventCollectionWithCustomMachineAttributeTest < Test::Unit::TestCase
  def setup
    @klass = Class.new do
      def save
      end
    end
    
    @machine = StateMachine::Machine.new(@klass, :state, :attribute => :state_id, :initial => :parked, :action => :save)
    @events = StateMachine::EventCollection.new(@machine)
    
    @machine.state :parked, :idling
    @events << @ignite = StateMachine::Event.new(@machine, :ignite)
    @machine.events.concat(@events)
    
    @object = @klass.new
  end
  
  def test_should_not_have_transition_if_nil
    @object.state_event = nil
    assert_nil @events.attribute_transition_for(@object)
  end
  
  def test_should_have_valid_transition_if_event_can_be_fired
    @ignite.transition :parked => :idling
    @object.state_event = 'ignite'
    
    assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  end
end