PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/state_machine/test/unit/machine_collection_test.rb

https://bitbucket.org/systech3/boxyroom
Ruby | 938 lines | 726 code | 207 blank | 5 comment | 0 complexity | d2ca0b66a490a56615cb0bc936db4c70 MD5 | raw file
Possible License(s): JSON, MIT
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class MachineCollectionByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @machines = StateMachine::MachineCollection.new
  5. end
  6. def test_should_not_have_any_machines
  7. assert @machines.empty?
  8. end
  9. end
  10. class MachineCollectionStateInitializationTest < Test::Unit::TestCase
  11. def setup
  12. @machines = StateMachine::MachineCollection.new
  13. @klass = Class.new
  14. @machines[:state] = StateMachine::Machine.new(@klass, :state, :initial => :parked)
  15. @machines[:alarm_state] = StateMachine::Machine.new(@klass, :alarm_state, :initial => lambda {|object| :active})
  16. @machines[:alarm_state].state :active, :value => lambda {'active'}
  17. # Prevent the auto-initialization hook from firing
  18. @klass.class_eval do
  19. def initialize
  20. end
  21. end
  22. @object = @klass.new
  23. @object.state = nil
  24. @object.alarm_state = nil
  25. end
  26. def test_should_set_states_if_nil
  27. @machines.initialize_states(@object)
  28. assert_equal 'parked', @object.state
  29. assert_equal 'active', @object.alarm_state
  30. end
  31. def test_should_set_states_if_empty
  32. @object.state = ''
  33. @object.alarm_state = ''
  34. @machines.initialize_states(@object)
  35. assert_equal 'parked', @object.state
  36. assert_equal 'active', @object.alarm_state
  37. end
  38. def test_should_not_set_states_if_not_empty
  39. @object.state = 'idling'
  40. @object.alarm_state = 'off'
  41. @machines.initialize_states(@object)
  42. assert_equal 'idling', @object.state
  43. assert_equal 'off', @object.alarm_state
  44. end
  45. def test_should_only_initialize_static_states_if_dynamic_disabled
  46. @machines.initialize_states(@object, :dynamic => false)
  47. assert_equal 'parked', @object.state
  48. assert_nil @object.alarm_state
  49. end
  50. def test_should_only_initialize_dynamic_states_if_dynamic_enabled
  51. @machines.initialize_states(@object, :dynamic => true)
  52. assert_nil @object.state
  53. assert_equal 'active', @object.alarm_state
  54. end
  55. def test_should_not_set_states_if_ignored
  56. @machines.initialize_states(@object, :ignore => [:state, :alarm_state])
  57. assert_nil @object.state
  58. assert_nil @object.alarm_state
  59. end
  60. def test_should_set_states_if_not_ignored_and_nil
  61. @machines.initialize_states(@object, :ignore => [])
  62. assert_equal 'parked', @object.state
  63. assert_equal 'active', @object.alarm_state
  64. end
  65. def test_should_set_states_if_not_ignored_and_empty
  66. @object.state = ''
  67. @object.alarm_state = ''
  68. @machines.initialize_states(@object, :ignore => [])
  69. assert_equal 'parked', @object.state
  70. assert_equal 'active', @object.alarm_state
  71. end
  72. def test_should_set_states_if_not_ignored_and_not_empty
  73. @object.state = 'idling'
  74. @object.alarm_state = 'inactive'
  75. @machines.initialize_states(@object, :ignore => [])
  76. assert_equal 'parked', @object.state
  77. assert_equal 'active', @object.alarm_state
  78. end
  79. end
  80. class MachineCollectionFireExplicitTest < Test::Unit::TestCase
  81. def setup
  82. @machines = StateMachine::MachineCollection.new
  83. @klass = Class.new do
  84. attr_reader :saved
  85. def save
  86. @saved = true
  87. end
  88. end
  89. # First machine
  90. @machines[:state] = @state = StateMachine::Machine.new(@klass, :state, :initial => :parked, :action => :save)
  91. @state.event :ignite do
  92. transition :parked => :idling
  93. end
  94. @state.event :park do
  95. transition :idling => :parked
  96. end
  97. # Second machine
  98. @machines[:alarm_state] = @alarm_state = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :action => :save, :namespace => 'alarm')
  99. @alarm_state.event :enable do
  100. transition :off => :active
  101. end
  102. @alarm_state.event :disable do
  103. transition :active => :off
  104. end
  105. @object = @klass.new
  106. end
  107. def test_should_raise_exception_if_invalid_event_specified
  108. exception = assert_raise(StateMachine::InvalidEvent) { @machines.fire_events(@object, :invalid) }
  109. assert_equal ':invalid is an unknown state machine event', exception.message
  110. exception = assert_raise(StateMachine::InvalidEvent) { @machines.fire_events(@object, :ignite, :invalid) }
  111. assert_equal ':invalid is an unknown state machine event', exception.message
  112. end
  113. def test_should_fail_if_any_event_cannot_transition
  114. assert !@machines.fire_events(@object, :park, :disable_alarm)
  115. assert_equal 'parked', @object.state
  116. assert_equal 'active', @object.alarm_state
  117. assert !@object.saved
  118. assert !@machines.fire_events(@object, :ignite, :enable_alarm)
  119. assert_equal 'parked', @object.state
  120. assert_equal 'active', @object.alarm_state
  121. assert !@object.saved
  122. end
  123. def test_should_be_successful_if_all_events_transition
  124. assert @machines.fire_events(@object, :ignite, :disable_alarm)
  125. assert_equal 'idling', @object.state
  126. assert_equal 'off', @object.alarm_state
  127. assert @object.saved
  128. end
  129. def test_should_not_save_if_skipping_action
  130. assert @machines.fire_events(@object, :ignite, :disable_alarm, false)
  131. assert_equal 'idling', @object.state
  132. assert_equal 'off', @object.alarm_state
  133. assert !@object.saved
  134. end
  135. end
  136. class MachineCollectionFireExplicitWithTransactionsTest < Test::Unit::TestCase
  137. def setup
  138. @machines = StateMachine::MachineCollection.new
  139. @klass = Class.new do
  140. attr_accessor :allow_save
  141. def save
  142. @allow_save
  143. end
  144. end
  145. StateMachine::Integrations.const_set('Custom', Module.new do
  146. attr_reader :rolled_back
  147. def transaction(object)
  148. @rolled_back = yield
  149. end
  150. end)
  151. # First machine
  152. @machines[:state] = @state = StateMachine::Machine.new(@klass, :state, :initial => :parked, :action => :save, :integration => :custom)
  153. @state.event :ignite do
  154. transition :parked => :idling
  155. end
  156. # Second machine
  157. @machines[:alarm_state] = @alarm_state = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :action => :save, :namespace => 'alarm', :integration => :custom)
  158. @alarm_state.event :disable do
  159. transition :active => :off
  160. end
  161. @object = @klass.new
  162. end
  163. def test_should_not_rollback_if_successful
  164. @object.allow_save = true
  165. assert @machines.fire_events(@object, :ignite, :disable_alarm)
  166. assert_equal true, @state.rolled_back
  167. assert_nil @alarm_state.rolled_back
  168. assert_equal 'idling', @object.state
  169. assert_equal 'off', @object.alarm_state
  170. end
  171. def test_should_rollback_if_not_successful
  172. @object.allow_save = false
  173. assert !@machines.fire_events(@object, :ignite, :disable_alarm)
  174. assert_equal false, @state.rolled_back
  175. assert_nil @alarm_state.rolled_back
  176. assert_equal 'parked', @object.state
  177. assert_equal 'active', @object.alarm_state
  178. end
  179. def teardown
  180. StateMachine::Integrations.send(:remove_const, 'Custom')
  181. end
  182. end
  183. class MachineCollectionFireExplicitWithValidationsTest < Test::Unit::TestCase
  184. def setup
  185. StateMachine::Integrations.const_set('Custom', Module.new do
  186. def invalidate(object, attribute, message, values = [])
  187. (object.errors ||= []) << generate_message(message, values)
  188. end
  189. def reset(object)
  190. object.errors = []
  191. end
  192. end)
  193. @klass = Class.new do
  194. attr_accessor :errors
  195. def initialize
  196. @errors = []
  197. super
  198. end
  199. end
  200. @machines = StateMachine::MachineCollection.new
  201. @machines[:state] = @state = StateMachine::Machine.new(@klass, :state, :initial => :parked, :integration => :custom)
  202. @state.event :ignite do
  203. transition :parked => :idling
  204. end
  205. @machines[:alarm_state] = @alarm_state = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :namespace => 'alarm', :integration => :custom)
  206. @alarm_state.event :disable do
  207. transition :active => :off
  208. end
  209. @object = @klass.new
  210. end
  211. def test_should_not_invalidate_if_transitions_exist
  212. assert @machines.fire_events(@object, :ignite, :disable_alarm)
  213. assert_equal [], @object.errors
  214. end
  215. def test_should_invalidate_if_no_transitions_exist
  216. @object.state = 'idling'
  217. @object.alarm_state = 'off'
  218. assert !@machines.fire_events(@object, :ignite, :disable_alarm)
  219. assert_equal ['cannot transition via "ignite"', 'cannot transition via "disable_alarm"'], @object.errors
  220. end
  221. def teardown
  222. StateMachine::Integrations.send(:remove_const, 'Custom')
  223. end
  224. end
  225. class MachineCollectionFireImplicitTest < Test::Unit::TestCase
  226. def setup
  227. @klass = Class.new
  228. @machines = StateMachine::MachineCollection.new
  229. @machines[:state] = @machine = StateMachine::Machine.new(@klass, :state, :initial => :parked, :action => :save)
  230. @machine.event :ignite do
  231. transition :parked => :idling
  232. end
  233. @saved = false
  234. @object = @klass.new
  235. end
  236. def default_test
  237. end
  238. end
  239. class MachineCollectionFireImplicitWithoutEventTest < MachineCollectionFireImplicitTest
  240. def setup
  241. super
  242. @object.state_event = nil
  243. @result = @machines.fire_event_attributes(@object, :save) { @saved = true }
  244. end
  245. def test_should_be_successful
  246. assert_equal true, @result
  247. end
  248. def test_should_run_action
  249. assert @saved
  250. end
  251. def test_should_not_transition_state
  252. assert_equal 'parked', @object.state
  253. end
  254. def test_should_not_change_event_attribute
  255. assert_nil @object.state_event
  256. end
  257. def test_should_not_have_event_transition
  258. assert_nil @object.send(:state_event_transition)
  259. end
  260. end
  261. class MachineCollectionFireImplicitWithBlankEventTest < MachineCollectionFireImplicitTest
  262. def setup
  263. super
  264. @object.state_event = ''
  265. @result = @machines.fire_event_attributes(@object, :save) { @saved = true }
  266. end
  267. def test_should_be_successful
  268. assert_equal true, @result
  269. end
  270. def test_should_run_action
  271. assert @saved
  272. end
  273. def test_should_not_transition_state
  274. assert_equal 'parked', @object.state
  275. end
  276. def test_should_not_change_event_attribute
  277. assert_nil @object.state_event
  278. end
  279. def test_should_not_have_event_transition
  280. assert_nil @object.send(:state_event_transition)
  281. end
  282. end
  283. class MachineCollectionFireImplicitWithInvalidEventTest < MachineCollectionFireImplicitTest
  284. def setup
  285. super
  286. @object.state_event = 'invalid'
  287. @result = @machines.fire_event_attributes(@object, :save) { @saved = true }
  288. end
  289. def test_should_not_be_successful
  290. assert_equal false, @result
  291. end
  292. def test_should_not_run_action
  293. assert !@saved
  294. end
  295. def test_should_not_transition_state
  296. assert_equal 'parked', @object.state
  297. end
  298. def test_should_not_reset_event_attribute
  299. assert_equal :invalid, @object.state_event
  300. end
  301. def test_should_not_have_event_transition
  302. assert_nil @object.send(:state_event_transition)
  303. end
  304. end
  305. class MachineCollectionFireImplicitWithoutTransitionTest < MachineCollectionFireImplicitTest
  306. def setup
  307. super
  308. @object.state = 'idling'
  309. @object.state_event = 'ignite'
  310. @result = @machines.fire_event_attributes(@object, :save) { @saved = true }
  311. end
  312. def test_should_not_be_successful
  313. assert_equal false, @result
  314. end
  315. def test_should_not_run_action
  316. assert !@saved
  317. end
  318. def test_should_not_transition_state
  319. assert_equal 'idling', @object.state
  320. end
  321. def test_should_not_reset_event_attribute
  322. assert_equal :ignite, @object.state_event
  323. end
  324. def test_should_not_have_event_transition
  325. assert_nil @object.send(:state_event_transition)
  326. end
  327. end
  328. class MachineCollectionFireImplicitWithTransitionTest < MachineCollectionFireImplicitTest
  329. def setup
  330. super
  331. @state_event = nil
  332. @object.state_event = 'ignite'
  333. @result = @machines.fire_event_attributes(@object, :save) do
  334. @state_event = @object.state_event
  335. @saved = true
  336. end
  337. end
  338. def test_should_be_successful
  339. assert_equal true, @result
  340. end
  341. def test_should_run_action
  342. assert @saved
  343. end
  344. def test_should_not_have_event_while_running_action
  345. assert_nil @state_event
  346. end
  347. def test_should_transition_state
  348. assert_equal 'idling', @object.state
  349. end
  350. def test_should_reset_event_attribute
  351. assert_nil @object.state_event
  352. end
  353. def test_should_not_have_event_transition
  354. assert_nil @object.send(:state_event_transition)
  355. end
  356. def test_should_not_be_successful_if_fired_again
  357. @object.state_event = 'ignite'
  358. assert !@machines.fire_event_attributes(@object, :save) { true }
  359. end
  360. end
  361. class MachineCollectionFireImplicitWithNonBooleanResultTest < MachineCollectionFireImplicitTest
  362. def setup
  363. super
  364. @action_value = Object.new
  365. @object.state_event = 'ignite'
  366. @result = @machines.fire_event_attributes(@object, :save) do
  367. @saved = true
  368. @action_value
  369. end
  370. end
  371. def test_should_be_successful
  372. assert_equal @action_value, @result
  373. end
  374. def test_should_run_action
  375. assert @saved
  376. end
  377. def test_should_transition_state
  378. assert_equal 'idling', @object.state
  379. end
  380. end
  381. class MachineCollectionFireImplicitWithActionFailureTest < MachineCollectionFireImplicitTest
  382. def setup
  383. super
  384. @object.state_event = 'ignite'
  385. @result = @machines.fire_event_attributes(@object, :save) { false }
  386. end
  387. def test_should_not_be_successful
  388. assert_equal false, @result
  389. end
  390. def test_should_not_transition_state
  391. assert_equal 'parked', @object.state
  392. end
  393. def test_should_not_reset_event_attribute
  394. assert_equal :ignite, @object.state_event
  395. end
  396. def test_should_not_have_event_transition
  397. assert_nil @object.send(:state_event_transition)
  398. end
  399. end
  400. class MachineCollectionFireImplicitWithActionErrorTest < MachineCollectionFireImplicitTest
  401. def setup
  402. super
  403. @object.state_event = 'ignite'
  404. assert_raise(ArgumentError) { @machines.fire_event_attributes(@object, :save) { raise ArgumentError } }
  405. end
  406. def test_should_not_transition_state
  407. assert_equal 'parked', @object.state
  408. end
  409. def test_should_not_reset_event_attribute
  410. assert_equal :ignite, @object.state_event
  411. end
  412. def test_should_not_have_event_transition
  413. assert_nil @object.send(:state_event_transition)
  414. end
  415. end
  416. class MachineCollectionFireImplicitPartialTest < MachineCollectionFireImplicitTest
  417. def setup
  418. super
  419. @state_event = nil
  420. @state_event_transition = nil
  421. @object.state_event = 'ignite'
  422. @result = @machines.fire_event_attributes(@object, :save, false) do
  423. @state_event = @object.state_event
  424. @state_event_transition = @object.send(:state_event_transition)
  425. true
  426. end
  427. end
  428. def test_should_be_successful
  429. assert @result
  430. end
  431. def test_should_not_have_event_while_running_action
  432. assert_nil @state_event
  433. end
  434. def test_should_not_have_event_transition_while_running_action
  435. assert_nil @state_event_transition
  436. end
  437. def test_should_transition_state
  438. assert_equal 'idling', @object.state
  439. end
  440. def test_should_reset_event_attribute
  441. assert_nil @object.state_event
  442. end
  443. def test_should_have_event_transition
  444. assert_not_nil @object.send(:state_event_transition)
  445. end
  446. def test_should_reset_event_after_next_fire_on_success
  447. assert @machines.fire_event_attributes(@object, :save) { true }
  448. assert_equal 'idling', @object.state
  449. assert_nil @object.state_event
  450. end
  451. def test_should_reset_event_transition_after_next_fire_on_success
  452. assert @machines.fire_event_attributes(@object, :save) { true }
  453. assert_nil @object.send(:state_event_transition)
  454. end
  455. def test_should_guard_transition_after_next_fire_on_success
  456. @machines.fire_event_attributes(@object, :save) { true }
  457. @object.state = 'idling'
  458. @object.state_event = 'ignite'
  459. assert !@machines.fire_event_attributes(@object, :save) { true }
  460. end
  461. def test_should_rollback_all_attributes_after_next_fire_on_failure
  462. assert !@machines.fire_event_attributes(@object, :save) { false }
  463. assert_equal 'parked', @object.state
  464. assert_equal :ignite, @object.state_event
  465. assert_nil @object.send(:state_event_transition)
  466. @object.state = 'idling'
  467. assert !@machines.fire_event_attributes(@object, :save) { false }
  468. end
  469. def test_should_guard_transition_after_next_fire_on_failure
  470. @machines.fire_event_attributes(@object, :save) { false }
  471. @object.state = 'idling'
  472. assert !@machines.fire_event_attributes(@object, :save) { true }
  473. end
  474. def test_should_rollback_all_attributes_after_next_fire_on_error
  475. assert_raise(ArgumentError) { @machines.fire_event_attributes(@object, :save) { raise ArgumentError } }
  476. assert_equal 'parked', @object.state
  477. assert_equal :ignite, @object.state_event
  478. assert_nil @object.send(:state_event_transition)
  479. end
  480. def test_should_guard_transition_after_next_fire_on_error
  481. begin
  482. @machines.fire_event_attributes(@object, :save) { raise ArgumentError }
  483. rescue ArgumentError
  484. end
  485. @object.state = 'idling'
  486. assert !@machines.fire_event_attributes(@object, :save) { true }
  487. end
  488. end
  489. class MachineCollectionFireImplicitPartialWithCallbacksTest < MachineCollectionFireImplicitTest
  490. def setup
  491. super
  492. @object.state_event = 'ignite'
  493. end
  494. def test_should_run_before_callbacks
  495. ran_callback = false
  496. @machine.before_transition { ran_callback = true }
  497. @machines.fire_event_attributes(@object, :save, false) { true }
  498. assert ran_callback
  499. end
  500. def test_should_not_have_event_during_before_callbacks
  501. state_event = nil
  502. @machine.before_transition {|object, transition| state_event = object.state_event }
  503. @machines.fire_event_attributes(@object, :save, false) { true }
  504. assert_nil state_event
  505. end
  506. def test_should_not_have_event_transition_during_before_callbacks
  507. state_event_transition = nil
  508. @machine.before_transition {|object, transition| state_event_transition = object.send(:state_event_transition) }
  509. @machines.fire_event_attributes(@object, :save, false) { true }
  510. assert_nil state_event_transition
  511. end
  512. def test_should_not_run_after_callbacks
  513. ran_callback = false
  514. @machine.after_transition { ran_callback = true }
  515. @machines.fire_event_attributes(@object, :save, false) { true }
  516. assert !ran_callback
  517. end
  518. def test_should_not_have_event_during_after_callbacks
  519. state_event = nil
  520. @machine.after_transition {|object, transition| state_event = object.state_event }
  521. @machines.fire_event_attributes(@object, :save, false) { true }
  522. assert_nil state_event
  523. end
  524. def test_should_not_have_event_transition_during_after_callbacks
  525. state_event_transition = nil
  526. @machine.after_transition {|object, transition| state_event_transition = object.send(:state_event_transition) }
  527. @machines.fire_event_attributes(@object, :save, false) { true }
  528. assert_nil state_event_transition
  529. end
  530. end
  531. class MachineCollectionFireImplicitNestedPartialTest < MachineCollectionFireImplicitTest
  532. def setup
  533. super
  534. @partial_result = nil
  535. @object.state_event = 'ignite'
  536. @result = @machines.fire_event_attributes(@object, :save) do
  537. @partial_result = @machines.fire_event_attributes(@object, :save, false) { true }
  538. true
  539. end
  540. end
  541. def test_should_be_successful
  542. assert @result
  543. end
  544. def test_should_have_successful_partial_fire
  545. assert @partial_result
  546. end
  547. def test_should_transition_state
  548. assert_equal 'idling', @object.state
  549. end
  550. def test_should_reset_event_attribute
  551. assert_nil @object.state_event
  552. end
  553. def test_should_reset_event_transition_attribute
  554. assert_nil @object.send(:state_event_transition)
  555. end
  556. end
  557. class MachineCollectionFireImplicitWithDifferentActionsTest < MachineCollectionFireImplicitTest
  558. def setup
  559. super
  560. @machines[:alarm_state] = @alarm_machine = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :action => :save_alarm)
  561. @alarm_machine.event :disable do
  562. transition :active => :off
  563. end
  564. @saved = false
  565. @object = @klass.new
  566. @object.state_event = 'ignite'
  567. @object.alarm_state_event = 'disable'
  568. @machines.fire_event_attributes(@object, :save) { true }
  569. end
  570. def test_should_transition_states_for_action
  571. assert_equal 'idling', @object.state
  572. end
  573. def test_should_reset_event_attribute_for_action
  574. assert_nil @object.state_event
  575. end
  576. def test_should_reset_event_transition_attribute_for_action
  577. assert_nil @object.send(:state_event_transition)
  578. end
  579. def test_should_not_transition_states_for_other_actions
  580. assert_equal 'active', @object.alarm_state
  581. end
  582. def test_should_not_reset_event_attributes_for_other_actions
  583. assert_equal :disable, @object.alarm_state_event
  584. end
  585. end
  586. class MachineCollectionFireImplicitWithSameActionsTest < MachineCollectionFireImplicitTest
  587. def setup
  588. super
  589. @machines[:alarm_state] = @alarm_machine = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :action => :save)
  590. @alarm_machine.event :disable do
  591. transition :active => :off
  592. end
  593. @saved = false
  594. @object = @klass.new
  595. @object.state_event = 'ignite'
  596. @object.alarm_state_event = 'disable'
  597. @machines.fire_event_attributes(@object, :save) { true }
  598. end
  599. def test_should_transition_all_states_for_action
  600. assert_equal 'idling', @object.state
  601. assert_equal 'off', @object.alarm_state
  602. end
  603. def test_should_reset_all_event_attributes_for_action
  604. assert_nil @object.state_event
  605. assert_nil @object.alarm_state_event
  606. end
  607. end
  608. class MachineCollectionFireImplicitWithValidationsTest < Test::Unit::TestCase
  609. def setup
  610. StateMachine::Integrations.const_set('Custom', Module.new do
  611. def invalidate(object, attribute, message, values = [])
  612. (object.errors ||= []) << generate_message(message, values)
  613. end
  614. def reset(object)
  615. object.errors = []
  616. end
  617. end)
  618. @klass = Class.new do
  619. attr_accessor :errors
  620. def initialize
  621. @errors = []
  622. super
  623. end
  624. end
  625. @machines = StateMachine::MachineCollection.new
  626. @machines[:state] = @machine = StateMachine::Machine.new(@klass, :state, :initial => :parked, :action => :save, :integration => :custom)
  627. @machine.event :ignite do
  628. transition :parked => :idling
  629. end
  630. @object = @klass.new
  631. end
  632. def test_should_invalidate_if_event_is_invalid
  633. @object.state_event = 'invalid'
  634. @machines.fire_event_attributes(@object, :save) { true }
  635. assert !@object.errors.empty?
  636. end
  637. def test_should_invalidate_if_no_transition_exists
  638. @object.state = 'idling'
  639. @object.state_event = 'ignite'
  640. @machines.fire_event_attributes(@object, :save) { true }
  641. assert !@object.errors.empty?
  642. end
  643. def test_should_not_invalidate_if_transition_exists
  644. @object.state_event = 'ignite'
  645. @machines.fire_event_attributes(@object, :save) { true }
  646. assert @object.errors.empty?
  647. end
  648. def teardown
  649. StateMachine::Integrations.send(:remove_const, 'Custom')
  650. end
  651. end
  652. class MachineCollectionFireImplicitWithCustomMachineNameTest < MachineCollectionFireImplicitTest
  653. def setup
  654. super
  655. @object.state_event = 'ignite'
  656. end
  657. def test_should_be_successful_on_complete_file
  658. assert @machines.fire_event_attributes(@object, :save) { true }
  659. assert_equal 'idling', @object.state
  660. assert_nil @object.state_event
  661. assert_nil @object.send(:state_event_transition)
  662. end
  663. def test_should_be_successful_on_partial_fire
  664. @machines.fire_event_attributes(@object, :save, false) { true }
  665. assert_equal 'idling', @object.state
  666. assert_nil @object.state_event
  667. assert_not_nil @object.send(:state_event_transition)
  668. end
  669. end
  670. class MachineFireImplicitWithMarshallingTest < MachineCollectionFireImplicitTest
  671. def setup
  672. super
  673. self.class.const_set('Example', @klass)
  674. @object.state_event = 'ignite'
  675. end
  676. def test_should_marshal_during_before_callbacks
  677. @machine.before_transition {|object, transition| Marshal.dump(object)}
  678. assert_nothing_raised { @machines.fire_event_attributes(@object, :save) { true } }
  679. end
  680. def test_should_marshal_during_action
  681. assert_nothing_raised do
  682. @machines.fire_event_attributes(@object, :save) do
  683. Marshal.dump(@object)
  684. true
  685. end
  686. end
  687. end
  688. def test_should_marshal_during_after_callbacks
  689. @machine.after_transition {|object, transition| Marshal.dump(object)}
  690. assert_nothing_raised { @machines.fire_event_attributes(@object, :save) { true } }
  691. end
  692. def teardown
  693. self.class.send(:remove_const, 'Example')
  694. end
  695. end
  696. class MachineFireImplicitPartialWithMarshallingTest < MachineCollectionFireImplicitTest
  697. def setup
  698. super
  699. self.class.const_set('Example', @klass)
  700. @object.state_event = 'ignite'
  701. end
  702. def test_should_marshal_during_before_callbacks
  703. @machine.before_transition {|object, transition| Marshal.dump(object)}
  704. assert_nothing_raised do
  705. @machines.fire_event_attributes(@object, :save, false) { true }
  706. @machines.fire_event_attributes(@object, :save) { true }
  707. end
  708. end
  709. def test_should_marshal_during_action
  710. assert_nothing_raised do
  711. @machines.fire_event_attributes(@object, :save, false) do
  712. Marshal.dump(@object)
  713. true
  714. end
  715. @machines.fire_event_attributes(@object, :save) do
  716. Marshal.dump(@object)
  717. true
  718. end
  719. end
  720. end
  721. def test_should_marshal_during_after_callbacks
  722. @machine.after_transition {|object, transition| Marshal.dump(object)}
  723. assert_nothing_raised do
  724. @machines.fire_event_attributes(@object, :save, false) { true }
  725. @machines.fire_event_attributes(@object, :save) { true }
  726. end
  727. end
  728. def teardown
  729. self.class.send(:remove_const, 'Example')
  730. end
  731. end