PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 441 lines | 343 code | 98 blank | 0 comment | 1 complexity | cf80f870fc21bc0871e087f4d083e3ed 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 Validateable
  3. class << self
  4. def validate(*args, &block)
  5. args << block if block_given?
  6. args
  7. end
  8. end
  9. end
  10. class StateContextTest < Test::Unit::TestCase
  11. def setup
  12. @klass = Class.new(Validateable)
  13. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  14. @state = @machine.state :parked
  15. @state_context = StateMachine::StateContext.new(@state)
  16. end
  17. def test_should_have_a_machine
  18. assert_equal @machine, @state_context.machine
  19. end
  20. def test_should_have_a_state
  21. assert_equal @state, @state_context.state
  22. end
  23. end
  24. class StateContextTransitionTest < Test::Unit::TestCase
  25. def setup
  26. @klass = Class.new
  27. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  28. @state = @machine.state :parked
  29. @state_context = StateMachine::StateContext.new(@state)
  30. end
  31. def test_should_not_allow_except_to
  32. exception = assert_raise(ArgumentError) { @state_context.transition(:except_to => :idling) }
  33. assert_equal 'Invalid key(s): except_to', exception.message
  34. end
  35. def test_should_not_allow_except_from
  36. exception = assert_raise(ArgumentError) { @state_context.transition(:except_from => :idling) }
  37. assert_equal 'Invalid key(s): except_from', exception.message
  38. end
  39. def test_should_not_allow_implicit_transitions
  40. exception = assert_raise(ArgumentError) { @state_context.transition(:parked => :idling) }
  41. assert_equal 'Invalid key(s): parked', exception.message
  42. end
  43. def test_should_not_allow_except_on
  44. exception = assert_raise(ArgumentError) { @state_context.transition(:except_on => :park) }
  45. assert_equal 'Invalid key(s): except_on', exception.message
  46. end
  47. def test_should_require_on_event
  48. exception = assert_raise(ArgumentError) { @state_context.transition(:to => :idling) }
  49. assert_equal 'Must specify :on event', exception.message
  50. end
  51. def test_should_not_allow_missing_from_and_to
  52. exception = assert_raise(ArgumentError) { @state_context.transition(:on => :ignite) }
  53. assert_equal 'Must specify either :to or :from state', exception.message
  54. end
  55. def test_should_not_allow_from_and_to
  56. exception = assert_raise(ArgumentError) { @state_context.transition(:on => :ignite, :from => :parked, :to => :idling) }
  57. assert_equal 'Must specify either :to or :from state', exception.message
  58. end
  59. def test_should_allow_to_state_if_missing_from_state
  60. assert_nothing_raised { @state_context.transition(:on => :park, :from => :parked) }
  61. end
  62. def test_should_allow_from_state_if_missing_to_state
  63. assert_nothing_raised { @state_context.transition(:on => :ignite, :to => :idling) }
  64. end
  65. def test_should_automatically_set_to_option_with_from_state
  66. branch = @state_context.transition(:from => :idling, :on => :park)
  67. assert_instance_of StateMachine::Branch, branch
  68. state_requirements = branch.state_requirements
  69. assert_equal 1, state_requirements.length
  70. from_requirement = state_requirements[0][:to]
  71. assert_instance_of StateMachine::WhitelistMatcher, from_requirement
  72. assert_equal [:parked], from_requirement.values
  73. end
  74. def test_should_automatically_set_from_option_with_to_state
  75. branch = @state_context.transition(:to => :idling, :on => :ignite)
  76. assert_instance_of StateMachine::Branch, branch
  77. state_requirements = branch.state_requirements
  78. assert_equal 1, state_requirements.length
  79. from_requirement = state_requirements[0][:from]
  80. assert_instance_of StateMachine::WhitelistMatcher, from_requirement
  81. assert_equal [:parked], from_requirement.values
  82. end
  83. def test_should_allow_if_condition
  84. assert_nothing_raised {@state_context.transition(:to => :idling, :on => :park, :if => :seatbelt_on?)}
  85. end
  86. def test_should_allow_unless_condition
  87. assert_nothing_raised {@state_context.transition(:to => :idling, :on => :park, :unless => :seatbelt_off?)}
  88. end
  89. def test_should_include_all_transition_states_in_machine_states
  90. @state_context.transition(:to => :idling, :on => :ignite)
  91. assert_equal [:parked, :idling], @machine.states.map {|state| state.name}
  92. end
  93. def test_should_include_all_transition_events_in_machine_events
  94. @state_context.transition(:to => :idling, :on => :ignite)
  95. assert_equal [:ignite], @machine.events.map {|event| event.name}
  96. end
  97. def test_should_allow_multiple_events
  98. @state_context.transition(:to => :idling, :on => [:ignite, :shift_up])
  99. assert_equal [:ignite, :shift_up], @machine.events.map {|event| event.name}
  100. end
  101. end
  102. class StateContextWithMatchingTransitionTest < Test::Unit::TestCase
  103. def setup
  104. @klass = Class.new
  105. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  106. @state = @machine.state :parked
  107. @state_context = StateMachine::StateContext.new(@state)
  108. @state_context.transition(:to => :idling, :on => :ignite)
  109. @event = @machine.event(:ignite)
  110. @object = @klass.new
  111. end
  112. def test_should_be_able_to_fire
  113. assert @event.can_fire?(@object)
  114. end
  115. def test_should_have_a_transition
  116. transition = @event.transition_for(@object)
  117. assert_not_nil transition
  118. assert_equal 'parked', transition.from
  119. assert_equal 'idling', transition.to
  120. assert_equal :ignite, transition.event
  121. end
  122. end
  123. class StateContextProxyTest < Test::Unit::TestCase
  124. def setup
  125. @klass = Class.new(Validateable)
  126. machine = StateMachine::Machine.new(@klass, :initial => :parked)
  127. state = machine.state :parked
  128. @state_context = StateMachine::StateContext.new(state)
  129. end
  130. def test_should_call_class_with_same_arguments
  131. options = {}
  132. validation = @state_context.validate(:name, options)
  133. assert_equal [:name, options], validation
  134. end
  135. def test_should_pass_block_through_to_class
  136. options = {}
  137. proxy_block = lambda {}
  138. validation = @state_context.validate(:name, options, &proxy_block)
  139. assert_equal [:name, options, proxy_block], validation
  140. end
  141. end
  142. class StateContextProxyWithoutConditionsTest < Test::Unit::TestCase
  143. def setup
  144. @klass = Class.new(Validateable)
  145. machine = StateMachine::Machine.new(@klass, :initial => :parked)
  146. state = machine.state :parked
  147. @state_context = StateMachine::StateContext.new(state)
  148. @object = @klass.new
  149. @options = @state_context.validate[0]
  150. end
  151. def test_should_have_options_configuration
  152. assert_instance_of Hash, @options
  153. end
  154. def test_should_have_if_option
  155. assert_not_nil @options[:if]
  156. end
  157. def test_should_be_false_if_state_is_different
  158. @object.state = nil
  159. assert !@options[:if].call(@object)
  160. end
  161. def test_should_be_true_if_state_matches
  162. assert @options[:if].call(@object)
  163. end
  164. end
  165. class StateContextProxyWithIfConditionTest < Test::Unit::TestCase
  166. def setup
  167. @klass = Class.new(Validateable)
  168. machine = StateMachine::Machine.new(@klass, :initial => :parked)
  169. state = machine.state :parked
  170. @state_context = StateMachine::StateContext.new(state)
  171. @object = @klass.new
  172. @condition_result = nil
  173. @options = @state_context.validate(:if => lambda {@condition_result})[0]
  174. end
  175. def test_should_have_if_option
  176. assert_not_nil @options[:if]
  177. end
  178. def test_should_be_false_if_state_is_different
  179. @object.state = nil
  180. assert !@options[:if].call(@object)
  181. end
  182. def test_should_be_false_if_original_condition_is_false
  183. @condition_result = false
  184. assert !@options[:if].call(@object)
  185. end
  186. def test_should_be_true_if_state_matches_and_original_condition_is_true
  187. @condition_result = true
  188. assert @options[:if].call(@object)
  189. end
  190. def test_should_evaluate_symbol_condition
  191. @klass.class_eval do
  192. attr_accessor :callback
  193. end
  194. options = @state_context.validate(:if => :callback)[0]
  195. object = @klass.new
  196. object.callback = false
  197. assert !options[:if].call(object)
  198. object.callback = true
  199. assert options[:if].call(object)
  200. end
  201. def test_should_evaluate_string_condition
  202. @klass.class_eval do
  203. attr_accessor :callback
  204. end
  205. options = @state_context.validate(:if => '@callback')[0]
  206. object = @klass.new
  207. object.callback = false
  208. assert !options[:if].call(object)
  209. object.callback = true
  210. assert options[:if].call(object)
  211. end
  212. end
  213. class StateContextProxyWithMultipleIfConditionsTest < Test::Unit::TestCase
  214. def setup
  215. @klass = Class.new(Validateable)
  216. machine = StateMachine::Machine.new(@klass, :initial => :parked)
  217. state = machine.state :parked
  218. @state_context = StateMachine::StateContext.new(state)
  219. @object = @klass.new
  220. @first_condition_result = nil
  221. @second_condition_result = nil
  222. @options = @state_context.validate(:if => [lambda {@first_condition_result}, lambda {@second_condition_result}])[0]
  223. end
  224. def test_should_be_true_if_all_conditions_are_true
  225. @first_condition_result = true
  226. @second_condition_result = true
  227. assert @options[:if].call(@object)
  228. end
  229. def test_should_be_false_if_any_condition_is_false
  230. @first_condition_result = true
  231. @second_condition_result = false
  232. assert !@options[:if].call(@object)
  233. @first_condition_result = false
  234. @second_condition_result = true
  235. assert !@options[:if].call(@object)
  236. end
  237. end
  238. class StateContextProxyWithUnlessConditionTest < Test::Unit::TestCase
  239. def setup
  240. @klass = Class.new(Validateable)
  241. machine = StateMachine::Machine.new(@klass, :initial => :parked)
  242. state = machine.state :parked
  243. @state_context = StateMachine::StateContext.new(state)
  244. @object = @klass.new
  245. @condition_result = nil
  246. @options = @state_context.validate(:unless => lambda {@condition_result})[0]
  247. end
  248. def test_should_have_if_option
  249. assert_not_nil @options[:if]
  250. end
  251. def test_should_be_false_if_state_is_different
  252. @object.state = nil
  253. assert !@options[:if].call(@object)
  254. end
  255. def test_should_be_false_if_original_condition_is_true
  256. @condition_result = true
  257. assert !@options[:if].call(@object)
  258. end
  259. def test_should_be_true_if_state_matches_and_original_condition_is_false
  260. @condition_result = false
  261. assert @options[:if].call(@object)
  262. end
  263. def test_should_evaluate_symbol_condition
  264. @klass.class_eval do
  265. attr_accessor :callback
  266. end
  267. options = @state_context.validate(:unless => :callback)[0]
  268. object = @klass.new
  269. object.callback = true
  270. assert !options[:if].call(object)
  271. object.callback = false
  272. assert options[:if].call(object)
  273. end
  274. def test_should_evaluate_string_condition
  275. @klass.class_eval do
  276. attr_accessor :callback
  277. end
  278. options = @state_context.validate(:unless => '@callback')[0]
  279. object = @klass.new
  280. object.callback = true
  281. assert !options[:if].call(object)
  282. object.callback = false
  283. assert options[:if].call(object)
  284. end
  285. end
  286. class StateContextProxyWithMultipleUnlessConditionsTest < Test::Unit::TestCase
  287. def setup
  288. @klass = Class.new(Validateable)
  289. machine = StateMachine::Machine.new(@klass, :initial => :parked)
  290. state = machine.state :parked
  291. @state_context = StateMachine::StateContext.new(state)
  292. @object = @klass.new
  293. @first_condition_result = nil
  294. @second_condition_result = nil
  295. @options = @state_context.validate(:unless => [lambda {@first_condition_result}, lambda {@second_condition_result}])[0]
  296. end
  297. def test_should_be_true_if_all_conditions_are_false
  298. @first_condition_result = false
  299. @second_condition_result = false
  300. assert @options[:if].call(@object)
  301. end
  302. def test_should_be_false_if_any_condition_is_true
  303. @first_condition_result = true
  304. @second_condition_result = false
  305. assert !@options[:if].call(@object)
  306. @first_condition_result = false
  307. @second_condition_result = true
  308. assert !@options[:if].call(@object)
  309. end
  310. end
  311. class StateContextProxyWithIfAndUnlessConditionsTest < Test::Unit::TestCase
  312. def setup
  313. @klass = Class.new(Validateable)
  314. machine = StateMachine::Machine.new(@klass, :initial => :parked)
  315. state = machine.state :parked
  316. @state_context = StateMachine::StateContext.new(state)
  317. @object = @klass.new
  318. @if_condition_result = nil
  319. @unless_condition_result = nil
  320. @options = @state_context.validate(:if => lambda {@if_condition_result}, :unless => lambda {@unless_condition_result})[0]
  321. end
  322. def test_should_be_false_if_if_condition_is_false
  323. @if_condition_result = false
  324. @unless_condition_result = false
  325. assert !@options[:if].call(@object)
  326. @if_condition_result = false
  327. @unless_condition_result = true
  328. assert !@options[:if].call(@object)
  329. end
  330. def test_should_be_false_if_unless_condition_is_true
  331. @if_condition_result = false
  332. @unless_condition_result = true
  333. assert !@options[:if].call(@object)
  334. @if_condition_result = true
  335. @unless_condition_result = true
  336. assert !@options[:if].call(@object)
  337. end
  338. def test_should_be_true_if_if_condition_is_true_and_unless_condition_is_false
  339. @if_condition_result = true
  340. @unless_condition_result = false
  341. assert @options[:if].call(@object)
  342. end
  343. end