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

/test/unit/transition_test.rb

http://github.com/pluginaweek/state_machine
Ruby | 1558 lines | 1230 code | 328 blank | 0 comment | 1 complexity | 7b7ce05570b849a2aac4e6daa6dff887 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class TransitionTest < Test::Unit::TestCase
  3. def setup
  4. @klass = Class.new
  5. @machine = StateMachine::Machine.new(@klass)
  6. @machine.state :parked, :idling
  7. @machine.event :ignite
  8. @object = @klass.new
  9. @object.state = 'parked'
  10. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  11. end
  12. def test_should_have_an_object
  13. assert_equal @object, @transition.object
  14. end
  15. def test_should_have_a_machine
  16. assert_equal @machine, @transition.machine
  17. end
  18. def test_should_have_an_event
  19. assert_equal :ignite, @transition.event
  20. end
  21. def test_should_have_a_qualified_event
  22. assert_equal :ignite, @transition.qualified_event
  23. end
  24. def test_should_have_a_human_event
  25. assert_equal 'ignite', @transition.human_event
  26. end
  27. def test_should_have_a_from_value
  28. assert_equal 'parked', @transition.from
  29. end
  30. def test_should_have_a_from_name
  31. assert_equal :parked, @transition.from_name
  32. end
  33. def test_should_have_a_qualified_from_name
  34. assert_equal :parked, @transition.qualified_from_name
  35. end
  36. def test_should_have_a_human_from_name
  37. assert_equal 'parked', @transition.human_from_name
  38. end
  39. def test_should_have_a_to_value
  40. assert_equal 'idling', @transition.to
  41. end
  42. def test_should_have_a_to_name
  43. assert_equal :idling, @transition.to_name
  44. end
  45. def test_should_have_a_qualified_to_name
  46. assert_equal :idling, @transition.qualified_to_name
  47. end
  48. def test_should_have_a_human_to_name
  49. assert_equal 'idling', @transition.human_to_name
  50. end
  51. def test_should_have_an_attribute
  52. assert_equal :state, @transition.attribute
  53. end
  54. def test_should_not_have_an_action
  55. assert_nil @transition.action
  56. end
  57. def test_should_not_be_transient
  58. assert_equal false, @transition.transient?
  59. end
  60. def test_should_generate_attributes
  61. expected = {:object => @object, :attribute => :state, :event => :ignite, :from => 'parked', :to => 'idling'}
  62. assert_equal expected, @transition.attributes
  63. end
  64. def test_should_have_empty_args
  65. assert_equal [], @transition.args
  66. end
  67. def test_should_not_have_a_result
  68. assert_nil @transition.result
  69. end
  70. def test_should_use_pretty_inspect
  71. assert_equal '#<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>', @transition.inspect
  72. end
  73. end
  74. class TransitionWithInvalidNodesTest < Test::Unit::TestCase
  75. def setup
  76. @klass = Class.new
  77. @machine = StateMachine::Machine.new(@klass)
  78. @machine.state :parked, :idling
  79. @machine.event :ignite
  80. @object = @klass.new
  81. @object.state = 'parked'
  82. end
  83. def test_should_raise_exception_without_event
  84. assert_raise(IndexError) { StateMachine::Transition.new(@object, @machine, nil, :parked, :idling) }
  85. end
  86. def test_should_raise_exception_with_invalid_event
  87. assert_raise(IndexError) { StateMachine::Transition.new(@object, @machine, :invalid, :parked, :idling) }
  88. end
  89. def test_should_raise_exception_with_invalid_from_state
  90. assert_raise(IndexError) { StateMachine::Transition.new(@object, @machine, :ignite, :invalid, :idling) }
  91. end
  92. def test_should_raise_exception_with_invalid_to_state
  93. assert_raise(IndexError) { StateMachine::Transition.new(@object, @machine, :ignite, :parked, :invalid) }
  94. end
  95. end
  96. class TransitionWithDynamicToValueTest < Test::Unit::TestCase
  97. def setup
  98. @klass = Class.new
  99. @machine = StateMachine::Machine.new(@klass)
  100. @machine.state :parked
  101. @machine.state :idling, :value => lambda {1}
  102. @machine.event :ignite
  103. @object = @klass.new
  104. @object.state = 'parked'
  105. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  106. end
  107. def test_should_evaluate_to_value
  108. assert_equal 1, @transition.to
  109. end
  110. end
  111. class TransitionLoopbackTest < Test::Unit::TestCase
  112. def setup
  113. @klass = Class.new
  114. @machine = StateMachine::Machine.new(@klass)
  115. @machine.state :parked
  116. @machine.event :park
  117. @object = @klass.new
  118. @object.state = 'parked'
  119. @transition = StateMachine::Transition.new(@object, @machine, :park, :parked, :parked)
  120. end
  121. def test_should_be_loopback
  122. assert @transition.loopback?
  123. end
  124. end
  125. class TransitionWithDifferentStatesTest < Test::Unit::TestCase
  126. def setup
  127. @klass = Class.new
  128. @machine = StateMachine::Machine.new(@klass)
  129. @machine.state :parked, :idling
  130. @machine.event :ignite
  131. @object = @klass.new
  132. @object.state = 'parked'
  133. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  134. end
  135. def test_should_not_be_loopback
  136. assert !@transition.loopback?
  137. end
  138. end
  139. class TransitionWithNamespaceTest < Test::Unit::TestCase
  140. def setup
  141. @klass = Class.new
  142. @machine = StateMachine::Machine.new(@klass, :namespace => 'alarm')
  143. @machine.state :off, :active
  144. @machine.event :activate
  145. @object = @klass.new
  146. @object.state = 'off'
  147. @transition = StateMachine::Transition.new(@object, @machine, :activate, :off, :active)
  148. end
  149. def test_should_have_an_event
  150. assert_equal :activate, @transition.event
  151. end
  152. def test_should_have_a_qualified_event
  153. assert_equal :activate_alarm, @transition.qualified_event
  154. end
  155. def test_should_have_a_from_name
  156. assert_equal :off, @transition.from_name
  157. end
  158. def test_should_have_a_qualified_from_name
  159. assert_equal :alarm_off, @transition.qualified_from_name
  160. end
  161. def test_should_have_a_human_from_name
  162. assert_equal 'off', @transition.human_from_name
  163. end
  164. def test_should_have_a_to_name
  165. assert_equal :active, @transition.to_name
  166. end
  167. def test_should_have_a_qualified_to_name
  168. assert_equal :alarm_active, @transition.qualified_to_name
  169. end
  170. def test_should_have_a_human_to_name
  171. assert_equal 'active', @transition.human_to_name
  172. end
  173. end
  174. class TransitionWithCustomMachineAttributeTest < Test::Unit::TestCase
  175. def setup
  176. @klass = Class.new
  177. @machine = StateMachine::Machine.new(@klass, :state, :attribute => :state_id)
  178. @machine.state :off, :value => 1
  179. @machine.state :active, :value => 2
  180. @machine.event :activate
  181. @object = @klass.new
  182. @object.state_id = 1
  183. @transition = StateMachine::Transition.new(@object, @machine, :activate, :off, :active)
  184. end
  185. def test_should_persist
  186. @transition.persist
  187. assert_equal 2, @object.state_id
  188. end
  189. def test_should_rollback
  190. @object.state_id = 2
  191. @transition.rollback
  192. assert_equal 1, @object.state_id
  193. end
  194. end
  195. class TransitionWithoutReadingStateTest < Test::Unit::TestCase
  196. def setup
  197. @klass = Class.new
  198. @machine = StateMachine::Machine.new(@klass)
  199. @machine.state :parked, :idling
  200. @machine.event :ignite
  201. @object = @klass.new
  202. @object.state = 'idling'
  203. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling, false)
  204. end
  205. def test_should_not_read_from_value_from_object
  206. assert_equal 'parked', @transition.from
  207. end
  208. def test_should_have_to_value
  209. assert_equal 'idling', @transition.to
  210. end
  211. end
  212. class TransitionWithActionTest < Test::Unit::TestCase
  213. def setup
  214. @klass = Class.new do
  215. def save
  216. end
  217. end
  218. @machine = StateMachine::Machine.new(@klass, :action => :save)
  219. @machine.state :parked, :idling
  220. @machine.event :ignite
  221. @object = @klass.new
  222. @object.state = 'parked'
  223. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  224. end
  225. def test_should_have_an_action
  226. assert_equal :save, @transition.action
  227. end
  228. def test_should_not_have_a_result
  229. assert_nil @transition.result
  230. end
  231. end
  232. class TransitionAfterBeingPersistedTest < Test::Unit::TestCase
  233. def setup
  234. @klass = Class.new
  235. @machine = StateMachine::Machine.new(@klass, :action => :save)
  236. @machine.state :parked, :idling
  237. @machine.event :ignite
  238. @object = @klass.new
  239. @object.state = 'parked'
  240. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  241. @transition.persist
  242. end
  243. def test_should_update_state_value
  244. assert_equal 'idling', @object.state
  245. end
  246. def test_should_not_change_from_state
  247. assert_equal 'parked', @transition.from
  248. end
  249. def test_should_not_change_to_state
  250. assert_equal 'idling', @transition.to
  251. end
  252. def test_should_not_be_able_to_persist_twice
  253. @object.state = 'parked'
  254. @transition.persist
  255. assert_equal 'parked', @object.state
  256. end
  257. def test_should_be_able_to_persist_again_after_resetting
  258. @object.state = 'parked'
  259. @transition.reset
  260. @transition.persist
  261. assert_equal 'idling', @object.state
  262. end
  263. def test_should_revert_to_from_state_on_rollback
  264. @transition.rollback
  265. assert_equal 'parked', @object.state
  266. end
  267. end
  268. class TransitionAfterBeingRolledBackTest < Test::Unit::TestCase
  269. def setup
  270. @klass = Class.new
  271. @machine = StateMachine::Machine.new(@klass, :action => :save)
  272. @machine.state :parked, :idling
  273. @machine.event :ignite
  274. @object = @klass.new
  275. @object.state = 'parked'
  276. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  277. @object.state = 'idling'
  278. @transition.rollback
  279. end
  280. def test_should_update_state_value_to_from_state
  281. assert_equal 'parked', @object.state
  282. end
  283. def test_should_not_change_from_state
  284. assert_equal 'parked', @transition.from
  285. end
  286. def test_should_not_change_to_state
  287. assert_equal 'idling', @transition.to
  288. end
  289. def test_should_still_be_able_to_persist
  290. @transition.persist
  291. assert_equal 'idling', @object.state
  292. end
  293. end
  294. class TransitionWithoutCallbacksTest < Test::Unit::TestCase
  295. def setup
  296. @klass = Class.new
  297. @machine = StateMachine::Machine.new(@klass)
  298. @machine.state :parked, :idling
  299. @machine.event :ignite
  300. @object = @klass.new
  301. @object.state = 'parked'
  302. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  303. end
  304. def test_should_succeed
  305. assert_equal true, @transition.run_callbacks
  306. end
  307. def test_should_succeed_if_after_callbacks_skipped
  308. assert_equal true, @transition.run_callbacks(:after => false)
  309. end
  310. def test_should_call_block_if_provided
  311. @transition.run_callbacks { @ran_block = true; {} }
  312. assert @ran_block
  313. end
  314. def test_should_track_block_result
  315. @transition.run_callbacks {{:result => 1}}
  316. assert_equal 1, @transition.result
  317. end
  318. end
  319. class TransitionWithBeforeCallbacksTest < Test::Unit::TestCase
  320. def setup
  321. @klass = Class.new
  322. @machine = StateMachine::Machine.new(@klass)
  323. @machine.state :parked, :idling
  324. @machine.event :ignite
  325. @object = @klass.new
  326. @object.state = 'parked'
  327. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  328. end
  329. def test_should_run_before_callbacks
  330. @machine.before_transition {@run = true}
  331. result = @transition.run_callbacks
  332. assert_equal true, result
  333. assert_equal true, @run
  334. end
  335. def test_should_only_run_those_that_match_transition_context
  336. @count = 0
  337. callback = lambda {@count += 1}
  338. @machine.before_transition :from => :parked, :to => :idling, :on => :park, :do => callback
  339. @machine.before_transition :from => :parked, :to => :parked, :on => :park, :do => callback
  340. @machine.before_transition :from => :parked, :to => :idling, :on => :ignite, :do => callback
  341. @machine.before_transition :from => :idling, :to => :idling, :on => :park, :do => callback
  342. @transition.run_callbacks
  343. assert_equal 1, @count
  344. end
  345. def test_should_pass_transition_as_argument
  346. @machine.before_transition {|*args| @args = args}
  347. @transition.run_callbacks
  348. assert_equal [@object, @transition], @args
  349. end
  350. def test_should_catch_halts
  351. @machine.before_transition {throw :halt}
  352. result = nil
  353. assert_nothing_thrown { result = @transition.run_callbacks }
  354. assert_equal false, result
  355. end
  356. def test_should_not_catch_exceptions
  357. @machine.before_transition {raise ArgumentError}
  358. assert_raise(ArgumentError) { @transition.run_callbacks }
  359. end
  360. def test_should_not_be_able_to_run_twice
  361. @count = 0
  362. @machine.before_transition {@count += 1}
  363. @transition.run_callbacks
  364. @transition.run_callbacks
  365. assert_equal 1, @count
  366. end
  367. def test_should_be_able_to_run_again_after_halt
  368. @count = 0
  369. @machine.before_transition {@count += 1; throw :halt}
  370. @transition.run_callbacks
  371. @transition.run_callbacks
  372. assert_equal 2, @count
  373. end
  374. def test_should_be_able_to_run_again_after_resetting
  375. @count = 0
  376. @machine.before_transition {@count += 1}
  377. @transition.run_callbacks
  378. @transition.reset
  379. @transition.run_callbacks
  380. assert_equal 2, @count
  381. end
  382. def test_should_succeed_if_block_result_is_false
  383. @machine.before_transition {@run = true}
  384. assert_equal true, @transition.run_callbacks {{:result => false}}
  385. assert @run
  386. end
  387. def test_should_succeed_if_block_result_is_true
  388. @machine.before_transition {@run = true}
  389. assert_equal true, @transition.run_callbacks {{:result => true}}
  390. assert @run
  391. end
  392. def test_should_succeed_if_block_success_is_false
  393. @machine.before_transition {@run = true}
  394. assert_equal true, @transition.run_callbacks {{:success => false}}
  395. assert @run
  396. end
  397. def test_should_succeed_if_block_success_is_true
  398. @machine.before_transition {@run = true}
  399. assert_equal true, @transition.run_callbacks {{:success => true}}
  400. assert @run
  401. end
  402. end
  403. class TransitionWithMultipleBeforeCallbacksTest < Test::Unit::TestCase
  404. def setup
  405. @klass = Class.new
  406. @machine = StateMachine::Machine.new(@klass)
  407. @machine.state :parked, :idling
  408. @machine.event :ignite
  409. @object = @klass.new
  410. @object.state = 'parked'
  411. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  412. end
  413. def test_should_run_in_the_order_they_were_defined
  414. @callbacks = []
  415. @machine.before_transition {@callbacks << 1}
  416. @machine.before_transition {@callbacks << 2}
  417. @transition.run_callbacks
  418. assert_equal [1, 2], @callbacks
  419. end
  420. def test_should_not_run_further_callbacks_if_halted
  421. @callbacks = []
  422. @machine.before_transition {@callbacks << 1; throw :halt}
  423. @machine.before_transition {@callbacks << 2}
  424. assert_equal false, @transition.run_callbacks
  425. assert_equal [1], @callbacks
  426. end
  427. def test_should_fail_if_any_callback_halted
  428. @machine.before_transition {true}
  429. @machine.before_transition {throw :halt}
  430. assert_equal false, @transition.run_callbacks
  431. end
  432. end
  433. class TransitionWithAfterCallbacksTest < Test::Unit::TestCase
  434. def setup
  435. @klass = Class.new
  436. @machine = StateMachine::Machine.new(@klass)
  437. @machine.state :parked, :idling
  438. @machine.event :ignite
  439. @object = @klass.new
  440. @object.state = 'parked'
  441. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  442. end
  443. def test_should_run_after_callbacks
  444. @machine.after_transition {|object| @run = true}
  445. result = @transition.run_callbacks
  446. assert_equal true, result
  447. assert_equal true, @run
  448. end
  449. def test_should_only_run_those_that_match_transition_context
  450. @count = 0
  451. callback = lambda {@count += 1}
  452. @machine.after_transition :from => :parked, :to => :idling, :on => :park, :do => callback
  453. @machine.after_transition :from => :parked, :to => :parked, :on => :park, :do => callback
  454. @machine.after_transition :from => :parked, :to => :idling, :on => :ignite, :do => callback
  455. @machine.after_transition :from => :idling, :to => :idling, :on => :park, :do => callback
  456. @transition.run_callbacks
  457. assert_equal 1, @count
  458. end
  459. def test_should_not_run_if_not_successful
  460. @run = false
  461. @machine.after_transition {|object| @run = true}
  462. @transition.run_callbacks {{:success => false}}
  463. assert !@run
  464. end
  465. def test_should_run_if_successful
  466. @machine.after_transition {|object| @run = true}
  467. @transition.run_callbacks {{:success => true}}
  468. assert @run
  469. end
  470. def test_should_pass_transition_as_argument
  471. @machine.after_transition {|*args| @args = args}
  472. @transition.run_callbacks
  473. assert_equal [@object, @transition], @args
  474. end
  475. def test_should_catch_halts
  476. @machine.after_transition {throw :halt}
  477. result = nil
  478. assert_nothing_thrown { result = @transition.run_callbacks }
  479. assert_equal true, result
  480. end
  481. def test_should_not_catch_exceptions
  482. @machine.after_transition {raise ArgumentError}
  483. assert_raise(ArgumentError) { @transition.run_callbacks }
  484. end
  485. def test_should_not_be_able_to_run_twice
  486. @count = 0
  487. @machine.after_transition {@count += 1}
  488. @transition.run_callbacks
  489. @transition.run_callbacks
  490. assert_equal 1, @count
  491. end
  492. def test_should_not_be_able_to_run_twice_if_halted
  493. @count = 0
  494. @machine.after_transition {@count += 1; throw :halt}
  495. @transition.run_callbacks
  496. @transition.run_callbacks
  497. assert_equal 1, @count
  498. end
  499. def test_should_be_able_to_run_again_after_resetting
  500. @count = 0
  501. @machine.after_transition {@count += 1}
  502. @transition.run_callbacks
  503. @transition.reset
  504. @transition.run_callbacks
  505. assert_equal 2, @count
  506. end
  507. end
  508. class TransitionWithMultipleAfterCallbacksTest < Test::Unit::TestCase
  509. def setup
  510. @klass = Class.new
  511. @machine = StateMachine::Machine.new(@klass)
  512. @machine.state :parked, :idling
  513. @machine.event :ignite
  514. @object = @klass.new
  515. @object.state = 'parked'
  516. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  517. end
  518. def test_should_run_in_the_order_they_were_defined
  519. @callbacks = []
  520. @machine.after_transition {@callbacks << 1}
  521. @machine.after_transition {@callbacks << 2}
  522. @transition.run_callbacks
  523. assert_equal [1, 2], @callbacks
  524. end
  525. def test_should_not_run_further_callbacks_if_halted
  526. @callbacks = []
  527. @machine.after_transition {@callbacks << 1; throw :halt}
  528. @machine.after_transition {@callbacks << 2}
  529. assert_equal true, @transition.run_callbacks
  530. assert_equal [1], @callbacks
  531. end
  532. def test_should_fail_if_any_callback_halted
  533. @machine.after_transition {true}
  534. @machine.after_transition {throw :halt}
  535. assert_equal true, @transition.run_callbacks
  536. end
  537. end
  538. class TransitionWithAroundCallbacksTest < Test::Unit::TestCase
  539. def setup
  540. @klass = Class.new
  541. @machine = StateMachine::Machine.new(@klass)
  542. @machine.state :parked, :idling
  543. @machine.event :ignite
  544. @object = @klass.new
  545. @object.state = 'parked'
  546. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  547. end
  548. def test_should_run_around_callbacks
  549. @machine.around_transition {|object, transition, block| @run_before = true; block.call; @run_after = true}
  550. result = @transition.run_callbacks
  551. assert_equal true, result
  552. assert_equal true, @run_before
  553. assert_equal true, @run_after
  554. end
  555. def test_should_only_run_those_that_match_transition_context
  556. @count = 0
  557. callback = lambda {|object, transition, block| @count += 1; block.call}
  558. @machine.around_transition :from => :parked, :to => :idling, :on => :park, :do => callback
  559. @machine.around_transition :from => :parked, :to => :parked, :on => :park, :do => callback
  560. @machine.around_transition :from => :parked, :to => :idling, :on => :ignite, :do => callback
  561. @machine.around_transition :from => :idling, :to => :idling, :on => :park, :do => callback
  562. @transition.run_callbacks
  563. assert_equal 1, @count
  564. end
  565. def test_should_pass_transition_as_argument
  566. @machine.around_transition {|*args| block = args.pop; @args = args; block.call}
  567. @transition.run_callbacks
  568. assert_equal [@object, @transition], @args
  569. end
  570. def test_should_run_block_between_callback
  571. @callbacks = []
  572. @machine.around_transition {|block| @callbacks << :before; block.call; @callbacks << :after}
  573. @transition.run_callbacks { @callbacks << :within; {:success => true} }
  574. assert_equal [:before, :within, :after], @callbacks
  575. end
  576. def test_should_have_access_to_result_after_yield
  577. @machine.around_transition {|block| @before_result = @transition.result; block.call; @after_result = @transition.result}
  578. @transition.run_callbacks {{:result => 1, :success => true}}
  579. assert_nil @before_result
  580. assert_equal 1, @after_result
  581. end
  582. def test_should_catch_before_yield_halts
  583. @machine.around_transition {throw :halt}
  584. result = nil
  585. assert_nothing_thrown { result = @transition.run_callbacks }
  586. assert_equal false, result
  587. end
  588. def test_should_catch_after_yield_halts
  589. @machine.around_transition {|block| block.call; throw :halt}
  590. result = nil
  591. assert_nothing_thrown { result = @transition.run_callbacks }
  592. assert_equal true, result
  593. end
  594. def test_should_not_catch_before_yield
  595. @machine.around_transition {raise ArgumentError}
  596. assert_raise(ArgumentError) { @transition.run_callbacks }
  597. end
  598. def test_should_not_catch_after_yield
  599. @machine.around_transition {|block| block.call; raise ArgumentError}
  600. assert_raise(ArgumentError) { @transition.run_callbacks }
  601. end
  602. def test_should_fail_if_not_yielded
  603. @machine.around_transition {}
  604. result = nil
  605. assert_nothing_thrown { result = @transition.run_callbacks }
  606. assert_equal false, result
  607. end
  608. def test_should_not_be_able_to_run_twice
  609. @before_count = 0
  610. @after_count = 0
  611. @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
  612. @transition.run_callbacks
  613. @transition.run_callbacks
  614. assert_equal 1, @before_count
  615. assert_equal 1, @after_count
  616. end
  617. def test_should_be_able_to_run_again_after_resetting
  618. @before_count = 0
  619. @after_count = 0
  620. @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
  621. @transition.run_callbacks
  622. @transition.reset
  623. @transition.run_callbacks
  624. assert_equal 2, @before_count
  625. assert_equal 2, @after_count
  626. end
  627. def test_should_succeed_if_block_result_is_false
  628. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  629. assert_equal true, @transition.run_callbacks {{:success => true, :result => false}}
  630. assert @before_run
  631. assert @after_run
  632. end
  633. def test_should_succeed_if_block_result_is_true
  634. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  635. assert_equal true, @transition.run_callbacks {{:success => true, :result => true}}
  636. assert @before_run
  637. assert @after_run
  638. end
  639. def test_should_only_run_before_if_block_success_is_false
  640. @after_run = false
  641. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  642. assert_equal true, @transition.run_callbacks {{:success => false}}
  643. assert @before_run
  644. assert !@after_run
  645. end
  646. def test_should_succeed_if_block_success_is_false
  647. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  648. assert_equal true, @transition.run_callbacks {{:success => true}}
  649. assert @before_run
  650. assert @after_run
  651. end
  652. end
  653. class TransitionWithMultipleAroundCallbacksTest < Test::Unit::TestCase
  654. def setup
  655. @klass = Class.new
  656. @machine = StateMachine::Machine.new(@klass)
  657. @machine.state :parked, :idling
  658. @machine.event :ignite
  659. @object = @klass.new
  660. @object.state = 'parked'
  661. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  662. end
  663. def test_should_before_yield_in_the_order_they_were_defined
  664. @callbacks = []
  665. @machine.around_transition {|block| @callbacks << 1; block.call}
  666. @machine.around_transition {|block| @callbacks << 2; block.call}
  667. @transition.run_callbacks
  668. assert_equal [1, 2], @callbacks
  669. end
  670. def test_should_before_yield_multiple_methods_in_the_order_they_were_defined
  671. @callbacks = []
  672. @machine.around_transition(lambda {|block| @callbacks << 1; block.call}, lambda {|block| @callbacks << 2; block.call})
  673. @machine.around_transition(lambda {|block| @callbacks << 3; block.call}, lambda {|block| @callbacks << 4; block.call})
  674. @transition.run_callbacks
  675. assert_equal [1, 2, 3, 4], @callbacks
  676. end
  677. def test_should_after_yield_in_the_reverse_order_they_were_defined
  678. @callbacks = []
  679. @machine.around_transition {|block| block.call; @callbacks << 1}
  680. @machine.around_transition {|block| block.call; @callbacks << 2}
  681. @transition.run_callbacks
  682. assert_equal [2, 1], @callbacks
  683. end
  684. def test_should_after_yield_multiple_methods_in_the_reverse_order_they_were_defined
  685. @callbacks = []
  686. @machine.around_transition(lambda {|block| block.call; @callbacks << 1}) {|block| block.call; @callbacks << 2}
  687. @machine.around_transition(lambda {|block| block.call; @callbacks << 3}) {|block| block.call; @callbacks << 4}
  688. @transition.run_callbacks
  689. assert_equal [4, 3, 2, 1], @callbacks
  690. end
  691. def test_should_run_block_between_callback
  692. @callbacks = []
  693. @machine.around_transition {|block| @callbacks << :before_1; block.call; @callbacks << :after_1}
  694. @machine.around_transition {|block| @callbacks << :before_2; block.call; @callbacks << :after_2}
  695. @transition.run_callbacks { @callbacks << :within; {:success => true} }
  696. assert_equal [:before_1, :before_2, :within, :after_2, :after_1], @callbacks
  697. end
  698. def test_should_have_access_to_result_after_yield
  699. @machine.around_transition {|block| @before_result_1 = @transition.result; block.call; @after_result_1 = @transition.result}
  700. @machine.around_transition {|block| @before_result_2 = @transition.result; block.call; @after_result_2 = @transition.result}
  701. @transition.run_callbacks {{:result => 1, :success => true}}
  702. assert_nil @before_result_1
  703. assert_nil @before_result_2
  704. assert_equal 1, @after_result_1
  705. assert_equal 1, @after_result_2
  706. end
  707. def test_should_fail_if_any_before_yield_halted
  708. @machine.around_transition {|block| block.call}
  709. @machine.around_transition {throw :halt}
  710. assert_equal false, @transition.run_callbacks
  711. end
  712. def test_should_not_continue_around_callbacks_if_before_yield_halted
  713. @callbacks = []
  714. @machine.around_transition {@callbacks << 1; throw :halt}
  715. @machine.around_transition {|block| @callbacks << 2; block.call; @callbacks << 3}
  716. assert_equal false, @transition.run_callbacks
  717. assert_equal [1], @callbacks
  718. end
  719. def test_should_not_continue_around_callbacks_if_later_before_yield_halted
  720. @callbacks = []
  721. @machine.around_transition {|block| block.call; @callbacks << 1}
  722. @machine.around_transition {throw :halt}
  723. @transition.run_callbacks
  724. assert_equal [], @callbacks
  725. end
  726. def test_should_not_run_further_callbacks_if_after_yield_halted
  727. @callbacks = []
  728. @machine.around_transition {|block| block.call; @callbacks << 1}
  729. @machine.around_transition {|block| block.call; throw :halt}
  730. assert_equal true, @transition.run_callbacks
  731. assert_equal [], @callbacks
  732. end
  733. def test_should_fail_if_any_fail_to_yield
  734. @callbacks = []
  735. @machine.around_transition {@callbacks << 1}
  736. @machine.around_transition {|block| @callbacks << 2; block.call; @callbacks << 3}
  737. assert_equal false, @transition.run_callbacks
  738. assert_equal [1], @callbacks
  739. end
  740. end
  741. class TransitionWithFailureCallbacksTest < Test::Unit::TestCase
  742. def setup
  743. @klass = Class.new
  744. @machine = StateMachine::Machine.new(@klass)
  745. @machine.state :parked, :idling
  746. @machine.event :ignite
  747. @object = @klass.new
  748. @object.state = 'parked'
  749. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  750. end
  751. def test_should_only_run_those_that_match_transition_context
  752. @count = 0
  753. callback = lambda {@count += 1}
  754. @machine.after_failure :do => callback
  755. @machine.after_failure :on => :park, :do => callback
  756. @machine.after_failure :on => :ignite, :do => callback
  757. @transition.run_callbacks {{:success => false}}
  758. assert_equal 2, @count
  759. end
  760. def test_should_run_if_not_successful
  761. @machine.after_failure {|object| @run = true}
  762. @transition.run_callbacks {{:success => false}}
  763. assert @run
  764. end
  765. def test_should_not_run_if_successful
  766. @run = false
  767. @machine.after_failure {|object| @run = true}
  768. @transition.run_callbacks {{:success => true}}
  769. assert !@run
  770. end
  771. def test_should_pass_transition_as_argument
  772. @machine.after_failure {|*args| @args = args}
  773. @transition.run_callbacks {{:success => false}}
  774. assert_equal [@object, @transition], @args
  775. end
  776. def test_should_catch_halts
  777. @machine.after_failure {throw :halt}
  778. result = nil
  779. assert_nothing_thrown { result = @transition.run_callbacks {{:success => false}} }
  780. assert_equal true, result
  781. end
  782. def test_should_not_catch_exceptions
  783. @machine.after_failure {raise ArgumentError}
  784. assert_raise(ArgumentError) { @transition.run_callbacks {{:success => false}} }
  785. end
  786. def test_should_not_be_able_to_run_twice
  787. @count = 0
  788. @machine.after_failure {@count += 1}
  789. @transition.run_callbacks {{:success => false}}
  790. @transition.run_callbacks {{:success => false}}
  791. assert_equal 1, @count
  792. end
  793. def test_should_not_be_able_to_run_twice_if_halted
  794. @count = 0
  795. @machine.after_failure {@count += 1; throw :halt}
  796. @transition.run_callbacks {{:success => false}}
  797. @transition.run_callbacks {{:success => false}}
  798. assert_equal 1, @count
  799. end
  800. def test_should_be_able_to_run_again_after_resetting
  801. @count = 0
  802. @machine.after_failure {@count += 1}
  803. @transition.run_callbacks {{:success => false}}
  804. @transition.reset
  805. @transition.run_callbacks {{:success => false}}
  806. assert_equal 2, @count
  807. end
  808. end
  809. class TransitionWithMultipleFailureCallbacksTest < Test::Unit::TestCase
  810. def setup
  811. @klass = Class.new
  812. @machine = StateMachine::Machine.new(@klass)
  813. @machine.state :parked, :idling
  814. @machine.event :ignite
  815. @object = @klass.new
  816. @object.state = 'parked'
  817. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  818. end
  819. def test_should_run_in_the_order_they_were_defined
  820. @callbacks = []
  821. @machine.after_failure {@callbacks << 1}
  822. @machine.after_failure {@callbacks << 2}
  823. @transition.run_callbacks {{:success => false}}
  824. assert_equal [1, 2], @callbacks
  825. end
  826. def test_should_not_run_further_callbacks_if_halted
  827. @callbacks = []
  828. @machine.after_failure {@callbacks << 1; throw :halt}
  829. @machine.after_failure {@callbacks << 2}
  830. assert_equal true, @transition.run_callbacks {{:success => false}}
  831. assert_equal [1], @callbacks
  832. end
  833. def test_should_fail_if_any_callback_halted
  834. @machine.after_failure {true}
  835. @machine.after_failure {throw :halt}
  836. assert_equal true, @transition.run_callbacks {{:success => false}}
  837. end
  838. end
  839. class TransitionWithMixedCallbacksTest < Test::Unit::TestCase
  840. def setup
  841. @klass = Class.new
  842. @machine = StateMachine::Machine.new(@klass)
  843. @machine.state :parked, :idling
  844. @machine.event :ignite
  845. @object = @klass.new
  846. @object.state = 'parked'
  847. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  848. end
  849. def test_should_before_and_around_callbacks_in_order_defined
  850. @callbacks = []
  851. @machine.before_transition {@callbacks << :before_1}
  852. @machine.around_transition {|block| @callbacks << :around; block.call}
  853. @machine.before_transition {@callbacks << :before_2}
  854. assert_equal true, @transition.run_callbacks
  855. assert_equal [:before_1, :around, :before_2], @callbacks
  856. end
  857. def test_should_run_around_callbacks_before_after_callbacks
  858. @callbacks = []
  859. @machine.after_transition {@callbacks << :after_1}
  860. @machine.around_transition {|block| block.call; @callbacks << :after_2}
  861. @machine.after_transition {@callbacks << :after_3}
  862. assert_equal true, @transition.run_callbacks
  863. assert_equal [:after_2, :after_1, :after_3], @callbacks
  864. end
  865. def test_should_have_access_to_result_for_both_after_and_around_callbacks
  866. @machine.after_transition {@after_result = @transition.result}
  867. @machine.around_transition {|block| block.call; @around_result = @transition.result}
  868. @transition.run_callbacks {{:result => 1, :success => true}}
  869. assert_equal 1, @after_result
  870. assert_equal 1, @around_result
  871. end
  872. def test_should_not_run_further_callbacks_if_before_callback_halts
  873. @callbacks = []
  874. @machine.before_transition {@callbacks << :before_1}
  875. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  876. @machine.before_transition {@callbacks << :before_2; throw :halt}
  877. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  878. @machine.after_transition {@callbacks << :after}
  879. assert_equal false, @transition.run_callbacks
  880. assert_equal [:before_1, :before_around_1, :before_2], @callbacks
  881. end
  882. def test_should_not_run_further_callbacks_if_before_yield_halts
  883. @callbacks = []
  884. @machine.before_transition {@callbacks << :before_1}
  885. @machine.around_transition {|block| @callbacks << :before_around_1; throw :halt}
  886. @machine.before_transition {@callbacks << :before_2; throw :halt}
  887. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  888. @machine.after_transition {@callbacks << :after}
  889. assert_equal false, @transition.run_callbacks
  890. assert_equal [:before_1, :before_around_1], @callbacks
  891. end
  892. def test_should_not_run_further_callbacks_if_around_callback_fails_to_yield
  893. @callbacks = []
  894. @machine.before_transition {@callbacks << :before_1}
  895. @machine.around_transition {|block| @callbacks << :before_around_1}
  896. @machine.before_transition {@callbacks << :before_2; throw :halt}
  897. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  898. @machine.after_transition {@callbacks << :after}
  899. assert_equal false, @transition.run_callbacks
  900. assert_equal [:before_1, :before_around_1], @callbacks
  901. end
  902. def test_should_not_run_further_callbacks_if_after_yield_halts
  903. @callbacks = []
  904. @machine.before_transition {@callbacks << :before_1}
  905. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1; throw :halt}
  906. @machine.before_transition {@callbacks << :before_2}
  907. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  908. @machine.after_transition {@callbacks << :after}
  909. assert_equal true, @transition.run_callbacks
  910. assert_equal [:before_1, :before_around_1, :before_2, :before_around_2, :after_around_2, :after_around_1], @callbacks
  911. end
  912. def test_should_not_run_further_callbacks_if_after_callback_halts
  913. @callbacks = []
  914. @machine.before_transition {@callbacks << :before_1}
  915. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  916. @machine.before_transition {@callbacks << :before_2}
  917. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  918. @machine.after_transition {@callbacks << :after_1; throw :halt}
  919. @machine.after_transition {@callbacks << :after_2}
  920. assert_equal true, @transition.run_callbacks
  921. assert_equal [:before_1, :before_around_1, :before_2, :before_around_2, :after_around_2, :after_around_1, :after_1], @callbacks
  922. end
  923. end
  924. class TransitionWithBeforeCallbacksSkippedTest < Test::Unit::TestCase
  925. def setup
  926. @klass = Class.new
  927. @machine = StateMachine::Machine.new(@klass)
  928. @machine.state :parked, :idling
  929. @machine.event :ignite
  930. @object = @klass.new
  931. @object.state = 'parked'
  932. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  933. end
  934. def test_should_not_run_before_callbacks
  935. @run = false
  936. @machine.before_transition {@run = true}
  937. assert_equal false, @transition.run_callbacks(:before => false)
  938. assert !@run
  939. end
  940. def test_should_run_failure_callbacks
  941. @machine.after_failure {@run = true}
  942. assert_equal false, @transition.run_callbacks(:before => false)
  943. assert @run
  944. end
  945. end
  946. class TransitionWithAfterCallbacksSkippedTest < Test::Unit::TestCase
  947. def setup
  948. @klass = Class.new
  949. @machine = StateMachine::Machine.new(@klass)
  950. @machine.state :parked, :idling
  951. @machine.event :ignite
  952. @object = @klass.new
  953. @object.state = 'parked'
  954. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  955. end
  956. def test_should_run_before_callbacks
  957. @machine.before_transition {@run = true}
  958. assert_equal true, @transition.run_callbacks(:after => false)
  959. assert @run
  960. end
  961. def test_should_not_run_after_callbacks
  962. @run = false
  963. @machine.after_transition {@run = true}
  964. assert_equal true, @transition.run_callbacks(:after => false)
  965. assert !@run
  966. end
  967. if StateMachine::Transition.pause_supported?
  968. def test_should_run_around_callbacks_before_yield
  969. @machine.around_transition {|block| @run = true; block.call}
  970. assert_equal true, @transition.run_callbacks(:after => false)
  971. assert @run
  972. end
  973. def test_should_not_run_around_callbacks_after_yield
  974. @run = false
  975. @machine.around_transition {|block| block.call; @run = true}
  976. assert_equal true, @transition.run_callbacks(:after => false)
  977. assert !@run
  978. end
  979. def test_should_continue_around_transition_execution_on_second_call
  980. @callbacks = []
  981. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  982. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  983. @machine.after_transition {@callbacks << :after}
  984. assert_equal true, @transition.run_callbacks(:after => false)
  985. assert_equal [:before_around_1, :before_around_2], @callbacks
  986. assert_equal true, @transition.run_callbacks
  987. assert_equal [:before_around_1, :before_around_2, :after_around_2, :after_around_1, :after], @callbacks
  988. end
  989. def test_should_not_run_further_callbacks_if_halted_during_continue_around_transition
  990. @callbacks = []
  991. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  992. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2; throw :halt}
  993. @machine.after_transition {@callbacks << :after}
  994. assert_equal true, @transition.run_callbacks(:after => false)
  995. assert_equal [:before_around_1, :before_around_2], @callbacks
  996. assert_equal true, @transition.run_callbacks
  997. assert_equal [:before_around_1, :before_around_2, :after_around_2], @callbacks
  998. end
  999. def test_should_not_be_able_to_continue_twice
  1000. @count = 0
  1001. @machine.around_transition {|block| block.call; @count += 1}
  1002. @machine.after_transition {@count += 1}
  1003. @transition.run_callbacks(:after => false)
  1004. 2.times do
  1005. assert_equal true, @transition.run_callbacks
  1006. assert_equal 2, @count
  1007. end
  1008. end
  1009. def test_should_not_be_able_to_continue_again_after_halted
  1010. @count = 0
  1011. @machine.around_transition {|block| block.call; @count += 1; throw :halt}
  1012. @machine.after_transition {@count += 1}
  1013. @transition.run_callbacks(:after => false)
  1014. 2.times do
  1015. assert_equal true, @transition.run_callbacks
  1016. assert_equal 1, @count
  1017. end
  1018. end
  1019. def test_should_have_access_to_result_after_continued
  1020. @machine.around_transition {|block| @around_before_result = @transition.result; block.call; @around_after_result = @transition.result}
  1021. @machine.after_transition {@after_result = @transition.result}
  1022. @transition.run_callbacks(:after => false)
  1023. @transition.run_callbacks {{:result => 1}}
  1024. assert_nil @around_before_result
  1025. assert_equal 1, @around_after_result
  1026. assert_equal 1, @after_result
  1027. end
  1028. def test_should_raise_exceptions_during_around_callbacks_after_yield_in_second_execution
  1029. @machine.around_transition {|block| block.call; raise ArgumentError}
  1030. assert_nothing_raised { @transition.run_callbacks(:after => false) }
  1031. assert_raise(ArgumentError) { @transition.run_callbacks }
  1032. end
  1033. else
  1034. def test_should_raise_exception_on_second_call
  1035. @callbacks = []
  1036. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  1037. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  1038. @machine.after_transition {@callbacks << :after}
  1039. assert_raise(ArgumentError) { @transition.run_callbacks(:after => false) }
  1040. end
  1041. end
  1042. end
  1043. class TransitionAfterBeingPerformedTest < Test::Unit::TestCase
  1044. def setup
  1045. @klass = Class.new do
  1046. attr_reader :saved, :save_state
  1047. def save
  1048. @save_state = state
  1049. @saved = true
  1050. 1
  1051. end
  1052. end
  1053. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1054. @machine.state :parked, :idling
  1055. @machine.event :ignite
  1056. @object = @klass.new
  1057. @object.state = 'parked'
  1058. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1059. @result = @transition.perform
  1060. end
  1061. def test_should_have_empty_args
  1062. assert_equal [], @transition.args
  1063. end
  1064. def test_should_have_a_result
  1065. assert_equal 1, @transition.result
  1066. end
  1067. def test_should_be_successful
  1068. assert_equal true, @result
  1069. end
  1070. def test_should_change_the_current_state
  1071. assert_equal 'idling', @object.state
  1072. end
  1073. def test_should_run_the_action
  1074. assert @object.saved
  1075. end
  1076. def test_should_run_the_action_after_saving_the_state
  1077. assert_equal 'idling', @object.save_state
  1078. end
  1079. end
  1080. class TransitionWithPerformArgumentsTest < Test::Unit::TestCase
  1081. def setup
  1082. @klass = Class.new do
  1083. attr_reader :saved
  1084. def save
  1085. @saved = true
  1086. end
  1087. end
  1088. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1089. @machine.state :parked, :idling
  1090. @machine.event :ignite
  1091. @object = @klass.new
  1092. @object.state = 'parked'
  1093. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1094. end
  1095. def test_should_have_arguments
  1096. @transition.perform(1, 2)
  1097. assert_equal [1, 2], @transition.args
  1098. assert @object.saved
  1099. end
  1100. def test_should_not_include_run_action_in_arguments
  1101. @transition.perform(1, 2, false)
  1102. assert_equal [1, 2], @transition.args
  1103. assert !@object.saved
  1104. end
  1105. end
  1106. class TransitionWithoutRunningActionTest < Test::Unit::TestCase
  1107. def setup
  1108. @klass = Class.new do
  1109. attr_reader :saved
  1110. def save
  1111. @saved = true
  1112. end
  1113. end
  1114. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1115. @machine.state :parked, :idling
  1116. @machine.event :ignite
  1117. @machine.after_transition {|object| @run_after = true}
  1118. @object = @klass.new
  1119. @object.state = 'parked'
  1120. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1121. @result = @transition.perform(false)
  1122. end
  1123. def test_should_have_empty_args
  1124. assert_equal [], @transition.args
  1125. end
  1126. def test_should_not_have_a_result
  1127. assert_nil @transition.result
  1128. end
  1129. def test_should_be_successful
  1130. assert_equal true, @result
  1131. end
  1132. def test_should_change_the_current_state
  1133. assert_equal 'idling', @object.state
  1134. end
  1135. def test_should_not_run_the_action
  1136. assert !@object.saved
  1137. end
  1138. def test_should_run_after_callbacks
  1139. assert @run_after
  1140. end
  1141. end
  1142. class TransitionWithTransactionsTest < Test::Unit::TestCase
  1143. def setup
  1144. @klass = Class.new do
  1145. class << self
  1146. attr_accessor :running_transaction
  1147. end
  1148. attr_accessor :result
  1149. def save
  1150. @result = self.class.running_transaction
  1151. true
  1152. end
  1153. end
  1154. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1155. @machine.state :parked, :idling
  1156. @machine.event :ignite
  1157. @object = @klass.new
  1158. @object.state = 'parked'
  1159. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1160. class << @machine
  1161. def within_transaction(object)
  1162. owner_class.running_transaction = object
  1163. yield
  1164. owner_class.running_transaction = false
  1165. end
  1166. end
  1167. end
  1168. def test_should_run_blocks_within_transaction_for_object
  1169. @transition.within_transaction do
  1170. @result = @klass.running_transaction
  1171. end
  1172. assert_equal @object, @result
  1173. end
  1174. end
  1175. class TransitionTransientTest < Test::Unit::TestCase
  1176. def setup
  1177. @klass = Class.new
  1178. @machine = StateMachine::Machine.new(@klass)
  1179. @machine.state :parked, :idling
  1180. @machine.event :ignite
  1181. @object = @klass.new
  1182. @object.state = 'parked'
  1183. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1184. @transition.transient = true
  1185. end
  1186. def test_should_be_transient
  1187. assert @transition.transient?
  1188. end
  1189. end
  1190. class TransitionEqualityTest < Test::Unit::TestCase
  1191. def setup
  1192. @klass = Class.new
  1193. @machine = StateMachine::Machine.new(@klass)
  1194. @machine.state :parked, :idling
  1195. @machine.event :ignite
  1196. @object = @klass.new
  1197. @object.state = 'parked'
  1198. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1199. end
  1200. def test_should_be_equal_with_same_properties
  1201. transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1202. assert_equal transition, @transition
  1203. end
  1204. def test_should_not_be_equal_with_different_machines
  1205. machine = StateMachine::Machine.new(@klass, :status, :namespace => :other)
  1206. machine.state :parked, :idling
  1207. machine.event :ignite
  1208. transition = StateMachine::Transition.new(@object, machine, :ignite, :parked, :idling)
  1209. assert_not_equal transition, @transition
  1210. end
  1211. def test_should_not_be_equal_with_different_objects
  1212. transition = StateMachine::Transition.new(@klass.new, @machine, :ignite, :parked, :idling)
  1213. assert_not_equal transition, @transition
  1214. end
  1215. def test_should_not_be_equal_with_different_event_names
  1216. @machine.event :park
  1217. transition = StateMachine::Transition.new(@object, @machine, :park, :parked, :idling)
  1218. assert_not_equal transition, @transition
  1219. end
  1220. def test_should_not_be_equal_with_different_from_state_names
  1221. @machine.state :first_gear
  1222. transition = StateMachine::Transition.new(@object, @machine, :ignite, :first_gear, :idling)
  1223. assert_not_equal transition, @transition
  1224. end
  1225. def test_should_not_be_equal_with_different_to_state_names
  1226. @machine.state :first_gear
  1227. transition = StateMachine::Transition.new(@object, @machine, :ignite, :idling, :first_gear)
  1228. assert_not_equal transition, @transition
  1229. end
  1230. end