aasm 4.4.1 → 4.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 160b57e7537df5bf2c7674413fc7f6161fcabd97
4
- data.tar.gz: d93ae11e0573b50630b3d51f4f8a288f604b7f71
3
+ metadata.gz: 92b6a0df28245debe06c0ecbd3bd95e91b0cf323
4
+ data.tar.gz: 99bfa6652b17a26388b073ee431a1878ce961a4e
5
5
  SHA512:
6
- metadata.gz: 7883c3bdee6cfd956892d7f28df277a018b7ca87818f43162a61d6d977d7c52569aa212c7fbc48e1063822b1e0d7190536fcedf29fd02fa4fd400abbd0046bf1
7
- data.tar.gz: 04402315cb576772b5fb42e704a65d1717c583ce7d690597cb1c311af27a9ac7098be39a3c3acfa524ef1b3002b7cc74cf030709e85523bb4f5ca77efeb4eb6e
6
+ metadata.gz: d6cf5a7eea74d1c20108d48370d0f6048722efefb6af0110c70de56dd13032bcaabf57749add95ae25dc086faab8cbb5dcf9ad08ad4cafbafdfb400481ce1fd4
7
+ data.tar.gz: 90a92ba66f2b4d67c898a9076f324e0c99ce768aa5160f6d70dc74bca48e8ba4cf95317bf5e203b66985e9dcd77a52fb814dc65cc45f498dfeab20fde31fecb8
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 4.5.0
4
+
5
+ * add RSpec matchers `have_state`, `allow_event` and `allow_transition_to` (see [issue #147](https://github.com/aasm/aasm/issues/147) for details)
6
+ * add RSpec matcher `transition_from` (see [issue #178](https://github.com/aasm/aasm/issues/178) for details, thanks to [@thomasstephane](https://github.com/thomasstephane))
7
+
3
8
  ## 4.4.1
4
9
 
5
10
  * add support for rejecting certain events on inspection (see [issue #272](https://github.com/aasm/aasm/issues/272) for details, thanks to [@dubroe](https://github.com/dubroe))
data/README.md CHANGED
@@ -721,6 +721,41 @@ Job.aasm.states_for_select
721
721
  ```
722
722
 
723
723
 
724
+ ### Testing
725
+
726
+ AASM provides some matchers for [RSpec](http://rspec.info): `transition_from`, `have_state`, `allow_event` and `allow_transition_to`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this
727
+
728
+ ```ruby
729
+ # classes with only the default state machine
730
+ job = Job.new
731
+ expect(job).to transition_from(:sleeping).to(:running).on_event(:run)
732
+ expect(job).not_to transition_from(:sleeping).to(:cleaning).on_event(:run)
733
+ expect(job).to have_state(:sleeping)
734
+ expect(job).not_to have_state(:running)
735
+ expect(job).to allow_event :run
736
+ expect(job).to_not allow_event :clean
737
+ expect(job).to allow_transition_to(:running)
738
+ expect(job).to_not allow_transition_to(:cleaning)
739
+
740
+ # classes with multiple state machine
741
+ multiple = SimpleMultipleExample.new
742
+ expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:move)
743
+ expect(multiple).to_not transition_from(:standing).to(:running).on_event(:walk).on(:move)
744
+ expect(multiple).to have_state(:standing).on(:move)
745
+ expect(multiple).not_to have_state(:walking).on(:move)
746
+ expect(multiple).to allow_event(:walk).on(:move)
747
+ expect(multiple).to_not allow_event(:hold).on(:move)
748
+ expect(multiple).to allow_transition_to(:walking).on(:move)
749
+ expect(multiple).to_not allow_transition_to(:running).on(:move)
750
+ expect(multiple).to transition_from(:sleeping).to(:processing).on_event(:start).on(:work)
751
+ expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
752
+ expect(multiple).to have_state(:sleeping).on(:work)
753
+ expect(multiple).not_to have_state(:processing).on(:work)
754
+ expect(multiple).to allow_event(:start).on(:move)
755
+ expect(multiple).to_not allow_event(:stop).on(:move)
756
+ expect(multiple).to allow_transition_to(:processing).on(:move)
757
+ expect(multiple).to_not allow_transition_to(:sleeping).on(:move)
758
+ ```
724
759
 
725
760
  ## <a id="installation">Installation ##
726
761
 
@@ -0,0 +1,5 @@
1
+ # relative-require all rspec files
2
+ Dir[File.dirname(__FILE__) + '/rspec/*.rb'].each do |file|
3
+ require 'aasm/rspec/' + File.basename(file, File.extname(file))
4
+ end
5
+
@@ -0,0 +1,22 @@
1
+ RSpec::Matchers.define :allow_event do |event|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).may_fire_event?(event)
5
+ end
6
+
7
+ chain :on do |state_machine_name|
8
+ @state_machine_name = state_machine_name
9
+ end
10
+
11
+ description do
12
+ "allow event #{expected} (on :#{@state_machine_name})"
13
+ end
14
+
15
+ failure_message do |obj|
16
+ "expected that the event :#{expected} would be allowed (on :#{@state_machine_name})"
17
+ end
18
+
19
+ failure_message_when_negated do |obj|
20
+ "expected that the event :#{expected} would not be allowed (on :#{@state_machine_name})"
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ RSpec::Matchers.define :allow_transition_to do |state|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).states(:permitted => true).include?(state)
5
+ end
6
+
7
+ chain :on do |state_machine_name|
8
+ @state_machine_name = state_machine_name
9
+ end
10
+
11
+ description do
12
+ "allow transition to #{expected} (on :#{@state_machine_name})"
13
+ end
14
+
15
+ failure_message do |obj|
16
+ "expected that the state :#{expected} would be reachable (on :#{@state_machine_name})"
17
+ end
18
+
19
+ failure_message_when_negated do |obj|
20
+ "expected that the state :#{expected} would not be reachable (on :#{@state_machine_name})"
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ RSpec::Matchers.define :have_state do |state|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).current_state == state.to_sym
5
+ end
6
+
7
+ chain :on do |state_machine_name|
8
+ @state_machine_name = state_machine_name
9
+ end
10
+
11
+ description do
12
+ "have state #{expected} (on :#{@state_machine_name})"
13
+ end
14
+
15
+ failure_message do |obj|
16
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would be :#{expected} (on :#{@state_machine_name})"
17
+ end
18
+
19
+ failure_message_when_negated do |obj|
20
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would not be :#{expected} (on :#{@state_machine_name})"
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ RSpec::Matchers.define :transition_from do |from_state|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).current_state = from_state.to_sym
5
+ # expect(obj).to receive(:broadcast_event).with(@event.to_s, obj, from_state, @to_state)
6
+ obj.send(@event) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
7
+ end
8
+
9
+ chain :on do |state_machine_name|
10
+ @state_machine_name = state_machine_name
11
+ end
12
+
13
+ chain :to do |state|
14
+ @to_state = state
15
+ end
16
+
17
+ chain :on_event do |event|
18
+ @event = event
19
+ end
20
+
21
+ description do
22
+ "transition state to :#{@to_state} from :#{expected} on event :#{@event} (on :#{@state_machine_name})"
23
+ end
24
+
25
+ failure_message do |obj|
26
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would be :#{@to_state} (on :#{@state_machine_name})"
27
+ end
28
+
29
+ failure_message_when_negated do |obj|
30
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would not be :#{@to_state} (on :#{@state_machine_name})"
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "4.4.1"
2
+ VERSION = "4.5.0"
3
3
  end
@@ -2,6 +2,7 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
2
2
  $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
3
  require 'aasm'
4
4
  require 'rspec'
5
+ require 'aasm/rspec'
5
6
 
6
7
  # require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying'
7
8
  # require 'ruby-debug/completion'
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'state machine' do
4
+ let(:simple) { SimpleExample.new }
5
+ let(:multiple) { SimpleMultipleExample.new }
6
+
7
+ describe 'transition_from' do
8
+ it "works for simple state machines" do
9
+ expect(simple).to transition_from(:initialised).to(:filled_out).on_event(:fill_out)
10
+ expect(simple).to_not transition_from(:initialised).to(:authorised).on_event(:fill_out)
11
+ end
12
+
13
+ it "works for multiple state machines" do
14
+ expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:move)
15
+ expect(multiple).to_not transition_from(:standing).to(:running).on_event(:walk).on(:move)
16
+
17
+ expect(multiple).to transition_from(:sleeping).to(:processing).on_event(:start).on(:work)
18
+ expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
19
+ end
20
+ end
21
+
22
+ describe 'allow_transition_to' do
23
+ it "works for simple state machines" do
24
+ expect(simple).to allow_transition_to(:filled_out)
25
+ expect(simple).to_not allow_transition_to(:authorised)
26
+ end
27
+
28
+ it "works for multiple state machines" do
29
+ expect(multiple).to allow_transition_to(:walking).on(:move)
30
+ expect(multiple).to_not allow_transition_to(:standing).on(:move)
31
+
32
+ expect(multiple).to allow_transition_to(:processing).on(:work)
33
+ expect(multiple).to_not allow_transition_to(:sleeping).on(:work)
34
+ end
35
+ end
36
+
37
+ describe "have_state" do
38
+ it "works for simple state machines" do
39
+ expect(simple).to have_state :initialised
40
+ expect(simple).to_not have_state :filled_out
41
+ simple.fill_out
42
+ expect(simple).to have_state :filled_out
43
+ end
44
+
45
+ it "works for multiple state machines" do
46
+ expect(multiple).to have_state(:standing).on(:move)
47
+ expect(multiple).to_not have_state(:walking).on(:move)
48
+ multiple.walk
49
+ expect(multiple).to have_state(:walking).on(:move)
50
+
51
+ expect(multiple).to have_state(:sleeping).on(:work)
52
+ expect(multiple).to_not have_state(:processing).on(:work)
53
+ multiple.start
54
+ expect(multiple).to have_state(:processing).on(:work)
55
+ end
56
+ end
57
+
58
+ describe "allow_event" do
59
+ it "works for simple state machines" do
60
+ expect(simple).to allow_event :fill_out
61
+ expect(simple).to_not allow_event :authorise
62
+ simple.fill_out
63
+ expect(simple).to allow_event :authorise
64
+ end
65
+
66
+ it "works for multiple state machines" do
67
+ expect(multiple).to allow_event(:walk).on(:move)
68
+ expect(multiple).to_not allow_event(:hold).on(:move)
69
+ multiple.walk
70
+ expect(multiple).to allow_event(:hold).on(:move)
71
+
72
+ expect(multiple).to allow_event(:start).on(:work)
73
+ expect(multiple).to_not allow_event(:stop).on(:work)
74
+ multiple.start
75
+ expect(multiple).to allow_event(:stop).on(:work)
76
+ end
77
+ end
78
+
79
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.1
4
+ version: 4.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-10-29 00:00:00.000000000 Z
13
+ date: 2015-11-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -116,6 +116,11 @@ files:
116
116
  - lib/aasm/persistence/mongoid_persistence.rb
117
117
  - lib/aasm/persistence/plain_persistence.rb
118
118
  - lib/aasm/persistence/sequel_persistence.rb
119
+ - lib/aasm/rspec.rb
120
+ - lib/aasm/rspec/allow_event.rb
121
+ - lib/aasm/rspec/allow_transition_to.rb
122
+ - lib/aasm/rspec/have_state.rb
123
+ - lib/aasm/rspec/transition_from.rb
119
124
  - lib/aasm/state_machine.rb
120
125
  - lib/aasm/version.rb
121
126
  - spec/database.rb
@@ -224,6 +229,7 @@ files:
224
229
  - spec/unit/persistence/sequel_persistence_spec.rb
225
230
  - spec/unit/readme_spec.rb
226
231
  - spec/unit/reloading_spec.rb
232
+ - spec/unit/rspec_matcher_spec.rb
227
233
  - spec/unit/simple_example_spec.rb
228
234
  - spec/unit/simple_multiple_example_spec.rb
229
235
  - spec/unit/state_spec.rb
@@ -250,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
250
256
  version: '0'
251
257
  requirements: []
252
258
  rubyforge_project:
253
- rubygems_version: 2.4.5
259
+ rubygems_version: 2.2.2
254
260
  signing_key:
255
261
  specification_version: 4
256
262
  summary: State machine mixin for Ruby objects
@@ -361,6 +367,7 @@ test_files:
361
367
  - spec/unit/persistence/sequel_persistence_spec.rb
362
368
  - spec/unit/readme_spec.rb
363
369
  - spec/unit/reloading_spec.rb
370
+ - spec/unit/rspec_matcher_spec.rb
364
371
  - spec/unit/simple_example_spec.rb
365
372
  - spec/unit/simple_multiple_example_spec.rb
366
373
  - spec/unit/state_spec.rb