state_machines 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (465) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +10 -4
  3. data/Changelog.md +0 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +1 -1
  6. data/README.md +6 -8
  7. data/Rakefile +9 -6
  8. data/Testing.md +0 -0
  9. data/lib/state_machines/assertions.rb +5 -5
  10. data/lib/state_machines/error.rb +2 -1
  11. data/lib/state_machines/integrations.rb +18 -11
  12. data/lib/state_machines/machine.rb +8 -12
  13. data/lib/state_machines/version.rb +1 -1
  14. data/state_machines.gemspec +6 -8
  15. data/test/files/integrations/event_on_failure_integration.rb +10 -0
  16. data/test/files/integrations/vehicle.rb +7 -0
  17. data/{spec/internal/app → test/files}/models/auto_shop.rb +0 -0
  18. data/{spec/internal/app → test/files}/models/car.rb +2 -0
  19. data/{spec/internal/app → test/files}/models/model_base.rb +0 -0
  20. data/{spec/internal/app → test/files}/models/motorcycle.rb +2 -0
  21. data/{spec/internal/app → test/files}/models/traffic_light.rb +2 -2
  22. data/{spec/internal/app → test/files}/models/vehicle.rb +9 -5
  23. data/test/files/node.rb +5 -0
  24. data/test/files/switch.rb +15 -0
  25. data/test/functional/auto_shop_available_test.rb +20 -0
  26. data/test/functional/auto_shop_busy_test.rb +25 -0
  27. data/test/functional/car_backing_up_test.rb +45 -0
  28. data/test/functional/car_test.rb +49 -0
  29. data/test/functional/motorcycle_test.rb +46 -0
  30. data/test/functional/traffic_light_caution_test.rb +17 -0
  31. data/test/functional/traffic_light_proceed_test.rb +17 -0
  32. data/test/functional/traffic_light_stop_test.rb +26 -0
  33. data/test/functional/vehicle_first_gear_test.rb +42 -0
  34. data/test/functional/vehicle_idling_test.rb +59 -0
  35. data/test/functional/vehicle_locked_test.rb +29 -0
  36. data/test/functional/vehicle_parked_test.rb +53 -0
  37. data/test/functional/vehicle_repaired_test.rb +20 -0
  38. data/test/functional/vehicle_second_gear_test.rb +42 -0
  39. data/test/functional/vehicle_stalled_test.rb +65 -0
  40. data/test/functional/vehicle_test.rb +20 -0
  41. data/test/functional/vehicle_third_gear_test.rb +42 -0
  42. data/test/functional/vehicle_unsaved_test.rb +181 -0
  43. data/test/functional/vehicle_with_event_attributes_test.rb +30 -0
  44. data/test/functional/vehicle_with_parallel_events_test.rb +36 -0
  45. data/test/test_helper.rb +15 -0
  46. data/test/unit/assertions/assert_exclusive_keys_test.rb +22 -0
  47. data/test/unit/assertions/assert_valid_key_test.rb +12 -0
  48. data/test/unit/branch/branch_test.rb +28 -0
  49. data/test/unit/branch/branch_with_conflicting_conditionals_test.rb +27 -0
  50. data/test/unit/branch/branch_with_conflicting_from_requirements_test.rb +8 -0
  51. data/test/unit/branch/branch_with_conflicting_on_requirements_test.rb +8 -0
  52. data/test/unit/branch/branch_with_conflicting_to_requirements_test.rb +8 -0
  53. data/test/unit/branch/branch_with_different_requirements_test.rb +41 -0
  54. data/test/unit/branch/branch_with_except_from_matcher_requirement_test.rb +8 -0
  55. data/test/unit/branch/branch_with_except_from_requirement_test.rb +36 -0
  56. data/test/unit/branch/branch_with_except_on_matcher_requirement_test.rb +8 -0
  57. data/test/unit/branch/branch_with_except_on_requirement_test.rb +36 -0
  58. data/test/unit/branch/branch_with_except_to_matcher_requirement_test.rb +8 -0
  59. data/test/unit/branch/branch_with_except_to_requirement_test.rb +36 -0
  60. data/test/unit/branch/branch_with_from_matcher_requirement_test.rb +20 -0
  61. data/test/unit/branch/branch_with_from_requirement_test.rb +45 -0
  62. data/test/unit/branch/branch_with_if_conditional_test.rb +27 -0
  63. data/test/unit/branch/branch_with_implicit_and_explicit_requirements_test.rb +23 -0
  64. data/test/unit/branch/branch_with_implicit_from_requirement_matcher_test.rb +20 -0
  65. data/test/unit/branch/branch_with_implicit_requirement_test.rb +20 -0
  66. data/test/unit/branch/branch_with_implicit_to_requirement_matcher_test.rb +16 -0
  67. data/test/unit/branch/branch_with_multiple_except_from_requirements_test.rb +20 -0
  68. data/test/unit/branch/branch_with_multiple_except_on_requirements_test.rb +16 -0
  69. data/test/unit/branch/branch_with_multiple_except_to_requirements_test.rb +20 -0
  70. data/test/unit/branch/branch_with_multiple_from_requirements_test.rb +16 -0
  71. data/test/unit/branch/branch_with_multiple_if_conditionals_test.rb +20 -0
  72. data/test/unit/branch/branch_with_multiple_implicit_requirements_test.rb +53 -0
  73. data/test/unit/branch/branch_with_multiple_on_requirements_test.rb +20 -0
  74. data/test/unit/branch/branch_with_multiple_to_requirements_test.rb +20 -0
  75. data/test/unit/branch/branch_with_multiple_unless_conditionals_test.rb +20 -0
  76. data/test/unit/branch/branch_with_nil_requirements_test.rb +28 -0
  77. data/test/unit/branch/branch_with_no_requirements_test.rb +36 -0
  78. data/test/unit/branch/branch_with_on_matcher_requirement_test.rb +16 -0
  79. data/test/unit/branch/branch_with_on_requirement_test.rb +45 -0
  80. data/test/unit/branch/branch_with_to_matcher_requirement_test.rb +20 -0
  81. data/test/unit/branch/branch_with_to_requirement_test.rb +45 -0
  82. data/test/unit/branch/branch_with_unless_conditional_test.rb +27 -0
  83. data/test/unit/branch/branch_without_guards_test.rb +27 -0
  84. data/test/unit/callback/callback_by_default_test.rb +25 -0
  85. data/test/unit/callback/callback_test.rb +53 -0
  86. data/test/unit/callback/callback_with_application_bound_object_test.rb +23 -0
  87. data/test/unit/callback/callback_with_application_terminator_test.rb +24 -0
  88. data/test/unit/callback/callback_with_arguments_test.rb +14 -0
  89. data/test/unit/callback/callback_with_around_type_and_arguments_test.rb +25 -0
  90. data/test/unit/callback/callback_with_around_type_and_block_test.rb +44 -0
  91. data/test/unit/callback/callback_with_around_type_and_bound_method_test.rb +23 -0
  92. data/test/unit/callback/callback_with_around_type_and_multiple_methods_test.rb +93 -0
  93. data/test/unit/callback/callback_with_around_type_and_terminator_test.rb +17 -0
  94. data/test/unit/callback/callback_with_block_test.rb +20 -0
  95. data/test/unit/callback/callback_with_bound_method_and_arguments_test.rb +28 -0
  96. data/test/unit/callback/callback_with_bound_method_test.rb +35 -0
  97. data/test/unit/callback/callback_with_do_method_test.rb +18 -0
  98. data/test/unit/callback/callback_with_explicit_requirements_test.rb +32 -0
  99. data/test/unit/callback/callback_with_if_condition_test.rb +17 -0
  100. data/test/unit/callback/callback_with_implicit_requirements_test.rb +32 -0
  101. data/test/unit/callback/callback_with_method_argument_test.rb +18 -0
  102. data/test/unit/callback/callback_with_mixed_methods_test.rb +31 -0
  103. data/test/unit/callback/callback_with_multiple_bound_methods_test.rb +21 -0
  104. data/test/unit/callback/callback_with_multiple_do_methods_test.rb +29 -0
  105. data/test/unit/callback/callback_with_multiple_method_arguments_test.rb +29 -0
  106. data/test/unit/callback/callback_with_terminator_test.rb +22 -0
  107. data/test/unit/callback/callback_with_unbound_method_test.rb +14 -0
  108. data/test/unit/callback/callback_with_unless_condition_test.rb +17 -0
  109. data/test/unit/callback/callback_without_arguments_test.rb +14 -0
  110. data/test/unit/callback/callback_without_terminator_test.rb +12 -0
  111. data/test/unit/error/error_by_default_test.rb +21 -0
  112. data/test/unit/error/error_with_message_test.rb +23 -0
  113. data/test/unit/eval_helper/eval_helpers_base_test.rb +8 -0
  114. data/test/unit/eval_helper/eval_helpers_proc_block_and_explicit_arguments_test.rb +14 -0
  115. data/test/unit/eval_helper/eval_helpers_proc_block_and_implicit_arguments_test.rb +14 -0
  116. data/test/unit/eval_helper/eval_helpers_proc_test.rb +13 -0
  117. data/test/unit/eval_helper/eval_helpers_proc_with_arguments_test.rb +13 -0
  118. data/test/unit/eval_helper/eval_helpers_proc_with_block_test.rb +13 -0
  119. data/test/unit/eval_helper/eval_helpers_proc_with_block_without_arguments_test.rb +18 -0
  120. data/test/unit/eval_helper/eval_helpers_proc_with_block_without_object_test.rb +14 -0
  121. data/test/unit/eval_helper/eval_helpers_proc_without_arguments_test.rb +19 -0
  122. data/test/unit/eval_helper/eval_helpers_string_test.rb +25 -0
  123. data/test/unit/eval_helper/eval_helpers_string_with_block_test.rb +12 -0
  124. data/test/unit/eval_helper/eval_helpers_symbol_method_missing_test.rb +20 -0
  125. data/test/unit/eval_helper/eval_helpers_symbol_private_test.rb +17 -0
  126. data/test/unit/eval_helper/eval_helpers_symbol_protected_test.rb +17 -0
  127. data/test/unit/eval_helper/eval_helpers_symbol_tainted_method_test.rb +18 -0
  128. data/test/unit/eval_helper/eval_helpers_symbol_test.rb +16 -0
  129. data/test/unit/eval_helper/eval_helpers_symbol_with_arguments_and_block_test.rb +16 -0
  130. data/test/unit/eval_helper/eval_helpers_symbol_with_arguments_test.rb +16 -0
  131. data/test/unit/eval_helper/eval_helpers_symbol_with_block_test.rb +16 -0
  132. data/test/unit/eval_helper/eval_helpers_test.rb +13 -0
  133. data/test/unit/event/event_after_being_copied_test.rb +17 -0
  134. data/test/unit/event/event_by_default_test.rb +60 -0
  135. data/test/unit/event/event_context_test.rb +16 -0
  136. data/test/unit/event/event_on_failure_test.rb +44 -0
  137. data/test/unit/event/event_test.rb +34 -0
  138. data/test/unit/event/event_transitions_test.rb +62 -0
  139. data/test/unit/event/event_with_conflicting_helpers_after_definition_test.rb +79 -0
  140. data/test/unit/event/event_with_conflicting_helpers_before_definition_test.rb +58 -0
  141. data/test/unit/event/event_with_conflicting_machine_test.rb +48 -0
  142. data/test/unit/event/event_with_dynamic_human_name_test.rb +26 -0
  143. data/test/unit/event/event_with_human_name_test.rb +13 -0
  144. data/test/unit/event/event_with_invalid_current_state_test.rb +30 -0
  145. data/test/unit/event/event_with_machine_action_test.rb +33 -0
  146. data/test/unit/event/event_with_marshalling_test.rb +47 -0
  147. data/test/unit/event/event_with_matching_disabled_transitions_test.rb +115 -0
  148. data/test/unit/event/event_with_matching_enabled_transitions_test.rb +75 -0
  149. data/test/unit/event/event_with_multiple_transitions_test.rb +61 -0
  150. data/test/unit/event/event_with_namespace_test.rb +34 -0
  151. data/test/unit/event/event_with_transition_with_blacklisted_to_state_test.rb +60 -0
  152. data/test/unit/event/event_with_transition_with_loopback_state_test.rb +36 -0
  153. data/test/unit/event/event_with_transition_with_nil_to_state_test.rb +36 -0
  154. data/test/unit/event/event_with_transition_with_whitelisted_to_state_test.rb +51 -0
  155. data/test/unit/event/event_with_transition_without_to_state_test.rb +36 -0
  156. data/test/unit/event/event_with_transitions_test.rb +32 -0
  157. data/test/unit/event/event_without_matching_transitions_test.rb +41 -0
  158. data/test/unit/event/event_without_transitions_test.rb +28 -0
  159. data/{spec/state_machines/invalid_event_spec.rb → test/unit/event/invalid_event_test.rb} +8 -7
  160. data/test/unit/event_collection/event_collection_attribute_with_machine_action_test.rb +62 -0
  161. data/test/unit/event_collection/event_collection_attribute_with_namespaced_machine_test.rb +36 -0
  162. data/test/unit/event_collection/event_collection_by_default_test.rb +26 -0
  163. data/test/unit/event_collection/event_collection_test.rb +39 -0
  164. data/test/unit/event_collection/event_collection_with_custom_machine_attribute_test.rb +31 -0
  165. data/test/unit/event_collection/event_collection_with_events_with_transitions_test.rb +76 -0
  166. data/test/unit/event_collection/event_collection_with_multiple_events_test.rb +27 -0
  167. data/test/unit/event_collection/event_collection_with_validations_test.rb +75 -0
  168. data/test/unit/event_collection/event_collection_without_machine_action_test.rb +18 -0
  169. data/test/unit/event_collection/event_string_collection_test.rb +31 -0
  170. data/test/unit/helper_module_test.rb +17 -0
  171. data/test/unit/integrations/integration_finder_test.rb +16 -0
  172. data/test/unit/integrations/integration_matcher_test.rb +27 -0
  173. data/test/unit/invalid_transition/invalid_parallel_transition_test.rb +18 -0
  174. data/test/unit/invalid_transition/invalid_transition_test.rb +47 -0
  175. data/test/unit/invalid_transition/invalid_transition_with_integration_test.rb +47 -0
  176. data/test/unit/invalid_transition/invalid_transition_with_namespace_test.rb +32 -0
  177. data/test/unit/machine/machine_after_being_copied_test.rb +62 -0
  178. data/test/unit/machine/machine_after_changing_initial_state.rb +28 -0
  179. data/test/unit/machine/machine_after_changing_owner_class_test.rb +31 -0
  180. data/test/unit/machine/machine_by_default_test.rb +160 -0
  181. data/test/unit/machine/machine_finder_custom_options_test.rb +17 -0
  182. data/test/unit/machine/machine_finder_with_existing_machine_on_superclass_test.rb +86 -0
  183. data/test/unit/machine/machine_finder_with_existing_on_same_class_test.rb +23 -0
  184. data/test/unit/machine/machine_finder_without_existing_machine_test.rb +25 -0
  185. data/test/unit/machine/machine_persistence_test.rb +52 -0
  186. data/test/unit/machine/machine_state_initialization_test.rb +56 -0
  187. data/test/unit/machine/machine_test.rb +30 -0
  188. data/test/unit/machine/machine_with_action_already_overridden_test.rb +23 -0
  189. data/test/unit/machine/machine_with_action_defined_in_class_test.rb +37 -0
  190. data/test/unit/machine/machine_with_action_defined_in_included_module_test.rb +46 -0
  191. data/test/unit/machine/machine_with_action_defined_in_superclass_test.rb +43 -0
  192. data/test/unit/machine/machine_with_action_undefined_test.rb +33 -0
  193. data/test/unit/machine/machine_with_cached_state_test.rb +20 -0
  194. data/test/unit/machine/machine_with_class_helpers_test.rb +179 -0
  195. data/test/unit/machine/machine_with_conflicting_helpers_after_definition_test.rb +243 -0
  196. data/test/unit/machine/machine_with_conflicting_helpers_before_definition_test.rb +175 -0
  197. data/test/unit/machine/machine_with_custom_action_test.rb +11 -0
  198. data/test/unit/machine/machine_with_custom_attribute_test.rb +103 -0
  199. data/test/unit/machine/machine_with_custom_initialize_test.rb +24 -0
  200. data/test/unit/machine/machine_with_custom_integration_test.rb +69 -0
  201. data/test/unit/machine/machine_with_custom_invalidation_test.rb +40 -0
  202. data/test/unit/machine/machine_with_custom_name_test.rb +57 -0
  203. data/test/unit/machine/machine_with_custom_plural_test.rb +53 -0
  204. data/test/unit/machine/machine_with_dynamic_initial_state_test.rb +65 -0
  205. data/test/unit/machine/machine_with_event_matchers_test.rb +41 -0
  206. data/test/unit/machine/machine_with_events_test.rb +52 -0
  207. data/test/unit/machine/machine_with_events_with_custom_human_names_test.rb +18 -0
  208. data/test/unit/machine/machine_with_events_with_transitions_test.rb +37 -0
  209. data/test/unit/machine/machine_with_existing_event_test.rb +17 -0
  210. data/test/unit/machine/machine_with_existing_machines_on_owner_class_test.rb +20 -0
  211. data/test/unit/machine/machine_with_existing_machines_with_same_attributes_on_owner_class_test.rb +71 -0
  212. data/test/unit/machine/machine_with_existing_machines_with_same_attributes_on_owner_subclass_test.rb +31 -0
  213. data/test/unit/machine/machine_with_existing_state_test.rb +27 -0
  214. data/test/unit/machine/machine_with_failure_callbacks_test.rb +48 -0
  215. data/test/unit/machine/machine_with_helpers_test.rb +14 -0
  216. data/test/unit/machine/machine_with_initial_state_with_value_and_owner_default.rb +25 -0
  217. data/test/unit/machine/machine_with_initialize_and_super_test.rb +17 -0
  218. data/test/unit/machine/machine_with_initialize_arguments_and_block_test.rb +31 -0
  219. data/test/unit/machine/machine_with_initialize_without_super_test.rb +17 -0
  220. data/test/unit/machine/machine_with_instance_helpers_test.rb +179 -0
  221. data/test/unit/machine/machine_with_integration_test.rb +71 -0
  222. data/test/unit/machine/machine_with_multiple_events_test.rb +32 -0
  223. data/test/unit/machine/machine_with_namespace_test.rb +48 -0
  224. data/test/unit/machine/machine_with_nil_action_test.rb +24 -0
  225. data/test/unit/machine/machine_with_other_states.rb +22 -0
  226. data/test/unit/machine/machine_with_owner_subclass_test.rb +18 -0
  227. data/test/unit/machine/machine_with_paths_test.rb +25 -0
  228. data/test/unit/machine/machine_with_private_action_test.rb +43 -0
  229. data/test/unit/machine/machine_with_state_matchers_test.rb +41 -0
  230. data/test/unit/machine/machine_with_state_with_matchers_test.rb +19 -0
  231. data/test/unit/machine/machine_with_states_test.rb +55 -0
  232. data/test/unit/machine/machine_with_states_with_behaviors_test.rb +23 -0
  233. data/test/unit/machine/machine_with_states_with_custom_human_names_test.rb +18 -0
  234. data/test/unit/machine/machine_with_states_with_custom_values_test.rb +21 -0
  235. data/test/unit/machine/machine_with_states_with_runtime_dependencies_test.rb +19 -0
  236. data/test/unit/machine/machine_with_static_initial_state_test.rb +60 -0
  237. data/test/unit/machine/machine_with_superclass_conflicting_helpers_after_definition_test.rb +36 -0
  238. data/test/unit/machine/machine_with_transition_callbacks_test.rb +144 -0
  239. data/test/unit/machine/machine_with_transitions_test.rb +87 -0
  240. data/test/unit/machine/machine_without_initialization_test.rb +31 -0
  241. data/test/unit/machine/machine_without_initialize_test.rb +14 -0
  242. data/test/unit/machine/machine_without_integration_test.rb +31 -0
  243. data/test/unit/machine_collection/machine_collection_by_default_test.rb +11 -0
  244. data/test/unit/machine_collection/machine_collection_fire_attributes_with_validations_test.rb +72 -0
  245. data/test/unit/machine_collection/machine_collection_fire_test.rb +80 -0
  246. data/test/unit/machine_collection/machine_collection_fire_with_transactions_test.rb +54 -0
  247. data/test/unit/machine_collection/machine_collection_fire_with_validations_test.rb +76 -0
  248. data/test/unit/machine_collection/machine_collection_state_initialization_test.rb +101 -0
  249. data/test/unit/machine_collection/machine_collection_transitions_with_blank_events_test.rb +25 -0
  250. data/test/unit/machine_collection/machine_collection_transitions_with_custom_options_test.rb +20 -0
  251. data/test/unit/machine_collection/machine_collection_transitions_with_different_actions_test.rb +26 -0
  252. data/test/unit/machine_collection/machine_collection_transitions_with_exisiting_transitions_test.rb +25 -0
  253. data/test/unit/machine_collection/machine_collection_transitions_with_invalid_events_test.rb +25 -0
  254. data/test/unit/machine_collection/machine_collection_transitions_with_same_actions_test.rb +31 -0
  255. data/test/unit/machine_collection/machine_collection_transitions_with_transition_test.rb +26 -0
  256. data/test/unit/machine_collection/machine_collection_transitions_without_events_test.rb +25 -0
  257. data/test/unit/machine_collection/machine_collection_transitions_without_transition_test.rb +27 -0
  258. data/test/unit/matcher/all_matcher_test.rb +29 -0
  259. data/test/unit/matcher/blacklist_matcher_test.rb +30 -0
  260. data/test/unit/matcher/loopback_matcher_test.rb +27 -0
  261. data/test/unit/matcher/matcher_by_default_test.rb +15 -0
  262. data/test/unit/matcher/matcher_with_multiple_values_test.rb +15 -0
  263. data/test/unit/matcher/matcher_with_value_test.rb +15 -0
  264. data/test/unit/matcher/whitelist_matcher_test.rb +30 -0
  265. data/test/unit/matcher_helpers/matcher_helpers_all_test.rb +14 -0
  266. data/test/unit/matcher_helpers/matcher_helpers_any_test.rb +14 -0
  267. data/test/unit/matcher_helpers/matcher_helpers_same_test.rb +13 -0
  268. data/test/unit/node_collection/node_collection_after_being_copied_test/n.rb +46 -0
  269. data/test/unit/node_collection/node_collection_after_update_test/n.rb +36 -0
  270. data/test/unit/node_collection/node_collection_by_default_test.rb +22 -0
  271. data/test/unit/node_collection/node_collection_test.rb +23 -0
  272. data/test/unit/node_collection/node_collection_with_indices_test/n.rb +42 -0
  273. data/test/unit/node_collection/node_collection_with_matcher_contexts_test.rb +25 -0
  274. data/test/unit/node_collection/node_collection_with_nodes_test/n.rb +46 -0
  275. data/test/unit/node_collection/node_collection_with_numeric_index_test/n.rb +24 -0
  276. data/test/unit/node_collection/node_collection_with_postdefined_contexts_test/n.rb +22 -0
  277. data/test/unit/node_collection/node_collection_with_predefined_contexts_test/n.rb +23 -0
  278. data/test/unit/node_collection/node_collection_with_string_index_test/n.rb +20 -0
  279. data/test/unit/node_collection/node_collection_with_symbol_index_test/n.rb +20 -0
  280. data/test/unit/node_collection/node_collection_without_indices_test/n.rb +30 -0
  281. data/test/unit/path/path_by_default_test.rb +54 -0
  282. data/test/unit/path/path_test.rb +14 -0
  283. data/test/unit/path/path_with_available_transitions_after_reaching_target_test.rb +40 -0
  284. data/test/unit/path/path_with_available_transitions_test.rb +54 -0
  285. data/test/unit/path/path_with_deep_target_reached_test.rb +50 -0
  286. data/test/unit/path/path_with_deep_target_test.rb +40 -0
  287. data/test/unit/path/path_with_duplicates_test.rb +32 -0
  288. data/test/unit/path/path_with_encountered_transitions_test.rb +34 -0
  289. data/test/unit/path/path_with_guarded_transitions_test.rb +42 -0
  290. data/test/unit/path/path_with_reached_target_test.rb +35 -0
  291. data/test/unit/path/path_with_transitions_test.rb +54 -0
  292. data/test/unit/path/path_with_unreached_target_test.rb +31 -0
  293. data/test/unit/path/path_without_transitions_test.rb +24 -0
  294. data/test/unit/path_collection/path_collection_by_default_test.rb +46 -0
  295. data/test/unit/path_collection/path_collection_test.rb +24 -0
  296. data/test/unit/path_collection/path_collection_with_deep_paths_test.rb +43 -0
  297. data/test/unit/path_collection/path_collection_with_duplicate_nodes_test.rb +31 -0
  298. data/test/unit/path_collection/path_collection_with_from_state_test.rb +27 -0
  299. data/test/unit/path_collection/path_collection_with_paths_test.rb +47 -0
  300. data/test/unit/path_collection/path_collection_with_to_state_test.rb +29 -0
  301. data/test/unit/path_collection/path_with_guarded_paths_test.rb +25 -0
  302. data/test/unit/state/state_after_being_copied_test.rb +19 -0
  303. data/test/unit/state/state_by_default_test.rb +41 -0
  304. data/test/unit/state/state_final_test.rb +28 -0
  305. data/test/unit/state/state_initial_test.rb +13 -0
  306. data/test/unit/state/state_not_final_test.rb +32 -0
  307. data/test/unit/state/state_not_initial_test.rb +13 -0
  308. data/test/unit/state/state_test.rb +44 -0
  309. data/test/unit/state/state_with_cached_lambda_value_test.rb +29 -0
  310. data/test/unit/state/state_with_conflicting_helpers_after_definition_test.rb +38 -0
  311. data/test/unit/state/state_with_conflicting_helpers_before_definition_test.rb +29 -0
  312. data/test/unit/state/state_with_conflicting_machine_name_test.rb +20 -0
  313. data/test/unit/state/state_with_conflicting_machine_test.rb +37 -0
  314. data/test/unit/state/state_with_context_test.rb +60 -0
  315. data/test/unit/state/state_with_dynamic_human_name_test.rb +25 -0
  316. data/test/unit/state/state_with_existing_context_method_test.rb +24 -0
  317. data/test/unit/state/state_with_human_name_test.rb +13 -0
  318. data/test/unit/state/state_with_integer_value_test.rb +32 -0
  319. data/test/unit/state/state_with_invalid_method_call_test.rb +21 -0
  320. data/test/unit/state/state_with_lambda_value_test.rb +37 -0
  321. data/test/unit/state/state_with_matcher_test.rb +18 -0
  322. data/test/unit/state/state_with_multiple_contexts_test.rb +57 -0
  323. data/test/unit/state/state_with_name_test.rb +43 -0
  324. data/test/unit/state/state_with_namespace_test.rb +22 -0
  325. data/test/unit/state/state_with_nil_value_test.rb +35 -0
  326. data/test/unit/state/state_with_redefined_context_method_test.rb +45 -0
  327. data/test/unit/state/state_with_symbolic_value_test.rb +32 -0
  328. data/test/unit/state/state_with_valid_inherited_method_call_for_current_state_test.rb +40 -0
  329. data/test/unit/state/state_with_valid_method_call_for_current_state_test.rb +33 -0
  330. data/test/unit/state/state_with_valid_method_call_for_different_state_test.rb +41 -0
  331. data/test/unit/state/state_without_cached_lambda_value_test.rb +25 -0
  332. data/test/unit/state/state_without_name_test.rb +39 -0
  333. data/test/unit/state_collection/state_collection_by_default_test.rb +21 -0
  334. data/test/unit/state_collection/state_collection_string_test.rb +35 -0
  335. data/test/unit/state_collection/state_collection_test.rb +74 -0
  336. data/test/unit/state_collection/state_collection_with_custom_state_values_test.rb +29 -0
  337. data/test/unit/state_collection/state_collection_with_event_transitions_test.rb +39 -0
  338. data/test/unit/state_collection/state_collection_with_initial_state_test.rb +40 -0
  339. data/test/unit/state_collection/state_collection_with_namespace_test.rb +21 -0
  340. data/test/unit/state_collection/state_collection_with_state_behaviors_test.rb +40 -0
  341. data/test/unit/state_collection/state_collection_with_state_matchers_test.rb +29 -0
  342. data/test/unit/state_collection/state_collection_with_transition_callbacks_test.rb +40 -0
  343. data/test/unit/state_context/state_context_proxy_test.rb +26 -0
  344. data/test/unit/state_context/state_context_proxy_with_if_and_unless_conditions_test.rb +42 -0
  345. data/test/unit/state_context/state_context_proxy_with_if_condition_test.rb +64 -0
  346. data/test/unit/state_context/state_context_proxy_with_multiple_if_conditions_test.rb +32 -0
  347. data/test/unit/state_context/state_context_proxy_with_multiple_unless_conditions_test.rb +32 -0
  348. data/test/unit/state_context/state_context_proxy_with_unless_condition_test.rb +64 -0
  349. data/test/unit/state_context/state_context_proxy_without_conditions_test.rb +31 -0
  350. data/test/unit/state_context/state_context_test.rb +28 -0
  351. data/test/unit/state_context/state_context_transition_test.rb +104 -0
  352. data/test/unit/state_context/state_context_with_matching_transition_test.rb +27 -0
  353. data/test/unit/state_machine/state_machine_by_default_test.rb +12 -0
  354. data/test/unit/state_machine/state_machine_test.rb +20 -0
  355. data/test/unit/transition/transition_after_being_performed_test.rb +48 -0
  356. data/test/unit/transition/transition_after_being_persisted_test.rb +46 -0
  357. data/test/unit/transition/transition_after_being_rolled_back_test.rb +35 -0
  358. data/test/unit/transition/transition_equality_test.rb +52 -0
  359. data/test/unit/transition/transition_loopback_test.rb +18 -0
  360. data/test/unit/transition/transition_test.rb +96 -0
  361. data/test/unit/transition/transition_transient_test.rb +20 -0
  362. data/test/unit/transition/transition_with_action_test.rb +27 -0
  363. data/test/unit/transition/transition_with_after_callbacks_skipped_test.rb +127 -0
  364. data/test/unit/transition/transition_with_after_callbacks_test.rb +93 -0
  365. data/test/unit/transition/transition_with_around_callbacks_test.rb +141 -0
  366. data/test/unit/transition/transition_with_before_callbacks_skipped_test.rb +30 -0
  367. data/test/unit/transition/transition_with_before_callbacks_test.rb +104 -0
  368. data/test/unit/transition/transition_with_custom_machine_attribute_test.rb +28 -0
  369. data/test/unit/transition/transition_with_different_states_test.rb +18 -0
  370. data/test/unit/transition/transition_with_dynamic_to_value_test.rb +19 -0
  371. data/test/unit/transition/transition_with_failure_callbacks_test.rb +84 -0
  372. data/test/unit/transition/transition_with_invalid_nodes_test.rb +29 -0
  373. data/test/unit/transition/transition_with_mixed_callbacks_test.rb +105 -0
  374. data/test/unit/transition/transition_with_multiple_after_callbacks_test.rb +40 -0
  375. data/test/unit/transition/transition_with_multiple_around_callbacks_test.rb +114 -0
  376. data/test/unit/transition/transition_with_multiple_before_callbacks_test.rb +40 -0
  377. data/test/unit/transition/transition_with_multiple_failure_callbacks_test.rb +40 -0
  378. data/test/unit/transition/transition_with_namespace_test.rb +47 -0
  379. data/test/unit/transition/transition_with_perform_arguments_test.rb +35 -0
  380. data/test/unit/transition/transition_with_transactions_test.rb +42 -0
  381. data/test/unit/transition/transition_without_callbacks_test.rb +33 -0
  382. data/test/unit/transition/transition_without_reading_state_test.rb +22 -0
  383. data/test/unit/transition/transition_without_running_action_test.rb +47 -0
  384. data/test/unit/transition_collection/attribute_transition_collection_by_default_test.rb +23 -0
  385. data/test/unit/transition_collection/attribute_transition_collection_marshalling_test.rb +64 -0
  386. data/test/unit/transition_collection/attribute_transition_collection_with_action_error_test.rb +44 -0
  387. data/test/unit/transition_collection/attribute_transition_collection_with_action_failed_test.rb +44 -0
  388. data/test/unit/transition_collection/attribute_transition_collection_with_after_callback_error_test.rb +32 -0
  389. data/test/unit/transition_collection/attribute_transition_collection_with_after_callback_halt_test.rb +33 -0
  390. data/test/unit/transition_collection/attribute_transition_collection_with_around_after_yield_callback_error_test.rb +32 -0
  391. data/test/unit/transition_collection/attribute_transition_collection_with_around_callback_after_yield_error_test.rb +32 -0
  392. data/test/unit/transition_collection/attribute_transition_collection_with_around_callback_after_yield_halt_test.rb +33 -0
  393. data/test/unit/transition_collection/attribute_transition_collection_with_around_callback_before_yield_halt_test.rb +33 -0
  394. data/test/unit/transition_collection/attribute_transition_collection_with_before_callback_error_test.rb +32 -0
  395. data/test/unit/transition_collection/attribute_transition_collection_with_before_callback_halt_test.rb +33 -0
  396. data/test/unit/transition_collection/attribute_transition_collection_with_callbacks_test.rb +68 -0
  397. data/test/unit/transition_collection/attribute_transition_collection_with_event_transitions_test.rb +41 -0
  398. data/test/unit/transition_collection/attribute_transition_collection_with_events_test.rb +44 -0
  399. data/test/unit/transition_collection/attribute_transition_collection_with_skipped_after_callbacks_test.rb +42 -0
  400. data/test/unit/transition_collection/transition_collection_by_default_test.rb +23 -0
  401. data/test/unit/transition_collection/transition_collection_empty_with_block_test.rb +23 -0
  402. data/test/unit/transition_collection/transition_collection_empty_without_block_test.rb +12 -0
  403. data/test/unit/transition_collection/transition_collection_invalid_test.rb +21 -0
  404. data/test/unit/transition_collection/transition_collection_partial_invalid_test.rb +69 -0
  405. data/test/unit/transition_collection/transition_collection_test.rb +26 -0
  406. data/test/unit/transition_collection/transition_collection_valid_test.rb +57 -0
  407. data/test/unit/transition_collection/transition_collection_with_action_error_test.rb +66 -0
  408. data/test/unit/transition_collection/transition_collection_with_action_failed_test.rb +60 -0
  409. data/test/unit/transition_collection/transition_collection_with_action_hook_and_block_test.rb +17 -0
  410. data/test/unit/transition_collection/transition_collection_with_action_hook_and_skipped_action_test.rb +17 -0
  411. data/test/unit/transition_collection/transition_collection_with_action_hook_and_skipped_after_callbacks_test.rb +37 -0
  412. data/test/unit/transition_collection/transition_collection_with_action_hook_base_test.rb +34 -0
  413. data/test/unit/transition_collection/transition_collection_with_action_hook_error_test.rb +29 -0
  414. data/test/unit/transition_collection/transition_collection_with_action_hook_invalid_test.rb +17 -0
  415. data/test/unit/transition_collection/transition_collection_with_action_hook_multiple_test.rb +79 -0
  416. data/test/unit/transition_collection/transition_collection_with_action_hook_test.rb +45 -0
  417. data/test/unit/transition_collection/transition_collection_with_action_hook_with_different_actions_test.rb +48 -0
  418. data/test/unit/transition_collection/transition_collection_with_action_hook_with_nil_action_test.rb +42 -0
  419. data/test/unit/transition_collection/transition_collection_with_after_callback_halt_test.rb +51 -0
  420. data/test/unit/transition_collection/transition_collection_with_before_callback_halt_test.rb +47 -0
  421. data/test/unit/transition_collection/transition_collection_with_block_test.rb +46 -0
  422. data/test/unit/transition_collection/transition_collection_with_callbacks_test.rb +135 -0
  423. data/test/unit/transition_collection/transition_collection_with_different_actions_test.rb +189 -0
  424. data/test/unit/transition_collection/transition_collection_with_duplicate_actions_test.rb +48 -0
  425. data/test/unit/transition_collection/transition_collection_with_empty_actions_test.rb +41 -0
  426. data/test/unit/transition_collection/transition_collection_with_mixed_actions_test.rb +41 -0
  427. data/test/unit/transition_collection/transition_collection_with_skipped_actions_and_block_test.rb +34 -0
  428. data/test/unit/transition_collection/transition_collection_with_skipped_actions_test.rb +69 -0
  429. data/test/unit/transition_collection/transition_collection_with_skipped_after_callbacks_and_around_callbacks_test.rb +53 -0
  430. data/test/unit/transition_collection/transition_collection_with_skipped_after_callbacks_test.rb +34 -0
  431. data/test/unit/transition_collection/transition_collection_with_transactions_test.rb +65 -0
  432. data/test/unit/transition_collection/transition_collection_without_transactions_test.rb +29 -0
  433. metadata +888 -101
  434. data/spec/models/auto_shop_spec.rb +0 -41
  435. data/spec/models/car_spec.rb +0 -90
  436. data/spec/models/motorcycle_spec.rb +0 -44
  437. data/spec/models/traffic_light_spec.rb +0 -56
  438. data/spec/models/vehicle_spec.rb +0 -580
  439. data/spec/spec_helper.rb +0 -7
  440. data/spec/state_machines/assertions_spec.rb +0 -31
  441. data/spec/state_machines/branch_spec.rb +0 -827
  442. data/spec/state_machines/callbacks_spec.rb +0 -706
  443. data/spec/state_machines/errors_spec.rb +0 -1
  444. data/spec/state_machines/event_collection_spec.rb +0 -401
  445. data/spec/state_machines/event_spec.rb +0 -1140
  446. data/spec/state_machines/helper_spec.rb +0 -14
  447. data/spec/state_machines/integration_base_spec.rb +0 -12
  448. data/spec/state_machines/integration_spec.rb +0 -132
  449. data/spec/state_machines/invalid_parallel_transition_spec.rb +0 -18
  450. data/spec/state_machines/invalid_transition_spec.rb +0 -114
  451. data/spec/state_machines/machine_collection_spec.rb +0 -606
  452. data/spec/state_machines/machine_spec.rb +0 -3176
  453. data/spec/state_machines/matcher_helpers_spec.rb +0 -39
  454. data/spec/state_machines/matcher_spec.rb +0 -157
  455. data/spec/state_machines/node_collection_spec.rb +0 -371
  456. data/spec/state_machines/path_collection_spec.rb +0 -271
  457. data/spec/state_machines/path_spec.rb +0 -488
  458. data/spec/state_machines/state_collection_spec.rb +0 -352
  459. data/spec/state_machines/state_context_spec.rb +0 -442
  460. data/spec/state_machines/state_machine_spec.rb +0 -29
  461. data/spec/state_machines/state_spec.rb +0 -970
  462. data/spec/state_machines/transition_collection_spec.rb +0 -2199
  463. data/spec/state_machines/transition_spec.rb +0 -1558
  464. data/spec/support/migration_helpers.rb +0 -59
  465. data/spec/support/models.rb +0 -6
