aasm 5.3.1 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. checksums.yaml +4 -4
  2. data/lib/aasm/version.rb +1 -1
  3. metadata +3 -409
  4. data/.document +0 -6
  5. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -27
  6. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  7. data/.github/workflows/build.yml +0 -102
  8. data/.gitignore +0 -20
  9. data/API +0 -34
  10. data/Appraisals +0 -51
  11. data/CHANGELOG.md +0 -469
  12. data/CODE_OF_CONDUCT.md +0 -13
  13. data/CONTRIBUTING.md +0 -24
  14. data/Dockerfile +0 -44
  15. data/Gemfile +0 -7
  16. data/Gemfile.lock_old +0 -151
  17. data/HOWTO +0 -12
  18. data/PLANNED_CHANGES.md +0 -11
  19. data/README_FROM_VERSION_3_TO_4.md +0 -240
  20. data/Rakefile +0 -31
  21. data/TESTING.md +0 -25
  22. data/aasm.gemspec +0 -37
  23. data/docker-compose.yml +0 -40
  24. data/gemfiles/norails.gemfile +0 -11
  25. data/gemfiles/rails_4.2.gemfile +0 -17
  26. data/gemfiles/rails_4.2_mongoid_5.gemfile +0 -12
  27. data/gemfiles/rails_4.2_nobrainer.gemfile +0 -10
  28. data/gemfiles/rails_5.2.gemfile +0 -14
  29. data/gemfiles/rails_6.0.gemfile +0 -14
  30. data/gemfiles/rails_6.1.gemfile +0 -14
  31. data/gemfiles/rails_7.0.gemfile +0 -14
  32. data/spec/database.rb +0 -57
  33. data/spec/database.yml +0 -3
  34. data/spec/en.yml +0 -9
  35. data/spec/generators/active_record_generator_spec.rb +0 -53
  36. data/spec/generators/mongoid_generator_spec.rb +0 -31
  37. data/spec/generators/no_brainer_generator_spec.rb +0 -29
  38. data/spec/localizer_test_model_deprecated_style.yml +0 -13
  39. data/spec/localizer_test_model_new_style.yml +0 -11
  40. data/spec/models/active_record/active_record_callback.rb +0 -93
  41. data/spec/models/active_record/basic_active_record_two_state_machines_example.rb +0 -25
  42. data/spec/models/active_record/complex_active_record_example.rb +0 -37
  43. data/spec/models/active_record/derivate_new_dsl.rb +0 -7
  44. data/spec/models/active_record/false_state.rb +0 -35
  45. data/spec/models/active_record/gate.rb +0 -39
  46. data/spec/models/active_record/instance_level_skip_validation_example.rb +0 -19
  47. data/spec/models/active_record/invalid_persistor.rb +0 -29
  48. data/spec/models/active_record/localizer_test_model.rb +0 -42
  49. data/spec/models/active_record/namespaced.rb +0 -16
  50. data/spec/models/active_record/no_direct_assignment.rb +0 -21
  51. data/spec/models/active_record/no_scope.rb +0 -21
  52. data/spec/models/active_record/persisted_state.rb +0 -12
  53. data/spec/models/active_record/person.rb +0 -23
  54. data/spec/models/active_record/provided_and_persisted_state.rb +0 -24
  55. data/spec/models/active_record/reader.rb +0 -7
  56. data/spec/models/active_record/readme_job.rb +0 -21
  57. data/spec/models/active_record/silent_persistor.rb +0 -29
  58. data/spec/models/active_record/simple_new_dsl.rb +0 -32
  59. data/spec/models/active_record/thief.rb +0 -29
  60. data/spec/models/active_record/timestamp_example.rb +0 -16
  61. data/spec/models/active_record/transactor.rb +0 -124
  62. data/spec/models/active_record/transient.rb +0 -6
  63. data/spec/models/active_record/validator.rb +0 -118
  64. data/spec/models/active_record/with_enum.rb +0 -39
  65. data/spec/models/active_record/with_enum_without_column.rb +0 -38
  66. data/spec/models/active_record/with_false_enum.rb +0 -31
  67. data/spec/models/active_record/with_true_enum.rb +0 -39
  68. data/spec/models/active_record/work.rb +0 -3
  69. data/spec/models/active_record/worker.rb +0 -2
  70. data/spec/models/active_record/writer.rb +0 -6
  71. data/spec/models/basic_two_state_machines_example.rb +0 -25
  72. data/spec/models/callbacks/basic.rb +0 -98
  73. data/spec/models/callbacks/basic_multiple.rb +0 -75
  74. data/spec/models/callbacks/guard_within_block.rb +0 -67
  75. data/spec/models/callbacks/guard_within_block_multiple.rb +0 -66
  76. data/spec/models/callbacks/multiple_transitions_transition_guard.rb +0 -66
  77. data/spec/models/callbacks/multiple_transitions_transition_guard_multiple.rb +0 -65
  78. data/spec/models/callbacks/private_method.rb +0 -44
  79. data/spec/models/callbacks/private_method_multiple.rb +0 -44
  80. data/spec/models/callbacks/with_args.rb +0 -62
  81. data/spec/models/callbacks/with_args_multiple.rb +0 -61
  82. data/spec/models/callbacks/with_state_arg.rb +0 -34
  83. data/spec/models/callbacks/with_state_arg_multiple.rb +0 -29
  84. data/spec/models/complex_example.rb +0 -222
  85. data/spec/models/conversation.rb +0 -93
  86. data/spec/models/default_state.rb +0 -12
  87. data/spec/models/double_definer.rb +0 -21
  88. data/spec/models/dynamoid/complex_dynamoid_example.rb +0 -37
  89. data/spec/models/dynamoid/dynamoid_multiple.rb +0 -18
  90. data/spec/models/dynamoid/dynamoid_simple.rb +0 -18
  91. data/spec/models/event_with_keyword_arguments.rb +0 -16
  92. data/spec/models/foo.rb +0 -106
  93. data/spec/models/foo_callback_multiple.rb +0 -45
  94. data/spec/models/guard_arguments_check.rb +0 -17
  95. data/spec/models/guard_with_params.rb +0 -24
  96. data/spec/models/guard_with_params_multiple.rb +0 -18
  97. data/spec/models/guardian.rb +0 -58
  98. data/spec/models/guardian_multiple.rb +0 -48
  99. data/spec/models/guardian_without_from_specified.rb +0 -18
  100. data/spec/models/initial_state_proc.rb +0 -31
  101. data/spec/models/mongoid/complex_mongoid_example.rb +0 -37
  102. data/spec/models/mongoid/invalid_persistor_mongoid.rb +0 -39
  103. data/spec/models/mongoid/mongoid_relationships.rb +0 -26
  104. data/spec/models/mongoid/no_scope_mongoid.rb +0 -21
  105. data/spec/models/mongoid/silent_persistor_mongoid.rb +0 -39
  106. data/spec/models/mongoid/simple_mongoid.rb +0 -23
  107. data/spec/models/mongoid/simple_new_dsl_mongoid.rb +0 -25
  108. data/spec/models/mongoid/timestamp_example_mongoid.rb +0 -20
  109. data/spec/models/mongoid/validator_mongoid.rb +0 -100
  110. data/spec/models/multi_transitioner.rb +0 -34
  111. data/spec/models/multiple_transitions_that_differ_only_by_guard.rb +0 -31
  112. data/spec/models/namespaced_multiple_example.rb +0 -42
  113. data/spec/models/no_initial_state.rb +0 -25
  114. data/spec/models/nobrainer/complex_no_brainer_example.rb +0 -36
  115. data/spec/models/nobrainer/invalid_persistor_no_brainer.rb +0 -39
  116. data/spec/models/nobrainer/no_scope_no_brainer.rb +0 -21
  117. data/spec/models/nobrainer/nobrainer_relationships.rb +0 -25
  118. data/spec/models/nobrainer/silent_persistor_no_brainer.rb +0 -39
  119. data/spec/models/nobrainer/simple_new_dsl_nobrainer.rb +0 -25
  120. data/spec/models/nobrainer/simple_no_brainer.rb +0 -23
  121. data/spec/models/nobrainer/validator_no_brainer.rb +0 -98
  122. data/spec/models/not_auto_loaded/process.rb +0 -21
  123. data/spec/models/parametrised_event.rb +0 -42
  124. data/spec/models/parametrised_event_multiple.rb +0 -29
  125. data/spec/models/process_with_new_dsl.rb +0 -31
  126. data/spec/models/provided_state.rb +0 -24
  127. data/spec/models/redis/complex_redis_example.rb +0 -40
  128. data/spec/models/redis/redis_multiple.rb +0 -20
  129. data/spec/models/redis/redis_simple.rb +0 -20
  130. data/spec/models/sequel/complex_sequel_example.rb +0 -46
  131. data/spec/models/sequel/invalid_persistor.rb +0 -52
  132. data/spec/models/sequel/sequel_multiple.rb +0 -25
  133. data/spec/models/sequel/sequel_simple.rb +0 -26
  134. data/spec/models/sequel/silent_persistor.rb +0 -50
  135. data/spec/models/sequel/transactor.rb +0 -112
  136. data/spec/models/sequel/validator.rb +0 -93
  137. data/spec/models/sequel/worker.rb +0 -12
  138. data/spec/models/silencer.rb +0 -27
  139. data/spec/models/simple_custom_example.rb +0 -53
  140. data/spec/models/simple_example.rb +0 -23
  141. data/spec/models/simple_example_with_guard_args.rb +0 -17
  142. data/spec/models/simple_multiple_example.rb +0 -42
  143. data/spec/models/state_machine_with_failed_event.rb +0 -20
  144. data/spec/models/states_on_one_line_example.rb +0 -8
  145. data/spec/models/sub_class.rb +0 -41
  146. data/spec/models/sub_class_with_more_states.rb +0 -18
  147. data/spec/models/sub_classing.rb +0 -3
  148. data/spec/models/super_class.rb +0 -46
  149. data/spec/models/this_name_better_not_be_in_use.rb +0 -11
  150. data/spec/models/timestamps_example.rb +0 -19
  151. data/spec/models/timestamps_with_named_machine_example.rb +0 -13
  152. data/spec/models/valid_state_name.rb +0 -23
  153. data/spec/spec_helper.rb +0 -41
  154. data/spec/spec_helpers/active_record.rb +0 -8
  155. data/spec/spec_helpers/dynamoid.rb +0 -39
  156. data/spec/spec_helpers/mongoid.rb +0 -27
  157. data/spec/spec_helpers/nobrainer.rb +0 -15
  158. data/spec/spec_helpers/redis.rb +0 -18
  159. data/spec/spec_helpers/remove_warnings.rb +0 -1
  160. data/spec/spec_helpers/sequel.rb +0 -7
  161. data/spec/unit/abstract_class_spec.rb +0 -27
  162. data/spec/unit/api_spec.rb +0 -104
  163. data/spec/unit/basic_two_state_machines_example_spec.rb +0 -10
  164. data/spec/unit/callback_multiple_spec.rb +0 -304
  165. data/spec/unit/callbacks_spec.rb +0 -551
  166. data/spec/unit/complex_example_spec.rb +0 -101
  167. data/spec/unit/complex_multiple_example_spec.rb +0 -115
  168. data/spec/unit/edge_cases_spec.rb +0 -16
  169. data/spec/unit/event_multiple_spec.rb +0 -73
  170. data/spec/unit/event_naming_spec.rb +0 -16
  171. data/spec/unit/event_spec.rb +0 -411
  172. data/spec/unit/event_with_keyword_arguments_spec.rb +0 -10
  173. data/spec/unit/exception_spec.rb +0 -11
  174. data/spec/unit/guard_arguments_check_spec.rb +0 -9
  175. data/spec/unit/guard_multiple_spec.rb +0 -60
  176. data/spec/unit/guard_spec.rb +0 -89
  177. data/spec/unit/guard_with_params_multiple_spec.rb +0 -10
  178. data/spec/unit/guard_with_params_spec.rb +0 -14
  179. data/spec/unit/guard_without_from_specified_spec.rb +0 -10
  180. data/spec/unit/initial_state_multiple_spec.rb +0 -15
  181. data/spec/unit/initial_state_spec.rb +0 -12
  182. data/spec/unit/inspection_multiple_spec.rb +0 -205
  183. data/spec/unit/inspection_spec.rb +0 -153
  184. data/spec/unit/invoker_spec.rb +0 -189
  185. data/spec/unit/invokers/base_invoker_spec.rb +0 -72
  186. data/spec/unit/invokers/class_invoker_spec.rb +0 -95
  187. data/spec/unit/invokers/literal_invoker_spec.rb +0 -86
  188. data/spec/unit/invokers/proc_invoker_spec.rb +0 -86
  189. data/spec/unit/localizer_spec.rb +0 -109
  190. data/spec/unit/memory_leak_spec.rb +0 -38
  191. data/spec/unit/multiple_transitions_that_differ_only_by_guard_spec.rb +0 -14
  192. data/spec/unit/namespaced_multiple_example_spec.rb +0 -75
  193. data/spec/unit/new_dsl_spec.rb +0 -12
  194. data/spec/unit/override_warning_spec.rb +0 -94
  195. data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +0 -635
  196. data/spec/unit/persistence/active_record_persistence_spec.rb +0 -852
  197. data/spec/unit/persistence/dynamoid_persistence_multiple_spec.rb +0 -135
  198. data/spec/unit/persistence/dynamoid_persistence_spec.rb +0 -84
  199. data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +0 -200
  200. data/spec/unit/persistence/mongoid_persistence_spec.rb +0 -177
  201. data/spec/unit/persistence/no_brainer_persistence_multiple_spec.rb +0 -198
  202. data/spec/unit/persistence/no_brainer_persistence_spec.rb +0 -158
  203. data/spec/unit/persistence/redis_persistence_multiple_spec.rb +0 -88
  204. data/spec/unit/persistence/redis_persistence_spec.rb +0 -53
  205. data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +0 -148
  206. data/spec/unit/persistence/sequel_persistence_spec.rb +0 -368
  207. data/spec/unit/readme_spec.rb +0 -41
  208. data/spec/unit/reloading_spec.rb +0 -15
  209. data/spec/unit/rspec_matcher_spec.rb +0 -88
  210. data/spec/unit/simple_custom_example_spec.rb +0 -39
  211. data/spec/unit/simple_example_spec.rb +0 -57
  212. data/spec/unit/simple_multiple_example_spec.rb +0 -91
  213. data/spec/unit/state_spec.rb +0 -105
  214. data/spec/unit/states_on_one_line_example_spec.rb +0 -16
  215. data/spec/unit/subclassing_multiple_spec.rb +0 -74
  216. data/spec/unit/subclassing_spec.rb +0 -46
  217. data/spec/unit/timestamps_spec.rb +0 -32
  218. data/spec/unit/transition_spec.rb +0 -436
  219. data/test/minitest_helper.rb +0 -61
  220. data/test/unit/minitest_matcher_test.rb +0 -80
