PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/state_collection_test.rb

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