PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/state_collection_test.rb

https://github.com/johnsonzes/state_machine
Ruby | 310 lines | 248 code | 62 blank | 0 comment | 0 complexity | 9852dac226dcff1f09672c9ce180376a MD5 | raw file
  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. @machine.states.concat(@states)
  26. @object = @klass.new
  27. end
  28. def test_should_index_by_name
  29. assert_equal @parked, @states[:parked, :name]
  30. end
  31. def test_should_index_by_name_by_default
  32. assert_equal @parked, @states[:parked]
  33. end
  34. def test_should_index_by_qualified_name
  35. assert_equal @parked, @states[:parked, :qualified_name]
  36. end
  37. def test_should_index_by_value
  38. assert_equal @parked, @states['parked', :value]
  39. end
  40. def test_should_not_match_if_value_does_not_match
  41. assert !@states.matches?(@object, :parked)
  42. assert !@states.matches?(@object, :idling)
  43. end
  44. def test_should_match_if_value_matches
  45. assert @states.matches?(@object, nil)
  46. end
  47. def test_raise_exception_if_matching_invalid_state
  48. assert_raise(IndexError) { @states.matches?(@object, :invalid) }
  49. end
  50. def test_should_find_state_for_object_if_value_is_known
  51. @object.state = 'parked'
  52. assert_equal @parked, @states.match(@object)
  53. end
  54. def test_should_find_bang_state_for_object_if_value_is_known
  55. @object.state = 'parked'
  56. assert_equal @parked, @states.match!(@object)
  57. end
  58. def test_should_not_find_state_for_object_with_unknown_value
  59. @object.state = 'invalid'
  60. assert_nil @states.match(@object)
  61. end
  62. def test_should_raise_exception_if_finding_bang_state_for_object_with_unknown_value
  63. @object.state = 'invalid'
  64. exception = assert_raise(ArgumentError) { @states.match!(@object) }
  65. assert_equal '"invalid" is not a known state value', exception.message
  66. end
  67. end
  68. class StateCollectionWithNamespaceTest < Test::Unit::TestCase
  69. def setup
  70. @klass = Class.new
  71. @machine = StateMachine::Machine.new(@klass, :namespace => 'vehicle')
  72. @states = StateMachine::StateCollection.new(@machine)
  73. @states << @state = StateMachine::State.new(@machine, :parked)
  74. @machine.states.concat(@states)
  75. end
  76. def test_should_index_by_name
  77. assert_equal @state, @states[:parked, :name]
  78. end
  79. def test_should_index_by_qualified_name
  80. assert_equal @state, @states[:vehicle_parked, :qualified_name]
  81. end
  82. end
  83. class StateCollectionWithCustomStateValuesTest < Test::Unit::TestCase
  84. def setup
  85. @klass = Class.new
  86. @machine = StateMachine::Machine.new(@klass)
  87. @states = StateMachine::StateCollection.new(@machine)
  88. @states << @state = StateMachine::State.new(@machine, :parked, :value => 1)
  89. @machine.states.concat(@states)
  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 = 2
  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 StateCollectionWithStateMatchersTest < Test::Unit::TestCase
  105. def setup
  106. @klass = Class.new
  107. @machine = StateMachine::Machine.new(@klass)
  108. @states = StateMachine::StateCollection.new(@machine)
  109. @states << @state = StateMachine::State.new(@machine, :parked, :if => lambda {|value| !value.nil?})
  110. @machine.states.concat(@states)
  111. @object = @klass.new
  112. @object.state = 1
  113. end
  114. def test_should_match_if_value_matches
  115. assert @states.matches?(@object, :parked)
  116. end
  117. def test_should_not_match_if_value_does_not_match
  118. @object.state = nil
  119. assert !@states.matches?(@object, :parked)
  120. end
  121. def test_should_find_state_for_object_if_value_is_known
  122. assert_equal @state, @states.match(@object)
  123. end
  124. end
  125. class StateCollectionWithInitialStateTest < Test::Unit::TestCase
  126. def setup
  127. @machine = StateMachine::Machine.new(Class.new)
  128. @states = StateMachine::StateCollection.new(@machine)
  129. @states << @parked = StateMachine::State.new(@machine, :parked)
  130. @states << @idling = StateMachine::State.new(@machine, :idling)
  131. @machine.states.concat(@states)
  132. @parked.initial = true
  133. end
  134. def test_should_order_state_before_transition_states
  135. @machine.event :ignite do
  136. transition :to => :idling
  137. end
  138. assert_equal [@parked, @idling], @states.by_priority
  139. end
  140. def test_should_order_state_before_states_with_behaviors
  141. @idling.context do
  142. def speed
  143. 0
  144. end
  145. end
  146. assert_equal [@parked, @idling], @states.by_priority
  147. end
  148. def test_should_order_state_before_other_states
  149. assert_equal [@parked, @idling], @states.by_priority
  150. end
  151. def test_should_order_state_before_callback_states
  152. @machine.before_transition :from => :idling, :do => lambda {}
  153. assert_equal [@parked, @idling], @states.by_priority
  154. end
  155. end
  156. class StateCollectionWithStateBehaviorsTest < Test::Unit::TestCase
  157. def setup
  158. @machine = StateMachine::Machine.new(Class.new)
  159. @states = StateMachine::StateCollection.new(@machine)
  160. @states << @parked = StateMachine::State.new(@machine, :parked)
  161. @states << @idling = StateMachine::State.new(@machine, :idling)
  162. @machine.states.concat(@states)
  163. @idling.context do
  164. def speed
  165. 0
  166. end
  167. end
  168. end
  169. def test_should_order_states_after_initial_state
  170. @parked.initial = true
  171. assert_equal [@parked, @idling], @states.by_priority
  172. end
  173. def test_should_order_states_after_transition_states
  174. @machine.event :ignite do
  175. transition :from => :parked
  176. end
  177. assert_equal [@parked, @idling], @states.by_priority
  178. end
  179. def test_should_order_states_before_other_states
  180. assert_equal [@idling, @parked], @states.by_priority
  181. end
  182. def test_should_order_state_before_callback_states
  183. @machine.before_transition :from => :parked, :do => lambda {}
  184. assert_equal [@idling, @parked], @states.by_priority
  185. end
  186. end
  187. class StateCollectionWithEventTransitionsTest < Test::Unit::TestCase
  188. def setup
  189. @machine = StateMachine::Machine.new(Class.new)
  190. @states = StateMachine::StateCollection.new(@machine)
  191. @states << @parked = StateMachine::State.new(@machine, :parked)
  192. @states << @idling = StateMachine::State.new(@machine, :idling)
  193. @machine.states.concat(@states)
  194. @machine.event :ignite do
  195. transition :to => :idling
  196. end
  197. end
  198. def test_should_order_states_after_initial_state
  199. @parked.initial = true
  200. assert_equal [@parked, @idling], @states.by_priority
  201. end
  202. def test_should_order_states_before_states_with_behaviors
  203. @parked.context do
  204. def speed
  205. 0
  206. end
  207. end
  208. assert_equal [@idling, @parked], @states.by_priority
  209. end
  210. def test_should_order_states_before_other_states
  211. assert_equal [@idling, @parked], @states.by_priority
  212. end
  213. def test_should_order_state_before_callback_states
  214. @machine.before_transition :from => :parked, :do => lambda {}
  215. assert_equal [@idling, @parked], @states.by_priority
  216. end
  217. end
  218. class StateCollectionWithTransitionCallbacksTest < Test::Unit::TestCase
  219. def setup
  220. @machine = StateMachine::Machine.new(Class.new)
  221. @states = StateMachine::StateCollection.new(@machine)
  222. @states << @parked = StateMachine::State.new(@machine, :parked)
  223. @states << @idling = StateMachine::State.new(@machine, :idling)
  224. @machine.states.concat(@states)
  225. @machine.before_transition :to => :idling, :do => lambda {}
  226. end
  227. def test_should_order_states_after_initial_state
  228. @parked.initial = true
  229. assert_equal [@parked, @idling], @states.by_priority
  230. end
  231. def test_should_order_states_after_transition_states
  232. @machine.event :ignite do
  233. transition :from => :parked
  234. end
  235. assert_equal [@parked, @idling], @states.by_priority
  236. end
  237. def test_should_order_states_after_states_with_behaviors
  238. @parked.context do
  239. def speed
  240. 0
  241. end
  242. end
  243. assert_equal [@parked, @idling], @states.by_priority
  244. end
  245. def test_should_order_states_after_other_states
  246. assert_equal [@parked, @idling], @states.by_priority
  247. end
  248. end