@@ -1,551 +0,0 @@
1
- require 'spec_helper'
2
- Dir[File.dirname(__FILE__) + "/../models/callbacks/*.rb"].sort.each { |f| require File.expand_path(f) }
3
-
4
- shared_examples 'an implemented callback that accepts error' do
5
- context 'with callback defined' do
6
- it "should run error_callback if an exception is raised and always return false" do
7
- aasm_model.class.send(:define_method, callback_name) do |e|
8
- @data = [e]
9
- end
10
-
11
- allow(aasm_model).to receive(:before_enter).and_raise(e = StandardError.new)
12
-
13
- expect(aasm_model.safe_close!).to be false
14
- expect(aasm_model.data).to eql [e]
15
- end
16
-
17
- it "should run error_callback without parameters if callback does not support any" do
18
- aasm_model.class.send(:define_method, callback_name) do |e|
19
- @data = []
20
- end
21
-
22
- allow(aasm_model).to receive(:before_enter).and_raise(e = StandardError.new)
23
-
24
- aasm_model.safe_close!('arg1', 'arg2')
25
- expect(aasm_model.data).to eql []
26
- end
27
-
28
- it "should run error_callback with parameters if callback supports them" do
29
- aasm_model.class.send(:define_method, callback_name) do |e, arg1, arg2|
30
- @data = [arg1, arg2]
31
- end
32
-
33
- allow(aasm_model).to receive(:before_enter).and_raise(e = StandardError.new)
34
-
35
- aasm_model.safe_close!('arg1', 'arg2')
36
- expect(aasm_model.data).to eql ['arg1', 'arg2']
37
- end
38
- end
39
- end
40
-
41
- shared_examples 'an implemented callback' do
42
- context 'with callback defined' do
43
- it 'should run callback without parameters if callback does not support any' do
44
- aasm_model.class.send(:define_method, callback_name) do
45
- @data = ['callback-was-called']
46
- end
47
-
48
- aasm_model.safe_close!
49
- expect(aasm_model.data).to eql ['callback-was-called']
50
- end
51
-
52
- it 'should run callback with parameters if callback supports them' do
53
- aasm_model.class.send(:define_method, callback_name) do |arg1, arg2|
54
- @data = [arg1, arg2]
55
- end
56
-
57
- aasm_model.safe_close!('arg1', 'arg2')
58
- expect(aasm_model.data).to eql ['arg1', 'arg2']
59
- end
60
- end
61
- end
62
-
63
- describe 'callbacks for the new DSL' do
64
-
65
- it "be called in order" do
66
- show_debug_log = false
67
-
68
- callback = Callbacks::Basic.new(:log => show_debug_log)
69
- callback.aasm.current_state
70
-
71
- unless show_debug_log
72
- expect(callback).to receive(:before_all_events).once.ordered
73
- expect(callback).to receive(:before_event).once.ordered
74
- expect(callback).to receive(:event_guard).once.ordered.and_return(true)
75
- expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
76
- expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes
77
- expect(callback).to receive(:exit_open).once.ordered
78
- # expect(callback).to receive(:event_guard).once.ordered.and_return(true)
79
- # expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
80
- expect(callback).to receive(:after_all_transitions).once.ordered
81
- expect(callback).to receive(:after_transition).once.ordered
82
- expect(callback).to receive(:before_enter_closed).once.ordered
83
- expect(callback).to receive(:enter_closed).once.ordered
84
- expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
85
- expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes
86
- expect(callback).to receive(:after_enter_closed).once.ordered
87
- expect(callback).to receive(:after_event).once.ordered
88
- expect(callback).to receive(:after_all_events).once.ordered
89
- expect(callback).to receive(:ensure_event).once.ordered
90
- expect(callback).to receive(:ensure_on_all_events).once.ordered
91
- end
92
-
93
- callback.close!
94
- end
95
-
96
- it "works fine after reload" do
97
- show_debug_log = false
98
-
99
- callback = Callbacks::Basic.new(:log => show_debug_log)
100
- callback.aasm.current_state
101
-
102
- # reload the class
103
- Callbacks.send(:remove_const, :Basic)
104
- load 'models/callbacks/basic.rb'
105
-
106
- unless show_debug_log
107
- expect(callback).to receive(:before_event).once.ordered
108
- expect(callback).to receive(:event_guard).once.ordered.and_return(true)
109
- expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
110
- expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes
111
- expect(callback).to receive(:exit_open).once.ordered
112
- # expect(callback).to receive(:event_guard).once.ordered.and_return(true)
113
- # expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
114
- expect(callback).to receive(:after_all_transitions).once.ordered
115
- expect(callback).to receive(:after_transition).once.ordered
116
- expect(callback).to receive(:before_enter_closed).once.ordered
117
- expect(callback).to receive(:enter_closed).once.ordered
118
- expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
119
- expect(callback).to receive(:event_before_success).once.ordered
120
- expect(callback).to receive(:success_transition).once.ordered.and_return(true) # these should be after the state changes
121
- expect(callback).to receive(:after_exit_open).once.ordered
122
- expect(callback).to receive(:after_enter_closed).once.ordered
123
- expect(callback).to receive(:after_event).once.ordered
124
- end
125
-
126
- callback.close!
127
- end
128
-
129
- it 'does not run callbacks if firing an unknown event' do
130
- show_debug_log = false
131
-
132
- callback = Callbacks::Basic.new(:log => show_debug_log)
133
-
134
- expect(callback).to_not receive(:before_all_events).ordered
135
- expect(callback).to_not receive(:before_event).ordered
136
- expect(callback).to_not receive(:event_guard).ordered
137
- expect(callback).to_not receive(:transition_guard)
138
- expect(callback).to_not receive(:before_exit_open)
139
- expect(callback).to_not receive(:exit_open)
140
- expect(callback).to_not receive(:after_all_transitions)
141
- expect(callback).to_not receive(:after_transition)
142
- expect(callback).to_not receive(:before_enter_closed)
143
- expect(callback).to_not receive(:enter_closed)
144
- expect(callback).to_not receive(:aasm_write_state)
145
- expect(callback).to_not receive(:event_before_success)
146
- expect(callback).to_not receive(:success_transition)
147
- expect(callback).to_not receive(:after_exit_open)
148
- expect(callback).to_not receive(:after_enter_closed)
149
- expect(callback).to_not receive(:after_event)
150
- expect(callback).to_not receive(:after_all_events)
151
- expect(callback).to_not receive(:ensure_event).ordered
152
- expect(callback).to_not receive(:ensure_on_all_events).ordered
153
-
154
- expect {
155
- callback.aasm.fire(:unknown)
156
- }.to raise_error(AASM::UndefinedEvent, "Event :unknown doesn't exist")
157
-
158
- expect {
159
- callback.aasm.fire!(:unknown)
160
- }.to raise_error(AASM::UndefinedEvent, "Event :unknown! doesn't exist")
161
- end
162
-
163
- it "does not run any state callback if the event guard fails" do
164
- callback = Callbacks::Basic.new(:log => false)
165
- callback.aasm.current_state
166
-
167
- expect(callback).to receive(:before_all_events).once.ordered
168
- expect(callback).to receive(:before_event).once.ordered
169
- expect(callback).to receive(:event_guard).once.ordered.and_return(false)
170
- expect(callback).to_not receive(:transition_guard)
171
- expect(callback).to_not receive(:before_exit_open)
172
- expect(callback).to_not receive(:exit_open)
173
- expect(callback).to_not receive(:after_all_transitions)
174
- expect(callback).to_not receive(:after_transition)
175
- expect(callback).to_not receive(:before_enter_closed)
176
- expect(callback).to_not receive(:enter_closed)
177
- expect(callback).to_not receive(:aasm_write_state)
178
- expect(callback).to_not receive(:event_before_success)
179
- expect(callback).to_not receive(:success_transition)
180
- expect(callback).to_not receive(:after_exit_open)
181
- expect(callback).to_not receive(:after_enter_closed)
182
- expect(callback).to_not receive(:after_event)
183
- expect(callback).to_not receive(:after_all_events)
184
- expect(callback).to receive(:ensure_event).once.ordered
185
- expect(callback).to receive(:ensure_on_all_events).once.ordered
186
-
187
- expect {
188
- callback.close!
189
- }.to raise_error(AASM::InvalidTransition)
190
- end
191
-
192
- it "handles private callback methods as well" do
193
- show_debug_log = false
194
-
195
- callback = Callbacks::PrivateMethod.new(:log => show_debug_log)
196
- callback.aasm.current_state
197
-
198
- expect {
199
- callback.close!
200
- }.to_not raise_error
201
- end
202
-
203
- context "if the transition guard fails" do
204
- it "does not run any state callback if guard is defined inline" do
205
- show_debug_log = false
206
- callback = Callbacks::Basic.new(:log => show_debug_log, :fail_transition_guard => true)
207
- callback.aasm.current_state
208
-
209
- unless show_debug_log
210
- expect(callback).to receive(:before_all_events).once.ordered
211
- expect(callback).to receive(:before_event).once.ordered
212
- expect(callback).to receive(:event_guard).once.ordered.and_return(true)
213
- expect(callback).to receive(:transition_guard).once.ordered.and_return(false)
214
- expect(callback).to_not receive(:before_exit_open)
215
- expect(callback).to_not receive(:exit_open)
216
- expect(callback).to_not receive(:after_all_transitions)
217
- expect(callback).to_not receive(:after_transition)
218
- expect(callback).to_not receive(:before_enter_closed)
219
- expect(callback).to_not receive(:enter_closed)
220
- expect(callback).to_not receive(:aasm_write_state)
221
- expect(callback).to_not receive(:event_before_success)
222
- expect(callback).to_not receive(:success_transition)
223
- expect(callback).to_not receive(:after_exit_open)
224
- expect(callback).to_not receive(:after_enter_closed)
225
- expect(callback).to_not receive(:after_event)
226
- expect(callback).to_not receive(:after_all_events)
227
- expect(callback).to receive(:ensure_event).once.ordered
228
- expect(callback).to receive(:ensure_on_all_events).once.ordered
229
- end
230
-
231
- expect {
232
- callback.close!
233
- }.to raise_error(AASM::InvalidTransition)
234
- end
235
-
236
- it "does not propagate failures to next attempt of same transition" do
237
- callback = Callbacks::Basic.new(:log => false, :fail_transition_guard => true)
238
-
239
- expect {
240
- callback.close!
241
- }.to raise_error(AASM::InvalidTransition, "Event 'close' cannot transition from 'open'. Failed callback(s): [:transition_guard].")
242
-
243
- expect {
244
- callback.close!
245
- }.to raise_error(AASM::InvalidTransition, "Event 'close' cannot transition from 'open'. Failed callback(s): [:transition_guard].")
246
- end
247
-
248
- it "does not propagate failures to next attempt of same event when no transition is applicable" do
249
- callback = Callbacks::Basic.new(:log => false, :fail_transition_guard => true)
250
-
251
- expect {
252
- callback.close!
253
- }.to raise_error(AASM::InvalidTransition, "Event 'close' cannot transition from 'open'. Failed callback(s): [:transition_guard].")
254
-
255
- callback.aasm.current_state = :closed
256
-
257
- expect {
258
- callback.close!
259
- }.to raise_error(AASM::InvalidTransition, "Event 'close' cannot transition from 'closed'.")
260
- end
261
-
262
- it "does not run transition_guard twice for multiple permitted transitions" do
263
- show_debug_log = false
264
- callback = Callbacks::MultipleTransitionsTransitionGuard.new(:log => show_debug_log, :fail_transition_guard => true)
265
- callback.aasm.current_state
266
-
267
- unless show_debug_log
268
- expect(callback).to receive(:before).once.ordered
269
- expect(callback).to receive(:event_guard).once.ordered.and_return(true)
270
- expect(callback).to receive(:transition_guard).once.ordered.and_return(false)
271
- expect(callback).to receive(:event_guard).once.ordered.and_return(true)
272
- expect(callback).to receive(:before_exit_open).once.ordered
273
- expect(callback).to receive(:exit_open).once.ordered
274
- expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
275
- expect(callback).to receive(:after_exit_open).once.ordered
276
- expect(callback).to receive(:after).once.ordered
277
-
278
- expect(callback).to_not receive(:transitioning)
279
- expect(callback).to_not receive(:event_before_success)
280
- expect(callback).to_not receive(:success_transition)
281
- expect(callback).to_not receive(:before_enter_closed)
282
- expect(callback).to_not receive(:enter_closed)
283
- expect(callback).to_not receive(:after_enter_closed)
284
- end
285
-
286
- callback.close!
287
- expect(callback.aasm.current_state).to eql :failed
288
- end
289
-
290
- it "does not run any state callback if guard is defined with block" do
291
- callback = Callbacks::GuardWithinBlock.new #(:log => true, :fail_transition_guard => true)
292
- callback.aasm.current_state
293
-
294
- expect(callback).to receive(:before).once.ordered
295
- expect(callback).to receive(:event_guard).once.ordered.and_return(true)
296
- expect(callback).to receive(:transition_guard).once.ordered.and_return(false)
297
- expect(callback).to_not receive(:before_exit_open)
298
- expect(callback).to_not receive(:exit_open)
299
- expect(callback).to_not receive(:transitioning)
300
- expect(callback).to_not receive(:before_enter_closed)
301
- expect(callback).to_not receive(:enter_closed)
302
- expect(callback).to_not receive(:aasm_write_state)
303
- expect(callback).to_not receive(:event_before_success)
304
- expect(callback).to_not receive(:success_transition)
305
- expect(callback).to_not receive(:after_exit_open)
306
- expect(callback).to_not receive(:after_enter_closed)
307
- expect(callback).to_not receive(:after)
308
-
309
- expect {
310
- callback.close!
311
- }.to raise_error(AASM::InvalidTransition)
312
- end
313
- end
314
-
315
- it "should properly pass arguments" do
316
- cb = Callbacks::WithArgs.new(:log => false)
317
- cb.aasm.current_state
318
-
319
- cb.reset_data
320
- cb.close!(:arg1, :arg2)
321
- expect(cb.data).to eql 'before(:arg1,:arg2) before_exit_open(:arg1,:arg2) transition_proc(:arg1,:arg2) before_enter_closed(:arg1,:arg2) aasm_write_state transition_success(:arg1,:arg2) after_exit_open(:arg1,:arg2) after_enter_closed(:arg1,:arg2) after(:arg1,:arg2)'
322
- end
323
-
324
- it "should call the callbacks given the to-state as argument" do
325
- cb = Callbacks::WithStateArg.new
326
- expect(cb).to receive(:before_method).with(:arg1).once.ordered
327
- expect(cb).to receive(:transition_method).never
328
- expect(cb).to receive(:success_method).never
329
- expect(cb).to receive(:transition_method2).with(:arg1).once.ordered
330
- expect(cb).to receive(:success_method2).with(:arg1).once.ordered
331
- expect(cb).to receive(:after_method).with(:arg1).once.ordered
332
- cb.close!(:out_to_lunch, :arg1)
333
-
334
- cb = Callbacks::WithStateArg.new
335
- some_object = double('some object')
336
- expect(cb).to receive(:before_method).with(some_object).once.ordered
337
- expect(cb).to receive(:transition_method2).with(some_object).once.ordered
338
- expect(cb).to receive(:success_method2).with(some_object).once.ordered
339
- expect(cb).to receive(:after_method).with(some_object).once.ordered
340
- cb.close!(:out_to_lunch, some_object)
341
- end
342
-
343
- it "should call the proper methods just with arguments" do
344
- cb = Callbacks::WithStateArg.new
345
- expect(cb).to receive(:before_method).with(:arg1).once.ordered
346
- expect(cb).to receive(:transition_method).with(:arg1).once.ordered
347
- expect(cb).to receive(:transition_method).never
348
- expect(cb).to receive(:before_success_method).with(:arg1).once.ordered
349
- expect(cb).to receive(:success_method).with(:arg1).once.ordered
350
- expect(cb).to receive(:success_method3).with(:arg1).once.ordered
351
- expect(cb).to receive(:success_method).never
352
- expect(cb).to receive(:after_method).with(:arg1).once.ordered
353
- cb.close!(:arg1)
354
-
355
- cb = Callbacks::WithStateArg.new
356
- some_object = double('some object')
357
- expect(cb).to receive(:before_method).with(some_object).once.ordered
358
- expect(cb).to receive(:transition_method).with(some_object).once.ordered
359
- expect(cb).to receive(:transition_method).never
360
- expect(cb).to receive(:before_success_method).with(some_object).once.ordered
361
- expect(cb).to receive(:success_method).with(some_object).once.ordered
362
- expect(cb).to receive(:success_method3).with(some_object).once.ordered
363
- expect(cb).to receive(:success_method).never
364
- expect(cb).to receive(:after_method).with(some_object).once.ordered
365
- cb.close!(some_object)
366
- end
367
- end
368
-
369
- describe 'event callbacks' do
370
- describe "with an error callback defined" do
371
- before do
372
- class Foo
373
- # this hack is needed to allow testing of parameters, since RSpec
374
- # destroys a method's arity when mocked
375
- attr_accessor :data
376
-
377
- aasm do
378
- event :safe_close, :success => :success_callback, :error => :error_callback do
379
- transitions :to => :closed, :from => [:open], :success => :transition_success_callback
380
- end
381
- end
382
- end
383
-
384
- @foo = Foo.new
385
- end
386
-
387
- it_behaves_like 'an implemented callback that accepts error' do
388
- let(:aasm_model) { @foo }
389
- let(:callback_name) { :error_callback }
390
- end
391
-
392
- it "should raise NoMethodError if exception is raised and error_callback is declared but not defined" do
393
- allow(@foo).to receive(:before_enter).and_raise(StandardError)
394
- expect{@foo.safe_close!}.to raise_error(NoMethodError)
395
- end
396
-
397
- it "should propagate an error if no error callback is declared" do
398
- allow(@foo).to receive(:before_enter).and_raise("Cannot enter safe")
399
- expect{@foo.close!}.to raise_error(StandardError, "Cannot enter safe")
400
- end
401
- end
402
-
403
- describe 'with an ensure callback defined' do
404
- before do
405
- class Foo
406
- # this hack is needed to allow testing of parameters, since RSpec
407
- # destroys a method's arity when mocked
408
- attr_accessor :data
409
-
410
- aasm do
411
- event :safe_close, :success => :success_callback, :ensure => :ensure_callback do
412
- transitions :to => :closed, :from => [:open]
413
- end
414
- end
415
- end
416
-
417
- @foo = Foo.new
418
- end
419
-
420
- it_behaves_like 'an implemented callback' do
421
- let(:aasm_model) { @foo }
422
- let(:callback_name) { :ensure_callback }
423
- end
424
-
425
- it "should raise NoMethodError if ensure_callback is declared but not defined" do
426
- expect{@foo.safe_close!}.to raise_error(NoMethodError)
427
- end
428
-
429
- it "should not raise any error if no ensure_callback is declared" do
430
- expect{@foo.close!}.to_not raise_error
431
- end
432
- end
433
-
434
- describe "with aasm_event_fired defined" do
435
- before do
436
- @foo = Foo.new
437
- def @foo.aasm_event_fired(event, from, to); end
438
- end
439
-
440
- it 'should call it for successful bang fire' do
441
- expect(@foo).to receive(:aasm_event_fired).with(:close, :open, :closed)
442
- @foo.close!
443
- end
444
-
445
- it 'should call it for successful non-bang fire' do
446
- expect(@foo).to receive(:aasm_event_fired)
447
- @foo.close
448
- end
449
-
450
- it 'should not call it for failing bang fire' do
451
- allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
452
- expect(@foo).not_to receive(:aasm_event_fired)
453
- @foo.close!
454
- end
455
- end
456
-
457
- describe "with aasm_event_failed defined" do
458
- before do
459
- @foo = Foo.new
460
- def @foo.aasm_event_failed(event, from); end
461
- end
462
-
463
- it 'should call it when transition failed for bang fire' do
464
- expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
465
- expect {@foo.null!}.to raise_error(AASM::InvalidTransition)
466
- end
467
-
468
- it 'should call it when transition failed for non-bang fire' do
469
- expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
470
- expect {@foo.null}.to raise_error(AASM::InvalidTransition)
471
- end
472
-
473
- it 'should not call it if persist fails for bang fire' do
474
- allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
475
- expect(@foo).to receive(:aasm_event_failed)
476
- @foo.close!
477
- end
478
- end
479
- end
480
-
481
- describe 'global error_on_all_events_callback callbacks' do
482
- describe "with an error_on_all_events" do
483
- before do
484
- class FooGlobal
485
- # this hack is needed to allow testing of parameters, since RSpec
486
- # destroys a method's arity when mocked
487
- attr_accessor :data
488
-
489
- aasm do
490
- error_on_all_events :error_on_all_events_callback
491
-
492
- event :safe_close do
493
- transitions :to => :closed, :from => [:open]
494
- end
495
- end
496
- end
497
-
498
- @foo = FooGlobal.new
499
- end
500
-
501
- it_behaves_like 'an implemented callback that accepts error' do
502
- let(:aasm_model) { @foo }
503
- let(:callback_name) { :error_on_all_events_callback }
504
- end
505
-
506
- it "should raise NoMethodError if exception is raised and error_callback is declared but not defined" do
507
- allow(@foo).to receive(:before_enter).and_raise(StandardError)
508
- expect{@foo.safe_close!}.to raise_error(NoMethodError)
509
- end
510
-
511
- it "should raise NoMethodError if no error callback is declared" do
512
- allow(@foo).to receive(:before_enter).and_raise("Cannot enter safe")
513
- expect{@foo.close!}.to raise_error(NoMethodError)
514
- end
515
- end
516
- end
517
-
518
- describe 'global ensure_on_all_events_callback callbacks' do
519
- describe "with an ensure_on_all_events" do
520
- before do
521
- class FooGlobal
522
- # this hack is needed to allow testing of parameters, since RSpec
523
- # destroys a method's arity when mocked
524
- attr_accessor :data
525
-
526
- aasm do
527
- ensure_on_all_events :ensure_on_all_events_callback
528
-
529
- event :safe_close do
530
- transitions :to => :closed, :from => [:open]
531
- end
532
- end
533
- end
534
-
535
- @foo = FooGlobal.new
536
- end
537
-
538
- it_behaves_like 'an implemented callback' do
539
- let(:aasm_model) { @foo }
540
- let(:callback_name) { :ensure_on_all_events_callback }
541
- end
542
-
543
- it "should raise NoMethodError if ensure_on_all_events callback is declared but not defined" do
544
- expect{@foo.safe_close!}.to raise_error(NoMethodError)
545
- end
546
-
547
- it "should raise NoMethodError if no ensure_on_all_events callback is declared" do
548
- expect{@foo.close!}.to raise_error(NoMethodError)
549
- end
550
- end
551
- end
@@ -1,101 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'on initialization' do
4
- let(:auth) {ComplexExample.new}
5
-
6
- it 'should be in the pending state' do
7
- expect(auth.aasm.current_state).to eq(:pending)
8
- end
9
-
10
- it 'should have an activation code' do
11
- expect(auth.has_activation_code?).to be_truthy
12
- expect(auth.activation_code).not_to be_nil
13
- end
14
- end
15
-
16
- describe 'when being unsuspended' do
17
- let(:auth) {ComplexExample.new}
18
-
19
- it 'should be able to be unsuspended' do
20
- auth.activate!
21
- auth.suspend!
22
- expect(auth.may_unsuspend?).to be true
23
- end
24
-
25
- it 'should not be able to be unsuspended into active' do
26
- auth.suspend!
27
- expect(auth.may_unsuspend?(:active)).not_to be true
28
- end
29
-
30
- it 'should be able to be unsuspended into active if polite' do
31
- auth.suspend!
32
- expect(auth.may_wait?(:waiting, :please)).to be true
33
- auth.wait!(:please)
34
- end
35
-
36
- it 'should not be able to be unsuspended into active if not polite' do
37
- auth.suspend!
38
- expect(auth.may_wait?(:waiting)).not_to be true
39
- expect(auth.may_wait?(:waiting, :rude)).not_to be true
40
- expect {auth.wait!(:rude)}.to raise_error(AASM::InvalidTransition)
41
- expect {auth.wait!}.to raise_error(AASM::InvalidTransition)
42
- end
43
-
44
- it 'should not be able to be unpassified' do
45
- auth.activate!
46
- auth.suspend!
47
- auth.unsuspend!
48
-
49
- expect(auth.may_unpassify?).not_to be true
50
- expect {auth.unpassify!}.to raise_error(AASM::InvalidTransition)
51
- end
52
-
53
- it 'should be active if previously activated' do
54
- auth.activate!
55
- auth.suspend!
56
- auth.unsuspend!
57
-
58
- expect(auth.aasm.current_state).to eq(:active)
59
- end
60
-
61
- it 'should be pending if not previously activated, but an activation code is present' do
62
- auth.suspend!
63
- auth.unsuspend!
64
-
65
- expect(auth.aasm.current_state).to eq(:pending)
66
- end
67
-
68
- it 'should be passive if not previously activated and there is no activation code' do
69
- auth.activation_code = nil
70
- auth.suspend!
71
- auth.unsuspend!
72
-
73
- expect(auth.aasm.current_state).to eq(:passive)
74
- end
75
-
76
- it "should be able to fire known events" do
77
- expect(auth.aasm.may_fire_event?(:activate)).to be true
78
- end
79
-
80
- it "should be able to fire event by name" do
81
- expect(auth.aasm.fire(:activate)).to be true
82
- expect(auth.aasm.current_state).to eq(:active)
83
- end
84
-
85
- it "should be able to fire! event by name" do
86
- expect(auth.aasm.fire!(:activate)).to be true
87
- expect(auth.aasm.current_state).to eq(:active)
88
- end
89
-
90
- it "should not be able to fire unknown events" do
91
- expect(auth.aasm.may_fire_event?(:unknown)).to be false
92
- end
93
-
94
- it "should raise AASM::UndefinedState when firing unknown events" do
95
- expect { auth.aasm.fire(:unknown) }.to raise_error(AASM::UndefinedEvent, "Event :unknown doesn't exist")
96
- end
97
-
98
- it "should raise AASM::UndefinedState when firing unknown bang events" do
99
- expect { auth.aasm.fire!(:unknown) }.to raise_error(AASM::UndefinedEvent, "Event :unknown! doesn't exist")
100
- end
101
- end