PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/gemcache/ruby/1.9.1/gems/state_machine-1.1.2/test/unit/event_collection_test.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 396 lines | 303 code | 93 blank | 0 comment | 0 complexity | e07283fbc2e64c1dde7befb693fbd689 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, GPL-2.0, 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. machine.events.concat(@events)
  26. end
  27. def test_should_index_by_name
  28. assert_equal @open, @events[:enable, :name]
  29. end
  30. def test_should_index_by_name_by_default
  31. assert_equal @open, @events[:enable]
  32. end
  33. def test_should_index_by_string_name
  34. assert_equal @open, @events['enable']
  35. end
  36. def test_should_index_by_qualified_name
  37. assert_equal @open, @events[:enable_alarm, :qualified_name]
  38. end
  39. def test_should_index_by_string_qualified_name
  40. assert_equal @open, @events['enable_alarm', :qualified_name]
  41. end
  42. end
  43. class EventStringCollectionTest < Test::Unit::TestCase
  44. def setup
  45. machine = StateMachine::Machine.new(Class.new, :namespace => 'alarm')
  46. @events = StateMachine::EventCollection.new(machine)
  47. @events << @open = StateMachine::Event.new(machine, 'enable')
  48. machine.events.concat(@events)
  49. end
  50. def test_should_index_by_name
  51. assert_equal @open, @events['enable', :name]
  52. end
  53. def test_should_index_by_name_by_default
  54. assert_equal @open, @events['enable']
  55. end
  56. def test_should_index_by_symbol_name
  57. assert_equal @open, @events[:enable]
  58. end
  59. def test_should_index_by_qualified_name
  60. assert_equal @open, @events['enable_alarm', :qualified_name]
  61. end
  62. def test_should_index_by_symbol_qualified_name
  63. assert_equal @open, @events[:enable_alarm, :qualified_name]
  64. end
  65. end
  66. class EventCollectionWithEventsWithTransitionsTest < Test::Unit::TestCase
  67. def setup
  68. @klass = Class.new
  69. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  70. @events = StateMachine::EventCollection.new(@machine)
  71. @machine.state :idling, :first_gear
  72. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  73. @ignite.transition :parked => :idling
  74. @events << @park = StateMachine::Event.new(@machine, :park)
  75. @park.transition :idling => :parked
  76. @events << @shift_up = StateMachine::Event.new(@machine, :shift_up)
  77. @shift_up.transition :parked => :first_gear
  78. @shift_up.transition :idling => :first_gear, :if => lambda{false}
  79. @machine.events.concat(@events)
  80. @object = @klass.new
  81. end
  82. def test_should_find_valid_events_based_on_current_state
  83. assert_equal [@ignite, @shift_up], @events.valid_for(@object)
  84. end
  85. def test_should_filter_valid_events_by_from_state
  86. assert_equal [@park], @events.valid_for(@object, :from => :idling)
  87. end
  88. def test_should_filter_valid_events_by_to_state
  89. assert_equal [@shift_up], @events.valid_for(@object, :to => :first_gear)
  90. end
  91. def test_should_filter_valid_events_by_event
  92. assert_equal [@ignite], @events.valid_for(@object, :on => :ignite)
  93. end
  94. def test_should_filter_valid_events_by_multiple_requirements
  95. assert_equal [], @events.valid_for(@object, :from => :idling, :to => :first_gear)
  96. end
  97. def test_should_allow_finding_valid_events_without_guards
  98. assert_equal [@shift_up], @events.valid_for(@object, :from => :idling, :to => :first_gear, :guard => false)
  99. end
  100. def test_should_find_valid_transitions_based_on_current_state
  101. assert_equal [
  102. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
  103. StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)
  104. ], @events.transitions_for(@object)
  105. end
  106. def test_should_filter_valid_transitions_by_from_state
  107. assert_equal [StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)], @events.transitions_for(@object, :from => :idling)
  108. end
  109. def test_should_filter_valid_transitions_by_to_state
  110. assert_equal [StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)], @events.transitions_for(@object, :to => :first_gear)
  111. end
  112. def test_should_filter_valid_transitions_by_event
  113. assert_equal [StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)], @events.transitions_for(@object, :on => :ignite)
  114. end
  115. def test_should_filter_valid_transitions_by_multiple_requirements
  116. assert_equal [], @events.transitions_for(@object, :from => :idling, :to => :first_gear)
  117. end
  118. def test_should_allow_finding_valid_transitions_without_guards
  119. assert_equal [StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)], @events.transitions_for(@object, :from => :idling, :to => :first_gear, :guard => false)
  120. end
  121. end
  122. class EventCollectionWithMultipleEventsTest < Test::Unit::TestCase
  123. def setup
  124. @klass = Class.new
  125. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  126. @events = StateMachine::EventCollection.new(@machine)
  127. @machine.state :first_gear
  128. @park, @shift_down = @machine.event :park, :shift_down
  129. @events << @park
  130. @park.transition :first_gear => :parked
  131. @events << @shift_down
  132. @shift_down.transition :first_gear => :parked
  133. @machine.events.concat(@events)
  134. end
  135. def test_should_only_include_all_valid_events_for_an_object
  136. object = @klass.new
  137. object.state = 'first_gear'
  138. assert_equal [@park, @shift_down], @events.valid_for(object)
  139. end
  140. end
  141. class EventCollectionWithoutMachineActionTest < Test::Unit::TestCase
  142. def setup
  143. @klass = Class.new
  144. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  145. @events = StateMachine::EventCollection.new(@machine)
  146. @events << StateMachine::Event.new(@machine, :ignite)
  147. @machine.events.concat(@events)
  148. @object = @klass.new
  149. end
  150. def test_should_not_have_an_attribute_transition
  151. assert_nil @events.attribute_transition_for(@object)
  152. end
  153. end
  154. class EventCollectionAttributeWithMachineActionTest < Test::Unit::TestCase
  155. def setup
  156. @klass = Class.new do
  157. def save
  158. end
  159. end
  160. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  161. @events = StateMachine::EventCollection.new(@machine)
  162. @machine.state :parked, :idling
  163. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  164. @machine.events.concat(@events)
  165. @object = @klass.new
  166. end
  167. def test_should_not_have_transition_if_nil
  168. @object.state_event = nil
  169. assert_nil @events.attribute_transition_for(@object)
  170. end
  171. def test_should_not_have_transition_if_empty
  172. @object.state_event = ''
  173. assert_nil @events.attribute_transition_for(@object)
  174. end
  175. def test_should_have_invalid_transition_if_invalid_event_specified
  176. @object.state_event = 'invalid'
  177. assert_equal false, @events.attribute_transition_for(@object)
  178. end
  179. def test_should_have_invalid_transition_if_event_cannot_be_fired
  180. @object.state_event = 'ignite'
  181. assert_equal false, @events.attribute_transition_for(@object)
  182. end
  183. def test_should_have_valid_transition_if_event_can_be_fired
  184. @ignite.transition :parked => :idling
  185. @object.state_event = 'ignite'
  186. assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  187. end
  188. def test_should_have_valid_transition_if_already_defined_in_transition_cache
  189. @ignite.transition :parked => :idling
  190. @object.state_event = nil
  191. @object.send(:state_event_transition=, transition = @ignite.transition_for(@object))
  192. assert_equal transition, @events.attribute_transition_for(@object)
  193. end
  194. def test_should_use_transition_cache_if_both_event_and_transition_are_present
  195. @ignite.transition :parked => :idling
  196. @object.state_event = 'ignite'
  197. @object.send(:state_event_transition=, transition = @ignite.transition_for(@object))
  198. assert_equal transition, @events.attribute_transition_for(@object)
  199. end
  200. end
  201. class EventCollectionAttributeWithNamespacedMachineTest < Test::Unit::TestCase
  202. def setup
  203. @klass = Class.new do
  204. def save
  205. end
  206. end
  207. @machine = StateMachine::Machine.new(@klass, :namespace => 'alarm', :initial => :active, :action => :save)
  208. @events = StateMachine::EventCollection.new(@machine)
  209. @machine.state :active, :off
  210. @events << @disable = StateMachine::Event.new(@machine, :disable)
  211. @machine.events.concat(@events)
  212. @object = @klass.new
  213. end
  214. def test_should_not_have_transition_if_nil
  215. @object.state_event = nil
  216. assert_nil @events.attribute_transition_for(@object)
  217. end
  218. def test_should_have_invalid_transition_if_event_cannot_be_fired
  219. @object.state_event = 'disable'
  220. assert_equal false, @events.attribute_transition_for(@object)
  221. end
  222. def test_should_have_valid_transition_if_event_can_be_fired
  223. @disable.transition :active => :off
  224. @object.state_event = 'disable'
  225. assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  226. end
  227. end
  228. class EventCollectionWithValidationsTest < Test::Unit::TestCase
  229. def setup
  230. StateMachine::Integrations.const_set('Custom', Module.new do
  231. include StateMachine::Integrations::Base
  232. def invalidate(object, attribute, message, values = [])
  233. (object.errors ||= []) << generate_message(message, values)
  234. end
  235. def reset(object)
  236. object.errors = []
  237. end
  238. end)
  239. @klass = Class.new do
  240. attr_accessor :errors
  241. def initialize
  242. @errors = []
  243. super
  244. end
  245. end
  246. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save, :integration => :custom)
  247. @events = StateMachine::EventCollection.new(@machine)
  248. @parked, @idling = @machine.state :parked, :idling
  249. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  250. @machine.events.concat(@events)
  251. @object = @klass.new
  252. end
  253. def test_should_invalidate_if_invalid_event_specified
  254. @object.state_event = 'invalid'
  255. @events.attribute_transition_for(@object, true)
  256. assert_equal ['is invalid'], @object.errors
  257. end
  258. def test_should_invalidate_if_event_cannot_be_fired
  259. @object.state = 'idling'
  260. @object.state_event = 'ignite'
  261. @events.attribute_transition_for(@object, true)
  262. assert_equal ['cannot transition when idling'], @object.errors
  263. end
  264. def test_should_invalidate_with_human_name_if_invalid_event_specified
  265. @idling.human_name = 'waiting'
  266. @object.state = 'idling'
  267. @object.state_event = 'ignite'
  268. @events.attribute_transition_for(@object, true)
  269. assert_equal ['cannot transition when waiting'], @object.errors
  270. end
  271. def test_should_not_invalidate_event_can_be_fired
  272. @ignite.transition :parked => :idling
  273. @object.state_event = 'ignite'
  274. @events.attribute_transition_for(@object, true)
  275. assert_equal [], @object.errors
  276. end
  277. def teardown
  278. StateMachine::Integrations.send(:remove_const, 'Custom')
  279. end
  280. end
  281. class EventCollectionWithCustomMachineAttributeTest < Test::Unit::TestCase
  282. def setup
  283. @klass = Class.new do
  284. def save
  285. end
  286. end
  287. @machine = StateMachine::Machine.new(@klass, :state, :attribute => :state_id, :initial => :parked, :action => :save)
  288. @events = StateMachine::EventCollection.new(@machine)
  289. @machine.state :parked, :idling
  290. @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  291. @machine.events.concat(@events)
  292. @object = @klass.new
  293. end
  294. def test_should_not_have_transition_if_nil
  295. @object.state_event = nil
  296. assert_nil @events.attribute_transition_for(@object)
  297. end
  298. def test_should_have_valid_transition_if_event_can_be_fired
  299. @ignite.transition :parked => :idling
  300. @object.state_event = 'ignite'
  301. assert_instance_of StateMachine::Transition, @events.attribute_transition_for(@object)
  302. end
  303. end