PageRenderTime 70ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/systech3/boxyroom
Ruby | 326 lines | 250 code | 75 blank | 1 comment | 0 complexity | 1788931453f32d95e7f400e9525d510a MD5 | raw file
Possible License(s): JSON, MIT
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class EventCollectionByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @machine = StateMachine::Machine.new(Class.new)
  5. @events = StateMachine::EventCollection.new(@machine)
  6. end
  7. def test_should_not_have_any_nodes
  8. assert_equal 0, @events.length
  9. end
  10. def test_should_have_a_machine
  11. assert_equal @machine, @events.machine
  12. end
  13. def test_should_not_have_any_valid_events_for_an_object
  14. assert @events.valid_for(@object).empty?
  15. end
  16. def test_should_not_have_any_transitions_for_an_object
  17. assert @events.transitions_for(@object).empty?
  18. end
  19. end
  20. class EventCollectionTest < Test::Unit::TestCase
  21. def setup
  22. machine = StateMachine::Machine.new(Class.new, :namespace => 'alarm')
  23. @events = StateMachine::EventCollection.new(machine)
  24. @events << @open = StateMachine::Event.new(machine, :enable)
  25. end
  26. def test_should_index_by_name
  27. assert_equal @open, @events[:enable, :name]
  28. end
  29. def test_should_index_by_name_by_default
  30. assert_equal @open, @events[:enable]
  31. end
  32. def test_should_index_by_qualified_name
  33. assert_equal @open, @events[:enable_alarm, :qualified_name]
  34. end
  35. end
  36. class EventCollectionWithEventsWithTransitionsTest < Test::Unit::TestCase
  37. def setup
  38. @klass = Class.new
  39. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  40. @events = StateMachine::EventCollection.new(@machine)
  41. @machine.state :idling, :stalled
  42. @machine.event :ignite
  43. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  44. @ignite.transition :parked => :idling
  45. @ignite.transition :stalled => :idling
  46. end
  47. def test_should_only_include_valid_events_for_an_object
  48. object = @klass.new
  49. object.state = 'parked'
  50. assert_equal [@ignite], @events.valid_for(object)
  51. object.state = 'stalled'
  52. assert_equal [@ignite], @events.valid_for(object)
  53. object.state = 'idling'
  54. assert_equal [], @events.valid_for(object)
  55. end
  56. def test_should_only_include_valid_transitions_for_an_object
  57. object = @klass.new
  58. object.state = 'parked'
  59. assert_equal [{:object => object, :attribute => :state, :event => :ignite, :from => 'parked', :to => 'idling'}], @events.transitions_for(object).map {|transition| transition.attributes}
  60. object.state = 'stalled'
  61. assert_equal [{:object => object, :attribute => :state, :event => :ignite, :from => 'stalled', :to => 'idling'}], @events.transitions_for(object).map {|transition| transition.attributes}
  62. object.state = 'idling'
  63. assert_equal [], @events.transitions_for(object)
  64. end
  65. def test_should_filter_valid_transitions_for_an_object_if_requirements_specified
  66. object = @klass.new
  67. assert_equal [{:object => object, :attribute => :state, :event => :ignite, :from => 'stalled', :to => 'idling'}], @events.transitions_for(object, :from => :stalled).map {|transition| transition.attributes}
  68. assert_equal [], @events.transitions_for(object, :from => :idling).map {|transition| transition.attributes}
  69. end
  70. end
  71. class EventCollectionWithMultipleEventsTest < Test::Unit::TestCase
  72. def setup
  73. @klass = Class.new
  74. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  75. @events = StateMachine::EventCollection.new(@machine)
  76. @machine.state :first_gear
  77. @machine.event :park, :shift_down
  78. @events << @park = StateMachine::Event.new(@machine, :park)
  79. @park.transition :first_gear => :parked
  80. @events << @shift_down = StateMachine::Event.new(@machine, :shift_down)
  81. @shift_down.transition :first_gear => :parked
  82. end
  83. def test_should_only_include_all_valid_events_for_an_object
  84. object = @klass.new
  85. object.state = 'first_gear'
  86. assert_equal [@park, @shift_down], @events.valid_for(object)
  87. end
  88. end
  89. class EventCollectionWithoutMachineActionTest < Test::Unit::TestCase
  90. def setup
  91. @klass = Class.new
  92. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  93. @events = StateMachine::EventCollection.new(@machine)
  94. @machine.event :ignite
  95. @events << StateMachine::Event.new(@machine, :ignite)
  96. @object = @klass.new
  97. end
  98. def test_should_not_have_an_attribute_transition
  99. assert_nil @events.attribute_transition_for(@object)
  100. end
  101. end
  102. class EventCollectionAttributeWithMachineActionTest < Test::Unit::TestCase
  103. def setup
  104. @klass = Class.new do
  105. def save
  106. end
  107. end
  108. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  109. @events = StateMachine::EventCollection.new(@machine)
  110. @machine.event :ignite
  111. @machine.state :parked, :idling
  112. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  113. @object = @klass.new
  114. end
  115. def test_should_not_have_transition_if_nil
  116. @object.state_event = nil
  117. assert_nil @events.attribute_transition_for(@object)
  118. end
  119. def test_should_not_have_transition_if_empty
  120. @object.state_event = ''
  121. assert_nil @events.attribute_transition_for(@object)
  122. end
  123. def test_should_have_invalid_transition_if_invalid_event_specified
  124. @object.state_event = 'invalid'
  125. assert_equal false, @events.attribute_transition_for(@object)
  126. end
  127. def test_should_have_invalid_transition_if_event_cannot_be_fired
  128. @object.state_event = 'ignite'
  129. assert_equal false, @events.attribute_transition_for(@object)
  130. end
  131. def test_should_have_valid_transition_if_event_can_be_fired
  132. @ignite.transition :parked => :idling
  133. @object.state_event = 'ignite'
  134. assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  135. end
  136. def test_should_have_valid_transition_if_already_defined_in_transition_cache
  137. @ignite.transition :parked => :idling
  138. @object.state_event = nil
  139. @object.send(:state_event_transition=, transition = @ignite.transition_for(@object))
  140. assert_equal transition, @events.attribute_transition_for(@object)
  141. end
  142. def test_should_use_transition_cache_if_both_event_and_transition_are_present
  143. @ignite.transition :parked => :idling
  144. @object.state_event = 'ignite'
  145. @object.send(:state_event_transition=, transition = @ignite.transition_for(@object))
  146. assert_equal transition, @events.attribute_transition_for(@object)
  147. end
  148. end
  149. class EventCollectionAttributeWithNamespacedMachineTest < Test::Unit::TestCase
  150. def setup
  151. @klass = Class.new do
  152. def save
  153. end
  154. end
  155. @machine = StateMachine::Machine.new(@klass, :namespace => 'alarm', :initial => :active, :action => :save)
  156. @events = StateMachine::EventCollection.new(@machine)
  157. @machine.event :disable
  158. @machine.state :active, :off
  159. @events << @disable = StateMachine::Event.new(@machine, :disable)
  160. @object = @klass.new
  161. end
  162. def test_should_not_have_transition_if_nil
  163. @object.state_event = nil
  164. assert_nil @events.attribute_transition_for(@object)
  165. end
  166. def test_should_have_invalid_transition_if_event_cannot_be_fired
  167. @object.state_event = 'disable'
  168. assert_equal false, @events.attribute_transition_for(@object)
  169. end
  170. def test_should_have_valid_transition_if_event_can_be_fired
  171. @disable.transition :active => :off
  172. @object.state_event = 'disable'
  173. assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  174. end
  175. end
  176. class EventCollectionWithValidationsTest < Test::Unit::TestCase
  177. def setup
  178. StateMachine::Integrations.const_set('Custom', Module.new do
  179. def invalidate(object, attribute, message, values = [])
  180. (object.errors ||= []) << generate_message(message, values)
  181. end
  182. def reset(object)
  183. object.errors = []
  184. end
  185. end)
  186. @klass = Class.new do
  187. attr_accessor :errors
  188. def initialize
  189. @errors = []
  190. super
  191. end
  192. end
  193. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save, :integration => :custom)
  194. @events = StateMachine::EventCollection.new(@machine)
  195. @machine.event :ignite
  196. @machine.state :parked, :idling
  197. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  198. @object = @klass.new
  199. end
  200. def test_should_invalidate_if_invalid_event_specified
  201. @object.state_event = 'invalid'
  202. @events.attribute_transition_for(@object, true)
  203. assert_equal ['is invalid'], @object.errors
  204. end
  205. def test_should_invalidate_if_event_cannot_be_fired
  206. @object.state = 'idling'
  207. @object.state_event = 'ignite'
  208. @events.attribute_transition_for(@object, true)
  209. assert_equal ['cannot transition when idling'], @object.errors
  210. end
  211. def test_should_invalidate_with_friendly_name_if_invalid_event_specified
  212. # Add a valid nil state
  213. @machine.state nil
  214. @object.state = nil
  215. @object.state_event = 'ignite'
  216. @events.attribute_transition_for(@object, true)
  217. assert_equal ['cannot transition when nil'], @object.errors
  218. end
  219. def test_should_not_invalidate_event_can_be_fired
  220. @ignite.transition :parked => :idling
  221. @object.state_event = 'ignite'
  222. @events.attribute_transition_for(@object, true)
  223. assert_equal [], @object.errors
  224. end
  225. def teardown
  226. StateMachine::Integrations.send(:remove_const, 'Custom')
  227. end
  228. end
  229. class EventCollectionWithCustomMachineAttributeTest < Test::Unit::TestCase
  230. def setup
  231. @klass = Class.new do
  232. def save
  233. end
  234. end
  235. @machine = StateMachine::Machine.new(@klass, :state, :attribute => :state_id, :initial => :parked, :action => :save)
  236. @events = StateMachine::EventCollection.new(@machine)
  237. @machine.event :ignite
  238. @machine.state :parked, :idling
  239. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  240. @object = @klass.new
  241. end
  242. def test_should_not_have_transition_if_nil
  243. @object.state_event = nil
  244. assert_nil @events.attribute_transition_for(@object)
  245. end
  246. def test_should_have_valid_transition_if_event_can_be_fired
  247. @ignite.transition :parked => :idling
  248. @object.state_event = 'ignite'
  249. assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  250. end
  251. end