File: state_context_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 (441 lines) | stat: -rw-r--r-- 13,329 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

class Validateable
  class << self
    def validate(*args, &block)
      args << block if block_given?
      args
    end
  end
end

class StateContextTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    @machine = StateMachine::Machine.new(@klass, :initial => :parked)
    @state = @machine.state :parked
    
    @state_context = StateMachine::StateContext.new(@state)
  end
  
  def test_should_have_a_machine
    assert_equal @machine, @state_context.machine
  end
  
  def test_should_have_a_state
    assert_equal @state, @state_context.state
  end
end

class StateContextTransitionTest < Test::Unit::TestCase
  def setup
    @klass = Class.new
    @machine = StateMachine::Machine.new(@klass, :initial => :parked)
    @state = @machine.state :parked
    
    @state_context = StateMachine::StateContext.new(@state)
  end
  
  def test_should_not_allow_except_to
    exception = assert_raise(ArgumentError) { @state_context.transition(:except_to => :idling) }
    assert_equal 'Invalid key(s): except_to', exception.message
  end
  
  def test_should_not_allow_except_from
    exception = assert_raise(ArgumentError) { @state_context.transition(:except_from => :idling) }
    assert_equal 'Invalid key(s): except_from', exception.message
  end
  
  def test_should_not_allow_implicit_transitions
    exception = assert_raise(ArgumentError) { @state_context.transition(:parked => :idling) }
    assert_equal 'Invalid key(s): parked', exception.message
  end
  
  def test_should_not_allow_except_on
    exception = assert_raise(ArgumentError) { @state_context.transition(:except_on => :park) }
    assert_equal 'Invalid key(s): except_on', exception.message
  end
  
  def test_should_require_on_event
    exception = assert_raise(ArgumentError) { @state_context.transition(:to => :idling) }
    assert_equal 'Must specify :on event', exception.message
  end
  
  def test_should_not_allow_missing_from_and_to
    exception = assert_raise(ArgumentError) { @state_context.transition(:on => :ignite) }
    assert_equal 'Must specify either :to or :from state', exception.message
  end
  
  def test_should_not_allow_from_and_to
    exception = assert_raise(ArgumentError) { @state_context.transition(:on => :ignite, :from => :parked, :to => :idling) }
    assert_equal 'Must specify either :to or :from state', exception.message
  end
  
  def test_should_allow_to_state_if_missing_from_state
    assert_nothing_raised { @state_context.transition(:on => :park, :from => :parked) }
  end
  
  def test_should_allow_from_state_if_missing_to_state
    assert_nothing_raised { @state_context.transition(:on => :ignite, :to => :idling) }
  end
  
  def test_should_automatically_set_to_option_with_from_state
    branch = @state_context.transition(:from => :idling, :on => :park)
    assert_instance_of StateMachine::Branch, branch
    
    state_requirements = branch.state_requirements
    assert_equal 1, state_requirements.length
    
    from_requirement = state_requirements[0][:to]
    assert_instance_of StateMachine::WhitelistMatcher, from_requirement
    assert_equal [:parked], from_requirement.values
  end
  
  def test_should_automatically_set_from_option_with_to_state
    branch = @state_context.transition(:to => :idling, :on => :ignite)
    assert_instance_of StateMachine::Branch, branch
    
    state_requirements = branch.state_requirements
    assert_equal 1, state_requirements.length
    
    from_requirement = state_requirements[0][:from]
    assert_instance_of StateMachine::WhitelistMatcher, from_requirement
    assert_equal [:parked], from_requirement.values
  end
  
  def test_should_allow_if_condition
    assert_nothing_raised {@state_context.transition(:to => :idling, :on => :park, :if => :seatbelt_on?)}
  end
  
  def test_should_allow_unless_condition
    assert_nothing_raised {@state_context.transition(:to => :idling, :on => :park, :unless => :seatbelt_off?)}
  end
  
  def test_should_include_all_transition_states_in_machine_states
    @state_context.transition(:to => :idling, :on => :ignite)
    
    assert_equal [:parked, :idling], @machine.states.map {|state| state.name}
  end
  
  def test_should_include_all_transition_events_in_machine_events
    @state_context.transition(:to => :idling, :on => :ignite)
    
    assert_equal [:ignite], @machine.events.map {|event| event.name}
  end
  
  def test_should_allow_multiple_events
    @state_context.transition(:to => :idling, :on => [:ignite, :shift_up])
    
    assert_equal [:ignite, :shift_up], @machine.events.map {|event| event.name}
  end
