PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 2148 lines | 1681 code | 467 blank | 0 comment | 7 complexity | 9b3e30e92805fd24c0188ec54f76cebc MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, GPL-2.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class TransitionCollectionTest < Test::Unit::TestCase
  3. def test_should_raise_exception_if_invalid_option_specified
  4. exception = assert_raise(ArgumentError) {StateMachine::TransitionCollection.new([], :invalid => true)}
  5. end
  6. def test_should_raise_exception_if_multiple_transitions_for_same_attribute_specified
  7. @klass = Class.new
  8. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  9. @machine.state :parked, :idling
  10. @machine.event :ignite
  11. @object = @klass.new
  12. exception = assert_raise(ArgumentError) do
  13. StateMachine::TransitionCollection.new([
  14. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
  15. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  16. ])
  17. end
  18. assert_equal 'Cannot perform multiple transitions in parallel for the same state machine attribute', exception.message
  19. end
  20. end
  21. class TransitionCollectionByDefaultTest < Test::Unit::TestCase
  22. def setup
  23. @transitions = StateMachine::TransitionCollection.new
  24. end
  25. def test_should_not_skip_actions
  26. assert !@transitions.skip_actions
  27. end
  28. def test_should_not_skip_after
  29. assert !@transitions.skip_after
  30. end
  31. def test_should_use_transaction
  32. assert @transitions.use_transaction
  33. end
  34. def test_should_be_empty
  35. assert @transitions.empty?
  36. end
  37. end
  38. class TransitionCollectionEmptyWithoutBlockTest < Test::Unit::TestCase
  39. def setup
  40. @transitions = StateMachine::TransitionCollection.new
  41. @result = @transitions.perform
  42. end
  43. def test_should_succeed
  44. assert_equal true, @result
  45. end
  46. end
  47. class TransitionCollectionEmptyWithBlockTest < Test::Unit::TestCase
  48. def setup
  49. @transitions = StateMachine::TransitionCollection.new
  50. end
  51. def test_should_raise_exception_if_perform_raises_exception
  52. assert_raise(ArgumentError) { @transitions.perform { raise ArgumentError } }
  53. end
  54. def test_should_use_block_result_if_non_boolean
  55. assert_equal 1, @transitions.perform { 1 }
  56. end
  57. def test_should_use_block_result_if_false
  58. assert_equal false, @transitions.perform { false }
  59. end
  60. def test_should_use_block_reslut_if_nil
  61. assert_equal nil, @transitions.perform { nil }
  62. end
  63. end
  64. class TransitionCollectionInvalidTest < Test::Unit::TestCase
  65. def setup
  66. @transitions = StateMachine::TransitionCollection.new([false])
  67. end
  68. def test_should_be_empty
  69. assert @transitions.empty?
  70. end
  71. def test_should_not_succeed
  72. assert_equal false, @transitions.perform
  73. end
  74. def test_should_not_run_perform_block
  75. ran_block = false
  76. @transitions.perform { ran_block = true }
  77. assert !ran_block
  78. end
  79. end
  80. class TransitionCollectionPartialInvalidTest < Test::Unit::TestCase
  81. def setup
  82. @klass = Class.new do
  83. attr_accessor :ran_transaction
  84. end
  85. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  86. @machine.state :idling
  87. @machine.event :ignite
  88. @machine.before_transition {@ran_before = true}
  89. @machine.after_transition {@ran_after = true}
  90. @machine.around_transition {|block| @ran_around_before = true; block.call; @ran_around_after = true}
  91. class << @machine
  92. def within_transaction(object)
  93. object.ran_transaction = true
  94. end
  95. end
  96. @object = @klass.new
  97. @transitions = StateMachine::TransitionCollection.new([
  98. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
  99. false
  100. ])
  101. end
  102. def test_should_not_store_invalid_values
  103. assert_equal 1, @transitions.length
  104. end
  105. def test_should_not_succeed
  106. assert_equal false, @transitions.perform
  107. end
  108. def test_should_not_start_transaction
  109. assert !@object.ran_transaction
  110. end
  111. def test_should_not_run_perform_block
  112. ran_block = false
  113. @transitions.perform { ran_block = true }
  114. assert !ran_block
  115. end
  116. def test_should_not_run_before_callbacks
  117. assert !@ran_before
  118. end
  119. def test_should_not_persist_states
  120. assert_equal 'parked', @object.state
  121. end
  122. def test_should_not_run_after_callbacks
  123. assert !@ran_after
  124. end
  125. def test_should_not_run_around_callbacks_before_yield
  126. assert !@ran_around_before
  127. end
  128. def test_should_not_run_around_callbacks_after_yield
  129. assert !@ran_around_after
  130. end
  131. end
  132. class TransitionCollectionValidTest < Test::Unit::TestCase
  133. def setup
  134. @klass = Class.new do
  135. attr_reader :persisted
  136. def initialize
  137. super
  138. @persisted = []
  139. end
  140. def state=(value)
  141. @persisted << 'state' if @persisted
  142. @state = value
  143. end
  144. def status=(value)
  145. @persisted << 'status' if @persisted
  146. @status = value
  147. end
  148. end
  149. @state = StateMachine::Machine.new(@klass, :initial => :parked)
  150. @state.state :idling
  151. @state.event :ignite
  152. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear)
  153. @status.state :second_gear
  154. @status.event :shift_up
  155. @object = @klass.new
  156. @result = StateMachine::TransitionCollection.new([
  157. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  158. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  159. ]).perform
  160. end
  161. def test_should_succeed
  162. assert_equal true, @result
  163. end
  164. def test_should_persist_each_state
  165. assert_equal 'idling', @object.state
  166. assert_equal 'second_gear', @object.status
  167. end
  168. def test_should_persist_in_order
  169. assert_equal ['state', 'status'], @object.persisted
  170. end
  171. def test_should_store_results_in_transitions
  172. assert_nil @state_transition.result
  173. assert_nil @status_transition.result
  174. end
  175. end
  176. class TransitionCollectionWithoutTransactionsTest < Test::Unit::TestCase
  177. def setup
  178. @klass = Class.new do
  179. attr_accessor :ran_transaction
  180. end
  181. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  182. @machine.state :idling
  183. @machine.event :ignite
  184. class << @machine
  185. def within_transaction(object)
  186. object.ran_transaction = true
  187. end
  188. end
  189. @object = @klass.new
  190. @transitions = StateMachine::TransitionCollection.new([
  191. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  192. ], :transaction => false)
  193. @transitions.perform
  194. end
  195. def test_should_not_run_within_transaction
  196. assert !@object.ran_transaction
  197. end
  198. end
  199. class TransitionCollectionWithTransactionsTest < Test::Unit::TestCase
  200. def setup
  201. @klass = Class.new do
  202. attr_accessor :running_transaction, :cancelled_transaction
  203. end
  204. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  205. @machine.state :idling
  206. @machine.event :ignite
  207. class << @machine
  208. def within_transaction(object)
  209. object.running_transaction = true
  210. object.cancelled_transaction = yield == false
  211. object.running_transaction = false
  212. end
  213. end
  214. @object = @klass.new
  215. @transitions = StateMachine::TransitionCollection.new([
  216. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  217. ], :transaction => true)
  218. end
  219. def test_should_run_before_callbacks_within_transaction
  220. @machine.before_transition {|object| @in_transaction = object.running_transaction}
  221. @transitions.perform
  222. assert @in_transaction
  223. end
  224. def test_should_run_action_within_transaction
  225. @transitions.perform { @in_transaction = @object.running_transaction }
  226. assert @in_transaction
  227. end
  228. def test_should_run_after_callbacks_within_transaction
  229. @machine.after_transition {|object| @in_transaction = object.running_transaction}
  230. @transitions.perform
  231. assert @in_transaction
  232. end
  233. def test_should_cancel_the_transaction_on_before_halt
  234. @machine.before_transition {throw :halt}
  235. @transitions.perform
  236. assert @object.cancelled_transaction
  237. end
  238. def test_should_cancel_the_transaction_on_action_failure
  239. @transitions.perform { false }
  240. assert @object.cancelled_transaction
  241. end
  242. def test_should_not_cancel_the_transaction_on_after_halt
  243. @machine.after_transition {throw :halt}
  244. @transitions.perform
  245. assert !@object.cancelled_transaction
  246. end
  247. end
  248. class TransitionCollectionWithEmptyActionsTest < Test::Unit::TestCase
  249. def setup
  250. @klass = Class.new
  251. @state = StateMachine::Machine.new(@klass, :initial => :parked)
  252. @state.state :idling
  253. @state.event :ignite
  254. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear)
  255. @status.state :second_gear
  256. @status.event :shift_up
  257. @object = @klass.new
  258. @transitions = StateMachine::TransitionCollection.new([
  259. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  260. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  261. ])
  262. @object.state = 'idling'
  263. @object.status = 'second_gear'
  264. @result = @transitions.perform
  265. end
  266. def test_should_succeed
  267. assert_equal true, @result
  268. end
  269. def test_should_persist_states
  270. assert_equal 'idling', @object.state
  271. assert_equal 'second_gear', @object.status
  272. end
  273. def test_should_store_results_in_transitions
  274. assert_nil @state_transition.result
  275. assert_nil @status_transition.result
  276. end
  277. end
  278. class TransitionCollectionWithSkippedActionsTest < Test::Unit::TestCase
  279. def setup
  280. @klass = Class.new do
  281. attr_reader :actions
  282. def save_state
  283. (@actions ||= []) << :save_state
  284. :save_state
  285. end
  286. def save_status
  287. (@actions ||= []) << :save_status
  288. :save_status
  289. end
  290. end
  291. @callbacks = []
  292. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save_state)
  293. @state.state :idling
  294. @state.event :ignite
  295. @state.before_transition {@callbacks << :state_before}
  296. @state.after_transition {@callbacks << :state_after}
  297. @state.around_transition {|block| @callbacks << :state_around_before; block.call; @callbacks << :state_around_after}
  298. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save_status)
  299. @status.state :second_gear
  300. @status.event :shift_up
  301. @status.before_transition {@callbacks << :status_before}
  302. @status.after_transition {@callbacks << :status_after}
  303. @status.around_transition {|block| @callbacks << :status_around_before; block.call; @callbacks << :status_around_after}
  304. @object = @klass.new
  305. @transitions = StateMachine::TransitionCollection.new([
  306. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  307. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  308. ], :actions => false)
  309. @result = @transitions.perform
  310. end
  311. def test_should_skip_actions
  312. assert_equal true, @transitions.skip_actions
  313. end
  314. def test_should_succeed
  315. assert_equal true, @result
  316. end
  317. def test_should_persist_states
  318. assert_equal 'idling', @object.state
  319. assert_equal 'second_gear', @object.status
  320. end
  321. def test_should_not_run_actions
  322. assert_nil @object.actions
  323. end
  324. def test_should_store_results_in_transitions
  325. assert_nil @state_transition.result
  326. assert_nil @status_transition.result
  327. end
  328. def test_should_run_all_callbacks
  329. assert_equal [:state_before, :state_around_before, :status_before, :status_around_before, :status_around_after, :status_after, :state_around_after, :state_after], @callbacks
  330. end
  331. end
  332. class TransitionCollectionWithSkippedActionsAndBlockTest < Test::Unit::TestCase
  333. def setup
  334. @klass = Class.new
  335. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save_state)
  336. @machine.state :idling
  337. @machine.event :ignite
  338. @object = @klass.new
  339. @transitions = StateMachine::TransitionCollection.new([
  340. @state_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  341. ], :actions => false)
  342. @result = @transitions.perform { @ran_block = true; 1 }
  343. end
  344. def test_should_succeed
  345. assert_equal 1, @result
  346. end
  347. def test_should_persist_states
  348. assert_equal 'idling', @object.state
  349. end
  350. def test_should_run_block
  351. assert @ran_block
  352. end
  353. def test_should_store_results_in_transitions
  354. assert_equal 1, @state_transition.result
  355. end
  356. end
  357. class TransitionCollectionWithDuplicateActionsTest < Test::Unit::TestCase
  358. def setup
  359. @klass = Class.new do
  360. attr_reader :actions
  361. def save
  362. (@actions ||= []) << :save
  363. :save
  364. end
  365. end
  366. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  367. @state.state :idling
  368. @state.event :ignite
  369. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  370. @status.state :second_gear
  371. @status.event :shift_up
  372. @object = @klass.new
  373. @transitions = StateMachine::TransitionCollection.new([
  374. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  375. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  376. ])
  377. @result = @transitions.perform
  378. end
  379. def test_should_succeed
  380. assert_equal :save, @result
  381. end
  382. def test_should_persist_states
  383. assert_equal 'idling', @object.state
  384. assert_equal 'second_gear', @object.status
  385. end
  386. def test_should_run_action_once
  387. assert_equal [:save], @object.actions
  388. end
  389. def test_should_store_results_in_transitions
  390. assert_equal :save, @state_transition.result
  391. assert_equal :save, @status_transition.result
  392. end
  393. end
  394. class TransitionCollectionWithDifferentActionsTest < Test::Unit::TestCase
  395. def setup
  396. @klass = Class.new do
  397. attr_reader :actions
  398. def save_state
  399. (@actions ||= []) << :save_state
  400. :save_state
  401. end
  402. def save_status
  403. (@actions ||= []) << :save_status
  404. :save_status
  405. end
  406. end
  407. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save_state)
  408. @state.state :idling
  409. @state.event :ignite
  410. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save_status)
  411. @status.state :second_gear
  412. @status.event :shift_up
  413. @object = @klass.new
  414. @transitions = StateMachine::TransitionCollection.new([
  415. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  416. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  417. ])
  418. end
  419. def test_should_succeed
  420. assert_equal true, @transitions.perform
  421. end
  422. def test_should_persist_states
  423. @transitions.perform
  424. assert_equal 'idling', @object.state
  425. assert_equal 'second_gear', @object.status
  426. end
  427. def test_should_run_actions_in_order
  428. @transitions.perform
  429. assert_equal [:save_state, :save_status], @object.actions
  430. end
  431. def test_should_store_results_in_transitions
  432. @transitions.perform
  433. assert_equal :save_state, @state_transition.result
  434. assert_equal :save_status, @status_transition.result
  435. end
  436. def test_should_not_halt_if_action_fails_for_first_transition
  437. @klass.class_eval do
  438. def save_state
  439. (@actions ||= []) << :save_state
  440. false
  441. end
  442. end
  443. assert_equal false, @transitions.perform
  444. assert_equal [:save_state, :save_status], @object.actions
  445. end
  446. def test_should_halt_if_action_fails_for_second_transition
  447. @klass.class_eval do
  448. def save_status
  449. (@actions ||= []) << :save_status
  450. false
  451. end
  452. end
  453. assert_equal false, @transitions.perform
  454. assert_equal [:save_state, :save_status], @object.actions
  455. end
  456. def test_should_rollback_if_action_errors_for_first_transition
  457. @klass.class_eval do
  458. def save_state
  459. raise ArgumentError
  460. end
  461. end
  462. begin; @transitions.perform; rescue; end
  463. assert_equal 'parked', @object.state
  464. assert_equal 'first_gear', @object.status
  465. end
  466. def test_should_rollback_if_action_errors_for_second_transition
  467. @klass.class_eval do
  468. def save_status
  469. raise ArgumentError
  470. end
  471. end
  472. begin; @transitions.perform; rescue; end
  473. assert_equal 'parked', @object.state
  474. assert_equal 'first_gear', @object.status
  475. end
  476. def test_should_not_run_after_callbacks_if_action_fails_for_first_transition
  477. @klass.class_eval do
  478. def save_state
  479. false
  480. end
  481. end
  482. @callbacks = []
  483. @state.after_transition { @callbacks << :state_after }
  484. @state.around_transition {|block| block.call; @callbacks << :state_around }
  485. @status.after_transition { @callbacks << :status_after }
  486. @status.around_transition {|block| block.call; @callbacks << :status_around }
  487. @transitions.perform
  488. assert_equal [], @callbacks
  489. end
  490. def test_should_not_run_after_callbacks_if_action_fails_for_second_transition
  491. @klass.class_eval do
  492. def save_status
  493. false
  494. end
  495. end
  496. @callbacks = []
  497. @state.after_transition { @callbacks << :state_after }
  498. @state.around_transition {|block| block.call; @callbacks << :state_around }
  499. @status.after_transition { @callbacks << :status_after }
  500. @status.around_transition {|block| block.call; @callbacks << :status_around }
  501. @transitions.perform
  502. assert_equal [], @callbacks
  503. end
  504. def test_should_run_after_failure_callbacks_if_action_fails_for_first_transition
  505. @klass.class_eval do
  506. def save_state
  507. false
  508. end
  509. end
  510. @callbacks = []
  511. @state.after_failure { @callbacks << :state_after }
  512. @status.after_failure { @callbacks << :status_after }
  513. @transitions.perform
  514. assert_equal [:status_after, :state_after], @callbacks
  515. end
  516. def test_should_run_after_failure_callbacks_if_action_fails_for_second_transition
  517. @klass.class_eval do
  518. def save_status
  519. false
  520. end
  521. end
  522. @callbacks = []
  523. @state.after_failure { @callbacks << :state_after }
  524. @status.after_failure { @callbacks << :status_after }
  525. @transitions.perform
  526. assert_equal [:status_after, :state_after], @callbacks
  527. end
  528. end
  529. class TransitionCollectionWithMixedActionsTest < Test::Unit::TestCase
  530. def setup
  531. @klass = Class.new do
  532. def save
  533. true
  534. end
  535. end
  536. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  537. @state.state :idling
  538. @state.event :ignite
  539. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear)
  540. @status.state :second_gear
  541. @status.event :shift_up
  542. @object = @klass.new
  543. @transitions = StateMachine::TransitionCollection.new([
  544. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  545. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  546. ])
  547. @result = @transitions.perform
  548. end
  549. def test_should_succeed
  550. assert_equal true, @result
  551. end
  552. def test_should_persist_states
  553. assert_equal 'idling', @object.state
  554. assert_equal 'second_gear', @object.status
  555. end
  556. def test_should_store_results_in_transitions
  557. assert_equal true, @state_transition.result
  558. assert_nil @status_transition.result
  559. end
  560. end
  561. class TransitionCollectionWithBlockTest < Test::Unit::TestCase
  562. def setup
  563. @klass = Class.new do
  564. attr_reader :actions
  565. def save
  566. (@actions ||= []) << :save
  567. end
  568. end
  569. @state = StateMachine::Machine.new(@klass, :state, :initial => :parked, :action => :save)
  570. @state.state :idling
  571. @state.event :ignite
  572. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  573. @status.state :second_gear
  574. @status.event :shift_up
  575. @object = @klass.new
  576. @transitions = StateMachine::TransitionCollection.new([
  577. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  578. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  579. ])
  580. @result = @transitions.perform { 1 }
  581. end
  582. def test_should_succeed
  583. assert_equal 1, @result
  584. end
  585. def test_should_persist_states
  586. assert_equal 'idling', @object.state
  587. assert_equal 'second_gear', @object.status
  588. end
  589. def test_should_not_run_machine_actions
  590. assert_nil @object.actions
  591. end
  592. def test_should_use_result_as_transition_result
  593. assert_equal 1, @state_transition.result
  594. assert_equal 1, @status_transition.result
  595. end
  596. end
  597. class TransitionCollectionWithActionFailedTest < Test::Unit::TestCase
  598. def setup
  599. @klass = Class.new do
  600. def save
  601. false
  602. end
  603. end
  604. @before_count = 0
  605. @around_before_count = 0
  606. @after_count = 0
  607. @around_after_count = 0
  608. @failure_count = 0
  609. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  610. @machine.state :idling
  611. @machine.event :ignite
  612. @machine.before_transition {@before_count += 1}
  613. @machine.after_transition {@after_count += 1}
  614. @machine.around_transition {|block| @around_before_count += 1; block.call; @around_after_count += 1}
  615. @machine.after_failure {@failure_count += 1}
  616. @object = @klass.new
  617. @transitions = StateMachine::TransitionCollection.new([
  618. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  619. ])
  620. @result = @transitions.perform
  621. end
  622. def test_should_not_succeed
  623. assert_equal false, @result
  624. end
  625. def test_should_not_persist_state
  626. assert_equal 'parked', @object.state
  627. end
  628. def test_should_run_before_callbacks
  629. assert_equal 1, @before_count
  630. end
  631. def test_should_run_around_callbacks_before_yield
  632. assert_equal 1, @around_before_count
  633. end
  634. def test_should_not_run_after_callbacks
  635. assert_equal 0, @after_count
  636. end
  637. def test_should_not_run_around_callbacks
  638. assert_equal 0, @around_after_count
  639. end
  640. def test_should_run_failure_callbacks
  641. assert_equal 1, @failure_count
  642. end
  643. end
  644. class TransitionCollectionWithActionErrorTest < Test::Unit::TestCase
  645. def setup
  646. @klass = Class.new do
  647. def save
  648. raise ArgumentError
  649. end
  650. end
  651. @before_count = 0
  652. @around_before_count = 0
  653. @after_count = 0
  654. @around_after_count = 0
  655. @failure_count = 0
  656. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  657. @machine.state :idling
  658. @machine.event :ignite
  659. @machine.before_transition {@before_count += 1}
  660. @machine.after_transition {@after_count += 1}
  661. @machine.around_transition {|block| @around_before_count += 1; block.call; @around_after_count += 1}
  662. @machine.after_failure {@failure_count += 1}
  663. @object = @klass.new
  664. @transitions = StateMachine::TransitionCollection.new([
  665. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  666. ])
  667. @raised = true
  668. begin
  669. @transitions.perform
  670. @raised = false
  671. rescue ArgumentError
  672. end
  673. end
  674. def test_should_not_catch_exception
  675. assert @raised
  676. end
  677. def test_should_not_persist_state
  678. assert_equal 'parked', @object.state
  679. end
  680. def test_should_run_before_callbacks
  681. assert_equal 1, @before_count
  682. end
  683. def test_should_run_around_callbacks_before_yield
  684. assert_equal 1, @around_before_count
  685. end
  686. def test_should_not_run_after_callbacks
  687. assert_equal 0, @after_count
  688. end
  689. def test_should_not_run_around_callbacks_after_yield
  690. assert_equal 0, @around_after_count
  691. end
  692. def test_should_not_run_failure_callbacks
  693. assert_equal 0, @failure_count
  694. end
  695. end
  696. class TransitionCollectionWithCallbacksTest < Test::Unit::TestCase
  697. def setup
  698. @klass = Class.new do
  699. attr_reader :saved
  700. def save
  701. @saved = true
  702. end
  703. end
  704. @before_callbacks = []
  705. @after_callbacks = []
  706. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  707. @state.state :idling
  708. @state.event :ignite
  709. @state.before_transition {@before_callbacks << :state_before}
  710. @state.after_transition {@after_callbacks << :state_after}
  711. @state.around_transition {|block| @before_callbacks << :state_around; block.call; @after_callbacks << :state_around}
  712. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  713. @status.state :second_gear
  714. @status.event :shift_up
  715. @status.before_transition {@before_callbacks << :status_before}
  716. @status.after_transition {@after_callbacks << :status_after}
  717. @status.around_transition {|block| @before_callbacks << :status_around; block.call; @after_callbacks << :status_around}
  718. @object = @klass.new
  719. @transitions = StateMachine::TransitionCollection.new([
  720. StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  721. StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  722. ])
  723. end
  724. def test_should_run_before_callbacks_in_order
  725. @transitions.perform
  726. assert_equal [:state_before, :state_around, :status_before, :status_around], @before_callbacks
  727. end
  728. def test_should_halt_if_before_callback_halted_for_first_transition
  729. @state.before_transition {throw :halt}
  730. assert_equal false, @transitions.perform
  731. assert_equal [:state_before, :state_around], @before_callbacks
  732. end
  733. def test_should_halt_if_before_callback_halted_for_second_transition
  734. @status.before_transition {throw :halt}
  735. assert_equal false, @transitions.perform
  736. assert_equal [:state_before, :state_around, :status_before, :status_around], @before_callbacks
  737. end
  738. def test_should_halt_if_around_callback_halted_before_yield_for_first_transition
  739. @state.around_transition {throw :halt}
  740. assert_equal false, @transitions.perform
  741. assert_equal [:state_before, :state_around], @before_callbacks
  742. end
  743. def test_should_halt_if_around_callback_halted_before_yield_for_second_transition
  744. @status.around_transition {throw :halt}
  745. assert_equal false, @transitions.perform
  746. assert_equal [:state_before, :state_around, :status_before, :status_around], @before_callbacks
  747. end
  748. def test_should_run_after_callbacks_in_reverse_order
  749. @transitions.perform
  750. assert_equal [:status_around, :status_after, :state_around, :state_after], @after_callbacks
  751. end
  752. def test_should_not_halt_if_after_callback_halted_for_first_transition
  753. @state.after_transition {throw :halt}
  754. assert_equal true, @transitions.perform
  755. assert_equal [:status_around, :status_after, :state_around, :state_after], @after_callbacks
  756. end
  757. def test_should_not_halt_if_around_callback_halted_for_second_transition
  758. @status.around_transition {|block| block.call; throw :halt}
  759. assert_equal true, @transitions.perform
  760. assert_equal [:state_around, :state_after], @after_callbacks
  761. end
  762. def test_should_run_before_callbacks_before_persisting_the_state
  763. @state.before_transition {|object| @before_state = object.state}
  764. @state.around_transition {|object, transition, block| @around_state = object.state; block.call}
  765. @transitions.perform
  766. assert_equal 'parked', @before_state
  767. assert_equal 'parked', @around_state
  768. end
  769. def test_should_persist_state_before_running_action
  770. @klass.class_eval do
  771. attr_reader :saved_on_persist
  772. def state=(value)
  773. @state = value
  774. @saved_on_persist = @saved
  775. end
  776. end
  777. @transitions.perform
  778. assert !@object.saved_on_persist
  779. end
  780. def test_should_persist_state_before_running_action_block
  781. @klass.class_eval do
  782. attr_writer :saved
  783. attr_reader :saved_on_persist
  784. def state=(value)
  785. @state = value
  786. @saved_on_persist = @saved
  787. end
  788. end
  789. @transitions.perform { @object.saved = true }
  790. assert !@object.saved_on_persist
  791. end
  792. def test_should_run_after_callbacks_after_running_the_action
  793. @state.after_transition {|object| @after_saved = object.saved}
  794. @state.around_transition {|object, transition, block| block.call; @around_saved = object.saved}
  795. @transitions.perform
  796. assert @after_saved
  797. assert @around_saved
  798. end
  799. end
  800. class TransitionCollectionWithBeforeCallbackHaltTest < Test::Unit::TestCase
  801. def setup
  802. @klass = Class.new do
  803. attr_reader :saved
  804. def save
  805. @saved = true
  806. end
  807. end
  808. @before_count = 0
  809. @after_count = 0
  810. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  811. @machine.state :idling
  812. @machine.event :ignite
  813. @machine.before_transition {@before_count += 1; throw :halt}
  814. @machine.before_transition {@before_count += 1}
  815. @machine.after_transition {@after_count += 1}
  816. @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
  817. @object = @klass.new
  818. @transitions = StateMachine::TransitionCollection.new([
  819. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  820. ])
  821. @result = @transitions.perform
  822. end
  823. def test_should_not_succeed
  824. assert_equal false, @result
  825. end
  826. def test_should_not_persist_state
  827. assert_equal 'parked', @object.state
  828. end
  829. def test_should_not_run_action
  830. assert !@object.saved
  831. end
  832. def test_should_not_run_further_before_callbacks
  833. assert_equal 1, @before_count
  834. end
  835. def test_should_not_run_after_callbacks
  836. assert_equal 0, @after_count
  837. end
  838. end
  839. class TransitionCollectionWithAfterCallbackHaltTest < Test::Unit::TestCase
  840. def setup
  841. @klass = Class.new do
  842. attr_reader :saved
  843. def save
  844. @saved = true
  845. end
  846. end
  847. @before_count = 0
  848. @after_count = 0
  849. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  850. @machine.state :idling
  851. @machine.event :ignite
  852. @machine.before_transition {@before_count += 1}
  853. @machine.after_transition {@after_count += 1; throw :halt}
  854. @machine.after_transition {@after_count += 1}
  855. @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
  856. @object = @klass.new
  857. @transitions = StateMachine::TransitionCollection.new([
  858. StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  859. ])
  860. @result = @transitions.perform
  861. end
  862. def test_should_succeed
  863. assert_equal true, @result
  864. end
  865. def test_should_persist_state
  866. assert_equal 'idling', @object.state
  867. end
  868. def test_should_run_before_callbacks
  869. assert_equal 2, @before_count
  870. end
  871. def test_should_not_run_further_after_callbacks
  872. assert_equal 2, @after_count
  873. end
  874. end
  875. class TransitionCollectionWithSkippedAfterCallbacksTest < Test::Unit::TestCase
  876. def setup
  877. @klass = Class.new
  878. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  879. @machine.state :idling
  880. @machine.event :ignite
  881. @machine.after_transition {@ran_after = true}
  882. @object = @klass.new
  883. @transitions = StateMachine::TransitionCollection.new([
  884. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  885. ], :after => false)
  886. @result = @transitions.perform
  887. end
  888. def test_should_succeed
  889. assert_equal true, @result
  890. end
  891. def test_should_not_run_after_callbacks
  892. assert !@ran_after
  893. end
  894. def test_should_run_after_callbacks_on_subsequent_perform
  895. StateMachine::TransitionCollection.new([@transition]).perform
  896. assert @ran_after
  897. end
  898. end
  899. if RUBY_PLATFORM != 'java'
  900. class TransitionCollectionWithSkippedAfterCallbacksAndAroundCallbacksTest < Test::Unit::TestCase
  901. def setup
  902. @klass = Class.new
  903. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  904. @machine.state :idling
  905. @machine.event :ignite
  906. @machine.around_transition {|block| @ran_around_before = true; block.call; @ran_around_after = true}
  907. @object = @klass.new
  908. @transitions = StateMachine::TransitionCollection.new([
  909. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  910. ], :after => false)
  911. @result = @transitions.perform
  912. end
  913. def test_should_succeed
  914. assert_equal true, @result
  915. end
  916. def test_should_not_run_around_callbacks_after_yield
  917. assert !@ran_around_after
  918. end
  919. def test_should_run_around_callbacks_after_yield_on_subsequent_perform
  920. StateMachine::TransitionCollection.new([@transition]).perform
  921. assert @ran_around_after
  922. end
  923. def test_should_not_rerun_around_callbacks_before_yield_on_subsequent_perform
  924. @ran_around_before = false
  925. StateMachine::TransitionCollection.new([@transition]).perform
  926. assert !@ran_around_before
  927. end
  928. end
  929. else
  930. class TransitionCollectionWithSkippedAfterCallbacksAndAroundCallbacksTest < Test::Unit::TestCase
  931. def setup
  932. @klass = Class.new
  933. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  934. @machine.state :idling
  935. @machine.event :ignite
  936. @machine.around_transition {|block| @ran_around_before = true; block.call; @ran_around_after = true}
  937. @object = @klass.new
  938. @transitions = StateMachine::TransitionCollection.new([
  939. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  940. ], :after => false)
  941. end
  942. def test_should_raise_exception
  943. assert_raise(ArgumentError) { @transitions.perform }
  944. end
  945. end
  946. end
  947. class TransitionCollectionWithActionHookBaseTest < Test::Unit::TestCase
  948. def setup
  949. @superclass = Class.new do
  950. def save
  951. true
  952. end
  953. end
  954. @klass = Class.new(@superclass) do
  955. attr_reader :saved, :state_on_save, :state_event_on_save, :state_event_transition_on_save
  956. def save
  957. @saved = true
  958. @state_on_save = state
  959. @state_event_on_save = state_event
  960. @state_event_transition_on_save = state_event_transition
  961. super
  962. end
  963. end
  964. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  965. @machine.state :idling
  966. @machine.event :ignite
  967. @object = @klass.new
  968. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  969. end
  970. def default_test
  971. end
  972. end
  973. class TransitionCollectionWithActionHookAndSkippedActionTest < TransitionCollectionWithActionHookBaseTest
  974. def setup
  975. super
  976. @result = StateMachine::TransitionCollection.new([@transition], :actions => false).perform
  977. end
  978. def test_should_succeed
  979. assert_equal true, @result
  980. end
  981. def test_should_not_run_action
  982. assert !@object.saved
  983. end
  984. end
  985. class TransitionCollectionWithActionHookAndSkippedAfterCallbacksTest < TransitionCollectionWithActionHookBaseTest
  986. def setup
  987. super
  988. @result = StateMachine::TransitionCollection.new([@transition], :after => false).perform
  989. end
  990. def test_should_succeed
  991. assert_equal true, @result
  992. end
  993. def test_should_run_action
  994. assert @object.saved
  995. end
  996. def test_should_have_already_persisted_when_running_action
  997. assert_equal 'idling', @object.state_on_save
  998. end
  999. def test_should_not_have_event_during_action
  1000. assert_nil @object.state_event_on_save
  1001. end
  1002. def test_should_not_write_event
  1003. assert_nil @object.state_event
  1004. end
  1005. def test_should_not_have_event_transition_during_save
  1006. assert_nil @object.state_event_transition_on_save
  1007. end
  1008. def test_should_not_write_event_attribute
  1009. assert_nil @object.send(:state_event_transition)
  1010. end
  1011. end
  1012. class TransitionCollectionWithActionHookAndBlockTest < TransitionCollectionWithActionHookBaseTest
  1013. def setup
  1014. super
  1015. @result = StateMachine::TransitionCollection.new([@transition]).perform { true }
  1016. end
  1017. def test_should_succeed
  1018. assert_equal true, @result
  1019. end
  1020. def test_should_not_run_action
  1021. assert !@object.saved
  1022. end
  1023. end
  1024. class TransitionCollectionWithActionHookInvalidTest < TransitionCollectionWithActionHookBaseTest
  1025. def setup
  1026. super
  1027. @result = StateMachine::TransitionCollection.new([@transition, nil]).perform
  1028. end
  1029. def test_should_not_succeed
  1030. assert_equal false, @result
  1031. end
  1032. def test_should_not_run_action
  1033. assert !@object.saved
  1034. end
  1035. end
  1036. class TransitionCollectionWithActionHookWithNilActionTest < TransitionCollectionWithActionHookBaseTest
  1037. def setup
  1038. super
  1039. @machine = StateMachine::Machine.new(@klass, :status, :initial => :first_gear)
  1040. @machine.state :second_gear
  1041. @machine.event :shift_up
  1042. @result = StateMachine::TransitionCollection.new([@transition, StateMachine::Transition.new(@object, @machine, :shift_up, :first_gear, :second_gear)]).perform
  1043. end
  1044. def test_should_succeed
  1045. assert_equal true, @result
  1046. end
  1047. def test_should_run_action
  1048. assert @object.saved
  1049. end
  1050. def test_should_have_already_persisted_when_running_action
  1051. assert_equal 'idling', @object.state_on_save
  1052. end
  1053. def test_should_not_have_event_during_action
  1054. assert_nil @object.state_event_on_save
  1055. end
  1056. def test_should_not_write_event
  1057. assert_nil @object.state_event
  1058. end
  1059. def test_should_not_have_event_transition_during_save
  1060. assert_nil @object.state_event_transition_on_save
  1061. end
  1062. def test_should_not_write_event_attribute
  1063. assert_nil @object.send(:state_event_transition)
  1064. end
  1065. end
  1066. class TransitionCollectionWithActionHookWithDifferentActionsTest < TransitionCollectionWithActionHookBaseTest
  1067. def setup
  1068. super
  1069. @klass.class_eval do
  1070. def save_status
  1071. true
  1072. end
  1073. end
  1074. @machine = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save_status)
  1075. @machine.state :second_gear
  1076. @machine.event :shift_up
  1077. @result = StateMachine::TransitionCollection.new([@transition, StateMachine::Transition.new(@object, @machine, :shift_up, :first_gear, :second_gear)]).perform
  1078. end
  1079. def test_should_succeed
  1080. assert_equal true, @result
  1081. end
  1082. def test_should_run_action
  1083. assert @object.saved
  1084. end
  1085. def test_should_have_already_persisted_when_running_action
  1086. assert_equal 'idling', @object.state_on_save
  1087. end
  1088. def test_should_not_have_event_during_action
  1089. assert_nil @object.state_event_on_save
  1090. end
  1091. def test_should_not_write_event
  1092. assert_nil @object.state_event
  1093. end
  1094. def test_should_not_have_event_transition_during_save
  1095. assert_nil @object.state_event_transition_on_save
  1096. end
  1097. def test_should_not_write_event_attribute
  1098. assert_nil @object.send(:state_event_transition)
  1099. end
  1100. end
  1101. class TransitionCollectionWithActionHookTest < TransitionCollectionWithActionHookBaseTest
  1102. def setup
  1103. super
  1104. @result = StateMachine::TransitionCollection.new([@transition]).perform
  1105. end
  1106. def test_should_succeed
  1107. assert_equal true, @result
  1108. end
  1109. def test_should_run_action
  1110. assert @object.saved
  1111. end
  1112. def test_should_not_have_already_persisted_when_running_action
  1113. assert_equal 'parked', @object.state_on_save
  1114. end
  1115. def test_should_persist
  1116. assert_equal 'idling', @object.state
  1117. end
  1118. def test_should_not_have_event_during_action
  1119. assert_nil @object.state_event_on_save
  1120. end
  1121. def test_should_not_write_event
  1122. assert_nil @object.state_event
  1123. end
  1124. def test_should_have_event_transition_during_action
  1125. assert_equal @transition, @object.state_event_transition_on_save
  1126. end
  1127. def test_should_not_write_event_transition
  1128. assert_nil @object.send(:state_event_transition)
  1129. end
  1130. def test_should_mark_event_transition_as_transient
  1131. assert @transition.transient?
  1132. end
  1133. end
  1134. class TransitionCollectionWithActionHookMultipleTest < TransitionCollectionWithActionHookBaseTest
  1135. def setup
  1136. super
  1137. @status_machine = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  1138. @status_machine.state :second_gear
  1139. @status_machine.event :shift_up
  1140. @klass.class_eval do
  1141. attr_reader :status_on_save, :status_event_on_save, :status_event_transition_on_save
  1142. def save
  1143. @saved = true
  1144. @state_on_save = state
  1145. @state_event_on_save = state_event
  1146. @state_event_transition_on_save = state_event_transition
  1147. @status_on_save = status
  1148. @status_event_on_save = status_event
  1149. @status_event_transition_on_save = status_event_transition
  1150. super
  1151. 1
  1152. end
  1153. end
  1154. @object = @klass.new
  1155. @state_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1156. @status_transition = StateMachine::Transition.new(@object, @status_machine, :shift_up, :first_gear, :second_gear)
  1157. @result = StateMachine::TransitionCollection.new([@state_transition, @status_transition]).perform
  1158. end
  1159. def test_should_succeed
  1160. assert_equal 1, @result
  1161. end
  1162. def test_should_run_action
  1163. assert @object.saved
  1164. end
  1165. def test_should_not_have_already_persisted_when_running_action
  1166. assert_equal 'parked', @object.state_on_save
  1167. assert_equal 'first_gear', @object.status_on_save
  1168. end
  1169. def test_should_persist
  1170. assert_equal 'idling', @object.state
  1171. assert_equal 'second_gear', @object.status
  1172. end
  1173. def test_should_not_have_events_during_action
  1174. assert_nil @object.state_event_on_save
  1175. assert_nil @object.status_event_on_save
  1176. end
  1177. def test_should_not_write_events
  1178. assert_nil @object.state_event
  1179. assert_nil @object.status_event
  1180. end
  1181. def test_should_have_event_transitions_during_action
  1182. assert_equal @state_transition, @object.state_event_transition_on_save
  1183. assert_equal @status_transition, @object.status_event_transition_on_save
  1184. end
  1185. def test_should_not_write_event_transitions
  1186. assert_nil @object.send(:state_event_transition)
  1187. assert_nil @object.send(:status_event_transition)
  1188. end
  1189. def test_should_mark_event_transitions_as_transient
  1190. assert @state_transition.transient?
  1191. assert @status_transition.transient?
  1192. end
  1193. end
  1194. class TransitionCollectionWithActionHookErrorTest < TransitionCollectionWithActionHookBaseTest
  1195. def setup
  1196. super
  1197. @superclass.class_eval do
  1198. def save
  1199. raise ArgumentError
  1200. end
  1201. end
  1202. begin; StateMachine::TransitionCollection.new([@transition]).perform; rescue; end
  1203. end
  1204. def test_should_not_write_event
  1205. assert_nil @object.state_event
  1206. end
  1207. def test_should_not_write_event_transition
  1208. assert_nil @object.send(:state_event_transition)
  1209. end
  1210. end
  1211. class AttributeTransitionCollectionByDefaultTest < Test::Unit::TestCase
  1212. def setup
  1213. @transitions = StateMachine::AttributeTransitionCollection.new
  1214. end
  1215. def test_should_skip_actions
  1216. assert @transitions.skip_actions
  1217. end
  1218. def test_should_not_skip_after
  1219. assert !@transitions.skip_after
  1220. end
  1221. def test_should_not_use_transaction
  1222. assert !@transitions.use_transaction
  1223. end
  1224. def test_should_be_empty
  1225. assert @transitions.empty?
  1226. end
  1227. end
  1228. class AttributeTransitionCollectionWithEventsTest < Test::Unit::TestCase
  1229. def setup
  1230. @klass = Class.new
  1231. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  1232. @state.state :idling
  1233. @state.event :ignite
  1234. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  1235. @status.state :second_gear
  1236. @status.event :shift_up
  1237. @object = @klass.new
  1238. @object.state_event = 'ignite'
  1239. @object.status_event = 'shift_up'
  1240. @transitions = StateMachine::AttributeTransitionCollection.new([
  1241. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  1242. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  1243. ])
  1244. @result = @transitions.perform
  1245. end
  1246. def test_should_succeed
  1247. assert_equal true, @result
  1248. end
  1249. def test_should_persist_states
  1250. assert_equal 'idling', @object.state
  1251. assert_equal 'second_gear', @object.status
  1252. end
  1253. def test_should_clear_events
  1254. assert_nil @object.state_event
  1255. assert_nil @object.status_event
  1256. end
  1257. def test_should_not_write_event_transitions
  1258. assert_nil @object.send(:state_event_transition)
  1259. assert_nil @object.send(:status_event_transition)
  1260. end
  1261. end
  1262. class AttributeTransitionCollectionWithEventTransitionsTest < Test::Unit::TestCase
  1263. def setup
  1264. @klass = Class.new
  1265. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  1266. @state.state :idling
  1267. @state.event :ignite
  1268. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  1269. @status.state :second_gear
  1270. @status.event :shift_up
  1271. @object = @klass.new
  1272. @object.send(:state_event_transition=, @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling))
  1273. @object.send(:status_event_transition=, @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear))
  1274. @transitions = StateMachine::AttributeTransitionCollection.new([@state_transition, @status_transition])
  1275. @result = @transitions.perform
  1276. end
  1277. def test_should_succeed
  1278. assert_equal true, @result
  1279. end
  1280. def test_should_persist_states
  1281. assert_equal 'idling', @object.state
  1282. assert_equal 'second_gear', @object.status
  1283. end
  1284. def test_should_not_write_events
  1285. assert_nil @object.state_event
  1286. assert_nil @object.status_event
  1287. end
  1288. def test_should_clear_event_transitions
  1289. assert_nil @object.send(:state_event_transition)
  1290. assert_nil @object.send(:status_event_transition)
  1291. end
  1292. end
  1293. class AttributeTransitionCollectionWithActionFailedTest < Test::Unit::TestCase
  1294. def setup
  1295. @klass = Class.new
  1296. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  1297. @state.state :idling
  1298. @state.event :ignite
  1299. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  1300. @status.state :second_gear
  1301. @status.event :shift_up
  1302. @object = @klass.new
  1303. @object.state_event = 'ignite'
  1304. @object.status_event = 'shift_up'
  1305. @transitions = StateMachine::AttributeTransitionCollection.new([
  1306. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  1307. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  1308. ])
  1309. @result = @transitions.perform { false }
  1310. end
  1311. def test_should_not_succeed
  1312. assert_equal false, @result
  1313. end
  1314. def test_should_not_persist_states
  1315. assert_equal 'parked', @object.state
  1316. assert_equal 'first_gear', @object.status
  1317. end
  1318. def test_should_not_clear_events
  1319. assert_equal :ignite, @object.state_event
  1320. assert_equal :shift_up, @object.status_event
  1321. end
  1322. def test_should_not_write_event_transitions
  1323. assert_nil @object.send(:state_event_transition)
  1324. assert_nil @object.send(:status_event_transition)
  1325. end
  1326. end
  1327. class AttributeTransitionCollectionWithActionErrorTest < Test::Unit::TestCase
  1328. def setup
  1329. @klass = Class.new
  1330. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  1331. @state.state :idling
  1332. @state.event :ignite
  1333. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  1334. @status.state :second_gear
  1335. @status.event :shift_up
  1336. @object = @klass.new
  1337. @object.state_event = 'ignite'
  1338. @object.status_event = 'shift_up'
  1339. @transitions = StateMachine::AttributeTransitionCollection.new([
  1340. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  1341. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  1342. ])
  1343. begin; @transitions.perform { raise ArgumentError }; rescue; end
  1344. end
  1345. def test_should_not_persist_states
  1346. assert_equal 'parked', @object.state
  1347. assert_equal 'first_gear', @object.status
  1348. end
  1349. def test_should_not_clear_events
  1350. assert_equal :ignite, @object.state_event
  1351. assert_equal :shift_up, @object.status_event
  1352. end
  1353. def test_should_not_write_event_transitions
  1354. assert_nil @object.send(:state_event_transition)
  1355. assert_nil @object.send(:status_event_transition)
  1356. end
  1357. end
  1358. class AttributeTransitionCollectionWithCallbacksTest < Test::Unit::TestCase
  1359. def setup
  1360. @klass = Class.new
  1361. @state = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
  1362. @state.state :idling
  1363. @state.event :ignite
  1364. @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
  1365. @status.state :second_gear
  1366. @status.event :shift_up
  1367. @object = @klass.new
  1368. @transitions = StateMachine::AttributeTransitionCollection.new([
  1369. @state_transition = StateMachine::Transition.new(@object, @state, :ignite, :parked, :idling),
  1370. @status_transition = StateMachine::Transition.new(@object, @status, :shift_up, :first_gear, :second_gear)
  1371. ])
  1372. end
  1373. def test_should_not_have_events_during_before_callbacks
  1374. @state.before_transition {|object, transition| @before_s

Large files files are truncated, but you can click here to view the full file