PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/Calamitous/spree
Ruby | 306 lines | 240 code | 66 blank | 0 comment | 0 complexity | 6d3a91a7064992c7ee1cdbb1c3db1805 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class MachineByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @machine = PluginAWeek::StateMachine::Machine.new(Switch)
  5. end
  6. def test_should_have_an_attribute
  7. assert_equal 'state', @machine.attribute
  8. end
  9. def test_should_not_have_an_initial_state
  10. assert_nil @machine.initial_state(new_switch)
  11. end
  12. def test_should_have_an_owner_class
  13. assert_equal Switch, @machine.owner_class
  14. end
  15. def test_should_not_have_any_events
  16. assert @machine.events.empty?
  17. end
  18. def test_should_not_have_any_states
  19. assert @machine.states.empty?
  20. end
  21. end
  22. class MachineWithInitialStateTest < Test::Unit::TestCase
  23. def setup
  24. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state', :initial => 'off')
  25. end
  26. def test_should_have_an_initial_state
  27. assert_equal 'off', @machine.initial_state(new_switch)
  28. end
  29. end
  30. class MachineWithDynamicInitialStateTest < Test::Unit::TestCase
  31. def setup
  32. @initial_state = lambda {|switch| switch.initial_state}
  33. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state', :initial => @initial_state)
  34. end
  35. def test_should_use_the_record_for_determining_the_initial_state
  36. assert_equal 'off', @machine.initial_state(new_switch(:initial_state => 'off'))
  37. assert_equal 'on', @machine.initial_state(new_switch(:initial_state => 'on'))
  38. end
  39. end
  40. class MachineTest < Test::Unit::TestCase
  41. def setup
  42. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  43. end
  44. def test_should_define_a_named_scope_for_the_attribute
  45. on = create_switch(:state => 'on')
  46. off = create_switch(:state => 'off')
  47. assert_equal [on], Switch.with_state('on')
  48. end
  49. def test_should_define_a_pluralized_named_scope_for_the_attribute
  50. on = create_switch(:state => 'on')
  51. off = create_switch(:state => 'off')
  52. assert_equal [on, off], Switch.with_states('on', 'off')
  53. end
  54. def test_should_raise_exception_if_invalid_option_specified
  55. assert_raise(ArgumentError) {PluginAWeek::StateMachine::Machine.new(Switch, 'state', :invalid => true)}
  56. end
  57. def test_should_symbolize_attribute
  58. machine = PluginAWeek::StateMachine::Machine.new(Switch, :state)
  59. assert_equal 'state', machine.attribute
  60. end
  61. end
  62. class MachineAfterBeingCopiedTest < Test::Unit::TestCase
  63. def setup
  64. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  65. @machine.event(:turn_on) {}
  66. @copied_machine = @machine.dup
  67. end
  68. def test_should_not_have_the_same_collection_of_states
  69. assert_not_same @copied_machine.states, @machine.states
  70. end
  71. def test_should_not_have_the_same_collection_of_events
  72. assert_not_same @copied_machine.events, @machine.events
  73. end
  74. def test_should_copy_each_event
  75. assert_not_same @copied_machine.events['turn_on'], @machine.events['turn_on']
  76. end
  77. def test_should_update_machine_for_each_event
  78. assert_equal @copied_machine, @copied_machine.events['turn_on'].machine
  79. end
  80. def test_should_not_update_machine_for_original_event
  81. assert_equal @machine, @machine.events['turn_on'].machine
  82. end
  83. end
  84. class MachineAfterChangingContextTest < Test::Unit::TestCase
  85. def setup
  86. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  87. end
  88. def test_should_create_copy_of_machine
  89. new_machine = @machine.within_context(ToggleSwitch)
  90. assert_not_same @machine, new_machine
  91. end
  92. def test_should_update_owner_clas
  93. new_machine = @machine.within_context(ToggleSwitch)
  94. assert_equal ToggleSwitch, new_machine.owner_class
  95. end
  96. def test_should_update_initial_state
  97. new_machine = @machine.within_context(ToggleSwitch, :initial => 'off')
  98. assert_equal 'off', new_machine.initial_state(new_switch)
  99. end
  100. def test_should_not_update_initial_state_if_not_provided
  101. new_machine = @machine.within_context(ToggleSwitch)
  102. assert_nil new_machine.initial_state(new_switch)
  103. end
  104. def test_raise_exception_if_invalid_option_specified
  105. assert_raise(ArgumentError) {@machine.within_context(ToggleSwitch, :invalid => true)}
  106. end
  107. end
  108. class MachineWithConflictingNamedScopesTest < Test::Unit::TestCase
  109. class Switch < ActiveRecord::Base
  110. def self.with_state
  111. :custom
  112. end
  113. def self.with_states
  114. :custom
  115. end
  116. end
  117. def setup
  118. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  119. end
  120. def test_should_not_define_a_named_scope_for_the_attribute
  121. assert_equal :custom, Switch.with_state
  122. end
  123. def test_should_not_define_a_pluralized_named_scope_for_the_attribute
  124. assert_equal :custom, Switch.with_states
  125. end
  126. end
  127. class MachineWithEventsTest < Test::Unit::TestCase
  128. def setup
  129. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  130. end
  131. def test_should_create_event_with_given_name
  132. event = @machine.event(:turn_on) {}
  133. assert_equal 'turn_on', event.name
  134. end
  135. def test_should_evaluate_block_within_event_context
  136. responded = false
  137. @machine.event :turn_on do
  138. responded = respond_to?(:transition)
  139. end
  140. assert responded
  141. end
  142. def test_should_have_events
  143. @machine.event(:turn_on) {}
  144. assert_equal %w(turn_on), @machine.events.keys
  145. end
  146. end
  147. class MachineWithExistingEventTest < Test::Unit::TestCase
  148. def setup
  149. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  150. @event = @machine.event(:turn_on) {}
  151. @same_event = @machine.event(:turn_on) {}
  152. end
  153. def test_should_not_create_new_event
  154. assert_same @event, @same_event
  155. end
  156. end
  157. class MachineWithEventsAndTransitionsTest < Test::Unit::TestCase
  158. def setup
  159. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  160. @machine.event(:turn_on) do
  161. transition :to => 'on', :from => 'off'
  162. transition :to => 'error', :from => 'unknown'
  163. end
  164. end
  165. def test_should_have_events
  166. assert_equal %w(turn_on), @machine.events.keys
  167. end
  168. def test_should_track_states_defined_in_event_transitions
  169. assert_equal %w(error off on unknown), @machine.states
  170. end
  171. def test_should_not_duplicate_states_defined_in_multiple_event_transitions
  172. @machine.event :turn_off do
  173. transition :to => 'off', :from => 'on'
  174. end
  175. assert_equal %w(error off on unknown), @machine.states
  176. end
  177. end
  178. class MachineWithTransitionCallbacksTest < Test::Unit::TestCase
  179. def setup
  180. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  181. @event = @machine.event :turn_on do
  182. transition :to => 'on', :from => 'off'
  183. end
  184. @switch = create_switch(:state => 'off')
  185. end
  186. def test_should_raise_exception_if_invalid_option_specified
  187. assert_raise(ArgumentError) {@machine.before_transition :invalid => true}
  188. end
  189. def test_should_raise_exception_if_do_option_not_specified
  190. assert_raise(ArgumentError) {@machine.before_transition :to => 'on'}
  191. end
  192. def test_should_invoke_callbacks_during_transition
  193. @machine.before_transition lambda {|switch| switch.callbacks << 'before'}
  194. @machine.after_transition lambda {|switch| switch.callbacks << 'after'}
  195. @event.fire(@switch)
  196. assert_equal %w(before after), @switch.callbacks
  197. end
  198. def test_should_support_from_query
  199. @machine.before_transition :from => 'off', :do => lambda {|switch| switch.callbacks << 'off'}
  200. @machine.before_transition :from => 'on', :do => lambda {|switch| switch.callbacks << 'on'}
  201. @event.fire(@switch)
  202. assert_equal %w(off), @switch.callbacks
  203. end
  204. def test_should_support_except_from_query
  205. @machine.before_transition :except_from => 'off', :do => lambda {|switch| switch.callbacks << 'off'}
  206. @machine.before_transition :except_from => 'on', :do => lambda {|switch| switch.callbacks << 'on'}
  207. @event.fire(@switch)
  208. assert_equal %w(on), @switch.callbacks
  209. end
  210. def test_should_support_to_query
  211. @machine.before_transition :to => 'off', :do => lambda {|switch| switch.callbacks << 'off'}
  212. @machine.before_transition :to => 'on', :do => lambda {|switch| switch.callbacks << 'on'}
  213. @event.fire(@switch)
  214. assert_equal %w(on), @switch.callbacks
  215. end
  216. def test_should_support_except_to_query
  217. @machine.before_transition :except_to => 'off', :do => lambda {|switch| switch.callbacks << 'off'}
  218. @machine.before_transition :except_to => 'on', :do => lambda {|switch| switch.callbacks << 'on'}
  219. @event.fire(@switch)
  220. assert_equal %w(off), @switch.callbacks
  221. end
  222. def test_should_support_on_query
  223. @machine.before_transition :on => 'turn_off', :do => lambda {|switch| switch.callbacks << 'turn_off'}
  224. @machine.before_transition :on => 'turn_on', :do => lambda {|switch| switch.callbacks << 'turn_on'}
  225. @event.fire(@switch)
  226. assert_equal %w(turn_on), @switch.callbacks
  227. end
  228. def test_should_support_except_on_query
  229. @machine.before_transition :except_on => 'turn_off', :do => lambda {|switch| switch.callbacks << 'turn_off'}
  230. @machine.before_transition :except_on => 'turn_on', :do => lambda {|switch| switch.callbacks << 'turn_on'}
  231. @event.fire(@switch)
  232. assert_equal %w(turn_off), @switch.callbacks
  233. end
  234. def teardown
  235. Switch.class_eval do
  236. @before_transition_state_callbacks = nil
  237. @after_transition_state_callbacks = nil
  238. end
  239. end
  240. end