end

class StateContextWithMatchingTransitionTest < Test::Unit::TestCase
  def setup
    @klass = Class.new
    @machine = StateMachine::Machine.new(@klass, :initial => :parked)
    @state = @machine.state :parked
    
    @state_context = StateMachine::StateContext.new(@state)
    @state_context.transition(:to => :idling, :on => :ignite)
    
    @event = @machine.event(:ignite)
    @object = @klass.new
  end
  
  def test_should_be_able_to_fire
    assert @event.can_fire?(@object)
  end
  
  def test_should_have_a_transition
    transition = @event.transition_for(@object)
    assert_not_nil transition
    assert_equal 'parked', transition.from
    assert_equal 'idling', transition.to
    assert_equal :ignite, transition.event
  end
end

class StateContextProxyTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachine::Machine.new(@klass, :initial => :parked)
    state = machine.state :parked
    
    @state_context = StateMachine::StateContext.new(state)
  end
  
  def test_should_call_class_with_same_arguments
    options = {}
    validation = @state_context.validate(:name, options)
    
    assert_equal [:name, options], validation
  end
  
  def test_should_pass_block_through_to_class
    options = {}
    proxy_block = lambda {}
    validation = @state_context.validate(:name, options, &proxy_block)
    
    assert_equal [:name, options, proxy_block], validation
  end
end

class StateContextProxyWithoutConditionsTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachine::Machine.new(@klass, :initial => :parked)
    state = machine.state :parked
    
    @state_context = StateMachine::StateContext.new(state)
    @object = @klass.new
    
    @options = @state_context.validate[0]
  end
  
  def test_should_have_options_configuration
    assert_instance_of Hash, @options
  end
  
  def test_should_have_if_option
    assert_not_nil @options[:if]
  end
  
  def test_should_be_false_if_state_is_different
    @object.state = nil
    assert !@options[:if].call(@object)
  end
  
  def test_should_be_true_if_state_matches
    assert @options[:if].call(@object)
  end
end

class StateContextProxyWithIfConditionTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachine::Machine.new(@klass, :initial => :parked)
    state = machine.state :parked
    
    @state_context = StateMachine::StateContext.new(state)
    @object = @klass.new
    
    @condition_result = nil
    @options = @state_context.validate(:if => lambda {@condition_result})[0]
  end
  
  def test_should_have_if_option
    assert_not_nil @options[:if]
  end
  
  def test_should_be_false_if_state_is_different
    @object.state = nil
    assert !@options[:if].call(@object)
  end
  
  def test_should_be_false_if_original_condition_is_false
    @condition_result = false
    assert !@options[:if].call(@object)
  end
  
  def test_should_be_true_if_state_matches_and_original_condition_is_true
    @condition_result = true
    assert @options[:if].call(@object)
  end
  
  def test_should_evaluate_symbol_condition
    @klass.class_eval do
      attr_accessor :callback
    end
    
    options = @state_context.validate(:if => :callback)[0]
    
    object = @klass.new
    object.callback = false
    assert !options[:if].call(object)
    
    object.callback = true
    assert options[:if].call(object)
  end
  
  def test_should_evaluate_string_condition
    @klass.class_eval do
      attr_accessor :callback
    end
    
    options = @state_context.validate(:if => '@callback')[0]
    
    object = @klass.new
    object.callback = false
    assert !options[:if].call(object)
    
    object.callback = true
    assert options[:if].call(object)
  end
end

class StateContextProxyWithMultipleIfConditionsTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachine::Machine.new(@klass, :initial => :parked)
    state = machine.state :parked
    
    @state_context = StateMachine::StateContext.new(state)
    @object = @klass.new
    
    @first_condition_result = nil
    @second_condition_result = nil
    @options = @state_context.validate(:if => [lambda {@first_condition_result}, lambda {@second_condition_result}])[0]
  end
  
  def test_should_be_true_if_all_conditions_are_true
    @first_condition_result = true
    @second_condition_result = true
    assert @options[:if].call(@object)
  end
  
  def test_should_be_false_if_any_condition_is_false
    @first_condition_result = true
    @second_condition_result = false
    assert !@options[:if].call(@object)
    
    @first_condition_result = false
    @second_condition_result = true
    assert !@options[:if].call(@object)
  end
end

class StateContextProxyWithUnlessConditionTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachine::Machine.new(@klass, :initial => :parked)
    state = machine.state :parked
    
    @state_context = StateMachine::StateContext.new(state)
    @object = @klass.new
    
    @condition_result = nil
    @options = @state_context.validate(:unless => lambda {@condition_result})[0]
  end
  
  def test_should_have_if_option
    assert_not_nil @options[:if]
  end
  
  def test_should_be_false_if_state_is_different
    @object.state = nil
    assert !@options[:if].call(@object)
  end
  
  def test_should_be_false_if_original_condition_is_true
    @condition_result = true
    assert !@options[:if].call(@object)
  end
  
  def test_should_be_true_if_state_matches_and_original_condition_is_false
    @condition_result = false
    assert @options[:if].call(@object)
  end
  
  def test_should_evaluate_symbol_condition
    @klass.class_eval do
      attr_accessor :callback
    end
    
    options = @state_context.validate(:unless => :callback)[0]
    
    object = @klass.new
    object.callback = true
    assert !options[:if].call(object)
    
    object.callback = false
    assert options[:if].call(object)
  end
  
  def test_should_evaluate_string_condition
    @klass.class_eval do
      attr_accessor :callback
    end
    
    options = @state_context.validate(:unless => '@callback')[0]
    
    object = @klass.new
    object.callback = true
    assert !options[:if].call(object)
    
    object.callback = false
    assert options[:if].call(object)
  end
end

class StateContextProxyWithMultipleUnlessConditionsTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachine::Machine.new(@klass, :initial => :parked)
    state = machine.state :parked
    
    @state_context = StateMachine::StateContext.new(state)
    @object = @klass.new
    
    @first_condition_result = nil
    @second_condition_result = nil
    @options = @state_context.validate(:unless => [lambda {@first_condition_result}, lambda {@second_condition_result}])[0]
  end
  
  def test_should_be_true_if_all_conditions_are_false
    @first_condition_result = false
    @second_condition_result = false
    assert @options[:if].call(@object)
  end
  
  def test_should_be_false_if_any_condition_is_true
    @first_condition_result = true
    @second_condition_result = false
    assert !@options[:if].call(@object)
    
    @first_condition_result = false
    @second_condition_result = true
    assert !@options[:if].call(@object)
  end
end

class StateContextProxyWithIfAndUnlessConditionsTest < Test::Unit::TestCase
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachine::Machine.new(@klass, :initial => :parked)
    state = machine.state :parked
    
    @state_context = StateMachine::StateContext.new(state)
    @object = @klass.new
    
    @if_condition_result = nil
    @unless_condition_result = nil
    @options = @state_context.validate(:if => lambda {@if_condition_result}, :unless => lambda {@unless_condition_result})[0]
  end
  
  def test_should_be_false_if_if_condition_is_false
    @if_condition_result = false
    @unless_condition_result = false
    assert !@options[:if].call(@object)
    
    @if_condition_result = false
    @unless_condition_result = true
    assert !@options[:if].call(@object)
  end
  
  def test_should_be_false_if_unless_condition_is_true
    @if_condition_result = false
    @unless_condition_result = true
    assert !@options[:if].call(@object)
    
    @if_condition_result = true
    @unless_condition_result = true
    assert !@options[:if].call(@object)
  end
  
  def test_should_be_true_if_if_condition_is_true_and_unless_condition_is_false
    @if_condition_result = true
    @unless_condition_result = false
    assert @options[:if].call(@object)
  end
end