PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/state_machine/test/unit/state_collection_test.rb

https://bitbucket.org/systech3/boxyroom
Ruby | 280 lines | 223 code | 57 blank | 0 comment | 0 complexity | de163687759d0a05c3eeb364d22cf764 MD5 | raw file
Possible License(s): JSON, MIT
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class StateCollectionByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @machine = StateMachine::Machine.new(Class.new)
  5. @states = StateMachine::StateCollection.new(@machine)
  6. end
  7. def test_should_not_have_any_nodes
  8. assert_equal 0, @states.length
  9. end
  10. def test_should_have_a_machine
  11. assert_equal @machine, @states.machine
  12. end
  13. def test_should_be_empty_by_priority
  14. assert_equal [], @states.by_priority
  15. end
  16. end
  17. class StateCollectionTest < Test::Unit::TestCase
  18. def setup
  19. @klass = Class.new
  20. @machine = StateMachine::Machine.new(@klass)
  21. @states = StateMachine::StateCollection.new(@machine)
  22. @states << @nil = StateMachine::State.new(@machine, nil)
  23. @states << @parked = StateMachine::State.new(@machine, :parked)
  24. @states << @idling = StateMachine::State.new(@machine, :idling)
  25. @object = @klass.new
  26. end
  27. def test_should_index_by_name
  28. assert_equal @parked, @states[:parked, :name]
  29. end
  30. def test_should_index_by_name_by_default
  31. assert_equal @parked, @states[:parked]
  32. end
  33. def test_should_index_by_value
  34. assert_equal @parked, @states['parked', :value]
  35. end
  36. def test_should_not_match_if_value_does_not_match
  37. assert !@states.matches?(@object, :parked)
  38. assert !@states.matches?(@object, :idling)
  39. end
  40. def test_should_match_if_value_matches
  41. assert @states.matches?(@object, nil)
  42. end
  43. def test_raise_exception_if_matching_invalid_state
  44. assert_raise(IndexError) { @states.matches?(@object, :invalid) }
  45. end
  46. def test_should_find_state_for_object_if_value_is_known
  47. @object.state = 'parked'
  48. assert_equal @parked, @states.match(@object)
  49. end
  50. def test_should_find_bang_state_for_object_if_value_is_known
  51. @object.state = 'parked'
  52. assert_equal @parked, @states.match!(@object)
  53. end
  54. def test_should_not_find_state_for_object_with_unknown_value
  55. @object.state = 'invalid'
  56. assert_nil @states.match(@object)
  57. end
  58. def test_should_raise_exception_if_finding_bang_state_for_object_with_unknown_value
  59. @object.state = 'invalid'
  60. exception = assert_raise(ArgumentError) { @states.match!(@object) }
  61. assert_equal '"invalid" is not a known state value', exception.message
  62. end
  63. end
  64. class StateCollectionWithCustomStateValuesTest < Test::Unit::TestCase
  65. def setup
  66. @klass = Class.new
  67. @machine = StateMachine::Machine.new(@klass)
  68. @states = StateMachine::StateCollection.new(@machine)
  69. @states << @state = StateMachine::State.new(@machine, :parked, :value => 1)
  70. @object = @klass.new
  71. @object.state = 1
  72. end
  73. def test_should_match_if_value_matches
  74. assert @states.matches?(@object, :parked)
  75. end
  76. def test_should_not_match_if_value_does_not_match
  77. @object.state = 2
  78. assert !@states.matches?(@object, :parked)
  79. end
  80. def test_should_find_state_for_object_if_value_is_known
  81. assert_equal @state, @states.match(@object)
  82. end
  83. end
  84. class StateCollectionWithStateMatchersTest < Test::Unit::TestCase
  85. def setup
  86. @klass = Class.new
  87. @machine = StateMachine::Machine.new(@klass)
  88. @states = StateMachine::StateCollection.new(@machine)
  89. @states << @state = StateMachine::State.new(@machine, :parked, :if => lambda {|value| !value.nil?})
  90. @object = @klass.new
  91. @object.state = 1
  92. end
  93. def test_should_match_if_value_matches
  94. assert @states.matches?(@object, :parked)
  95. end
  96. def test_should_not_match_if_value_does_not_match
  97. @object.state = nil
  98. assert !@states.matches?(@object, :parked)
  99. end
  100. def test_should_find_state_for_object_if_value_is_known
  101. assert_equal @state, @states.match(@object)
  102. end
  103. end
  104. class StateCollectionWithInitialStateTest < Test::Unit::TestCase
  105. def setup
  106. @machine = StateMachine::Machine.new(Class.new)
  107. @states = StateMachine::StateCollection.new(@machine)
  108. @states << @parked = StateMachine::State.new(@machine, :parked)
  109. @states << @idling = StateMachine::State.new(@machine, :idling)
  110. @parked.initial = true
  111. end
  112. def test_should_order_state_before_transition_states
  113. @machine.event :ignite do
  114. transition :to => :idling
  115. end
  116. assert_equal [@parked, @idling], @states.by_priority
  117. end
  118. def test_should_order_state_before_states_with_behaviors
  119. @idling.context do
  120. def speed
  121. 0
  122. end
  123. end
  124. assert_equal [@parked, @idling], @states.by_priority
  125. end
  126. def test_should_order_state_before_other_states
  127. assert_equal [@parked, @idling], @states.by_priority
  128. end
  129. def test_should_order_state_before_callback_states
  130. @machine.before_transition :from => :idling, :do => lambda {}
  131. assert_equal [@parked, @idling], @states.by_priority
  132. end
  133. end
  134. class StateCollectionWithStateBehaviorsTest < Test::Unit::TestCase
  135. def setup
  136. @machine = StateMachine::Machine.new(Class.new)
  137. @states = StateMachine::StateCollection.new(@machine)
  138. @states << @parked = StateMachine::State.new(@machine, :parked)
  139. @states << @idling = StateMachine::State.new(@machine, :idling)
  140. @idling.context do
  141. def speed
  142. 0
  143. end
  144. end
  145. end
  146. def test_should_order_states_after_initial_state
  147. @parked.initial = true
  148. assert_equal [@parked, @idling], @states.by_priority
  149. end
  150. def test_should_order_states_after_transition_states
  151. @machine.event :ignite do
  152. transition :from => :parked
  153. end
  154. assert_equal [@parked, @idling], @states.by_priority
  155. end
  156. def test_should_order_states_before_other_states
  157. assert_equal [@idling, @parked], @states.by_priority
  158. end
  159. def test_should_order_state_before_callback_states
  160. @machine.before_transition :from => :parked, :do => lambda {}
  161. assert_equal [@idling, @parked], @states.by_priority
  162. end
  163. end
  164. class StateCollectionWithEventTransitionsTest < Test::Unit::TestCase
  165. def setup
  166. @machine = StateMachine::Machine.new(Class.new)
  167. @states = StateMachine::StateCollection.new(@machine)
  168. @states << @parked = StateMachine::State.new(@machine, :parked)
  169. @states << @idling = StateMachine::State.new(@machine, :idling)
  170. @machine.event :ignite do
  171. transition :to => :idling
  172. end
  173. end
  174. def test_should_order_states_after_initial_state
  175. @parked.initial = true
  176. assert_equal [@parked, @idling], @states.by_priority
  177. end
  178. def test_should_order_states_before_states_with_behaviors
  179. @parked.context do
  180. def speed
  181. 0
  182. end
  183. end
  184. assert_equal [@idling, @parked], @states.by_priority
  185. end
  186. def test_should_order_states_before_other_states
  187. assert_equal [@idling, @parked], @states.by_priority
  188. end
  189. def test_should_order_state_before_callback_states
  190. @machine.before_transition :from => :parked, :do => lambda {}
  191. assert_equal [@idling, @parked], @states.by_priority
  192. end
  193. end
  194. class StateCollectionWithTransitionCallbacksTest < Test::Unit::TestCase
  195. def setup
  196. @machine = StateMachine::Machine.new(Class.new)
  197. @states = StateMachine::StateCollection.new(@machine)
  198. @states << @parked = StateMachine::State.new(@machine, :parked)
  199. @states << @idling = StateMachine::State.new(@machine, :idling)
  200. @machine.before_transition :to => :idling, :do => lambda {}
  201. end
  202. def test_should_order_states_after_initial_state
  203. @parked.initial = true
  204. assert_equal [@parked, @idling], @states.by_priority
  205. end
  206. def test_should_order_states_after_transition_states
  207. @machine.event :ignite do
  208. transition :from => :parked
  209. end
  210. assert_equal [@parked, @idling], @states.by_priority
  211. end
  212. def test_should_order_states_after_states_with_behaviors
  213. @parked.context do
  214. def speed
  215. 0
  216. end
  217. end
  218. assert_equal [@parked, @idling], @states.by_priority
  219. end
  220. def test_should_order_states_after_other_states
  221. assert_equal [@parked, @idling], @states.by_priority
  222. end
  223. end