PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 266 lines | 222 code | 44 blank | 0 comment | 0 complexity | 4ab9eaba9df59822f3c1f9ce4a7bde82 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 PathCollectionByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @klass = Class.new
  5. @machine = StateMachine::Machine.new(@klass)
  6. @machine.state :parked
  7. @object = @klass.new
  8. @object.state = 'parked'
  9. @paths = StateMachine::PathCollection.new(@object, @machine)
  10. end
  11. def test_should_have_an_object
  12. assert_equal @object, @paths.object
  13. end
  14. def test_should_have_a_machine
  15. assert_equal @machine, @paths.machine
  16. end
  17. def test_should_have_a_from_name
  18. assert_equal :parked, @paths.from_name
  19. end
  20. def test_should_not_have_a_to_name
  21. assert_nil @paths.to_name
  22. end
  23. def test_should_have_no_from_states
  24. assert_equal [], @paths.from_states
  25. end
  26. def test_should_have_no_to_states
  27. assert_equal [], @paths.to_states
  28. end
  29. def test_should_have_no_events
  30. assert_equal [], @paths.events
  31. end
  32. def test_should_have_no_paths
  33. assert @paths.empty?
  34. end
  35. end
  36. class PathCollectionTest < Test::Unit::TestCase
  37. def setup
  38. @klass = Class.new
  39. @machine = StateMachine::Machine.new(@klass)
  40. @object = @klass.new
  41. end
  42. def test_should_raise_exception_if_invalid_option_specified
  43. exception = assert_raise(ArgumentError) {StateMachine::PathCollection.new(@object, @machine, :invalid => true)}
  44. assert_equal 'Invalid key(s): invalid', exception.message
  45. end
  46. def test_should_raise_exception_if_invalid_from_state_specified
  47. exception = assert_raise(IndexError) {StateMachine::PathCollection.new(@object, @machine, :from => :invalid)}
  48. assert_equal ':invalid is an invalid name', exception.message
  49. end
  50. def test_should_raise_exception_if_invalid_to_state_specified
  51. exception = assert_raise(IndexError) {StateMachine::PathCollection.new(@object, @machine, :to => :invalid)}
  52. assert_equal ':invalid is an invalid name', exception.message
  53. end
  54. end
  55. class PathCollectionWithPathsTest < Test::Unit::TestCase
  56. def setup
  57. @klass = Class.new
  58. @machine = StateMachine::Machine.new(@klass)
  59. @machine.state :parked, :idling, :first_gear
  60. @machine.event :ignite do
  61. transition :parked => :idling
  62. end
  63. @machine.event :shift_up do
  64. transition :idling => :first_gear
  65. end
  66. @object = @klass.new
  67. @object.state = 'parked'
  68. @paths = StateMachine::PathCollection.new(@object, @machine)
  69. end
  70. def test_should_enumerate_paths
  71. assert_equal [[
  72. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
  73. StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)
  74. ]], @paths
  75. end
  76. def test_should_have_a_from_name
  77. assert_equal :parked, @paths.from_name
  78. end
  79. def test_should_not_have_a_to_name
  80. assert_nil @paths.to_name
  81. end
  82. def test_should_have_from_states
  83. assert_equal [:parked, :idling], @paths.from_states
  84. end
  85. def test_should_have_to_states
  86. assert_equal [:idling, :first_gear], @paths.to_states
  87. end
  88. def test_should_have_no_events
  89. assert_equal [:ignite, :shift_up], @paths.events
  90. end
  91. end
  92. class PathWithGuardedPathsTest < Test::Unit::TestCase
  93. def setup
  94. @klass = Class.new
  95. @machine = StateMachine::Machine.new(@klass)
  96. @machine.state :parked, :idling, :first_gear
  97. @machine.event :ignite do
  98. transition :parked => :idling, :if => lambda {false}
  99. end
  100. @object = @klass.new
  101. @object.state = 'parked'
  102. end
  103. def test_should_not_enumerate_paths_if_guard_enabled
  104. assert_equal [], StateMachine::PathCollection.new(@object, @machine)
  105. end
  106. def test_should_enumerate_paths_if_guard_disabled
  107. paths = StateMachine::PathCollection.new(@object, @machine, :guard => false)
  108. assert_equal [[
  109. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  110. ]], paths
  111. end
  112. end
  113. class PathCollectionWithDuplicateNodesTest < Test::Unit::TestCase
  114. def setup
  115. @klass = Class.new
  116. @machine = StateMachine::Machine.new(@klass)
  117. @machine.state :parked, :idling
  118. @machine.event :shift_up do
  119. transition :parked => :idling, :idling => :first_gear
  120. end
  121. @machine.event :park do
  122. transition :first_gear => :idling
  123. end
  124. @object = @klass.new
  125. @object.state = 'parked'
  126. @paths = StateMachine::PathCollection.new(@object, @machine)
  127. end
  128. def test_should_not_include_duplicates_in_from_states
  129. assert_equal [:parked, :idling, :first_gear], @paths.from_states
  130. end
  131. def test_should_not_include_duplicates_in_to_states
  132. assert_equal [:idling, :first_gear], @paths.to_states
  133. end
  134. def test_should_not_include_duplicates_in_events
  135. assert_equal [:shift_up, :park], @paths.events
  136. end
  137. end
  138. class PathCollectionWithFromStateTest < Test::Unit::TestCase
  139. def setup
  140. @klass = Class.new
  141. @machine = StateMachine::Machine.new(@klass)
  142. @machine.state :parked, :idling, :first_gear
  143. @machine.event :park do
  144. transition :idling => :parked
  145. end
  146. @object = @klass.new
  147. @object.state = 'parked'
  148. @paths = StateMachine::PathCollection.new(@object, @machine, :from => :idling)
  149. end
  150. def test_should_generate_paths_from_custom_from_state
  151. assert_equal [[
  152. StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
  153. ]], @paths
  154. end
  155. def test_should_have_a_from_name
  156. assert_equal :idling, @paths.from_name
  157. end
  158. end
  159. class PathCollectionWithToStateTest < Test::Unit::TestCase
  160. def setup
  161. @klass = Class.new
  162. @machine = StateMachine::Machine.new(@klass)
  163. @machine.state :parked, :idling
  164. @machine.event :ignite do
  165. transition :parked => :idling
  166. end
  167. @machine.event :shift_up do
  168. transition :parked => :idling, :idling => :first_gear
  169. end
  170. @machine.event :shift_down do
  171. transition :first_gear => :idling
  172. end
  173. @object = @klass.new
  174. @object.state = 'parked'
  175. @paths = StateMachine::PathCollection.new(@object, @machine, :to => :idling)
  176. end
  177. def test_should_stop_paths_once_target_state_reached
  178. assert_equal [
  179. [StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)],
  180. [StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :idling)]
  181. ], @paths
  182. end
  183. end
  184. class PathCollectionWithDeepPathsTest < Test::Unit::TestCase
  185. def setup
  186. @klass = Class.new
  187. @machine = StateMachine::Machine.new(@klass)
  188. @machine.state :parked, :idling
  189. @machine.event :ignite do
  190. transition :parked => :idling
  191. end
  192. @machine.event :shift_up do
  193. transition :parked => :idling, :idling => :first_gear
  194. end
  195. @machine.event :shift_down do
  196. transition :first_gear => :idling
  197. end
  198. @object = @klass.new
  199. @object.state = 'parked'
  200. @paths = StateMachine::PathCollection.new(@object, @machine, :to => :idling, :deep => true)
  201. end
  202. def test_should_allow_target_to_be_reached_more_than_once_per_path
  203. assert_equal [
  204. [
  205. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  206. ],
  207. [
  208. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
  209. StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear),
  210. StateMachine::Transition.new(@object, @machine, :shift_down, :first_gear, :idling)
  211. ],
  212. [
  213. StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :idling)
  214. ],
  215. [
  216. StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :idling),
  217. StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear),
  218. StateMachine::Transition.new(@object, @machine, :shift_down, :first_gear, :idling)
  219. ]
  220. ], @paths
  221. end
  222. end