PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/transition_test.rb

https://github.com/johnsonzes/state_machine
Ruby | 1541 lines | 1214 code | 327 blank | 0 comment | 0 complexity | ae6720b210d8cca9d5b7357da0b8d875 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_false
  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. @machine.after_transition {|object| @run = true}
  461. @transition.run_callbacks {{:success => false}}
  462. assert !@run
  463. end
  464. def test_should_run_if_successful
  465. @machine.after_transition {|object| @run = true}
  466. @transition.run_callbacks {{:success => true}}
  467. assert @run
  468. end
  469. def test_should_pass_transition_as_argument
  470. @machine.after_transition {|*args| @args = args}
  471. @transition.run_callbacks
  472. assert_equal [@object, @transition], @args
  473. end
  474. def test_should_catch_halts
  475. @machine.after_transition {throw :halt}
  476. result = nil
  477. assert_nothing_thrown { result = @transition.run_callbacks }
  478. assert_equal true, result
  479. end
  480. def test_should_not_catch_exceptions
  481. @machine.after_transition {raise ArgumentError}
  482. assert_raise(ArgumentError) { @transition.run_callbacks }
  483. end
  484. def test_should_not_be_able_to_run_twice
  485. @count = 0
  486. @machine.after_transition {@count += 1}
  487. @transition.run_callbacks
  488. @transition.run_callbacks
  489. assert_equal 1, @count
  490. end
  491. def test_should_not_be_able_to_run_twice_if_halted
  492. @count = 0
  493. @machine.after_transition {@count += 1; throw :halt}
  494. @transition.run_callbacks
  495. @transition.run_callbacks
  496. assert_equal 1, @count
  497. end
  498. def test_should_be_able_to_run_again_after_resetting
  499. @count = 0
  500. @machine.after_transition {@count += 1}
  501. @transition.run_callbacks
  502. @transition.reset
  503. @transition.run_callbacks
  504. assert_equal 2, @count
  505. end
  506. end
  507. class TransitionWithMultipleAfterCallbacksTest < Test::Unit::TestCase
  508. def setup
  509. @klass = Class.new
  510. @machine = StateMachine::Machine.new(@klass)
  511. @machine.state :parked, :idling
  512. @machine.event :ignite
  513. @object = @klass.new
  514. @object.state = 'parked'
  515. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  516. end
  517. def test_should_run_in_the_order_they_were_defined
  518. @callbacks = []
  519. @machine.after_transition {@callbacks << 1}
  520. @machine.after_transition {@callbacks << 2}
  521. @transition.run_callbacks
  522. assert_equal [1, 2], @callbacks
  523. end
  524. def test_should_not_run_further_callbacks_if_halted
  525. @callbacks = []
  526. @machine.after_transition {@callbacks << 1; throw :halt}
  527. @machine.after_transition {@callbacks << 2}
  528. assert_equal true, @transition.run_callbacks
  529. assert_equal [1], @callbacks
  530. end
  531. def test_should_fail_if_any_callback_halted
  532. @machine.after_transition {true}
  533. @machine.after_transition {throw :halt}
  534. assert_equal true, @transition.run_callbacks
  535. end
  536. end
  537. class TransitionWithAroundCallbacksTest < Test::Unit::TestCase
  538. def setup
  539. @klass = Class.new
  540. @machine = StateMachine::Machine.new(@klass)
  541. @machine.state :parked, :idling
  542. @machine.event :ignite
  543. @object = @klass.new
  544. @object.state = 'parked'
  545. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  546. end
  547. def test_should_run_around_callbacks
  548. @machine.around_transition {|object, transition, block| @run_before = true; block.call; @run_after = true}
  549. result = @transition.run_callbacks
  550. assert_equal true, result
  551. assert_equal true, @run_before
  552. assert_equal true, @run_after
  553. end
  554. def test_should_only_run_those_that_match_transition_context
  555. @count = 0
  556. callback = lambda {|object, transition, block| @count += 1; block.call}
  557. @machine.around_transition :from => :parked, :to => :idling, :on => :park, :do => callback
  558. @machine.around_transition :from => :parked, :to => :parked, :on => :park, :do => callback
  559. @machine.around_transition :from => :parked, :to => :idling, :on => :ignite, :do => callback
  560. @machine.around_transition :from => :idling, :to => :idling, :on => :park, :do => callback
  561. @transition.run_callbacks
  562. assert_equal 1, @count
  563. end
  564. def test_should_pass_transition_as_argument
  565. @machine.around_transition {|*args| block = args.pop; @args = args; block.call}
  566. @transition.run_callbacks
  567. assert_equal [@object, @transition], @args
  568. end
  569. def test_should_run_block_between_callback
  570. @callbacks = []
  571. @machine.around_transition {|block| @callbacks << :before; block.call; @callbacks << :after}
  572. @transition.run_callbacks { @callbacks << :within; {:success => true} }
  573. assert_equal [:before, :within, :after], @callbacks
  574. end
  575. def test_should_have_access_to_result_after_yield
  576. @machine.around_transition {|block| @before_result = @transition.result; block.call; @after_result = @transition.result}
  577. @transition.run_callbacks {{:result => 1, :success => true}}
  578. assert_nil @before_result
  579. assert_equal 1, @after_result
  580. end
  581. def test_should_catch_before_yield_halts
  582. @machine.around_transition {throw :halt}
  583. result = nil
  584. assert_nothing_thrown { result = @transition.run_callbacks }
  585. assert_equal false, result
  586. end
  587. def test_should_catch_after_yield_halts
  588. @machine.around_transition {|block| block.call; throw :halt}
  589. result = nil
  590. assert_nothing_thrown { result = @transition.run_callbacks }
  591. assert_equal true, result
  592. end
  593. def test_should_not_catch_before_yield
  594. @machine.around_transition {raise ArgumentError}
  595. assert_raise(ArgumentError) { @transition.run_callbacks }
  596. end
  597. def test_should_not_catch_after_yield
  598. @machine.around_transition {|block| block.call; raise ArgumentError}
  599. assert_raise(ArgumentError) { @transition.run_callbacks }
  600. end
  601. def test_should_fail_if_not_yielded
  602. @machine.around_transition {}
  603. result = nil
  604. assert_nothing_thrown { result = @transition.run_callbacks }
  605. assert_equal false, result
  606. end
  607. def test_should_not_be_able_to_run_twice
  608. @before_count = 0
  609. @after_count = 0
  610. @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
  611. @transition.run_callbacks
  612. @transition.run_callbacks
  613. assert_equal 1, @before_count
  614. assert_equal 1, @after_count
  615. end
  616. def test_should_be_able_to_run_again_after_resetting
  617. @before_count = 0
  618. @after_count = 0
  619. @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
  620. @transition.run_callbacks
  621. @transition.reset
  622. @transition.run_callbacks
  623. assert_equal 2, @before_count
  624. assert_equal 2, @after_count
  625. end
  626. def test_should_succeed_if_block_result_is_false
  627. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  628. assert_equal true, @transition.run_callbacks {{:success => true, :result => false}}
  629. assert @before_run
  630. assert @after_run
  631. end
  632. def test_should_succeed_if_block_result_is_true
  633. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  634. assert_equal true, @transition.run_callbacks {{:success => true, :result => true}}
  635. assert @before_run
  636. assert @after_run
  637. end
  638. def test_should_only_run_before_if_block_success_is_false
  639. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  640. assert_equal true, @transition.run_callbacks {{:success => false}}
  641. assert @before_run
  642. assert !@after_run
  643. end
  644. def test_should_succeed_if_block_success_is_false
  645. @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
  646. assert_equal true, @transition.run_callbacks {{:success => true}}
  647. assert @before_run
  648. assert @after_run
  649. end
  650. end
  651. class TransitionWithMultipleAroundCallbacksTest < Test::Unit::TestCase
  652. def setup
  653. @klass = Class.new
  654. @machine = StateMachine::Machine.new(@klass)
  655. @machine.state :parked, :idling
  656. @machine.event :ignite
  657. @object = @klass.new
  658. @object.state = 'parked'
  659. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  660. end
  661. def test_should_before_yield_in_the_order_they_were_defined
  662. @callbacks = []
  663. @machine.around_transition {|block| @callbacks << 1; block.call}
  664. @machine.around_transition {|block| @callbacks << 2; block.call}
  665. @transition.run_callbacks
  666. assert_equal [1, 2], @callbacks
  667. end
  668. def test_should_before_yield_multiple_methods_in_the_order_they_were_defined
  669. @callbacks = []
  670. @machine.around_transition(lambda {|block| @callbacks << 1; block.call}, lambda {|block| @callbacks << 2; block.call})
  671. @machine.around_transition(lambda {|block| @callbacks << 3; block.call}, lambda {|block| @callbacks << 4; block.call})
  672. @transition.run_callbacks
  673. assert_equal [1, 2, 3, 4], @callbacks
  674. end
  675. def test_should_after_yield_in_the_reverse_order_they_were_defined
  676. @callbacks = []
  677. @machine.around_transition {|block| block.call; @callbacks << 1}
  678. @machine.around_transition {|block| block.call; @callbacks << 2}
  679. @transition.run_callbacks
  680. assert_equal [2, 1], @callbacks
  681. end
  682. def test_should_after_yield_multiple_methods_in_the_reverse_order_they_were_defined
  683. @callbacks = []
  684. @machine.around_transition(lambda {|block| block.call; @callbacks << 1}) {|block| block.call; @callbacks << 2}
  685. @machine.around_transition(lambda {|block| block.call; @callbacks << 3}) {|block| block.call; @callbacks << 4}
  686. @transition.run_callbacks
  687. assert_equal [4, 3, 2, 1], @callbacks
  688. end
  689. def test_should_run_block_between_callback
  690. @callbacks = []
  691. @machine.around_transition {|block| @callbacks << :before_1; block.call; @callbacks << :after_1}
  692. @machine.around_transition {|block| @callbacks << :before_2; block.call; @callbacks << :after_2}
  693. @transition.run_callbacks { @callbacks << :within; {:success => true} }
  694. assert_equal [:before_1, :before_2, :within, :after_2, :after_1], @callbacks
  695. end
  696. def test_should_have_access_to_result_after_yield
  697. @machine.around_transition {|block| @before_result_1 = @transition.result; block.call; @after_result_1 = @transition.result}
  698. @machine.around_transition {|block| @before_result_2 = @transition.result; block.call; @after_result_2 = @transition.result}
  699. @transition.run_callbacks {{:result => 1, :success => true}}
  700. assert_nil @before_result_1
  701. assert_nil @before_result_2
  702. assert_equal 1, @after_result_1
  703. assert_equal 1, @after_result_2
  704. end
  705. def test_should_fail_if_any_before_yield_halted
  706. @machine.around_transition {|block| block.call}
  707. @machine.around_transition {throw :halt}
  708. assert_equal false, @transition.run_callbacks
  709. end
  710. def test_should_not_continue_around_callbacks_if_before_yield_halted
  711. @callbacks = []
  712. @machine.around_transition {@callbacks << 1; throw :halt}
  713. @machine.around_transition {|block| @callbacks << 2; block.call; @callbacks << 3}
  714. assert_equal false, @transition.run_callbacks
  715. assert_equal [1], @callbacks
  716. end
  717. def test_should_not_continue_around_callbacks_if_later_before_yield_halted
  718. @callbacks = []
  719. @machine.around_transition {|block| block.call; @callbacks << 1}
  720. @machine.around_transition {throw :halt}
  721. @transition.run_callbacks
  722. assert_equal [], @callbacks
  723. end
  724. def test_should_not_run_further_callbacks_if_after_yield_halted
  725. @callbacks = []
  726. @machine.around_transition {|block| block.call; @callbacks << 1}
  727. @machine.around_transition {|block| block.call; throw :halt}
  728. assert_equal true, @transition.run_callbacks
  729. assert_equal [], @callbacks
  730. end
  731. def test_should_fail_if_any_fail_to_yield
  732. @callbacks = []
  733. @machine.around_transition {@callbacks << 1}
  734. @machine.around_transition {|block| @callbacks << 2; block.call; @callbacks << 3}
  735. assert_equal false, @transition.run_callbacks
  736. assert_equal [1], @callbacks
  737. end
  738. end
  739. class TransitionWithFailureCallbacksTest < Test::Unit::TestCase
  740. def setup
  741. @klass = Class.new
  742. @machine = StateMachine::Machine.new(@klass)
  743. @machine.state :parked, :idling
  744. @machine.event :ignite
  745. @object = @klass.new
  746. @object.state = 'parked'
  747. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  748. end
  749. def test_should_only_run_those_that_match_transition_context
  750. @count = 0
  751. callback = lambda {@count += 1}
  752. @machine.after_failure :do => callback
  753. @machine.after_failure :on => :park, :do => callback
  754. @machine.after_failure :on => :ignite, :do => callback
  755. @transition.run_callbacks {{:success => false}}
  756. assert_equal 2, @count
  757. end
  758. def test_should_run_if_not_successful
  759. @machine.after_failure {|object| @run = true}
  760. @transition.run_callbacks {{:success => false}}
  761. assert @run
  762. end
  763. def test_should_not_run_if_successful
  764. @machine.after_failure {|object| @run = true}
  765. @transition.run_callbacks {{:success => true}}
  766. assert !@run
  767. end
  768. def test_should_pass_transition_as_argument
  769. @machine.after_failure {|*args| @args = args}
  770. @transition.run_callbacks {{:success => false}}
  771. assert_equal [@object, @transition], @args
  772. end
  773. def test_should_catch_halts
  774. @machine.after_failure {throw :halt}
  775. result = nil
  776. assert_nothing_thrown { result = @transition.run_callbacks {{:success => false}} }
  777. assert_equal true, result
  778. end
  779. def test_should_not_catch_exceptions
  780. @machine.after_failure {raise ArgumentError}
  781. assert_raise(ArgumentError) { @transition.run_callbacks {{:success => false}} }
  782. end
  783. def test_should_not_be_able_to_run_twice
  784. @count = 0
  785. @machine.after_failure {@count += 1}
  786. @transition.run_callbacks {{:success => false}}
  787. @transition.run_callbacks {{:success => false}}
  788. assert_equal 1, @count
  789. end
  790. def test_should_not_be_able_to_run_twice_if_halted
  791. @count = 0
  792. @machine.after_failure {@count += 1; throw :halt}
  793. @transition.run_callbacks {{:success => false}}
  794. @transition.run_callbacks {{:success => false}}
  795. assert_equal 1, @count
  796. end
  797. def test_should_be_able_to_run_again_after_resetting
  798. @count = 0
  799. @machine.after_failure {@count += 1}
  800. @transition.run_callbacks {{:success => false}}
  801. @transition.reset
  802. @transition.run_callbacks {{:success => false}}
  803. assert_equal 2, @count
  804. end
  805. end
  806. class TransitionWithMultipleFailureCallbacksTest < Test::Unit::TestCase
  807. def setup
  808. @klass = Class.new
  809. @machine = StateMachine::Machine.new(@klass)
  810. @machine.state :parked, :idling
  811. @machine.event :ignite
  812. @object = @klass.new
  813. @object.state = 'parked'
  814. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  815. end
  816. def test_should_run_in_the_order_they_were_defined
  817. @callbacks = []
  818. @machine.after_failure {@callbacks << 1}
  819. @machine.after_failure {@callbacks << 2}
  820. @transition.run_callbacks {{:success => false}}
  821. assert_equal [1, 2], @callbacks
  822. end
  823. def test_should_not_run_further_callbacks_if_halted
  824. @callbacks = []
  825. @machine.after_failure {@callbacks << 1; throw :halt}
  826. @machine.after_failure {@callbacks << 2}
  827. assert_equal true, @transition.run_callbacks {{:success => false}}
  828. assert_equal [1], @callbacks
  829. end
  830. def test_should_fail_if_any_callback_halted
  831. @machine.after_failure {true}
  832. @machine.after_failure {throw :halt}
  833. assert_equal true, @transition.run_callbacks {{:success => false}}
  834. end
  835. end
  836. class TransitionWithMixedCallbacksTest < Test::Unit::TestCase
  837. def setup
  838. @klass = Class.new
  839. @machine = StateMachine::Machine.new(@klass)
  840. @machine.state :parked, :idling
  841. @machine.event :ignite
  842. @object = @klass.new
  843. @object.state = 'parked'
  844. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  845. end
  846. def test_should_before_and_around_callbacks_in_order_defined
  847. @callbacks = []
  848. @machine.before_transition {@callbacks << :before_1}
  849. @machine.around_transition {|block| @callbacks << :around; block.call}
  850. @machine.before_transition {@callbacks << :before_2}
  851. assert_equal true, @transition.run_callbacks
  852. assert_equal [:before_1, :around, :before_2], @callbacks
  853. end
  854. def test_should_run_around_callbacks_before_after_callbacks
  855. @callbacks = []
  856. @machine.after_transition {@callbacks << :after_1}
  857. @machine.around_transition {|block| block.call; @callbacks << :after_2}
  858. @machine.after_transition {@callbacks << :after_3}
  859. assert_equal true, @transition.run_callbacks
  860. assert_equal [:after_2, :after_1, :after_3], @callbacks
  861. end
  862. def test_should_have_access_to_result_for_both_after_and_around_callbacks
  863. @machine.after_transition {@after_result = @transition.result}
  864. @machine.around_transition {|block| block.call; @around_result = @transition.result}
  865. @transition.run_callbacks {{:result => 1, :success => true}}
  866. assert_equal 1, @after_result
  867. assert_equal 1, @around_result
  868. end
  869. def test_should_not_run_further_callbacks_if_before_callback_halts
  870. @callbacks = []
  871. @machine.before_transition {@callbacks << :before_1}
  872. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  873. @machine.before_transition {@callbacks << :before_2; throw :halt}
  874. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  875. @machine.after_transition {@callbacks << :after}
  876. assert_equal false, @transition.run_callbacks
  877. assert_equal [:before_1, :before_around_1, :before_2], @callbacks
  878. end
  879. def test_should_not_run_further_callbacks_if_before_yield_halts
  880. @callbacks = []
  881. @machine.before_transition {@callbacks << :before_1}
  882. @machine.around_transition {|block| @callbacks << :before_around_1; throw :halt}
  883. @machine.before_transition {@callbacks << :before_2; throw :halt}
  884. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  885. @machine.after_transition {@callbacks << :after}
  886. assert_equal false, @transition.run_callbacks
  887. assert_equal [:before_1, :before_around_1], @callbacks
  888. end
  889. def test_should_not_run_further_callbacks_if_around_callback_fails_to_yield
  890. @callbacks = []
  891. @machine.before_transition {@callbacks << :before_1}
  892. @machine.around_transition {|block| @callbacks << :before_around_1}
  893. @machine.before_transition {@callbacks << :before_2; throw :halt}
  894. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  895. @machine.after_transition {@callbacks << :after}
  896. assert_equal false, @transition.run_callbacks
  897. assert_equal [:before_1, :before_around_1], @callbacks
  898. end
  899. def test_should_not_run_further_callbacks_if_after_yield_halts
  900. @callbacks = []
  901. @machine.before_transition {@callbacks << :before_1}
  902. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1; throw :halt}
  903. @machine.before_transition {@callbacks << :before_2}
  904. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  905. @machine.after_transition {@callbacks << :after}
  906. assert_equal true, @transition.run_callbacks
  907. assert_equal [:before_1, :before_around_1, :before_2, :before_around_2, :after_around_2, :after_around_1], @callbacks
  908. end
  909. def test_should_not_run_further_callbacks_if_after_callback_halts
  910. @callbacks = []
  911. @machine.before_transition {@callbacks << :before_1}
  912. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  913. @machine.before_transition {@callbacks << :before_2}
  914. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  915. @machine.after_transition {@callbacks << :after_1; throw :halt}
  916. @machine.after_transition {@callbacks << :after_2}
  917. assert_equal true, @transition.run_callbacks
  918. assert_equal [:before_1, :before_around_1, :before_2, :before_around_2, :after_around_2, :after_around_1, :after_1], @callbacks
  919. end
  920. end
  921. class TransitionWithBeforeCallbacksSkippedTest < Test::Unit::TestCase
  922. def setup
  923. @klass = Class.new
  924. @machine = StateMachine::Machine.new(@klass)
  925. @machine.state :parked, :idling
  926. @machine.event :ignite
  927. @object = @klass.new
  928. @object.state = 'parked'
  929. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  930. end
  931. def test_should_not_run_before_callbacks
  932. @machine.before_transition {@run = true}
  933. assert_equal false, @transition.run_callbacks(:before => false)
  934. assert !@run
  935. end
  936. def test_should_run_failure_callbacks
  937. @machine.after_failure {@run = true}
  938. assert_equal false, @transition.run_callbacks(:before => false)
  939. assert @run
  940. end
  941. end
  942. class TransitionWithAfterCallbacksSkippedTest < Test::Unit::TestCase
  943. def setup
  944. @klass = Class.new
  945. @machine = StateMachine::Machine.new(@klass)
  946. @machine.state :parked, :idling
  947. @machine.event :ignite
  948. @object = @klass.new
  949. @object.state = 'parked'
  950. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  951. end
  952. def test_should_run_before_callbacks
  953. @machine.before_transition {@run = true}
  954. assert_equal true, @transition.run_callbacks(:after => false)
  955. assert @run
  956. end
  957. def test_should_run_around_callbacks_before_yield
  958. @machine.around_transition {|block| @run = true; block.call}
  959. assert_equal true, @transition.run_callbacks(:after => false)
  960. assert @run
  961. end
  962. def test_should_not_run_after_callbacks
  963. @machine.after_transition {@run = true}
  964. assert_equal true, @transition.run_callbacks(:after => false)
  965. assert !@run
  966. end
  967. def test_should_not_run_around_callbacks_after_yield
  968. @machine.around_transition {|block| block.call; @run = true}
  969. assert_equal true, @transition.run_callbacks(:after => false)
  970. assert !@run
  971. end
  972. def test_should_continue_around_transition_execution_on_second_call
  973. @callbacks = []
  974. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  975. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
  976. @machine.after_transition {@callbacks << :after}
  977. assert_equal true, @transition.run_callbacks(:after => false)
  978. assert_equal [:before_around_1, :before_around_2], @callbacks
  979. assert_equal true, @transition.run_callbacks
  980. assert_equal [:before_around_1, :before_around_2, :after_around_2, :after_around_1, :after], @callbacks
  981. end
  982. def test_should_not_run_further_callbacks_if_halted_during_continue_around_transition
  983. @callbacks = []
  984. @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
  985. @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2; throw :halt}
  986. @machine.after_transition {@callbacks << :after}
  987. assert_equal true, @transition.run_callbacks(:after => false)
  988. assert_equal [:before_around_1, :before_around_2], @callbacks
  989. assert_equal true, @transition.run_callbacks
  990. assert_equal [:before_around_1, :before_around_2, :after_around_2], @callbacks
  991. end
  992. def test_should_not_be_able_to_continue_twice
  993. @count = 0
  994. @machine.around_transition {|block| block.call; @count += 1}
  995. @machine.after_transition {@count += 1}
  996. @transition.run_callbacks(:after => false)
  997. 2.times do
  998. assert_equal true, @transition.run_callbacks
  999. assert_equal 2, @count
  1000. end
  1001. end
  1002. def test_should_not_be_able_to_continue_again_after_halted
  1003. @count = 0
  1004. @machine.around_transition {|block| block.call; @count += 1; throw :halt}
  1005. @machine.after_transition {@count += 1}
  1006. @transition.run_callbacks(:after => false)
  1007. 2.times do
  1008. assert_equal true, @transition.run_callbacks
  1009. assert_equal 1, @count
  1010. end
  1011. end
  1012. def test_should_have_access_to_result_after_continued
  1013. @machine.around_transition {|block| @around_before_result = @transition.result; block.call; @around_after_result = @transition.result}
  1014. @machine.after_transition {@after_result = @transition.result}
  1015. @transition.run_callbacks(:after => false)
  1016. @transition.run_callbacks {{:result => 1}}
  1017. assert_nil @around_before_result
  1018. assert_equal 1, @around_after_result
  1019. assert_equal 1, @after_result
  1020. end
  1021. def test_should_raise_exceptions_during_around_callbacks_after_yield_in_second_execution
  1022. @machine.around_transition {|block| block.call; raise ArgumentError}
  1023. assert_nothing_raised { @transition.run_callbacks(:after => false) }
  1024. assert_raise(ArgumentError) { @transition.run_callbacks }
  1025. end
  1026. end
  1027. class TransitionAfterBeingPerformedTest < Test::Unit::TestCase
  1028. def setup
  1029. @klass = Class.new do
  1030. attr_reader :saved, :save_state
  1031. def save
  1032. @save_state = state
  1033. @saved = true
  1034. 1
  1035. end
  1036. end
  1037. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1038. @machine.state :parked, :idling
  1039. @machine.event :ignite
  1040. @object = @klass.new
  1041. @object.state = 'parked'
  1042. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1043. @result = @transition.perform
  1044. end
  1045. def test_should_have_empty_args
  1046. assert_equal [], @transition.args
  1047. end
  1048. def test_should_have_a_result
  1049. assert_equal 1, @transition.result
  1050. end
  1051. def test_should_be_successful
  1052. assert_equal true, @result
  1053. end
  1054. def test_should_change_the_current_state
  1055. assert_equal 'idling', @object.state
  1056. end
  1057. def test_should_run_the_action
  1058. assert @object.saved
  1059. end
  1060. def test_should_run_the_action_after_saving_the_state
  1061. assert_equal 'idling', @object.save_state
  1062. end
  1063. end
  1064. class TransitionWithPerformArgumentsTest < Test::Unit::TestCase
  1065. def setup
  1066. @klass = Class.new do
  1067. attr_reader :saved
  1068. def save
  1069. @saved = true
  1070. end
  1071. end
  1072. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1073. @machine.state :parked, :idling
  1074. @machine.event :ignite
  1075. @object = @klass.new
  1076. @object.state = 'parked'
  1077. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1078. end
  1079. def test_should_have_arguments
  1080. @transition.perform(1, 2)
  1081. assert_equal [1, 2], @transition.args
  1082. assert @object.saved
  1083. end
  1084. def test_should_not_include_run_action_in_arguments
  1085. @transition.perform(1, 2, false)
  1086. assert_equal [1, 2], @transition.args
  1087. assert !@object.saved
  1088. end
  1089. end
  1090. class TransitionWithoutRunningActionTest < Test::Unit::TestCase
  1091. def setup
  1092. @klass = Class.new do
  1093. attr_reader :saved
  1094. def save
  1095. @saved = true
  1096. end
  1097. end
  1098. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1099. @machine.state :parked, :idling
  1100. @machine.event :ignite
  1101. @machine.after_transition {|object| @run_after = true}
  1102. @object = @klass.new
  1103. @object.state = 'parked'
  1104. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1105. @result = @transition.perform(false)
  1106. end
  1107. def test_should_have_empty_args
  1108. assert_equal [], @transition.args
  1109. end
  1110. def test_should_not_have_a_result
  1111. assert_nil @transition.result
  1112. end
  1113. def test_should_be_successful
  1114. assert_equal true, @result
  1115. end
  1116. def test_should_change_the_current_state
  1117. assert_equal 'idling', @object.state
  1118. end
  1119. def test_should_not_run_the_action
  1120. assert !@object.saved
  1121. end
  1122. def test_should_run_after_callbacks
  1123. assert @run_after
  1124. end
  1125. end
  1126. class TransitionWithTransactionsTest < Test::Unit::TestCase
  1127. def setup
  1128. @klass = Class.new do
  1129. class << self
  1130. attr_accessor :running_transaction
  1131. end
  1132. attr_accessor :result
  1133. def save
  1134. @result = self.class.running_transaction
  1135. true
  1136. end
  1137. end
  1138. @machine = StateMachine::Machine.new(@klass, :action => :save)
  1139. @machine.state :parked, :idling
  1140. @machine.event :ignite
  1141. @object = @klass.new
  1142. @object.state = 'parked'
  1143. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1144. class << @machine
  1145. def within_transaction(object)
  1146. owner_class.running_transaction = object
  1147. yield
  1148. owner_class.running_transaction = false
  1149. end
  1150. end
  1151. end
  1152. def test_should_run_blocks_within_transaction_for_object
  1153. @transition.within_transaction do
  1154. @result = @klass.running_transaction
  1155. end
  1156. assert_equal @object, @result
  1157. end
  1158. end
  1159. class TransitionTransientTest < Test::Unit::TestCase
  1160. def setup
  1161. @klass = Class.new
  1162. @machine = StateMachine::Machine.new(@klass)
  1163. @machine.state :parked, :idling
  1164. @machine.event :ignite
  1165. @object = @klass.new
  1166. @object.state = 'parked'
  1167. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1168. @transition.transient = true
  1169. end
  1170. def test_should_be_transient
  1171. assert @transition.transient?
  1172. end
  1173. end
  1174. class TransitionEqualityTest < Test::Unit::TestCase
  1175. def setup
  1176. @klass = Class.new
  1177. @machine = StateMachine::Machine.new(@klass)
  1178. @machine.state :parked, :idling
  1179. @machine.event :ignite
  1180. @object = @klass.new
  1181. @object.state = 'parked'
  1182. @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1183. end
  1184. def test_should_be_equal_with_same_properties
  1185. transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
  1186. assert_equal transition, @transition
  1187. end
  1188. def test_should_not_be_equal_with_different_machines
  1189. machine = StateMachine::Machine.new(@klass, :namespace => :other)
  1190. machine.state :parked, :idling
  1191. machine.event :ignite
  1192. transition = StateMachine::Transition.new(@object, machine, :ignite, :parked, :idling)
  1193. assert_not_equal transition, @transition
  1194. end
  1195. def test_should_not_be_equal_with_different_objects
  1196. transition = StateMachine::Transition.new(@klass.new, @machine, :ignite, :parked, :idling)
  1197. assert_not_equal transition, @transition
  1198. end
  1199. def test_should_not_be_equal_with_different_event_names
  1200. @machine.event :park
  1201. transition = StateMachine::Transition.new(@object, @machine, :park, :parked, :idling)
  1202. assert_not_equal transition, @transition
  1203. end
  1204. def test_should_not_be_equal_with_different_from_state_names
  1205. @machine.state :first_gear
  1206. transition = StateMachine::Transition.new(@object, @machine, :ignite, :first_gear, :idling)
  1207. assert_not_equal transition, @transition
  1208. end
  1209. def test_should_not_be_equal_with_different_to_state_names
  1210. @machine.state :first_gear
  1211. transition = StateMachine::Transition.new(@object, @machine, :ignite, :idling, :first_gear)
  1212. assert_not_equal transition, @transition
  1213. end
  1214. end