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/callback_test.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 701 lines | 554 code | 147 blank | 0 comment | 6 complexity | 9572d17ff6461e8f56bbe974f1cb5106 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 CallbackTest < Test::Unit::TestCase
  3. def test_should_raise_exception_if_invalid_type_specified
  4. exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:invalid) {} }
  5. assert_equal 'Type must be :before, :after, :around, or :failure', exception.message
  6. end
  7. def test_should_not_raise_exception_if_using_before_type
  8. assert_nothing_raised { StateMachine::Callback.new(:before) {} }
  9. end
  10. def test_should_not_raise_exception_if_using_after_type
  11. assert_nothing_raised { StateMachine::Callback.new(:after) {} }
  12. end
  13. def test_should_not_raise_exception_if_using_around_type
  14. assert_nothing_raised { StateMachine::Callback.new(:around) {} }
  15. end
  16. def test_should_not_raise_exception_if_using_failure_type
  17. assert_nothing_raised { StateMachine::Callback.new(:failure) {} }
  18. end
  19. def test_should_raise_exception_if_no_methods_specified
  20. exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:before) }
  21. assert_equal 'Method(s) for callback must be specified', exception.message
  22. end
  23. def test_should_not_raise_exception_if_method_specified_in_do_option
  24. assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run) }
  25. end
  26. def test_should_not_raise_exception_if_method_specified_as_argument
  27. assert_nothing_raised { StateMachine::Callback.new(:before, :run) }
  28. end
  29. def test_should_not_raise_exception_if_method_specified_as_block
  30. assert_nothing_raised { StateMachine::Callback.new(:before, :run) {} }
  31. end
  32. def test_should_not_raise_exception_if_implicit_option_specified
  33. assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run, :invalid => :valid) }
  34. end
  35. def test_should_not_bind_to_objects
  36. assert !StateMachine::Callback.bind_to_object
  37. end
  38. def test_should_not_have_a_terminator
  39. assert_nil StateMachine::Callback.terminator
  40. end
  41. end
  42. class CallbackByDefaultTest < Test::Unit::TestCase
  43. def setup
  44. @callback = StateMachine::Callback.new(:before) {}
  45. end
  46. def test_should_have_type
  47. assert_equal :before, @callback.type
  48. end
  49. def test_should_not_have_a_terminator
  50. assert_nil @callback.terminator
  51. end
  52. def test_should_have_a_branch_with_all_matcher_requirements
  53. assert_equal StateMachine::AllMatcher.instance, @callback.branch.event_requirement
  54. assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:from]
  55. assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:to]
  56. end
  57. def test_should_not_have_any_known_states
  58. assert_equal [], @callback.known_states
  59. end
  60. end
  61. class CallbackWithMethodArgumentTest < Test::Unit::TestCase
  62. def setup
  63. @callback = StateMachine::Callback.new(:before, lambda {|*args| @args = args})
  64. @object = Object.new
  65. @result = @callback.call(@object)
  66. end
  67. def test_should_be_successful
  68. assert @result
  69. end
  70. def test_should_call_with_empty_context
  71. assert_equal [@object], @args
  72. end
  73. end
  74. class CallbackWithMultipleMethodArgumentsTest < Test::Unit::TestCase
  75. def setup
  76. @callback = StateMachine::Callback.new(:before, :run_1, :run_2)
  77. class << @object = Object.new
  78. attr_accessor :callbacks
  79. def run_1
  80. (@callbacks ||= []) << :run_1
  81. end
  82. def run_2
  83. (@callbacks ||= []) << :run_2
  84. end
  85. end
  86. @result = @callback.call(@object)
  87. end
  88. def test_should_be_successful
  89. assert @result
  90. end
  91. def test_should_call_each_callback_in_order
  92. assert_equal [:run_1, :run_2], @object.callbacks
  93. end
  94. end
  95. class CallbackWithDoMethodTest < Test::Unit::TestCase
  96. def setup
  97. @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})
  98. @object = Object.new
  99. @result = @callback.call(@object)
  100. end
  101. def test_should_be_successful
  102. assert @result
  103. end
  104. def test_should_call_with_empty_context
  105. assert_equal [@object], @args
  106. end
  107. end
  108. class CallbackWithMultipleDoMethodsTest < Test::Unit::TestCase
  109. def setup
  110. @callback = StateMachine::Callback.new(:before, :do => [:run_1, :run_2])
  111. class << @object = Object.new
  112. attr_accessor :callbacks
  113. def run_1
  114. (@callbacks ||= []) << :run_1
  115. end
  116. def run_2
  117. (@callbacks ||= []) << :run_2
  118. end
  119. end
  120. @result = @callback.call(@object)
  121. end
  122. def test_should_be_successful
  123. assert @result
  124. end
  125. def test_should_call_each_callback_in_order
  126. assert_equal [:run_1, :run_2], @object.callbacks
  127. end
  128. end
  129. class CallbackWithBlockTest < Test::Unit::TestCase
  130. def setup
  131. @callback = StateMachine::Callback.new(:before) do |*args|
  132. @args = args
  133. end
  134. @object = Object.new
  135. @result = @callback.call(@object)
  136. end
  137. def test_should_be_successful
  138. assert @result
  139. end
  140. def test_should_call_with_empty_context
  141. assert_equal [@object], @args
  142. end
  143. end
  144. class CallbackWithMixedMethodsTest < Test::Unit::TestCase
  145. def setup
  146. @callback = StateMachine::Callback.new(:before, :run_argument, :do => :run_do) do |object|
  147. object.callbacks << :block
  148. end
  149. class << @object = Object.new
  150. attr_accessor :callbacks
  151. def run_argument
  152. (@callbacks ||= []) << :argument
  153. end
  154. def run_do
  155. (@callbacks ||= []) << :do
  156. end
  157. end
  158. @result = @callback.call(@object)
  159. end
  160. def test_should_be_successful
  161. assert @result
  162. end
  163. def test_should_call_each_callback_in_order
  164. assert_equal [:argument, :do, :block], @object.callbacks
  165. end
  166. end
  167. class CallbackWithExplicitRequirementsTest < Test::Unit::TestCase
  168. def setup
  169. @object = Object.new
  170. @callback = StateMachine::Callback.new(:before, :from => :parked, :to => :idling, :on => :ignite, :do => lambda {})
  171. end
  172. def test_should_call_with_empty_context
  173. assert @callback.call(@object, {})
  174. end
  175. def test_should_not_call_if_from_not_included
  176. assert !@callback.call(@object, :from => :idling)
  177. end
  178. def test_should_not_call_if_to_not_included
  179. assert !@callback.call(@object, :to => :parked)
  180. end
  181. def test_should_not_call_if_on_not_included
  182. assert !@callback.call(@object, :on => :park)
  183. end
  184. def test_should_call_if_all_requirements_met
  185. assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)
  186. end
  187. def test_should_include_in_known_states
  188. assert_equal [:parked, :idling], @callback.known_states
  189. end
  190. end
  191. class CallbackWithImplicitRequirementsTest < Test::Unit::TestCase
  192. def setup
  193. @object = Object.new
  194. @callback = StateMachine::Callback.new(:before, :parked => :idling, :on => :ignite, :do => lambda {})
  195. end
  196. def test_should_call_with_empty_context
  197. assert @callback.call(@object, {})
  198. end
  199. def test_should_not_call_if_from_not_included
  200. assert !@callback.call(@object, :from => :idling)
  201. end
  202. def test_should_not_call_if_to_not_included
  203. assert !@callback.call(@object, :to => :parked)
  204. end
  205. def test_should_not_call_if_on_not_included
  206. assert !@callback.call(@object, :on => :park)
  207. end
  208. def test_should_call_if_all_requirements_met
  209. assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)
  210. end
  211. def test_should_include_in_known_states
  212. assert_equal [:parked, :idling], @callback.known_states
  213. end
  214. end
  215. class CallbackWithIfConditionTest < Test::Unit::TestCase
  216. def setup
  217. @object = Object.new
  218. end
  219. def test_should_call_if_true
  220. callback = StateMachine::Callback.new(:before, :if => lambda {true}, :do => lambda {})
  221. assert callback.call(@object)
  222. end
  223. def test_should_not_call_if_false
  224. callback = StateMachine::Callback.new(:before, :if => lambda {false}, :do => lambda {})
  225. assert !callback.call(@object)
  226. end
  227. end
  228. class CallbackWithUnlessConditionTest < Test::Unit::TestCase
  229. def setup
  230. @object = Object.new
  231. end
  232. def test_should_call_if_false
  233. callback = StateMachine::Callback.new(:before, :unless => lambda {false}, :do => lambda {})
  234. assert callback.call(@object)
  235. end
  236. def test_should_not_call_if_true
  237. callback = StateMachine::Callback.new(:before, :unless => lambda {true}, :do => lambda {})
  238. assert !callback.call(@object)
  239. end
  240. end
  241. class CallbackWithoutTerminatorTest < Test::Unit::TestCase
  242. def setup
  243. @object = Object.new
  244. end
  245. def test_should_not_halt_if_result_is_false
  246. callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => nil)
  247. assert_nothing_thrown { callback.call(@object) }
  248. end
  249. end
  250. class CallbackWithTerminatorTest < Test::Unit::TestCase
  251. def setup
  252. @object = Object.new
  253. end
  254. def test_should_not_halt_if_terminator_does_not_match
  255. callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == true})
  256. assert_nothing_thrown { callback.call(@object) }
  257. end
  258. def test_should_halt_if_terminator_matches
  259. callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == false})
  260. assert_throws(:halt) { callback.call(@object) }
  261. end
  262. def test_should_halt_if_terminator_matches_any_method
  263. callback = StateMachine::Callback.new(:before, :do => [lambda {true}, lambda {false}], :terminator => lambda {|result| result == false})
  264. assert_throws(:halt) { callback.call(@object) }
  265. end
  266. end
  267. class CallbackWithoutArgumentsTest < Test::Unit::TestCase
  268. def setup
  269. @callback = StateMachine::Callback.new(:before, :do => lambda {|object| @arg = object})
  270. @object = Object.new
  271. @callback.call(@object, {}, 1, 2, 3)
  272. end
  273. def test_should_call_method_with_object_as_argument
  274. assert_equal @object, @arg
  275. end
  276. end
  277. class CallbackWithArgumentsTest < Test::Unit::TestCase
  278. def setup
  279. @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})
  280. @object = Object.new
  281. @callback.call(@object, {}, 1, 2, 3)
  282. end
  283. def test_should_call_method_with_all_arguments
  284. assert_equal [@object, 1, 2, 3], @args
  285. end
  286. end
  287. class CallbackWithUnboundMethodTest < Test::Unit::TestCase
  288. def setup
  289. @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @context = args.unshift(self)})
  290. @object = Object.new
  291. @callback.call(@object, {}, 1, 2, 3)
  292. end
  293. def test_should_call_method_outside_the_context_of_the_object
  294. assert_equal [self, @object, 1, 2, 3], @context
  295. end
  296. end
  297. class CallbackWithBoundMethodTest < Test::Unit::TestCase
  298. def setup
  299. @object = Object.new
  300. end
  301. def test_should_call_method_within_the_context_of_the_object_for_block_methods
  302. context = nil
  303. callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = [self] + args}, :bind_to_object => true)
  304. callback.call(@object, {}, 1, 2, 3)
  305. assert_equal [@object, 1, 2, 3], context
  306. end
  307. def test_should_ignore_option_for_symbolic_methods
  308. class << @object
  309. attr_reader :context
  310. def after_ignite(*args)
  311. @context = args
  312. end
  313. end
  314. callback = StateMachine::Callback.new(:before, :do => :after_ignite, :bind_to_object => true)
  315. callback.call(@object)
  316. assert_equal [], @object.context
  317. end
  318. def test_should_ignore_option_for_string_methods
  319. callback = StateMachine::Callback.new(:before, :do => '[1, 2, 3]', :bind_to_object => true)
  320. assert callback.call(@object)
  321. end
  322. end
  323. class CallbackWithMultipleBoundMethodsTest < Test::Unit::TestCase
  324. def setup
  325. @object = Object.new
  326. first_context = nil
  327. second_context = nil
  328. @callback = StateMachine::Callback.new(:before, :do => [lambda {first_context = self}, lambda {second_context = self}], :bind_to_object => true)
  329. @callback.call(@object)
  330. @first_context = first_context
  331. @second_context = second_context
  332. end
  333. def test_should_call_each_method_within_the_context_of_the_object
  334. assert_equal @object, @first_context
  335. assert_equal @object, @second_context
  336. end
  337. end
  338. class CallbackWithApplicationBoundObjectTest < Test::Unit::TestCase
  339. def setup
  340. @original_bind_to_object = StateMachine::Callback.bind_to_object
  341. StateMachine::Callback.bind_to_object = true
  342. context = nil
  343. @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = self})
  344. @object = Object.new
  345. @callback.call(@object)
  346. @context = context
  347. end
  348. def test_should_call_method_within_the_context_of_the_object
  349. assert_equal @object, @context
  350. end
  351. def teardown
  352. StateMachine::Callback.bind_to_object = @original_bind_to_object
  353. end
  354. end
  355. class CallbackWithBoundMethodAndArgumentsTest < Test::Unit::TestCase
  356. def setup
  357. @object = Object.new
  358. end
  359. def test_should_include_single_argument_if_specified
  360. context = nil
  361. callback = StateMachine::Callback.new(:before, :do => lambda {|arg1| context = [arg1]}, :bind_to_object => true)
  362. callback.call(@object, {}, 1)
  363. assert_equal [1], context
  364. end
  365. def test_should_include_multiple_arguments_if_specified
  366. context = nil
  367. callback = StateMachine::Callback.new(:before, :do => lambda {|arg1, arg2, arg3| context = [arg1, arg2, arg3]}, :bind_to_object => true)
  368. callback.call(@object, {}, 1, 2, 3)
  369. assert_equal [1, 2, 3], context
  370. end
  371. def test_should_include_arguments_if_splat_used
  372. context = nil
  373. callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = args}, :bind_to_object => true)
  374. callback.call(@object, {}, 1, 2, 3)
  375. assert_equal [1, 2, 3], context
  376. end
  377. end
  378. class CallbackWithApplicationTerminatorTest < Test::Unit::TestCase
  379. def setup
  380. @original_terminator = StateMachine::Callback.bind_to_object
  381. StateMachine::Callback.terminator = lambda {|result| result == false}
  382. @object = Object.new
  383. end
  384. def test_should_not_halt_if_terminator_does_not_match
  385. callback = StateMachine::Callback.new(:before, :do => lambda {true})
  386. assert_nothing_thrown { callback.call(@object) }
  387. end
  388. def test_should_halt_if_terminator_matches
  389. callback = StateMachine::Callback.new(:before, :do => lambda {false})
  390. assert_throws(:halt) { callback.call(@object) }
  391. end
  392. def teardown
  393. StateMachine::Callback.bind_to_object = @original_bind_to_object
  394. end
  395. end
  396. class CallbackWithAroundTypeAndBlockTest < Test::Unit::TestCase
  397. def setup
  398. @object = Object.new
  399. @callbacks = []
  400. end
  401. def test_should_evaluate_before_without_after
  402. callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})
  403. assert callback.call(@object)
  404. assert_equal [@object], @args
  405. end
  406. def test_should_evaluate_after_without_before
  407. callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; block.call; @args = args})
  408. assert callback.call(@object)
  409. assert_equal [@object], @args
  410. end
  411. def test_should_halt_if_not_yielded
  412. callback = StateMachine::Callback.new(:around, lambda {|block|})
  413. assert_throws(:halt) { callback.call(@object) }
  414. end
  415. def test_should_call_block_after_before
  416. callback = StateMachine::Callback.new(:around, lambda {|block| @callbacks << :before; block.call})
  417. assert callback.call(@object) { @callbacks << :block }
  418. assert_equal [:before, :block], @callbacks
  419. end
  420. def test_should_call_block_before_after
  421. @callbacks = []
  422. callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})
  423. assert callback.call(@object) { @callbacks << :block }
  424. assert_equal [:block, :after], @callbacks
  425. end
  426. def test_should_halt_if_block_halts
  427. callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})
  428. assert_throws(:halt) { callback.call(@object) { throw :halt } }
  429. assert_equal [], @callbacks
  430. end
  431. end
  432. class CallbackWithAroundTypeAndMultipleMethodsTest < Test::Unit::TestCase
  433. def setup
  434. @callback = StateMachine::Callback.new(:around, :run_1, :run_2)
  435. class << @object = Object.new
  436. attr_accessor :before_callbacks
  437. attr_accessor :after_callbacks
  438. def run_1
  439. (@before_callbacks ||= []) << :run_1
  440. yield
  441. (@after_callbacks ||= []) << :run_1
  442. end
  443. def run_2
  444. (@before_callbacks ||= []) << :run_2
  445. yield
  446. (@after_callbacks ||= []) << :run_2
  447. end
  448. end
  449. end
  450. def test_should_succeed
  451. assert @callback.call(@object)
  452. end
  453. def test_should_evaluate_before_callbacks_in_order
  454. @callback.call(@object)
  455. assert_equal [:run_1, :run_2], @object.before_callbacks
  456. end
  457. def test_should_evaluate_after_callbacks_in_reverse_order
  458. @callback.call(@object)
  459. assert_equal [:run_2, :run_1], @object.after_callbacks
  460. end
  461. def test_should_call_block_after_before_callbacks
  462. @callback.call(@object) { (@object.before_callbacks ||= []) << :block }
  463. assert_equal [:run_1, :run_2, :block], @object.before_callbacks
  464. end
  465. def test_should_call_block_before_after_callbacks
  466. @callback.call(@object) { (@object.after_callbacks ||= []) << :block }
  467. assert_equal [:block, :run_2, :run_1], @object.after_callbacks
  468. end
  469. def test_should_halt_if_first_doesnt_yield
  470. class << @object
  471. def run_1
  472. (@before_callbacks ||= []) << :run_1
  473. end
  474. end
  475. catch(:halt) do
  476. @callback.call(@object) { (@object.before_callbacks ||= []) << :block }
  477. end
  478. assert_equal [:run_1], @object.before_callbacks
  479. assert_nil @object.after_callbacks
  480. end
  481. def test_should_halt_if_last_doesnt_yield
  482. class << @object
  483. def run_2
  484. (@before_callbacks ||= []) << :run_2
  485. end
  486. end
  487. catch(:halt) { @callback.call(@object) }
  488. assert_equal [:run_1, :run_2], @object.before_callbacks
  489. assert_nil @object.after_callbacks
  490. end
  491. def test_should_not_evaluate_further_methods_if_after_halts
  492. class << @object
  493. def run_2
  494. (@before_callbacks ||= []) << :run_2
  495. yield
  496. (@after_callbacks ||= []) << :run_2
  497. throw :halt
  498. end
  499. end
  500. catch(:halt) { @callback.call(@object) }
  501. assert_equal [:run_1, :run_2], @object.before_callbacks
  502. assert_equal [:run_2], @object.after_callbacks
  503. end
  504. end
  505. class CallbackWithAroundTypeAndArgumentsTest < Test::Unit::TestCase
  506. def setup
  507. @object = Object.new
  508. end
  509. def test_should_include_object_if_specified
  510. callback = StateMachine::Callback.new(:around, lambda {|object, block| @args = [object]; block.call})
  511. callback.call(@object)
  512. assert_equal [@object], @args
  513. end
  514. def test_should_include_arguments_if_specified
  515. callback = StateMachine::Callback.new(:around, lambda {|object, arg1, arg2, arg3, block| @args = [object, arg1, arg2, arg3]; block.call})
  516. callback.call(@object, {}, 1, 2, 3)
  517. assert_equal [@object, 1, 2, 3], @args
  518. end
  519. def test_should_include_arguments_if_splat_used
  520. callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})
  521. callback.call(@object, {}, 1, 2, 3)
  522. assert_equal [@object, 1, 2, 3], @args
  523. end
  524. end
  525. class CallbackWithAroundTypeAndTerminatorTest < Test::Unit::TestCase
  526. def setup
  527. @object = Object.new
  528. end
  529. def test_should_not_halt_if_terminator_does_not_match
  530. callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == true})
  531. assert_nothing_thrown { callback.call(@object) }
  532. end
  533. def test_should_not_halt_if_terminator_matches
  534. callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == false})
  535. assert_nothing_thrown { callback.call(@object) }
  536. end
  537. end
  538. class CallbackWithAroundTypeAndBoundMethodTest < Test::Unit::TestCase
  539. def setup
  540. @object = Object.new
  541. end
  542. def test_should_call_method_within_the_context_of_the_object
  543. context = nil
  544. callback = StateMachine::Callback.new(:around, :do => lambda {|block| context = self; block.call}, :bind_to_object => true)
  545. callback.call(@object, {}, 1, 2, 3)
  546. assert_equal @object, context
  547. end
  548. def test_should_include_arguments_if_specified
  549. context = nil
  550. callback = StateMachine::Callback.new(:around, :do => lambda {|*args| block = args.pop; context = args; block.call}, :bind_to_object => true)
  551. callback.call(@object, {}, 1, 2, 3)
  552. assert_equal [1, 2, 3], context
  553. end
  554. end