@@ -1,1558 +0,0 @@
1
- require 'spec_helper'
2
-
3
- context '' do
4
- before(:each) do
5
- @klass = Class.new
6
- @machine = StateMachines::Machine.new(@klass)
7
- @machine.state :parked, :idling
8
- @machine.event :ignite
9
-
10
- @object = @klass.new
11
- @object.state = 'parked'
12
-
13
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
14
- end
15
-
16
- it 'should_have_an_object' do
17
- assert_equal @object, @transition.object
18
- end
19
-
20
- it 'should_have_a_machine' do
21
- assert_equal @machine, @transition.machine
22
- end
23
-
24
- it 'should_have_an_event' do
25
- assert_equal :ignite, @transition.event
26
- end
27
-
28
- it 'should_have_a_qualified_event' do
29
- assert_equal :ignite, @transition.qualified_event
30
- end
31
-
32
- it 'should_have_a_human_event' do
33
- assert_equal 'ignite', @transition.human_event
34
- end
35
-
36
- it 'should_have_a_from_value' do
37
- assert_equal 'parked', @transition.from
38
- end
39
-
40
- it 'should_have_a_from_name' do
41
- assert_equal :parked, @transition.from_name
42
- end
43
-
44
- it 'should_have_a_qualified_from_name' do
45
- assert_equal :parked, @transition.qualified_from_name
46
- end
47
-
48
- it 'should_have_a_human_from_name' do
49
- assert_equal 'parked', @transition.human_from_name
50
- end
51
-
52
- it 'should_have_a_to_value' do
53
- assert_equal 'idling', @transition.to
54
- end
55
-
56
- it 'should_have_a_to_name' do
57
- assert_equal :idling, @transition.to_name
58
- end
59
-
60
- it 'should_have_a_qualified_to_name' do
61
- assert_equal :idling, @transition.qualified_to_name
62
- end
63
-
64
- it 'should_have_a_human_to_name' do
65
- assert_equal 'idling', @transition.human_to_name
66
- end
67
-
68
- it 'should_have_an_attribute' do
69
- assert_equal :state, @transition.attribute
70
- end
71
-
72
- it 'should_not_have_an_action' do
73
- assert_nil @transition.action
74
- end
75
-
76
- it 'should_not_be_transient' do
77
- assert_equal false, @transition.transient?
78
- end
79
-
80
- it 'should_generate_attributes' do
81
- expected = {:object => @object, :attribute => :state, :event => :ignite, :from => 'parked', :to => 'idling'}
82
- assert_equal expected, @transition.attributes
83
- end
84
-
85
- it 'should_have_empty_args' do
86
- assert_equal [], @transition.args
87
- end
88
-
89
- it 'should_not_have_a_result' do
90
- assert_nil @transition.result
91
- end
92
-
93
- it 'should_use_pretty_inspect' do
94
- assert_equal '#<StateMachines::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>', @transition.inspect
95
- end
96
- end
97
-
98
- context 'WithInvalidNodes' do
99
- before(:each) do
100
- @klass = Class.new
101
- @machine = StateMachines::Machine.new(@klass)
102
- @machine.state :parked, :idling
103
- @machine.event :ignite
104
-
105
- @object = @klass.new
106
- @object.state = 'parked'
107
- end
108
-
109
- it 'should_raise_exception_without_event' do
110
- assert_raise(IndexError) { StateMachines::Transition.new(@object, @machine, nil, :parked, :idling) }
111
- end
112
-
113
- it 'should_raise_exception_with_invalid_event' do
114
- assert_raise(IndexError) { StateMachines::Transition.new(@object, @machine, :invalid, :parked, :idling) }
115
- end
116
-
117
- it 'should_raise_exception_with_invalid_from_state' do
118
- assert_raise(IndexError) { StateMachines::Transition.new(@object, @machine, :ignite, :invalid, :idling) }
119
- end
120
-
121
- it 'should_raise_exception_with_invalid_to_state' do
122
- assert_raise(IndexError) { StateMachines::Transition.new(@object, @machine, :ignite, :parked, :invalid) }
123
- end
124
- end
125
-
126
- context 'WithDynamicToValue' do
127
- before(:each) do
128
- @klass = Class.new
129
- @machine = StateMachines::Machine.new(@klass)
130
- @machine.state :parked
131
- @machine.state :idling, :value => lambda {1}
132
- @machine.event :ignite
133
-
134
- @object = @klass.new
135
- @object.state = 'parked'
136
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
137
- end
138
-
139
- it 'should_evaluate_to_value' do
140
- assert_equal 1, @transition.to
141
- end
142
- end
143
-
144
- context 'Loopback' do
145
- before(:each) do
146
- @klass = Class.new
147
- @machine = StateMachines::Machine.new(@klass)
148
- @machine.state :parked
149
- @machine.event :park
150
-
151
- @object = @klass.new
152
- @object.state = 'parked'
153
- @transition = StateMachines::Transition.new(@object, @machine, :park, :parked, :parked)
154
- end
155
-
156
- it 'should_be_loopback' do
157
- assert @transition.loopback?
158
- end
159
- end
160
-
161
- context 'WithDifferentStates' do
162
- before(:each) do
163
- @klass = Class.new
164
- @machine = StateMachines::Machine.new(@klass)
165
- @machine.state :parked, :idling
166
- @machine.event :ignite
167
-
168
- @object = @klass.new
169
- @object.state = 'parked'
170
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
171
- end
172
-
173
- it 'should_not_be_loopback' do
174
- assert !@transition.loopback?
175
- end
176
- end
177
-
178
- context 'WithNamespace' do
179
- before(:each) do
180
- @klass = Class.new
181
- @machine = StateMachines::Machine.new(@klass, :namespace => 'alarm')
182
- @machine.state :off, :active
183
- @machine.event :activate
184
-
185
- @object = @klass.new
186
- @object.state = 'off'
187
-
188
- @transition = StateMachines::Transition.new(@object, @machine, :activate, :off, :active)
189
- end
190
-
191
- it 'should_have_an_event' do
192
- assert_equal :activate, @transition.event
193
- end
194
-
195
- it 'should_have_a_qualified_event' do
196
- assert_equal :activate_alarm, @transition.qualified_event
197
- end
198
-
199
- it 'should_have_a_from_name' do
200
- assert_equal :off, @transition.from_name
201
- end
202
-
203
- it 'should_have_a_qualified_from_name' do
204
- assert_equal :alarm_off, @transition.qualified_from_name
205
- end
206
-
207
- it 'should_have_a_human_from_name' do
208
- assert_equal 'off', @transition.human_from_name
209
- end
210
-
211
- it 'should_have_a_to_name' do
212
- assert_equal :active, @transition.to_name
213
- end
214
-
215
- it 'should_have_a_qualified_to_name' do
216
- assert_equal :alarm_active, @transition.qualified_to_name
217
- end
218
-
219
- it 'should_have_a_human_to_name' do
220
- assert_equal 'active', @transition.human_to_name
221
- end
222
- end
223
-
224
- context 'WithCustomMachineAttribute' do
225
- before(:each) do
226
- @klass = Class.new
227
- @machine = StateMachines::Machine.new(@klass, :state, :attribute => :state_id)
228
- @machine.state :off, :value => 1
229
- @machine.state :active, :value => 2
230
- @machine.event :activate
231
-
232
- @object = @klass.new
233
- @object.state_id = 1
234
-
235
- @transition = StateMachines::Transition.new(@object, @machine, :activate, :off, :active)
236
- end
237
-
238
- it 'should_persist' do
239
- @transition.persist
240
- assert_equal 2, @object.state_id
241
- end
242
-
243
- it 'should_rollback' do
244
- @object.state_id = 2
245
- @transition.rollback
246
-
247
- assert_equal 1, @object.state_id
248
- end
249
- end
250
-
251
- context 'WithoutReadingState' do
252
- before(:each) do
253
- @klass = Class.new
254
- @machine = StateMachines::Machine.new(@klass)
255
- @machine.state :parked, :idling
256
- @machine.event :ignite
257
-
258
- @object = @klass.new
259
- @object.state = 'idling'
260
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling, false)
261
- end
262
-
263
- it 'should_not_read_from_value_from_object' do
264
- assert_equal 'parked', @transition.from
265
- end
266
-
267
- it 'should_have_to_value' do
268
- assert_equal 'idling', @transition.to
269
- end
270
- end
271
-
272
- context 'WithAction' do
273
- before(:each) do
274
- @klass = Class.new do
275
- def save
276
- end
277
- end
278
-
279
- @machine = StateMachines::Machine.new(@klass, :action => :save)
280
- @machine.state :parked, :idling
281
- @machine.event :ignite
282
-
283
- @object = @klass.new
284
- @object.state = 'parked'
285
-
286
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
287
- end
288
-
289
- it 'should_have_an_action' do
290
- assert_equal :save, @transition.action
291
- end
292
-
293
- it 'should_not_have_a_result' do
294
- assert_nil @transition.result
295
- end
296
- end
297
-
298
- context 'AfterBeingPersisted' do
299
- before(:each) do
300
- @klass = Class.new
301
- @machine = StateMachines::Machine.new(@klass, :action => :save)
302
- @machine.state :parked, :idling
303
- @machine.event :ignite
304
-
305
- @object = @klass.new
306
- @object.state = 'parked'
307
-
308
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
309
- @transition.persist
310
- end
311
-
312
- it 'should_update_state_value' do
313
- assert_equal 'idling', @object.state
314
- end
315
-
316
- it 'should_not_change_from_state' do
317
- assert_equal 'parked', @transition.from
318
- end
319
-
320
- it 'should_not_change_to_state' do
321
- assert_equal 'idling', @transition.to
322
- end
323
-
324
- it 'should_not_be_able_to_persist_twice' do
325
- @object.state = 'parked'
326
- @transition.persist
327
- assert_equal 'parked', @object.state
328
- end
329
-
330
- it 'should_be_able_to_persist_again_after_resetting' do
331
- @object.state = 'parked'
332
- @transition.reset
333
- @transition.persist
334
- assert_equal 'idling', @object.state
335
- end
336
-
337
- it 'should_revert_to_from_state_on_rollback' do
338
- @transition.rollback
339
- assert_equal 'parked', @object.state
340
- end
341
- end
342
-
343
- context 'AfterBeingRolledBack' do
344
- before(:each) do
345
- @klass = Class.new
346
- @machine = StateMachines::Machine.new(@klass, :action => :save)
347
- @machine.state :parked, :idling
348
- @machine.event :ignite
349
-
350
- @object = @klass.new
351
- @object.state = 'parked'
352
-
353
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
354
- @object.state = 'idling'
355
-
356
- @transition.rollback
357
- end
358
-
359
- it 'should_update_state_value_to_from_state' do
360
- assert_equal 'parked', @object.state
361
- end
362
-
363
- it 'should_not_change_from_state' do
364
- assert_equal 'parked', @transition.from
365
- end
366
-
367
- it 'should_not_change_to_state' do
368
- assert_equal 'idling', @transition.to
369
- end
370
-
371
- it 'should_still_be_able_to_persist' do
372
- @transition.persist
373
- assert_equal 'idling', @object.state
374
- end
375
- end
376
-
377
- context 'WithoutCallbacks' do
378
- before(:each) do
379
- @klass = Class.new
380
-
381
- @machine = StateMachines::Machine.new(@klass)
382
- @machine.state :parked, :idling
383
- @machine.event :ignite
384
-
385
- @object = @klass.new
386
- @object.state = 'parked'
387
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
388
- end
389
-
390
- it 'should_succeed' do
391
- assert_equal true, @transition.run_callbacks
392
- end
393
-
394
- it 'should_succeed_if_after_callbacks_skipped' do
395
- assert_equal true, @transition.run_callbacks(:after => false)
396
- end
397
-
398
- it 'should_call_block_if_provided' do
399
- @transition.run_callbacks { @ran_block = true; {} }
400
- assert @ran_block
401
- end
402
-
403
- it 'should_track_block_result' do
404
- @transition.run_callbacks {{:result => 1}}
405
- assert_equal 1, @transition.result
406
- end
407
- end
408
-
409
- context 'WithBeforeCallbacks' do
410
- before(:each) do
411
- @klass = Class.new
412
-
413
- @machine = StateMachines::Machine.new(@klass)
414
- @machine.state :parked, :idling
415
- @machine.event :ignite
416
-
417
- @object = @klass.new
418
- @object.state = 'parked'
419
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
420
- end
421
-
422
- it 'should_run_before_callbacks' do
423
- @machine.before_transition {@run = true}
424
- result = @transition.run_callbacks
425
-
426
- assert_equal true, result
427
- assert_equal true, @run
428
- end
429
-
430
- it 'should_only_run_those_that_match_transition_context' do
431
- @count = 0
432
- callback = lambda {@count += 1}
433
-
434
- @machine.before_transition :from => :parked, :to => :idling, :on => :park, :do => callback
435
- @machine.before_transition :from => :parked, :to => :parked, :on => :park, :do => callback
436
- @machine.before_transition :from => :parked, :to => :idling, :on => :ignite, :do => callback
437
- @machine.before_transition :from => :idling, :to => :idling, :on => :park, :do => callback
438
- @transition.run_callbacks
439
-
440
- assert_equal 1, @count
441
- end
442
-
443
- it 'should_pass_transition_as_argument' do
444
- @machine.before_transition {|*args| @args = args}
445
- @transition.run_callbacks
446
-
447
- assert_equal [@object, @transition], @args
448
- end
449
-
450
- it 'should_catch_halts' do
451
- @machine.before_transition {throw :halt}
452
-
453
- result = nil
454
- assert_nothing_thrown { result = @transition.run_callbacks }
455
- assert_equal false, result
456
- end
457
-
458
- it 'should_not_catch_exceptions' do
459
- @machine.before_transition {raise ArgumentError}
460
- assert_raise(ArgumentError) { @transition.run_callbacks }
461
- end
462
-
463
- it 'should_not_be_able_to_run_twice' do
464
- @count = 0
465
- @machine.before_transition {@count += 1}
466
- @transition.run_callbacks
467
- @transition.run_callbacks
468
- assert_equal 1, @count
469
- end
470
-
471
- it 'should_be_able_to_run_again_after_halt' do
472
- @count = 0
473
- @machine.before_transition {@count += 1; throw :halt}
474
- @transition.run_callbacks
475
- @transition.run_callbacks
476
- assert_equal 2, @count
477
- end
478
-
479
- it 'should_be_able_to_run_again_after_resetting' do
480
- @count = 0
481
- @machine.before_transition {@count += 1}
482
- @transition.run_callbacks
483
- @transition.reset
484
- @transition.run_callbacks
485
- assert_equal 2, @count
486
- end
487
-
488
- it 'should_succeed_if_block_result_is_false' do
489
- @machine.before_transition {@run = true}
490
- assert_equal true, @transition.run_callbacks {{:result => false}}
491
- assert @run
492
- end
493
-
494
- it 'should_succeed_if_block_result_is_true' do
495
- @machine.before_transition {@run = true}
496
- assert_equal true, @transition.run_callbacks {{:result => true}}
497
- assert @run
498
- end
499
-
500
- it 'should_succeed_if_block_success_is_false' do
501
- @machine.before_transition {@run = true}
502
- assert_equal true, @transition.run_callbacks {{:success => false}}
503
- assert @run
504
- end
505
-
506
- it 'should_succeed_if_block_success_is_true' do
507
- @machine.before_transition {@run = true}
508
- assert_equal true, @transition.run_callbacks {{:success => true}}
509
- assert @run
510
- end
511
- end
512
-
513
- context 'WithMultipleBeforeCallbacks' do
514
- before(:each) do
515
- @klass = Class.new
516
-
517
- @machine = StateMachines::Machine.new(@klass)
518
- @machine.state :parked, :idling
519
- @machine.event :ignite
520
-
521
- @object = @klass.new
522
- @object.state = 'parked'
523
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
524
- end
525
-
526
- it 'should_run_in_the_order_they_were_defined' do
527
- @callbacks = []
528
- @machine.before_transition {@callbacks << 1}
529
- @machine.before_transition {@callbacks << 2}
530
- @transition.run_callbacks
531
-
532
- assert_equal [1, 2], @callbacks
533
- end
534
-
535
- it 'should_not_run_further_callbacks_if_halted' do
536
- @callbacks = []
537
- @machine.before_transition {@callbacks << 1; throw :halt}
538
- @machine.before_transition {@callbacks << 2}
539
-
540
- assert_equal false, @transition.run_callbacks
541
- assert_equal [1], @callbacks
542
- end
543
-
544
- it 'should_fail_if_any_callback_halted' do
545
- @machine.before_transition {true}
546
- @machine.before_transition {throw :halt}
547
-
548
- assert_equal false, @transition.run_callbacks
549
- end
550
- end
551
-
552
- context 'WithAfterCallbacks' do
553
- before(:each) do
554
- @klass = Class.new
555
-
556
- @machine = StateMachines::Machine.new(@klass)
557
- @machine.state :parked, :idling
558
- @machine.event :ignite
559
-
560
- @object = @klass.new
561
- @object.state = 'parked'
562
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
563
- end
564
-
565
- it 'should_run_after_callbacks' do
566
- @machine.after_transition {|object| @run = true}
567
- result = @transition.run_callbacks
568
-
569
- assert_equal true, result
570
- assert_equal true, @run
571
- end
572
-
573
- it 'should_only_run_those_that_match_transition_context' do
574
- @count = 0
575
- callback = lambda {@count += 1}
576
-
577
- @machine.after_transition :from => :parked, :to => :idling, :on => :park, :do => callback
578
- @machine.after_transition :from => :parked, :to => :parked, :on => :park, :do => callback
579
- @machine.after_transition :from => :parked, :to => :idling, :on => :ignite, :do => callback
580
- @machine.after_transition :from => :idling, :to => :idling, :on => :park, :do => callback
581
- @transition.run_callbacks
582
-
583
- assert_equal 1, @count
584
- end
585
-
586
- it 'should_not_run_if_not_successful' do
587
- @run = false
588
- @machine.after_transition {|object| @run = true}
589
- @transition.run_callbacks {{:success => false}}
590
- assert !@run
591
- end
592
-
593
- it 'should_run_if_successful' do
594
- @machine.after_transition {|object| @run = true}
595
- @transition.run_callbacks {{:success => true}}
596
- assert @run
597
- end
598
-
599
- it 'should_pass_transition_as_argument' do
600
- @machine.after_transition {|*args| @args = args}
601
-
602
- @transition.run_callbacks
603
- assert_equal [@object, @transition], @args
604
- end
605
-
606
- it 'should_catch_halts' do
607
- @machine.after_transition {throw :halt}
608
-
609
- result = nil
610
- assert_nothing_thrown { result = @transition.run_callbacks }
611
- assert_equal true, result
612
- end
613
-
614
- it 'should_not_catch_exceptions' do
615
- @machine.after_transition {raise ArgumentError}
616
- assert_raise(ArgumentError) { @transition.run_callbacks }
617
- end
618
-
619
- it 'should_not_be_able_to_run_twice' do
620
- @count = 0
621
- @machine.after_transition {@count += 1}
622
- @transition.run_callbacks
623
- @transition.run_callbacks
624
- assert_equal 1, @count
625
- end
626
-
627
- it 'should_not_be_able_to_run_twice_if_halted' do
628
- @count = 0
629
- @machine.after_transition {@count += 1; throw :halt}
630
- @transition.run_callbacks
631
- @transition.run_callbacks
632
- assert_equal 1, @count
633
- end
634
-
635
- it 'should_be_able_to_run_again_after_resetting' do
636
- @count = 0
637
- @machine.after_transition {@count += 1}
638
- @transition.run_callbacks
639
- @transition.reset
640
- @transition.run_callbacks
641
- assert_equal 2, @count
642
- end
643
- end
644
-
645
- context 'WithMultipleAfterCallbacks' do
646
- before(:each) do
647
- @klass = Class.new
648
-
649
- @machine = StateMachines::Machine.new(@klass)
650
- @machine.state :parked, :idling
651
- @machine.event :ignite
652
-
653
- @object = @klass.new
654
- @object.state = 'parked'
655
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
656
- end
657
-
658
- it 'should_run_in_the_order_they_were_defined' do
659
- @callbacks = []
660
- @machine.after_transition {@callbacks << 1}
661
- @machine.after_transition {@callbacks << 2}
662
- @transition.run_callbacks
663
-
664
- assert_equal [1, 2], @callbacks
665
- end
666
-
667
- it 'should_not_run_further_callbacks_if_halted' do
668
- @callbacks = []
669
- @machine.after_transition {@callbacks << 1; throw :halt}
670
- @machine.after_transition {@callbacks << 2}
671
-
672
- assert_equal true, @transition.run_callbacks
673
- assert_equal [1], @callbacks
674
- end
675
-
676
- it 'should_fail_if_any_callback_halted' do
677
- @machine.after_transition {true}
678
- @machine.after_transition {throw :halt}
679
-
680
- assert_equal true, @transition.run_callbacks
681
- end
682
- end
683
-
684
- context 'WithAroundCallbacks' do
685
- before(:each) do
686
- @klass = Class.new
687
-
688
- @machine = StateMachines::Machine.new(@klass)
689
- @machine.state :parked, :idling
690
- @machine.event :ignite
691
-
692
- @object = @klass.new
693
- @object.state = 'parked'
694
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
695
- end
696
-
697
- it 'should_run_around_callbacks' do
698
- @machine.around_transition {|object, transition, block| @run_before = true; block.call; @run_after = true}
699
- result = @transition.run_callbacks
700
-
701
- assert_equal true, result
702
- assert_equal true, @run_before
703
- assert_equal true, @run_after
704
- end
705
-
706
- it 'should_only_run_those_that_match_transition_context' do
707
- @count = 0
708
- callback = lambda {|object, transition, block| @count += 1; block.call}
709
-
710
- @machine.around_transition :from => :parked, :to => :idling, :on => :park, :do => callback
711
- @machine.around_transition :from => :parked, :to => :parked, :on => :park, :do => callback
712
- @machine.around_transition :from => :parked, :to => :idling, :on => :ignite, :do => callback
713
- @machine.around_transition :from => :idling, :to => :idling, :on => :park, :do => callback
714
- @transition.run_callbacks
715
-
716
- assert_equal 1, @count
717
- end
718
-
719
- it 'should_pass_transition_as_argument' do
720
- @machine.around_transition {|*args| block = args.pop; @args = args; block.call}
721
- @transition.run_callbacks
722
-
723
- assert_equal [@object, @transition], @args
724
- end
725
-
726
- it 'should_run_block_between_callback' do
727
- @callbacks = []
728
- @machine.around_transition {|block| @callbacks << :before; block.call; @callbacks << :after}
729
- @transition.run_callbacks { @callbacks << :within; {:success => true} }
730
-
731
- assert_equal [:before, :within, :after], @callbacks
732
- end
733
-
734
- it 'should_have_access_to_result_after_yield' do
735
- @machine.around_transition {|block| @before_result = @transition.result; block.call; @after_result = @transition.result}
736
- @transition.run_callbacks {{:result => 1, :success => true}}
737
-
738
- assert_nil @before_result
739
- assert_equal 1, @after_result
740
- end
741
-
742
- it 'should_catch_before_yield_halts' do
743
- @machine.around_transition {throw :halt}
744
-
745
- result = nil
746
- assert_nothing_thrown { result = @transition.run_callbacks }
747
- assert_equal false, result
748
- end
749
-
750
- it 'should_catch_after_yield_halts' do
751
- @machine.around_transition {|block| block.call; throw :halt}
752
-
753
- result = nil
754
- assert_nothing_thrown { result = @transition.run_callbacks }
755
- assert_equal true, result
756
- end
757
-
758
- it 'should_not_catch_before_yield' do
759
- @machine.around_transition {raise ArgumentError}
760
- assert_raise(ArgumentError) { @transition.run_callbacks }
761
- end
762
-
763
- it 'should_not_catch_after_yield' do
764
- @machine.around_transition {|block| block.call; raise ArgumentError}
765
- assert_raise(ArgumentError) { @transition.run_callbacks }
766
- end
767
-
768
- it 'should_fail_if_not_yielded' do
769
- @machine.around_transition {}
770
-
771
- result = nil
772
- assert_nothing_thrown { result = @transition.run_callbacks }
773
- assert_equal false, result
774
- end
775
-
776
- it 'should_not_be_able_to_run_twice' do
777
- @before_count = 0
778
- @after_count = 0
779
- @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
780
- @transition.run_callbacks
781
- @transition.run_callbacks
782
- assert_equal 1, @before_count
783
- assert_equal 1, @after_count
784
- end
785
-
786
- it 'should_be_able_to_run_again_after_resetting' do
787
- @before_count = 0
788
- @after_count = 0
789
- @machine.around_transition {|block| @before_count += 1; block.call; @after_count += 1}
790
- @transition.run_callbacks
791
- @transition.reset
792
- @transition.run_callbacks
793
- assert_equal 2, @before_count
794
- assert_equal 2, @after_count
795
- end
796
-
797
- it 'should_succeed_if_block_result_is_false' do
798
- @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
799
- assert_equal true, @transition.run_callbacks {{:success => true, :result => false}}
800
- assert @before_run
801
- assert @after_run
802
- end
803
-
804
- it 'should_succeed_if_block_result_is_true' do
805
- @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
806
- assert_equal true, @transition.run_callbacks {{:success => true, :result => true}}
807
- assert @before_run
808
- assert @after_run
809
- end
810
-
811
- it 'should_only_run_before_if_block_success_is_false' do
812
- @after_run = false
813
- @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
814
- assert_equal true, @transition.run_callbacks {{:success => false}}
815
- assert @before_run
816
- assert !@after_run
817
- end
818
-
819
- it 'should_succeed_if_block_success_is_false' do
820
- @machine.around_transition {|block| @before_run = true; block.call; @after_run = true}
821
- assert_equal true, @transition.run_callbacks {{:success => true}}
822
- assert @before_run
823
- assert @after_run
824
- end
825
- end
826
-
827
- context 'WithMultipleAroundCallbacks' do
828
- before(:each) do
829
- @klass = Class.new
830
-
831
- @machine = StateMachines::Machine.new(@klass)
832
- @machine.state :parked, :idling
833
- @machine.event :ignite
834
-
835
- @object = @klass.new
836
- @object.state = 'parked'
837
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
838
- end
839
-
840
- it 'should_before_yield_in_the_order_they_were_defined' do
841
- @callbacks = []
842
- @machine.around_transition {|block| @callbacks << 1; block.call}
843
- @machine.around_transition {|block| @callbacks << 2; block.call}
844
- @transition.run_callbacks
845
-
846
- assert_equal [1, 2], @callbacks
847
- end
848
-
849
- it 'should_before_yield_multiple_methods_in_the_order_they_were_defined' do
850
- @callbacks = []
851
- @machine.around_transition(lambda {|block| @callbacks << 1; block.call}, lambda {|block| @callbacks << 2; block.call})
852
- @machine.around_transition(lambda {|block| @callbacks << 3; block.call}, lambda {|block| @callbacks << 4; block.call})
853
- @transition.run_callbacks
854
-
855
- assert_equal [1, 2, 3, 4], @callbacks
856
- end
857
-
858
- it 'should_after_yield_in_the_reverse_order_they_were_defined' do
859
- @callbacks = []
860
- @machine.around_transition {|block| block.call; @callbacks << 1}
861
- @machine.around_transition {|block| block.call; @callbacks << 2}
862
- @transition.run_callbacks
863
-
864
- assert_equal [2, 1], @callbacks
865
- end
866
-
867
- it 'should_after_yield_multiple_methods_in_the_reverse_order_they_were_defined' do
868
- @callbacks = []
869
- @machine.around_transition(lambda {|block| block.call; @callbacks << 1}) {|block| block.call; @callbacks << 2}
870
- @machine.around_transition(lambda {|block| block.call; @callbacks << 3}) {|block| block.call; @callbacks << 4}
871
- @transition.run_callbacks
872
-
873
- assert_equal [4, 3, 2, 1], @callbacks
874
- end
875
-
876
- it 'should_run_block_between_callback' do
877
- @callbacks = []
878
- @machine.around_transition {|block| @callbacks << :before_1; block.call; @callbacks << :after_1}
879
- @machine.around_transition {|block| @callbacks << :before_2; block.call; @callbacks << :after_2}
880
- @transition.run_callbacks { @callbacks << :within; {:success => true} }
881
-
882
- assert_equal [:before_1, :before_2, :within, :after_2, :after_1], @callbacks
883
- end
884
-
885
- it 'should_have_access_to_result_after_yield' do
886
- @machine.around_transition {|block| @before_result_1 = @transition.result; block.call; @after_result_1 = @transition.result}
887
- @machine.around_transition {|block| @before_result_2 = @transition.result; block.call; @after_result_2 = @transition.result}
888
- @transition.run_callbacks {{:result => 1, :success => true}}
889
-
890
- assert_nil @before_result_1
891
- assert_nil @before_result_2
892
- assert_equal 1, @after_result_1
893
- assert_equal 1, @after_result_2
894
- end
895
-
896
- it 'should_fail_if_any_before_yield_halted' do
897
- @machine.around_transition {|block| block.call}
898
- @machine.around_transition {throw :halt}
899
-
900
- assert_equal false, @transition.run_callbacks
901
- end
902
-
903
- it 'should_not_continue_around_callbacks_if_before_yield_halted' do
904
- @callbacks = []
905
- @machine.around_transition {@callbacks << 1; throw :halt}
906
- @machine.around_transition {|block| @callbacks << 2; block.call; @callbacks << 3}
907
-
908
- assert_equal false, @transition.run_callbacks
909
- assert_equal [1], @callbacks
910
- end
911
-
912
- it 'should_not_continue_around_callbacks_if_later_before_yield_halted' do
913
- @callbacks = []
914
- @machine.around_transition {|block| block.call; @callbacks << 1}
915
- @machine.around_transition {throw :halt}
916
-
917
- @transition.run_callbacks
918
- assert_equal [], @callbacks
919
- end
920
-
921
- it 'should_not_run_further_callbacks_if_after_yield_halted' do
922
- @callbacks = []
923
- @machine.around_transition {|block| block.call; @callbacks << 1}
924
- @machine.around_transition {|block| block.call; throw :halt}
925
-
926
- assert_equal true, @transition.run_callbacks
927
- assert_equal [], @callbacks
928
- end
929
-
930
- it 'should_fail_if_any_fail_to_yield' do
931
- @callbacks = []
932
- @machine.around_transition {@callbacks << 1}
933
- @machine.around_transition {|block| @callbacks << 2; block.call; @callbacks << 3}
934
-
935
- assert_equal false, @transition.run_callbacks
936
- assert_equal [1], @callbacks
937
- end
938
- end
939
-
940
- context 'WithFailureCallbacks' do
941
- before(:each) do
942
- @klass = Class.new
943
-
944
- @machine = StateMachines::Machine.new(@klass)
945
- @machine.state :parked, :idling
946
- @machine.event :ignite
947
-
948
- @object = @klass.new
949
- @object.state = 'parked'
950
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
951
- end
952
-
953
- it 'should_only_run_those_that_match_transition_context' do
954
- @count = 0
955
- callback = lambda {@count += 1}
956
-
957
- @machine.after_failure :do => callback
958
- @machine.after_failure :on => :park, :do => callback
959
- @machine.after_failure :on => :ignite, :do => callback
960
- @transition.run_callbacks {{:success => false}}
961
-
962
- assert_equal 2, @count
963
- end
964
-
965
- it 'should_run_if_not_successful' do
966
- @machine.after_failure {|object| @run = true}
967
- @transition.run_callbacks {{:success => false}}
968
- assert @run
969
- end
970
-
971
- it 'should_not_run_if_successful' do
972
- @run = false
973
- @machine.after_failure {|object| @run = true}
974
- @transition.run_callbacks {{:success => true}}
975
- assert !@run
976
- end
977
-
978
- it 'should_pass_transition_as_argument' do
979
- @machine.after_failure {|*args| @args = args}
980
-
981
- @transition.run_callbacks {{:success => false}}
982
- assert_equal [@object, @transition], @args
983
- end
984
-
985
- it 'should_catch_halts' do
986
- @machine.after_failure {throw :halt}
987
-
988
- result = nil
989
- assert_nothing_thrown { result = @transition.run_callbacks {{:success => false}} }
990
- assert_equal true, result
991
- end
992
-
993
- it 'should_not_catch_exceptions' do
994
- @machine.after_failure {raise ArgumentError}
995
- assert_raise(ArgumentError) { @transition.run_callbacks {{:success => false}} }
996
- end
997
-
998
- it 'should_not_be_able_to_run_twice' do
999
- @count = 0
1000
- @machine.after_failure {@count += 1}
1001
- @transition.run_callbacks {{:success => false}}
1002
- @transition.run_callbacks {{:success => false}}
1003
- assert_equal 1, @count
1004
- end
1005
-
1006
- it 'should_not_be_able_to_run_twice_if_halted' do
1007
- @count = 0
1008
- @machine.after_failure {@count += 1; throw :halt}
1009
- @transition.run_callbacks {{:success => false}}
1010
- @transition.run_callbacks {{:success => false}}
1011
- assert_equal 1, @count
1012
- end
1013
-
1014
- it 'should_be_able_to_run_again_after_resetting' do
1015
- @count = 0
1016
- @machine.after_failure {@count += 1}
1017
- @transition.run_callbacks {{:success => false}}
1018
- @transition.reset
1019
- @transition.run_callbacks {{:success => false}}
1020
- assert_equal 2, @count
1021
- end
1022
- end
1023
-
1024
- context 'WithMultipleFailureCallbacks' do
1025
- before(:each) do
1026
- @klass = Class.new
1027
-
1028
- @machine = StateMachines::Machine.new(@klass)
1029
- @machine.state :parked, :idling
1030
- @machine.event :ignite
1031
-
1032
- @object = @klass.new
1033
- @object.state = 'parked'
1034
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1035
- end
1036
-
1037
- it 'should_run_in_the_order_they_were_defined' do
1038
- @callbacks = []
1039
- @machine.after_failure {@callbacks << 1}
1040
- @machine.after_failure {@callbacks << 2}
1041
- @transition.run_callbacks {{:success => false}}
1042
-
1043
- assert_equal [1, 2], @callbacks
1044
- end
1045
-
1046
- it 'should_not_run_further_callbacks_if_halted' do
1047
- @callbacks = []
1048
- @machine.after_failure {@callbacks << 1; throw :halt}
1049
- @machine.after_failure {@callbacks << 2}
1050
-
1051
- assert_equal true, @transition.run_callbacks {{:success => false}}
1052
- assert_equal [1], @callbacks
1053
- end
1054
-
1055
- it 'should_fail_if_any_callback_halted' do
1056
- @machine.after_failure {true}
1057
- @machine.after_failure {throw :halt}
1058
-
1059
- assert_equal true, @transition.run_callbacks {{:success => false}}
1060
- end
1061
- end
1062
-
1063
- context 'WithMixedCallbacks' do
1064
- before(:each) do
1065
- @klass = Class.new
1066
-
1067
- @machine = StateMachines::Machine.new(@klass)
1068
- @machine.state :parked, :idling
1069
- @machine.event :ignite
1070
-
1071
- @object = @klass.new
1072
- @object.state = 'parked'
1073
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1074
- end
1075
-
1076
- it 'should_before_and_around_callbacks_in_order_defined' do
1077
- @callbacks = []
1078
- @machine.before_transition {@callbacks << :before_1}
1079
- @machine.around_transition {|block| @callbacks << :around; block.call}
1080
- @machine.before_transition {@callbacks << :before_2}
1081
-
1082
- assert_equal true, @transition.run_callbacks
1083
- assert_equal [:before_1, :around, :before_2], @callbacks
1084
- end
1085
-
1086
- it 'should_run_around_callbacks_before_after_callbacks' do
1087
- @callbacks = []
1088
- @machine.after_transition {@callbacks << :after_1}
1089
- @machine.around_transition {|block| block.call; @callbacks << :after_2}
1090
- @machine.after_transition {@callbacks << :after_3}
1091
-
1092
- assert_equal true, @transition.run_callbacks
1093
- assert_equal [:after_2, :after_1, :after_3], @callbacks
1094
- end
1095
-
1096
- it 'should_have_access_to_result_for_both_after_and_around_callbacks' do
1097
- @machine.after_transition {@after_result = @transition.result}
1098
- @machine.around_transition {|block| block.call; @around_result = @transition.result}
1099
-
1100
- @transition.run_callbacks {{:result => 1, :success => true}}
1101
- assert_equal 1, @after_result
1102
- assert_equal 1, @around_result
1103
- end
1104
-
1105
- it 'should_not_run_further_callbacks_if_before_callback_halts' do
1106
- @callbacks = []
1107
- @machine.before_transition {@callbacks << :before_1}
1108
- @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
1109
- @machine.before_transition {@callbacks << :before_2; throw :halt}
1110
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
1111
- @machine.after_transition {@callbacks << :after}
1112
-
1113
- assert_equal false, @transition.run_callbacks
1114
- assert_equal [:before_1, :before_around_1, :before_2], @callbacks
1115
- end
1116
-
1117
- it 'should_not_run_further_callbacks_if_before_yield_halts' do
1118
- @callbacks = []
1119
- @machine.before_transition {@callbacks << :before_1}
1120
- @machine.around_transition {|block| @callbacks << :before_around_1; throw :halt}
1121
- @machine.before_transition {@callbacks << :before_2; throw :halt}
1122
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
1123
- @machine.after_transition {@callbacks << :after}
1124
-
1125
- assert_equal false, @transition.run_callbacks
1126
- assert_equal [:before_1, :before_around_1], @callbacks
1127
- end
1128
-
1129
- it 'should_not_run_further_callbacks_if_around_callback_fails_to_yield' do
1130
- @callbacks = []
1131
- @machine.before_transition {@callbacks << :before_1}
1132
- @machine.around_transition {|block| @callbacks << :before_around_1}
1133
- @machine.before_transition {@callbacks << :before_2; throw :halt}
1134
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
1135
- @machine.after_transition {@callbacks << :after}
1136
-
1137
- assert_equal false, @transition.run_callbacks
1138
- assert_equal [:before_1, :before_around_1], @callbacks
1139
- end
1140
-
1141
- it 'should_not_run_further_callbacks_if_after_yield_halts' do
1142
- @callbacks = []
1143
- @machine.before_transition {@callbacks << :before_1}
1144
- @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1; throw :halt}
1145
- @machine.before_transition {@callbacks << :before_2}
1146
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
1147
- @machine.after_transition {@callbacks << :after}
1148
-
1149
- assert_equal true, @transition.run_callbacks
1150
- assert_equal [:before_1, :before_around_1, :before_2, :before_around_2, :after_around_2, :after_around_1], @callbacks
1151
- end
1152
-
1153
- it 'should_not_run_further_callbacks_if_after_callback_halts' do
1154
- @callbacks = []
1155
- @machine.before_transition {@callbacks << :before_1}
1156
- @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
1157
- @machine.before_transition {@callbacks << :before_2}
1158
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
1159
- @machine.after_transition {@callbacks << :after_1; throw :halt}
1160
- @machine.after_transition {@callbacks << :after_2}
1161
-
1162
- assert_equal true, @transition.run_callbacks
1163
- assert_equal [:before_1, :before_around_1, :before_2, :before_around_2, :after_around_2, :after_around_1, :after_1], @callbacks
1164
- end
1165
- end
1166
-
1167
- context 'WithBeforeCallbacksSkipped' do
1168
- before(:each) do
1169
- @klass = Class.new
1170
-
1171
- @machine = StateMachines::Machine.new(@klass)
1172
- @machine.state :parked, :idling
1173
- @machine.event :ignite
1174
-
1175
- @object = @klass.new
1176
- @object.state = 'parked'
1177
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1178
- end
1179
-
1180
- it 'should_not_run_before_callbacks' do
1181
- @run = false
1182
- @machine.before_transition {@run = true}
1183
-
1184
- assert_equal false, @transition.run_callbacks(:before => false)
1185
- assert !@run
1186
- end
1187
-
1188
- it 'should_run_failure_callbacks' do
1189
- @machine.after_failure {@run = true}
1190
-
1191
- assert_equal false, @transition.run_callbacks(:before => false)
1192
- assert @run
1193
- end
1194
- end
1195
-
1196
- context 'WithAfterCallbacksSkipped' do
1197
- before(:each) do
1198
- @klass = Class.new
1199
-
1200
- @machine = StateMachines::Machine.new(@klass)
1201
- @machine.state :parked, :idling
1202
- @machine.event :ignite
1203
-
1204
- @object = @klass.new
1205
- @object.state = 'parked'
1206
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1207
- end
1208
-
1209
- it 'should_run_before_callbacks' do
1210
- @machine.before_transition {@run = true}
1211
-
1212
- assert_equal true, @transition.run_callbacks(:after => false)
1213
- assert @run
1214
- end
1215
-
1216
- it 'should_not_run_after_callbacks' do
1217
- @run = false
1218
- @machine.after_transition {@run = true}
1219
-
1220
- assert_equal true, @transition.run_callbacks(:after => false)
1221
- assert !@run
1222
- end
1223
-
1224
- if StateMachines::Transition.pause_supported?
1225
- it 'should_run_around_callbacks_before_yield' do
1226
- @machine.around_transition {|block| @run = true; block.call}
1227
-
1228
- assert_equal true, @transition.run_callbacks(:after => false)
1229
- assert @run
1230
- end
1231
-
1232
- it 'should_not_run_around_callbacks_after_yield' do
1233
- @run = false
1234
- @machine.around_transition {|block| block.call; @run = true}
1235
-
1236
- assert_equal true, @transition.run_callbacks(:after => false)
1237
- assert !@run
1238
- end
1239
-
1240
- it 'should_continue_around_transition_execution_on_second_call' do
1241
- @callbacks = []
1242
- @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
1243
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
1244
- @machine.after_transition {@callbacks << :after}
1245
-
1246
- assert_equal true, @transition.run_callbacks(:after => false)
1247
- assert_equal [:before_around_1, :before_around_2], @callbacks
1248
-
1249
- assert_equal true, @transition.run_callbacks
1250
- assert_equal [:before_around_1, :before_around_2, :after_around_2, :after_around_1, :after], @callbacks
1251
- end
1252
-
1253
- it 'should_not_run_further_callbacks_if_halted_during_continue_around_transition' do
1254
- @callbacks = []
1255
- @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
1256
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2; throw :halt}
1257
- @machine.after_transition {@callbacks << :after}
1258
-
1259
- assert_equal true, @transition.run_callbacks(:after => false)
1260
- assert_equal [:before_around_1, :before_around_2], @callbacks
1261
-
1262
- assert_equal true, @transition.run_callbacks
1263
- assert_equal [:before_around_1, :before_around_2, :after_around_2], @callbacks
1264
- end
1265
-
1266
- it 'should_not_be_able_to_continue_twice' do
1267
- @count = 0
1268
- @machine.around_transition {|block| block.call; @count += 1}
1269
- @machine.after_transition {@count += 1}
1270
-
1271
- @transition.run_callbacks(:after => false)
1272
-
1273
- 2.times do
1274
- assert_equal true, @transition.run_callbacks
1275
- assert_equal 2, @count
1276
- end
1277
- end
1278
-
1279
- it 'should_not_be_able_to_continue_again_after_halted' do
1280
- @count = 0
1281
- @machine.around_transition {|block| block.call; @count += 1; throw :halt}
1282
- @machine.after_transition {@count += 1}
1283
-
1284
- @transition.run_callbacks(:after => false)
1285
-
1286
- 2.times do
1287
- assert_equal true, @transition.run_callbacks
1288
- assert_equal 1, @count
1289
- end
1290
- end
1291
-
1292
- it 'should_have_access_to_result_after_continued' do
1293
- @machine.around_transition {|block| @around_before_result = @transition.result; block.call; @around_after_result = @transition.result}
1294
- @machine.after_transition {@after_result = @transition.result}
1295
-
1296
- @transition.run_callbacks(:after => false)
1297
- @transition.run_callbacks {{:result => 1}}
1298
-
1299
- assert_nil @around_before_result
1300
- assert_equal 1, @around_after_result
1301
- assert_equal 1, @after_result
1302
- end
1303
-
1304
- it 'should_raise_exceptions_during_around_callbacks_after_yield_in_second_execution' do
1305
- @machine.around_transition {|block| block.call; raise ArgumentError}
1306
-
1307
- assert_nothing_raised { @transition.run_callbacks(:after => false) }
1308
- assert_raise(ArgumentError) { @transition.run_callbacks }
1309
- end
1310
- else
1311
- it 'should_raise_exception_on_second_call' do
1312
- @callbacks = []
1313
- @machine.around_transition {|block| @callbacks << :before_around_1; block.call; @callbacks << :after_around_1}
1314
- @machine.around_transition {|block| @callbacks << :before_around_2; block.call; @callbacks << :after_around_2}
1315
- @machine.after_transition {@callbacks << :after}
1316
-
1317
- assert_raise(ArgumentError) { @transition.run_callbacks(:after => false) }
1318
- end
1319
- end
1320
- end
1321
-
1322
- context 'AfterBeingPerformed' do
1323
- before(:each) do
1324
- @klass = Class.new do
1325
- attr_reader :saved, :save_state
1326
-
1327
- def save
1328
- @save_state = state
1329
- @saved = true
1330
- 1
1331
- end
1332
- end
1333
-
1334
- @machine = StateMachines::Machine.new(@klass, :action => :save)
1335
- @machine.state :parked, :idling
1336
- @machine.event :ignite
1337
-
1338
- @object = @klass.new
1339
- @object.state = 'parked'
1340
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1341
- @result = @transition.perform
1342
- end
1343
-
1344
- it 'should_have_empty_args' do
1345
- assert_equal [], @transition.args
1346
- end
1347
-
1348
- it 'should_have_a_result' do
1349
- assert_equal 1, @transition.result
1350
- end
1351
-
1352
- it 'should_be_successful' do
1353
- assert_equal true, @result
1354
- end
1355
-
1356
- it 'should_change_the_current_state' do
1357
- assert_equal 'idling', @object.state
1358
- end
1359
-
1360
- it 'should_run_the_action' do
1361
- assert @object.saved
1362
- end
1363
-
1364
- it 'should_run_the_action_after_saving_the_state' do
1365
- assert_equal 'idling', @object.save_state
1366
- end
1367
- end
1368
-
1369
- context 'WithPerformArguments' do
1370
- before(:each) do
1371
- @klass = Class.new do
1372
- attr_reader :saved
1373
-
1374
- def save
1375
- @saved = true
1376
- end
1377
- end
1378
-
1379
- @machine = StateMachines::Machine.new(@klass, :action => :save)
1380
- @machine.state :parked, :idling
1381
- @machine.event :ignite
1382
-
1383
- @object = @klass.new
1384
- @object.state = 'parked'
1385
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1386
- end
1387
-
1388
- it 'should_have_arguments' do
1389
- @transition.perform(1, 2)
1390
-
1391
- assert_equal [1, 2], @transition.args
1392
- assert @object.saved
1393
- end
1394
-
1395
- it 'should_not_include_run_action_in_arguments' do
1396
- @transition.perform(1, 2, false)
1397
-
1398
- assert_equal [1, 2], @transition.args
1399
- assert !@object.saved
1400
- end
1401
- end
1402
-
1403
- context 'WithoutRunningAction' do
1404
- before(:each) do
1405
- @klass = Class.new do
1406
- attr_reader :saved
1407
-
1408
- def save
1409
- @saved = true
1410
- end
1411
- end
1412
-
1413
- @machine = StateMachines::Machine.new(@klass, :action => :save)
1414
- @machine.state :parked, :idling
1415
- @machine.event :ignite
1416
- @machine.after_transition {|object| @run_after = true}
1417
-
1418
- @object = @klass.new
1419
- @object.state = 'parked'
1420
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1421
- @result = @transition.perform(false)
1422
- end
1423
-
1424
- it 'should_have_empty_args' do
1425
- assert_equal [], @transition.args
1426
- end
1427
-
1428
- it 'should_not_have_a_result' do
1429
- assert_nil @transition.result
1430
- end
1431
-
1432
- it 'should_be_successful' do
1433
- assert_equal true, @result
1434
- end
1435
-
1436
- it 'should_change_the_current_state' do
1437
- assert_equal 'idling', @object.state
1438
- end
1439
-
1440
- it 'should_not_run_the_action' do
1441
- assert !@object.saved
1442
- end
1443
-
1444
- it 'should_run_after_callbacks' do
1445
- assert @run_after
1446
- end
1447
- end
1448
-
1449
- context 'WithTransactions' do
1450
- before(:each) do
1451
- @klass = Class.new do
1452
- class << self
1453
- attr_accessor :running_transaction
1454
- end
1455
-
1456
- attr_accessor :result
1457
-
1458
- def save
1459
- @result = self.class.running_transaction
1460
- true
1461
- end
1462
- end
1463
-
1464
- @machine = StateMachines::Machine.new(@klass, :action => :save)
1465
- @machine.state :parked, :idling
1466
- @machine.event :ignite
1467
-
1468
- @object = @klass.new
1469
- @object.state = 'parked'
1470
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1471
-
1472
- class << @machine
1473
- def within_transaction(object)
1474
- owner_class.running_transaction = object
1475
- yield
1476
- owner_class.running_transaction = false
1477
- end
1478
- end
1479
- end
1480
-
1481
- it 'should_run_blocks_within_transaction_for_object' do
1482
- @transition.within_transaction do
1483
- @result = @klass.running_transaction
1484
- end
1485
-
1486
- assert_equal @object, @result
1487
- end
1488
- end
1489
-
1490
- context 'Transient' do
1491
- before(:each) do
1492
- @klass = Class.new
1493
-
1494
- @machine = StateMachines::Machine.new(@klass)
1495
- @machine.state :parked, :idling
1496
- @machine.event :ignite
1497
-
1498
- @object = @klass.new
1499
- @object.state = 'parked'
1500
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1501
- @transition.transient = true
1502
- end
1503
-
1504
- it 'should_be_transient' do
1505
- assert @transition.transient?
1506
- end
1507
- end
1508
-
1509
- context 'Equality' do
1510
- before(:each) do
1511
- @klass = Class.new
1512
-
1513
- @machine = StateMachines::Machine.new(@klass)
1514
- @machine.state :parked, :idling
1515
- @machine.event :ignite
1516
-
1517
- @object = @klass.new
1518
- @object.state = 'parked'
1519
- @transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1520
- end
1521
-
1522
- it 'should_be_equal_with_same_properties' do
1523
- transition = StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
1524
- assert_equal transition, @transition
1525
- end
1526
-
1527
- it 'should_not_be_equal_with_different_machines' do
1528
- machine = StateMachines::Machine.new(@klass, :status, :namespace => :other)
1529
- machine.state :parked, :idling
1530
- machine.event :ignite
1531
- transition = StateMachines::Transition.new(@object, machine, :ignite, :parked, :idling)
1532
-
1533
- assert_not_equal transition, @transition
1534
- end
1535
-
1536
- it 'should_not_be_equal_with_different_objects' do
1537
- transition = StateMachines::Transition.new(@klass.new, @machine, :ignite, :parked, :idling)
1538
- assert_not_equal transition, @transition
1539
- end
1540
-
1541
- it 'should_not_be_equal_with_different_event_names' do
1542
- @machine.event :park
1543
- transition = StateMachines::Transition.new(@object, @machine, :park, :parked, :idling)
1544
- assert_not_equal transition, @transition
1545
- end
1546
-
1547
- it 'should_not_be_equal_with_different_from_state_names' do
1548
- @machine.state :first_gear
1549
- transition = StateMachines::Transition.new(@object, @machine, :ignite, :first_gear, :idling)
1550
- assert_not_equal transition, @transition
1551
- end
1552
-
1553
- it 'should_not_be_equal_with_different_to_state_names' do
1554
- @machine.state :first_gear
1555
- transition = StateMachines::Transition.new(@object, @machine, :ignite, :idling, :first_gear)
1556
- assert_not_equal transition, @transition
1557
- end
1558
- end