PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/machine_test.rb

http://github.com/pluginaweek/state_machine
Ruby | 3407 lines | 2703 code | 702 blank | 2 comment | 10 complexity | ed0267e8a6db2b963dc205033b3167e8 MD5 | raw file

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

  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class MachineByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @klass = Class.new
  5. @machine = StateMachine::Machine.new(@klass)
  6. @object = @klass.new
  7. end
  8. def test_should_have_an_owner_class
  9. assert_equal @klass, @machine.owner_class
  10. end
  11. def test_should_have_a_name
  12. assert_equal :state, @machine.name
  13. end
  14. def test_should_have_an_attribute
  15. assert_equal :state, @machine.attribute
  16. end
  17. def test_should_prefix_custom_attributes_with_attribute
  18. assert_equal :state_event, @machine.attribute(:event)
  19. end
  20. def test_should_have_an_initial_state
  21. assert_not_nil @machine.initial_state(@object)
  22. end
  23. def test_should_have_a_nil_initial_state
  24. assert_nil @machine.initial_state(@object).value
  25. end
  26. def test_should_not_have_any_events
  27. assert !@machine.events.any?
  28. end
  29. def test_should_not_have_any_before_callbacks
  30. assert @machine.callbacks[:before].empty?
  31. end
  32. def test_should_not_have_any_after_callbacks
  33. assert @machine.callbacks[:after].empty?
  34. end
  35. def test_should_not_have_any_failure_callbacks
  36. assert @machine.callbacks[:failure].empty?
  37. end
  38. def test_should_not_have_an_action
  39. assert_nil @machine.action
  40. end
  41. def test_should_use_tranactions
  42. assert_equal true, @machine.use_transactions
  43. end
  44. def test_should_not_have_a_namespace
  45. assert_nil @machine.namespace
  46. end
  47. def test_should_have_a_nil_state
  48. assert_equal [nil], @machine.states.keys
  49. end
  50. def test_should_set_initial_on_nil_state
  51. assert @machine.state(nil).initial
  52. end
  53. def test_should_generate_default_messages
  54. assert_equal 'is invalid', @machine.generate_message(:invalid)
  55. assert_equal 'cannot transition when parked', @machine.generate_message(:invalid_event, [[:state, :parked]])
  56. assert_equal 'cannot transition via "park"', @machine.generate_message(:invalid_transition, [[:event, :park]])
  57. end
  58. def test_should_not_be_extended_by_the_base_integration
  59. assert !(class << @machine; ancestors; end).include?(StateMachine::Integrations::Base)
  60. end
  61. def test_should_not_be_extended_by_the_active_model_integration
  62. assert !(class << @machine; ancestors; end).include?(StateMachine::Integrations::ActiveModel)
  63. end
  64. def test_should_not_be_extended_by_the_active_record_integration
  65. assert !(class << @machine; ancestors; end).include?(StateMachine::Integrations::ActiveRecord)
  66. end
  67. def test_should_not_be_extended_by_the_datamapper_integration
  68. assert !(class << @machine; ancestors; end).include?(StateMachine::Integrations::DataMapper)
  69. end
  70. def test_should_not_be_extended_by_the_mongo_mapper_integration
  71. assert !(class << @machine; ancestors; end).include?(StateMachine::Integrations::MongoMapper)
  72. end
  73. def test_should_not_be_extended_by_the_sequel_integration
  74. assert !(class << @machine; ancestors; end).include?(StateMachine::Integrations::Sequel)
  75. end
  76. def test_should_define_a_reader_attribute_for_the_attribute
  77. assert @object.respond_to?(:state)
  78. end
  79. def test_should_define_a_writer_attribute_for_the_attribute
  80. assert @object.respond_to?(:state=)
  81. end
  82. def test_should_define_a_predicate_for_the_attribute
  83. assert @object.respond_to?(:state?)
  84. end
  85. def test_should_define_a_name_reader_for_the_attribute
  86. assert @object.respond_to?(:state_name)
  87. end
  88. def test_should_define_an_event_reader_for_the_attribute
  89. assert @object.respond_to?(:state_events)
  90. end
  91. def test_should_define_a_transition_reader_for_the_attribute
  92. assert @object.respond_to?(:state_transitions)
  93. end
  94. def test_should_define_a_path_reader_for_the_attribute
  95. assert @object.respond_to?(:state_paths)
  96. end
  97. def test_should_define_an_event_runner_for_the_attribute
  98. assert @object.respond_to?(:fire_state_event)
  99. end
  100. def test_should_not_define_an_event_attribute_reader
  101. assert !@object.respond_to?(:state_event)
  102. end
  103. def test_should_not_define_an_event_attribute_writer
  104. assert !@object.respond_to?(:state_event=)
  105. end
  106. def test_should_not_define_an_event_transition_attribute_reader
  107. assert !@object.respond_to?(:state_event_transition)
  108. end
  109. def test_should_not_define_an_event_transition_attribute_writer
  110. assert !@object.respond_to?(:state_event_transition=)
  111. end
  112. def test_should_define_a_human_attribute_name_reader_for_the_attribute
  113. assert @klass.respond_to?(:human_state_name)
  114. end
  115. def test_should_define_a_human_event_name_reader_for_the_attribute
  116. assert @klass.respond_to?(:human_state_event_name)
  117. end
  118. def test_should_not_define_singular_with_scope
  119. assert !@klass.respond_to?(:with_state)
  120. end
  121. def test_should_not_define_singular_without_scope
  122. assert !@klass.respond_to?(:without_state)
  123. end
  124. def test_should_not_define_plural_with_scope
  125. assert !@klass.respond_to?(:with_states)
  126. end
  127. def test_should_not_define_plural_without_scope
  128. assert !@klass.respond_to?(:without_states)
  129. end
  130. def test_should_extend_owner_class_with_class_methods
  131. assert((class << @klass; ancestors; end).include?(StateMachine::ClassMethods))
  132. end
  133. def test_should_include_instance_methods_in_owner_class
  134. assert @klass.included_modules.include?(StateMachine::InstanceMethods)
  135. end
  136. def test_should_define_state_machines_reader
  137. expected = {:state => @machine}
  138. assert_equal expected, @klass.state_machines
  139. end
  140. end
  141. class MachineWithCustomNameTest < Test::Unit::TestCase
  142. def setup
  143. @klass = Class.new
  144. @machine = StateMachine::Machine.new(@klass, :status)
  145. @object = @klass.new
  146. end
  147. def test_should_use_custom_name
  148. assert_equal :status, @machine.name
  149. end
  150. def test_should_use_custom_name_for_attribute
  151. assert_equal :status, @machine.attribute
  152. end
  153. def test_should_prefix_custom_attributes_with_custom_name
  154. assert_equal :status_event, @machine.attribute(:event)
  155. end
  156. def test_should_define_a_reader_attribute_for_the_attribute
  157. assert @object.respond_to?(:status)
  158. end
  159. def test_should_define_a_writer_attribute_for_the_attribute
  160. assert @object.respond_to?(:status=)
  161. end
  162. def test_should_define_a_predicate_for_the_attribute
  163. assert @object.respond_to?(:status?)
  164. end
  165. def test_should_define_a_name_reader_for_the_attribute
  166. assert @object.respond_to?(:status_name)
  167. end
  168. def test_should_define_an_event_reader_for_the_attribute
  169. assert @object.respond_to?(:status_events)
  170. end
  171. def test_should_define_a_transition_reader_for_the_attribute
  172. assert @object.respond_to?(:status_transitions)
  173. end
  174. def test_should_define_an_event_runner_for_the_attribute
  175. assert @object.respond_to?(:fire_status_event)
  176. end
  177. def test_should_define_a_human_attribute_name_reader_for_the_attribute
  178. assert @klass.respond_to?(:human_status_name)
  179. end
  180. def test_should_define_a_human_event_name_reader_for_the_attribute
  181. assert @klass.respond_to?(:human_status_event_name)
  182. end
  183. end
  184. class MachineWithoutInitializationTest < Test::Unit::TestCase
  185. def setup
  186. @klass = Class.new do
  187. def initialize(attributes = {})
  188. attributes.each {|attr, value| send("#{attr}=", value)}
  189. super()
  190. end
  191. end
  192. @machine = StateMachine::Machine.new(@klass, :initial => :parked, :initialize => false)
  193. end
  194. def test_should_not_have_an_initial_state
  195. object = @klass.new
  196. assert_nil object.state
  197. end
  198. def test_should_still_allow_manual_initialization
  199. @klass.send(:include, Module.new do
  200. def initialize(attributes = {})
  201. super()
  202. initialize_state_machines
  203. end
  204. end)
  205. object = @klass.new
  206. assert_equal 'parked', object.state
  207. end
  208. end
  209. class MachineWithStaticInitialStateTest < Test::Unit::TestCase
  210. def setup
  211. @klass = Class.new
  212. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  213. end
  214. def test_should_not_have_dynamic_initial_state
  215. assert !@machine.dynamic_initial_state?
  216. end
  217. def test_should_have_an_initial_state
  218. object = @klass.new
  219. assert_equal 'parked', @machine.initial_state(object).value
  220. end
  221. def test_should_write_to_attribute_when_initializing_state
  222. object = @klass.allocate
  223. @machine.initialize_state(object)
  224. assert_equal 'parked', object.state
  225. end
  226. def test_should_set_initial_on_state_object
  227. assert @machine.state(:parked).initial
  228. end
  229. def test_should_set_initial_state_on_created_object
  230. assert_equal 'parked', @klass.new.state
  231. end
  232. def test_not_set_initial_state_even_if_not_empty
  233. @klass.class_eval do
  234. def initialize(attributes = {})
  235. self.state = 'idling'
  236. super()
  237. end
  238. end
  239. object = @klass.new
  240. assert_equal 'idling', object.state
  241. end
  242. def test_should_set_initial_state_prior_to_initialization
  243. base = Class.new do
  244. attr_accessor :state_on_init
  245. def initialize
  246. self.state_on_init = state
  247. end
  248. end
  249. klass = Class.new(base)
  250. StateMachine::Machine.new(klass, :initial => :parked)
  251. assert_equal 'parked', klass.new.state_on_init
  252. end
  253. def test_should_be_included_in_known_states
  254. assert_equal [:parked], @machine.states.keys
  255. end
  256. end
  257. class MachineWithDynamicInitialStateTest < Test::Unit::TestCase
  258. def setup
  259. @klass = Class.new do
  260. attr_accessor :initial_state
  261. end
  262. @machine = StateMachine::Machine.new(@klass, :initial => lambda {|object| object.initial_state || :default})
  263. @machine.state :parked, :idling, :default
  264. @object = @klass.new
  265. end
  266. def test_should_have_dynamic_initial_state
  267. assert @machine.dynamic_initial_state?
  268. end
  269. def test_should_use_the_record_for_determining_the_initial_state
  270. @object.initial_state = :parked
  271. assert_equal :parked, @machine.initial_state(@object).name
  272. @object.initial_state = :idling
  273. assert_equal :idling, @machine.initial_state(@object).name
  274. end
  275. def test_should_write_to_attribute_when_initializing_state
  276. object = @klass.allocate
  277. object.initial_state = :parked
  278. @machine.initialize_state(object)
  279. assert_equal 'parked', object.state
  280. end
  281. def test_should_set_initial_state_on_created_object
  282. assert_equal 'default', @object.state
  283. end
  284. def test_should_not_set_initial_state_even_if_not_empty
  285. @klass.class_eval do
  286. def initialize(attributes = {})
  287. self.state = 'parked'
  288. super()
  289. end
  290. end
  291. object = @klass.new
  292. assert_equal 'parked', object.state
  293. end
  294. def test_should_set_initial_state_after_initialization
  295. base = Class.new do
  296. attr_accessor :state_on_init
  297. def initialize
  298. self.state_on_init = state
  299. end
  300. end
  301. klass = Class.new(base)
  302. machine = StateMachine::Machine.new(klass, :initial => lambda {|object| :parked})
  303. machine.state :parked
  304. assert_nil klass.new.state_on_init
  305. end
  306. def test_should_not_be_included_in_known_states
  307. assert_equal [:parked, :idling, :default], @machine.states.map {|state| state.name}
  308. end
  309. end
  310. class MachineStateInitializationTest < Test::Unit::TestCase
  311. def setup
  312. @klass = Class.new
  313. @machine = StateMachine::Machine.new(@klass, :state, :initial => :parked, :initialize => false)
  314. @object = @klass.new
  315. @object.state = nil
  316. end
  317. def test_should_set_states_if_nil
  318. @machine.initialize_state(@object)
  319. assert_equal 'parked', @object.state
  320. end
  321. def test_should_set_states_if_empty
  322. @object.state = ''
  323. @machine.initialize_state(@object)
  324. assert_equal 'parked', @object.state
  325. end
  326. def test_should_not_set_states_if_not_empty
  327. @object.state = 'idling'
  328. @machine.initialize_state(@object)
  329. assert_equal 'idling', @object.state
  330. end
  331. def test_should_set_states_if_not_empty_and_forced
  332. @object.state = 'idling'
  333. @machine.initialize_state(@object, :force => true)
  334. assert_equal 'parked', @object.state
  335. end
  336. def test_should_not_set_state_if_nil_and_nil_is_valid_state
  337. @machine.state :initial, :value => nil
  338. @machine.initialize_state(@object)
  339. assert_nil @object.state
  340. end
  341. def test_should_write_to_hash_if_specified
  342. @machine.initialize_state(@object, :to => hash = {})
  343. assert_equal({'state' => 'parked'}, hash)
  344. end
  345. def test_should_not_write_to_object_if_writing_to_hash
  346. @machine.initialize_state(@object, :to => {})
  347. assert_nil @object.state
  348. end
  349. end
  350. class MachineWithCustomActionTest < Test::Unit::TestCase
  351. def setup
  352. @machine = StateMachine::Machine.new(Class.new, :action => :save)
  353. end
  354. def test_should_use_the_custom_action
  355. assert_equal :save, @machine.action
  356. end
  357. end
  358. class MachineWithNilActionTest < Test::Unit::TestCase
  359. def setup
  360. integration = Module.new do
  361. include StateMachine::Integrations::Base
  362. @defaults = {:action => :save}
  363. end
  364. StateMachine::Integrations.const_set('Custom', integration)
  365. @machine = StateMachine::Machine.new(Class.new, :action => nil, :integration => :custom)
  366. end
  367. def test_should_have_a_nil_action
  368. assert_nil @machine.action
  369. end
  370. def teardown
  371. StateMachine::Integrations.send(:remove_const, 'Custom')
  372. end
  373. end
  374. class MachineWithoutIntegrationTest < Test::Unit::TestCase
  375. def setup
  376. @klass = Class.new
  377. @machine = StateMachine::Machine.new(@klass)
  378. @object = @klass.new
  379. end
  380. def test_transaction_should_yield
  381. @yielded = false
  382. @machine.within_transaction(@object) do
  383. @yielded = true
  384. end
  385. assert @yielded
  386. end
  387. def test_invalidation_should_do_nothing
  388. assert_nil @machine.invalidate(@object, :state, :invalid_transition, [[:event, 'park']])
  389. end
  390. def test_reset_should_do_nothing
  391. assert_nil @machine.reset(@object)
  392. end
  393. def test_errors_for_should_be_empty
  394. assert_equal '', @machine.errors_for(@object)
  395. end
  396. end
  397. class MachineWithCustomIntegrationTest < Test::Unit::TestCase
  398. def setup
  399. integration = Module.new do
  400. include StateMachine::Integrations::Base
  401. def self.matching_ancestors
  402. ['MachineWithCustomIntegrationTest::Vehicle']
  403. end
  404. end
  405. StateMachine::Integrations.const_set('Custom', integration)
  406. superclass = Class.new
  407. self.class.const_set('Vehicle', superclass)
  408. @klass = Class.new(superclass)
  409. end
  410. def test_should_be_extended_by_the_integration_if_explicit
  411. machine = StateMachine::Machine.new(@klass, :integration => :custom)
  412. assert((class << machine; ancestors; end).include?(StateMachine::Integrations::Custom))
  413. end
  414. def test_should_not_be_extended_by_the_integration_if_implicit_but_not_available
  415. StateMachine::Integrations::Custom.class_eval do
  416. class << self; remove_method :matching_ancestors; end
  417. def self.matching_ancestors
  418. []
  419. end
  420. end
  421. machine = StateMachine::Machine.new(@klass)
  422. assert(!(class << machine; ancestors; end).include?(StateMachine::Integrations::Custom))
  423. end
  424. def test_should_not_be_extended_by_the_integration_if_implicit_but_not_matched
  425. StateMachine::Integrations::Custom.class_eval do
  426. class << self; remove_method :matching_ancestors; end
  427. def self.matching_ancestors
  428. []
  429. end
  430. end
  431. machine = StateMachine::Machine.new(@klass)
  432. assert(!(class << machine; ancestors; end).include?(StateMachine::Integrations::Custom))
  433. end
  434. def test_should_be_extended_by_the_integration_if_implicit_and_available_and_matches
  435. machine = StateMachine::Machine.new(@klass)
  436. assert((class << machine; ancestors; end).include?(StateMachine::Integrations::Custom))
  437. end
  438. def test_should_not_be_extended_by_the_integration_if_nil
  439. machine = StateMachine::Machine.new(@klass, :integration => nil)
  440. assert(!(class << machine; ancestors; end).include?(StateMachine::Integrations::Custom))
  441. end
  442. def test_should_not_be_extended_by_the_integration_if_false
  443. machine = StateMachine::Machine.new(@klass, :integration => false)
  444. assert(!(class << machine; ancestors; end).include?(StateMachine::Integrations::Custom))
  445. end
  446. def teardown
  447. self.class.send(:remove_const, 'Vehicle')
  448. StateMachine::Integrations.send(:remove_const, 'Custom')
  449. end
  450. end
  451. class MachineWithIntegrationTest < Test::Unit::TestCase
  452. def setup
  453. StateMachine::Integrations.const_set('Custom', Module.new do
  454. include StateMachine::Integrations::Base
  455. @defaults = {:action => :save, :use_transactions => false}
  456. attr_reader :initialized, :with_scopes, :without_scopes, :ran_transaction
  457. def after_initialize
  458. @initialized = true
  459. end
  460. def create_with_scope(name)
  461. (@with_scopes ||= []) << name
  462. lambda {}
  463. end
  464. def create_without_scope(name)
  465. (@without_scopes ||= []) << name
  466. lambda {}
  467. end
  468. def transaction(object)
  469. @ran_transaction = true
  470. yield
  471. end
  472. end)
  473. @machine = StateMachine::Machine.new(Class.new, :integration => :custom)
  474. end
  475. def test_should_call_after_initialize_hook
  476. assert @machine.initialized
  477. end
  478. def test_should_use_the_default_action
  479. assert_equal :save, @machine.action
  480. end
  481. def test_should_use_the_custom_action_if_specified
  482. machine = StateMachine::Machine.new(Class.new, :integration => :custom, :action => :save!)
  483. assert_equal :save!, machine.action
  484. end
  485. def test_should_use_the_default_use_transactions
  486. assert_equal false, @machine.use_transactions
  487. end
  488. def test_should_use_the_custom_use_transactions_if_specified
  489. machine = StateMachine::Machine.new(Class.new, :integration => :custom, :use_transactions => true)
  490. assert_equal true, machine.use_transactions
  491. end
  492. def test_should_define_a_singular_and_plural_with_scope
  493. assert_equal %w(with_state with_states), @machine.with_scopes
  494. end
  495. def test_should_define_a_singular_and_plural_without_scope
  496. assert_equal %w(without_state without_states), @machine.without_scopes
  497. end
  498. def teardown
  499. StateMachine::Integrations.send(:remove_const, 'Custom')
  500. end
  501. end
  502. class MachineWithActionUndefinedTest < Test::Unit::TestCase
  503. def setup
  504. @klass = Class.new
  505. @machine = StateMachine::Machine.new(@klass, :action => :save)
  506. @object = @klass.new
  507. end
  508. def test_should_define_an_event_attribute_reader
  509. assert @object.respond_to?(:state_event)
  510. end
  511. def test_should_define_an_event_attribute_writer
  512. assert @object.respond_to?(:state_event=)
  513. end
  514. def test_should_define_an_event_transition_attribute_reader
  515. assert @object.respond_to?(:state_event_transition, true)
  516. end
  517. def test_should_define_an_event_transition_attribute_writer
  518. assert @object.respond_to?(:state_event_transition=, true)
  519. end
  520. def test_should_not_define_action
  521. assert !@object.respond_to?(:save)
  522. end
  523. def test_should_not_mark_action_hook_as_defined
  524. assert !@machine.action_hook?
  525. end
  526. end
  527. class MachineWithActionDefinedInClassTest < Test::Unit::TestCase
  528. def setup
  529. @klass = Class.new do
  530. def save
  531. end
  532. end
  533. @machine = StateMachine::Machine.new(@klass, :action => :save)
  534. @object = @klass.new
  535. end
  536. def test_should_define_an_event_attribute_reader
  537. assert @object.respond_to?(:state_event)
  538. end
  539. def test_should_define_an_event_attribute_writer
  540. assert @object.respond_to?(:state_event=)
  541. end
  542. def test_should_define_an_event_transition_attribute_reader
  543. assert @object.respond_to?(:state_event_transition, true)
  544. end
  545. def test_should_define_an_event_transition_attribute_writer
  546. assert @object.respond_to?(:state_event_transition=, true)
  547. end
  548. def test_should_not_define_action
  549. assert !@klass.ancestors.any? {|ancestor| ancestor != @klass && ancestor.method_defined?(:save)}
  550. end
  551. def test_should_not_mark_action_hook_as_defined
  552. assert !@machine.action_hook?
  553. end
  554. end
  555. class MachineWithActionDefinedInIncludedModuleTest < Test::Unit::TestCase
  556. def setup
  557. @mod = mod = Module.new do
  558. def save
  559. end
  560. end
  561. @klass = Class.new do
  562. include mod
  563. end
  564. @machine = StateMachine::Machine.new(@klass, :action => :save)
  565. @object = @klass.new
  566. end
  567. def test_should_define_an_event_attribute_reader
  568. assert @object.respond_to?(:state_event)
  569. end
  570. def test_should_define_an_event_attribute_writer
  571. assert @object.respond_to?(:state_event=)
  572. end
  573. def test_should_define_an_event_transition_attribute_reader
  574. assert @object.respond_to?(:state_event_transition, true)
  575. end
  576. def test_should_define_an_event_transition_attribute_writer
  577. assert @object.respond_to?(:state_event_transition=, true)
  578. end
  579. def test_should_define_action
  580. assert @klass.ancestors.any? {|ancestor| ![@klass, @mod].include?(ancestor) && ancestor.method_defined?(:save)}
  581. end
  582. def test_should_keep_action_public
  583. assert @klass.public_method_defined?(:save)
  584. end
  585. def test_should_mark_action_hook_as_defined
  586. assert @machine.action_hook?
  587. end
  588. end
  589. class MachineWithActionDefinedInSuperclassTest < Test::Unit::TestCase
  590. def setup
  591. @superclass = Class.new do
  592. def save
  593. end
  594. end
  595. @klass = Class.new(@superclass)
  596. @machine = StateMachine::Machine.new(@klass, :action => :save)
  597. @object = @klass.new
  598. end
  599. def test_should_define_an_event_attribute_reader
  600. assert @object.respond_to?(:state_event)
  601. end
  602. def test_should_define_an_event_attribute_writer
  603. assert @object.respond_to?(:state_event=)
  604. end
  605. def test_should_define_an_event_transition_attribute_reader
  606. assert @object.respond_to?(:state_event_transition, true)
  607. end
  608. def test_should_define_an_event_transition_attribute_writer
  609. assert @object.respond_to?(:state_event_transition=, true)
  610. end
  611. def test_should_define_action
  612. assert @klass.ancestors.any? {|ancestor| ![@klass, @superclass].include?(ancestor) && ancestor.method_defined?(:save)}
  613. end
  614. def test_should_keep_action_public
  615. assert @klass.public_method_defined?(:save)
  616. end
  617. def test_should_mark_action_hook_as_defined
  618. assert @machine.action_hook?
  619. end
  620. end
  621. class MachineWithPrivateActionTest < Test::Unit::TestCase
  622. def setup
  623. @superclass = Class.new do
  624. private
  625. def save
  626. end
  627. end
  628. @klass = Class.new(@superclass)
  629. @machine = StateMachine::Machine.new(@klass, :action => :save)
  630. @object = @klass.new
  631. end
  632. def test_should_define_an_event_attribute_reader
  633. assert @object.respond_to?(:state_event)
  634. end
  635. def test_should_define_an_event_attribute_writer
  636. assert @object.respond_to?(:state_event=)
  637. end
  638. def test_should_define_an_event_transition_attribute_reader
  639. assert @object.respond_to?(:state_event_transition, true)
  640. end
  641. def test_should_define_an_event_transition_attribute_writer
  642. assert @object.respond_to?(:state_event_transition=, true)
  643. end
  644. def test_should_define_action
  645. assert @klass.ancestors.any? {|ancestor| ![@klass, @superclass].include?(ancestor) && ancestor.private_method_defined?(:save)}
  646. end
  647. def test_should_keep_action_private
  648. assert @klass.private_method_defined?(:save)
  649. end
  650. def test_should_mark_action_hook_as_defined
  651. assert @machine.action_hook?
  652. end
  653. end
  654. class MachineWithActionAlreadyOverriddenTest < Test::Unit::TestCase
  655. def setup
  656. @superclass = Class.new do
  657. def save
  658. end
  659. end
  660. @klass = Class.new(@superclass)
  661. StateMachine::Machine.new(@klass, :action => :save)
  662. @machine = StateMachine::Machine.new(@klass, :status, :action => :save)
  663. @object = @klass.new
  664. end
  665. def test_should_not_redefine_action
  666. assert_equal 1, @klass.ancestors.select {|ancestor| ![@klass, @superclass].include?(ancestor) && ancestor.method_defined?(:save)}.length
  667. end
  668. def test_should_mark_action_hook_as_defined
  669. assert @machine.action_hook?
  670. end
  671. end
  672. class MachineWithCustomPluralTest < Test::Unit::TestCase
  673. def setup
  674. @integration = Module.new do
  675. include StateMachine::Integrations::Base
  676. class << self; attr_accessor :with_scopes, :without_scopes; end
  677. @with_scopes = []
  678. @without_scopes = []
  679. def create_with_scope(name)
  680. StateMachine::Integrations::Custom.with_scopes << name
  681. lambda {}
  682. end
  683. def create_without_scope(name)
  684. StateMachine::Integrations::Custom.without_scopes << name
  685. lambda {}
  686. end
  687. end
  688. StateMachine::Integrations.const_set('Custom', @integration)
  689. end
  690. def test_should_define_a_singular_and_plural_with_scope
  691. StateMachine::Machine.new(Class.new, :integration => :custom, :plural => 'staties')
  692. assert_equal %w(with_state with_staties), @integration.with_scopes
  693. end
  694. def test_should_define_a_singular_and_plural_without_scope
  695. StateMachine::Machine.new(Class.new, :integration => :custom, :plural => 'staties')
  696. assert_equal %w(without_state without_staties), @integration.without_scopes
  697. end
  698. def test_should_define_single_with_scope_if_singular_same_as_plural
  699. StateMachine::Machine.new(Class.new, :integration => :custom, :plural => 'state')
  700. assert_equal %w(with_state), @integration.with_scopes
  701. end
  702. def test_should_define_single_without_scope_if_singular_same_as_plural
  703. StateMachine::Machine.new(Class.new, :integration => :custom, :plural => 'state')
  704. assert_equal %w(without_state), @integration.without_scopes
  705. end
  706. def teardown
  707. StateMachine::Integrations.send(:remove_const, 'Custom')
  708. end
  709. end
  710. class MachineWithCustomInvalidationTest < Test::Unit::TestCase
  711. def setup
  712. @integration = Module.new do
  713. include StateMachine::Integrations::Base
  714. def invalidate(object, attribute, message, values = [])
  715. object.error = generate_message(message, values)
  716. end
  717. end
  718. StateMachine::Integrations.const_set('Custom', @integration)
  719. @klass = Class.new do
  720. attr_accessor :error
  721. end
  722. @machine = StateMachine::Machine.new(@klass, :integration => :custom, :messages => {:invalid_transition => 'cannot %s'})
  723. @machine.state :parked
  724. @object = @klass.new
  725. @object.state = 'parked'
  726. end
  727. def test_generate_custom_message
  728. assert_equal 'cannot park', @machine.generate_message(:invalid_transition, [[:event, :park]])
  729. end
  730. def test_use_custom_message
  731. @machine.invalidate(@object, :state, :invalid_transition, [[:event, 'park']])
  732. assert_equal 'cannot park', @object.error
  733. end
  734. def teardown
  735. StateMachine::Integrations.send(:remove_const, 'Custom')
  736. end
  737. end
  738. class MachineTest < Test::Unit::TestCase
  739. def test_should_raise_exception_if_invalid_option_specified
  740. assert_raise(ArgumentError) {StateMachine::Machine.new(Class.new, :invalid => true)}
  741. end
  742. def test_should_not_raise_exception_if_custom_messages_specified
  743. assert_nothing_raised {StateMachine::Machine.new(Class.new, :messages => {:invalid_transition => 'custom'})}
  744. end
  745. def test_should_evaluate_a_block_during_initialization
  746. called = true
  747. StateMachine::Machine.new(Class.new) do
  748. called = respond_to?(:event)
  749. end
  750. assert called
  751. end
  752. def test_should_provide_matcher_helpers_during_initialization
  753. matchers = []
  754. StateMachine::Machine.new(Class.new) do
  755. matchers = [all, any, same]
  756. end
  757. assert_equal [StateMachine::AllMatcher.instance, StateMachine::AllMatcher.instance, StateMachine::LoopbackMatcher.instance], matchers
  758. end
  759. end
  760. class MachineAfterBeingCopiedTest < Test::Unit::TestCase
  761. def setup
  762. @machine = StateMachine::Machine.new(Class.new, :state, :initial => :parked)
  763. @machine.event(:ignite) {}
  764. @machine.before_transition(lambda {})
  765. @machine.after_transition(lambda {})
  766. @machine.around_transition(lambda {})
  767. @machine.after_failure(lambda {})
  768. @copied_machine = @machine.clone
  769. end
  770. def test_should_not_have_the_same_collection_of_states
  771. assert_not_same @copied_machine.states, @machine.states
  772. end
  773. def test_should_copy_each_state
  774. assert_not_same @copied_machine.states[:parked], @machine.states[:parked]
  775. end
  776. def test_should_update_machine_for_each_state
  777. assert_equal @copied_machine, @copied_machine.states[:parked].machine
  778. end
  779. def test_should_not_update_machine_for_original_state
  780. assert_equal @machine, @machine.states[:parked].machine
  781. end
  782. def test_should_not_have_the_same_collection_of_events
  783. assert_not_same @copied_machine.events, @machine.events
  784. end
  785. def test_should_copy_each_event
  786. assert_not_same @copied_machine.events[:ignite], @machine.events[:ignite]
  787. end
  788. def test_should_update_machine_for_each_event
  789. assert_equal @copied_machine, @copied_machine.events[:ignite].machine
  790. end
  791. def test_should_not_update_machine_for_original_event
  792. assert_equal @machine, @machine.events[:ignite].machine
  793. end
  794. def test_should_not_have_the_same_callbacks
  795. assert_not_same @copied_machine.callbacks, @machine.callbacks
  796. end
  797. def test_should_not_have_the_same_before_callbacks
  798. assert_not_same @copied_machine.callbacks[:before], @machine.callbacks[:before]
  799. end
  800. def test_should_not_have_the_same_after_callbacks
  801. assert_not_same @copied_machine.callbacks[:after], @machine.callbacks[:after]
  802. end
  803. def test_should_not_have_the_same_failure_callbacks
  804. assert_not_same @copied_machine.callbacks[:failure], @machine.callbacks[:failure]
  805. end
  806. end
  807. class MachineAfterChangingOwnerClassTest < Test::Unit::TestCase
  808. def setup
  809. @original_class = Class.new
  810. @machine = StateMachine::Machine.new(@original_class)
  811. @new_class = Class.new(@original_class)
  812. @new_machine = @machine.clone
  813. @new_machine.owner_class = @new_class
  814. @object = @new_class.new
  815. end
  816. def test_should_update_owner_class
  817. assert_equal @new_class, @new_machine.owner_class
  818. end
  819. def test_should_not_change_original_owner_class
  820. assert_equal @original_class, @machine.owner_class
  821. end
  822. def test_should_change_the_associated_machine_in_the_new_class
  823. assert_equal @new_machine, @new_class.state_machines[:state]
  824. end
  825. def test_should_not_change_the_associated_machine_in_the_original_class
  826. assert_equal @machine, @original_class.state_machines[:state]
  827. end
  828. end
  829. class MachineAfterChangingInitialState < Test::Unit::TestCase
  830. def setup
  831. @klass = Class.new
  832. @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  833. @machine.initial_state = :idling
  834. @object = @klass.new
  835. end
  836. def test_should_change_the_initial_state
  837. assert_equal :idling, @machine.initial_state(@object).name
  838. end
  839. def test_should_include_in_known_states
  840. assert_equal [:parked, :idling], @machine.states.map {|state| state.name}
  841. end
  842. def test_should_reset_original_initial_state
  843. assert !@machine.state(:parked).initial
  844. end
  845. def test_should_set_new_state_to_initial
  846. assert @machine.state(:idling).initial
  847. end
  848. end
  849. class MachineWithHelpersTest < Test::Unit::TestCase
  850. def setup
  851. @klass = Class.new
  852. @machine = StateMachine::Machine.new(@klass)
  853. @object = @klass.new
  854. end
  855. def test_should_throw_exception_with_invalid_scope
  856. assert_raise(RUBY_VERSION < '1.9' ? IndexError : KeyError) { @machine.define_helper(:invalid, :park) {} }
  857. end
  858. end
  859. class MachineWithInstanceHelpersTest < Test::Unit::TestCase
  860. def setup
  861. @klass = Class.new
  862. @machine = StateMachine::Machine.new(@klass)
  863. @object = @klass.new
  864. end
  865. def test_should_not_redefine_existing_public_methods
  866. @klass.class_eval do
  867. def park
  868. true
  869. end
  870. end
  871. @machine.define_helper(:instance, :park) {}
  872. assert_equal true, @object.park
  873. end
  874. def test_should_not_redefine_existing_protected_methods
  875. @klass.class_eval do
  876. protected
  877. def park
  878. true
  879. end
  880. end
  881. @machine.define_helper(:instance, :park) {}
  882. assert_equal true, @object.send(:park)
  883. end
  884. def test_should_not_redefine_existing_private_methods
  885. @klass.class_eval do
  886. private
  887. def park
  888. true
  889. end
  890. end
  891. @machine.define_helper(:instance, :park) {}
  892. assert_equal true, @object.send(:park)
  893. end
  894. def test_should_warn_if_defined_in_superclass
  895. require 'stringio'
  896. @original_stderr, $stderr = $stderr, StringIO.new
  897. superclass = Class.new do
  898. def park
  899. end
  900. end
  901. klass = Class.new(superclass)
  902. machine = StateMachine::Machine.new(klass)
  903. machine.define_helper(:instance, :park) {}
  904. assert_equal "Instance method \"park\" is already defined in #{superclass.to_s}, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  905. ensure
  906. $stderr = @original_stderr
  907. end
  908. def test_should_warn_if_defined_in_multiple_superclasses
  909. require 'stringio'
  910. @original_stderr, $stderr = $stderr, StringIO.new
  911. superclass1 = Class.new do
  912. def park
  913. end
  914. end
  915. superclass2 = Class.new(superclass1) do
  916. def park
  917. end
  918. end
  919. klass = Class.new(superclass2)
  920. machine = StateMachine::Machine.new(klass)
  921. machine.define_helper(:instance, :park) {}
  922. assert_equal "Instance method \"park\" is already defined in #{superclass1.to_s}, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  923. ensure
  924. $stderr = @original_stderr
  925. end
  926. def test_should_warn_if_defined_in_module_prior_to_helper_module
  927. require 'stringio'
  928. @original_stderr, $stderr = $stderr, StringIO.new
  929. mod = Module.new do
  930. def park
  931. end
  932. end
  933. klass = Class.new do
  934. include mod
  935. end
  936. machine = StateMachine::Machine.new(klass)
  937. machine.define_helper(:instance, :park) {}
  938. assert_equal "Instance method \"park\" is already defined in #{mod.to_s}, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  939. ensure
  940. $stderr = @original_stderr
  941. end
  942. def test_should_not_warn_if_defined_in_module_after_helper_module
  943. require 'stringio'
  944. @original_stderr, $stderr = $stderr, StringIO.new
  945. klass = Class.new
  946. machine = StateMachine::Machine.new(klass)
  947. mod = Module.new do
  948. def park
  949. end
  950. end
  951. klass.class_eval do
  952. include mod
  953. end
  954. machine.define_helper(:instance, :park) {}
  955. assert_equal '', $stderr.string
  956. ensure
  957. $stderr = @original_stderr
  958. end
  959. def test_should_define_if_ignoring_method_conflicts_and_defined_in_superclass
  960. require 'stringio'
  961. @original_stderr, $stderr = $stderr, StringIO.new
  962. StateMachine::Machine.ignore_method_conflicts = true
  963. superclass = Class.new do
  964. def park
  965. end
  966. end
  967. klass = Class.new(superclass)
  968. machine = StateMachine::Machine.new(klass)
  969. machine.define_helper(:instance, :park) {true}
  970. assert_equal '', $stderr.string
  971. assert_equal true, klass.new.park
  972. ensure
  973. StateMachine::Machine.ignore_method_conflicts = false
  974. $stderr = @original_stderr
  975. end
  976. def test_should_define_nonexistent_methods
  977. @machine.define_helper(:instance, :park) {false}
  978. assert_equal false, @object.park
  979. end
  980. def test_should_warn_if_defined_multiple_times
  981. require 'stringio'
  982. @original_stderr, $stderr = $stderr, StringIO.new
  983. @machine.define_helper(:instance, :park) {}
  984. @machine.define_helper(:instance, :park) {}
  985. assert_equal "Instance method \"park\" is already defined in #{@klass} :state instance helpers, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  986. ensure
  987. $stderr = @original_stderr
  988. end
  989. def test_should_pass_context_as_arguments
  990. helper_args = nil
  991. @machine.define_helper(:instance, :park) {|*args| helper_args = args}
  992. @object.park
  993. assert_equal 2, helper_args.length
  994. assert_equal [@machine, @object], helper_args
  995. end
  996. def test_should_pass_method_arguments_through
  997. helper_args = nil
  998. @machine.define_helper(:instance, :park) {|*args| helper_args = args}
  999. @object.park(1, 2, 3)
  1000. assert_equal 5, helper_args.length
  1001. assert_equal [@machine, @object, 1, 2, 3], helper_args
  1002. end
  1003. def test_should_allow_string_evaluation
  1004. @machine.define_helper :instance, <<-end_eval, __FILE__, __LINE__ + 1
  1005. def park
  1006. false
  1007. end
  1008. end_eval
  1009. assert_equal false, @object.park
  1010. end
  1011. end
  1012. class MachineWithClassHelpersTest < Test::Unit::TestCase
  1013. def setup
  1014. @klass = Class.new
  1015. @machine = StateMachine::Machine.new(@klass)
  1016. end
  1017. def test_should_not_redefine_existing_public_methods
  1018. class << @klass
  1019. def states
  1020. []
  1021. end
  1022. end
  1023. @machine.define_helper(:class, :states) {}
  1024. assert_equal [], @klass.states
  1025. end
  1026. def test_should_not_redefine_existing_protected_methods
  1027. class << @klass
  1028. protected
  1029. def states
  1030. []
  1031. end
  1032. end
  1033. @machine.define_helper(:class, :states) {}
  1034. assert_equal [], @klass.send(:states)
  1035. end
  1036. def test_should_not_redefine_existing_private_methods
  1037. class << @klass
  1038. private
  1039. def states
  1040. []
  1041. end
  1042. end
  1043. @machine.define_helper(:class, :states) {}
  1044. assert_equal [], @klass.send(:states)
  1045. end
  1046. def test_should_warn_if_defined_in_superclass
  1047. require 'stringio'
  1048. @original_stderr, $stderr = $stderr, StringIO.new
  1049. superclass = Class.new do
  1050. def self.park
  1051. end
  1052. end
  1053. klass = Class.new(superclass)
  1054. machine = StateMachine::Machine.new(klass)
  1055. machine.define_helper(:class, :park) {}
  1056. assert_equal "Class method \"park\" is already defined in #{superclass.to_s}, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  1057. ensure
  1058. $stderr = @original_stderr
  1059. end
  1060. def test_should_warn_if_defined_in_multiple_superclasses
  1061. require 'stringio'
  1062. @original_stderr, $stderr = $stderr, StringIO.new
  1063. superclass1 = Class.new do
  1064. def self.park
  1065. end
  1066. end
  1067. superclass2 = Class.new(superclass1) do
  1068. def self.park
  1069. end
  1070. end
  1071. klass = Class.new(superclass2)
  1072. machine = StateMachine::Machine.new(klass)
  1073. machine.define_helper(:class, :park) {}
  1074. assert_equal "Class method \"park\" is already defined in #{superclass1.to_s}, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  1075. ensure
  1076. $stderr = @original_stderr
  1077. end
  1078. def test_should_warn_if_defined_in_module_prior_to_helper_module
  1079. require 'stringio'
  1080. @original_stderr, $stderr = $stderr, StringIO.new
  1081. mod = Module.new do
  1082. def park
  1083. end
  1084. end
  1085. klass = Class.new do
  1086. extend mod
  1087. end
  1088. machine = StateMachine::Machine.new(klass)
  1089. machine.define_helper(:class, :park) {}
  1090. assert_equal "Class method \"park\" is already defined in #{mod.to_s}, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  1091. ensure
  1092. $stderr = @original_stderr
  1093. end
  1094. def test_should_not_warn_if_defined_in_module_after_helper_module
  1095. require 'stringio'
  1096. @original_stderr, $stderr = $stderr, StringIO.new
  1097. klass = Class.new
  1098. machine = StateMachine::Machine.new(klass)
  1099. mod = Module.new do
  1100. def park
  1101. end
  1102. end
  1103. klass.class_eval do
  1104. extend mod
  1105. end
  1106. machine.define_helper(:class, :park) {}
  1107. assert_equal '', $stderr.string
  1108. ensure
  1109. $stderr = @original_stderr
  1110. end
  1111. def test_should_define_if_ignoring_method_conflicts_and_defined_in_superclass
  1112. require 'stringio'
  1113. @original_stderr, $stderr = $stderr, StringIO.new
  1114. StateMachine::Machine.ignore_method_conflicts = true
  1115. superclass = Class.new do
  1116. def self.park
  1117. end
  1118. end
  1119. klass = Class.new(superclass)
  1120. machine = StateMachine::Machine.new(klass)
  1121. machine.define_helper(:class, :park) {true}
  1122. assert_equal '', $stderr.string
  1123. assert_equal true, klass.park
  1124. ensure
  1125. StateMachine::Machine.ignore_method_conflicts = false
  1126. $stderr = @original_stderr
  1127. end
  1128. def test_should_define_nonexistent_methods
  1129. @machine.define_helper(:class, :states) {[]}
  1130. assert_equal [], @klass.states
  1131. end
  1132. def test_should_warn_if_defined_multiple_times
  1133. require 'stringio'
  1134. @original_stderr, $stderr = $stderr, StringIO.new
  1135. @machine.define_helper(:class, :states) {}
  1136. @machine.define_helper(:class, :states) {}
  1137. assert_equal "Class method \"states\" is already defined in #{@klass} :state class helpers, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n", $stderr.string
  1138. ensure
  1139. $stderr = @original_stderr
  1140. end
  1141. def test_should_pass_context_as_arguments
  1142. helper_args = nil
  1143. @machine.define_helper(:class, :states) {|*args| helper_args = args}
  1144. @klass.states
  1145. assert_equal 2, helper_args.length
  1146. assert_equal [@machine, @klass], helper_args
  1147. end
  1148. def test_should_pass_method_arguments_through
  1149. helper_args = nil
  1150. @machine.define_helper(:class, :states) {|*args| helper_args = args}
  1151. @klass.states(1, 2, 3)
  1152. assert_equal 5, helper_args.length
  1153. assert_equal [@machine, @klass, 1, 2, 3], helper_args
  1154. end
  1155. def test_should_allow_string_evaluation
  1156. @machine.define_helper :class, <<-end_eval, __FILE__, __LINE__ + 1
  1157. def states
  1158. []
  1159. end
  1160. end_eval
  1161. assert_equal [], @klass.states
  1162. end
  1163. end
  1164. class MachineWithConflictingHelpersBeforeDefinitionTest < Test::Unit::TestCase
  1165. def setup
  1166. require 'stringio'
  1167. @original_stderr, $stderr = $stderr, StringIO.new
  1168. @superclass = Class.new do
  1169. def self.with_state
  1170. :with_state
  1171. end
  1172. def self.with_states
  1173. :with_states
  1174. end
  1175. def self.without_state
  1176. :without_state
  1177. end
  1178. def self.without_states
  1179. :without_states
  1180. end
  1181. def self.human_state_name
  1182. :human_state_name
  1183. end
  1184. def self.human_state_event_name
  1185. :human_state_event_name
  1186. end
  1187. attr_accessor :status
  1188. def state
  1189. 'parked'
  1190. end
  1191. def state=(value)
  1192. self.status = value
  1193. end
  1194. def state?
  1195. true
  1196. end
  1197. def state_name
  1198. :parked
  1199. end
  1200. def human_state_name
  1201. 'parked'
  1202. end
  1203. def state_events
  1204. [:ignite]
  1205. end
  1206. def state_transitions
  1207. [{:parked => :idling}]
  1208. end
  1209. def state_paths
  1210. [[{:parked => :idling}]]
  1211. end
  1212. def fire_state_event
  1213. true
  1214. end
  1215. end
  1216. @klass = Class.new(@superclass)
  1217. StateMachine::Integrations.const_set('Custom', Module.new do
  1218. include StateMachine::Integrations::Base
  1219. def create_with_scope(name)
  1220. lambda {|klass, values| []}
  1221. end
  1222. def create_without_scope(name)
  1223. lambda {|klass, values| []}
  1224. end
  1225. end)
  1226. @machine = StateMachine::Machine.new(@klass, :integration => :custom)
  1227. @machine.state :parked, :idling
  1228. @machine.event :ignite
  1229. @object = @klass.new
  1230. end
  1231. def test_should_not_redefine_singular_with_scope
  1232. assert_equal :with_state, @klass.with_state
  1233. end
  1234. def test_should_not_redefine_plural_with_scope
  1235. assert_equal :with_states, @klass.with_states
  1236. end
  1237. def test_should_not_redefine_singular_without_scope
  1238. assert_equal :without_state, @klass.without_state
  1239. end
  1240. def test_should_not_redefine_plural_without_scope
  1241. assert_equal :without_states, @klass.without_states
  1242. end
  1243. def test_should_not_redefine_human_attribute_name_reader
  1244. assert_equal :human_state_name, @klass.human_state_name
  1245. end
  1246. def test_should_not_redefine_human_event_name_reader
  1247. assert_equal :human_state_event_name, @klass.human_state_event_name
  1248. end
  1249. def test_should_not_redefine_attribute_reader
  1250. assert_equal 'parked', @object.state
  1251. end
  1252. def test_should_not_redefine_attribute_writer
  1253. @object.state = 'parked'
  1254. assert_equal 'parked', @object.status
  1255. end
  1256. def test_should_not_define_attribute_predicate
  1257. assert @object.state?
  1258. end
  1259. def test_should_not_redefine_attribute_name_reader
  1260. assert_equal :parked, @object.state_name
  1261. end
  1262. def test_should_not_redefine_attribute_human_name_reader
  1263. assert_equal 'parked', @object.human_state_name
  1264. end
  1265. def test_should_not_redefine_attribute_events_reader
  1266. assert_equal [:ignite], @object.state_events
  1267. end
  1268. def test_should_not_redefine_attribute_transitions_reader
  1269. assert_equal [{:parked => :idling}], @object.state_transitions
  1270. end
  1271. def test_should_not_redefine_attribute_paths_reader
  1272. assert_equal [[{:parked => :idling}]], @object.state_paths
  1273. end
  1274. def test_should_not_redefine_event_runner
  1275. assert_equal true, @object.fire_state_event
  1276. end
  1277. def test_should_output_warning
  1278. expected = [
  1279. 'Instance method "state_events"',
  1280. 'Instance method "state_transitions"',
  1281. 'Instance method "fire_state_event"',
  1282. 'Instance method "state_paths"',
  1283. 'Class method "human_state_name"',
  1284. 'Class method "human_state_event_name"',
  1285. 'Instance method "state_name"',
  1286. 'Instance method "human_state_name"',
  1287. 'Class method "with_state"',
  1288. 'Class method "with_states"',
  1289. 'Class method "without_state"',
  1290. 'Class method "without_states"'
  1291. ].map {|method| "#{method} is already defined in #{@superclass.to_s}, use generic helper instead or set StateMachine::Machine.ignore_method_conflicts = true.\n"}.join
  1292. assert_equal expected, $stderr.string
  1293. end
  1294. def teardown
  1295. $stderr = @original_stderr
  1296. StateMachine::Integrations.send(:remove_const, 'Custom')
  1297. end
  1298. end
  1299. class MachineWithConflictingHelpersAfterDefinitionTest < Test::Unit::TestCase
  1300. def setup
  1301. require 'stringio'
  1302. @original_stderr, $stderr = $stderr, StringIO.new
  1303. @klass = Class.new do
  1304. def self.with_state
  1305. :with_state
  1306. end
  1307. def self.with_states
  1308. :with_states
  1309. end
  1310. def self.without_state
  1311. :without_state
  1312. end
  1313. def self.without_states
  1314. :without_states
  1315. end
  1316. def self.human_state_name
  1317. :human_state_name
  1318. end
  1319. def self.human_state_event_name
  1320. :human_state_event_name
  1321. end
  1322. attr_accessor :status
  1323. def state
  1324. 'parked'
  1325. end
  1326. def state=(value)
  1327. self.status = value
  1328. end
  1329. def state?
  1330. true
  1331. end
  1332. def state_name
  1333. :parked
  1334. end
  1335. def human_state_name
  1336. 'parked'
  1337. end
  1338. def state_events
  1339. [:ignite]
  1340. end
  1341. def state_transitions
  1342. [{:parked => :idling}]
  1343. end
  1344. def state_paths
  1345. [[{:parked => :idling}]]
  1346. end
  1347. def fire_state_event
  1348. true
  1349. end
  1350. end
  1351. StateMachine::Integrations.const_set('Custom', Module.new do
  1352. include StateMachine::Integrations::Base
  1353. def create_with_scope(name)
  1354. lambda {|klass, values| []}
  1355. end
  1356. def create_without_scope(name)
  1357. lambda {|klass, values| []}
  1358. end
  1359. end)
  1360. @machine = StateMachine::Machine.new(@klass, :integration => :custom)
  1361. @machine.state :parked, :idling
  1362. @machine.event :ignite
  1363. @object = @klass.new
  1364. end
  1365. def test_should_not_redefine_singular_with_scope
  1366. assert_equal :with_state, @klass.with_state
  1367. end
  1368. def test_should_not_redefine_plural_with_scope
  1369. assert_equal :with_states, @klass.with_states
  1370. end
  1371. def test_should_not_redefine_singular_without_scope
  1372. assert_equal :without_state, @klass.without_state
  1373. end
  1374. def test_should_not_redefine_plural_without_scope
  1375. assert_equal :without_states, @klass.without_states
  1376. end
  1377. def test_should_not_redefine_human_attribute_name_reader
  1378. assert_equal :human_state_name, @klass.human_state_name
  1379. end
  1380. def test_should_not_redefine_human_event_name_reader
  1381. assert_equal :human_state_event_name, @klass.human_state_event_name
  1382. end
  1383. def test_should_not_redefine_attribute_reader
  1384. assert_equal 'parked', @object.state
  1385. end
  1386. def test_should_not_redefine_attribute_writer
  1387. @object.state = 'parked'
  1388. assert_equal 'parked', @object.status
  1389. end
  1390. def test_should_not_define_attribute_predicate
  1391. assert @object.state?
  1392. end
  1393. def test_should_not_redefine_attribute_name_reader
  1394. assert_equal :parked, @object.state_name
  1395. end
  1396. def test_should_not_redefine_attribute_human_name_reader
  1397. assert_equal 'parked', @object.human_state_name
  1398. end
  1399. def test_should_not_redefine_attribute_events_reader
  1400. assert_equal [:ignite], @object.state_events
  1401. end
  1402. def test_should_not_redefine_attribute_transitions_reader
  1403. assert_equal [{:parked => :idling}], @object.state_transitions
  1404. end
  1405. def test_should_not_redefine_attribute_paths_reader
  1406. assert_equal [[{:parked => :idling}]], @object.state_paths
  1407. end
  1408. def test_should_not_redefine_event_runner
  1409. assert_equal true, @object.fire_state_event
  1410. end
  1411. def test_should_allow_super_chaining
  1412. @klass.class_eval do
  1413. def self.with_state(*states)
  1414. super
  1415. end
  1416. def self.with_states(*states)
  1417. super
  1418. end
  1419. def self.without_state(*states)
  1420. super
  1421. end
  1422. def self.without_states(*states)
  1423. super
  1424. end
  1425. def self.human_state_name(state)
  1426. super
  1427. end
  1428. def self.human_state_event_name(event)
  1429. super
  1430. end
  1431. attr_accessor :status
  1432. def

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