end_state 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/examples/machine_spec.rb +28 -0
- data/lib/end_state/version.rb +1 -1
- data/lib/end_state_matchers.rb +87 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 809272ea728fbfd921f3cf13974883104447861e
|
4
|
+
data.tar.gz: 9660e13251dccad1d37fa22c5e5bfdf7cbe917ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f452a250ae2e681e53b79902312903ab8a98ad19c2cf37d72748af0208f6405170cb8b1470c96b6ce7e0e6ef84bfd04f424af9d5bea60633a789758c0c132a0
|
7
|
+
data.tar.gz: 3103761a9adc01570e0d4788a961dc594d72b706eacc34f99a69d15e1a5df52c4a3aa64ba3035140840a63a60de3dd42941990669152f667ab80a8409790fba9
|
data/README.md
CHANGED
@@ -193,6 +193,29 @@ transition to the new state.
|
|
193
193
|
You also have the option to use `transition!` which will instead raise an error for failures. If your guards and/or finalizers
|
194
194
|
add to the `failure_messages` array then they will be included in the error message.
|
195
195
|
|
196
|
+
## Testing
|
197
|
+
|
198
|
+
Included is a custom RSpec matcher for testing your machines.
|
199
|
+
|
200
|
+
In your `spec_helper.rb` add:
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
require 'end_state_matchers'
|
204
|
+
```
|
205
|
+
|
206
|
+
In the spec for your state machine:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
describe Machine do
|
210
|
+
specify { expect(Machine).to have_transition(a: :b).with_guard(MyGuard) }
|
211
|
+
specify { expect(Machine).to have_transition(a: :b).with_finalizer(MyFinalizer) }
|
212
|
+
specify { expect(Machine).to have_transition(a: :b).with_guard(MyGuard).with_finalizer(MyFinalizer) }
|
213
|
+
specify { expect(Machine).to have_transition(a: :b).with_guards(MyGuard, AnotherGuard) }
|
214
|
+
specify { expect(Machine).to have_transition(a: :b).with_finalizers(MyFinalizer, AnotherFinalizer) }
|
215
|
+
specify { expect(Machine).not_to have_transition(a: :c) }
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
196
219
|
## Contributing
|
197
220
|
|
198
221
|
1. Fork it ( https://github.com/Originate/end_state/fork )
|
@@ -0,0 +1,28 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'end_state'
|
3
|
+
require 'end_state_matchers'
|
4
|
+
|
5
|
+
class Easy < EndState::Guard
|
6
|
+
def will_allow?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class NoOp < EndState::Finalizer
|
12
|
+
def call
|
13
|
+
true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Machine < EndState::StateMachine
|
18
|
+
transition a: :b do |t|
|
19
|
+
t.guard Easy
|
20
|
+
t.finalizer NoOp
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe Machine do
|
25
|
+
specify { expect(Machine).to have_transition(a: :b).with_guard(Easy).with_finalizer(NoOp) }
|
26
|
+
specify { expect(Machine).to have_transition(a: :b).with_guards(Easy, Easy).with_finalizers(NoOp, NoOp) }
|
27
|
+
specify { expect(Machine).not_to have_transition(a: :c) }
|
28
|
+
end
|
data/lib/end_state/version.rb
CHANGED
@@ -0,0 +1,87 @@
|
|
1
|
+
module EndStateMatchers
|
2
|
+
def have_transition(transition)
|
3
|
+
TransitionMatcher.new(transition)
|
4
|
+
end
|
5
|
+
|
6
|
+
class TransitionMatcher
|
7
|
+
attr_reader :transition, :machine, :failure_messages, :guards, :finalizers
|
8
|
+
|
9
|
+
def initialize(transition)
|
10
|
+
@transition = transition
|
11
|
+
@failure_messages = []
|
12
|
+
@guards = []
|
13
|
+
@finalizers = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches?(actual)
|
17
|
+
@machine = actual
|
18
|
+
verify
|
19
|
+
end
|
20
|
+
|
21
|
+
def failure_message
|
22
|
+
failure_messages.join("\n")
|
23
|
+
end
|
24
|
+
|
25
|
+
def description
|
26
|
+
"have transition :#{transition.keys.first} => :#{transition.values.first}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_guard(guard)
|
30
|
+
@guards << guard
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def with_guards(*guards)
|
35
|
+
@guards += Array(guards)
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def with_finalizer(finalizer)
|
40
|
+
@finalizers << finalizer
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def with_finalizers(*finalizers)
|
45
|
+
@finalizers += Array(finalizers)
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def verify
|
52
|
+
result = true
|
53
|
+
if machine.transitions.keys.include? transition
|
54
|
+
result = (result && verify_guards) if guards.any?
|
55
|
+
result = (result && verify_finalizers) if finalizers.any?
|
56
|
+
result
|
57
|
+
else
|
58
|
+
failure_messages << "expected that #{machine.name} would have transition :#{transition.keys.first} => :#{transition.values.first}"
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def verify_guards
|
64
|
+
result = true
|
65
|
+
guards.each do |guard|
|
66
|
+
unless machine.transitions[transition].guards.any? { |g| g[:guard] == guard }
|
67
|
+
failure_messages << "expected that transition :#{transition.keys.first} => :#{transition.values.first} would have guard #{guard.name}"
|
68
|
+
result = false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
result
|
72
|
+
end
|
73
|
+
|
74
|
+
def verify_finalizers
|
75
|
+
result = true
|
76
|
+
finalizers.each do |finalizer|
|
77
|
+
unless machine.transitions[transition].finalizers.any? { |f| f[:finalizer] == finalizer }
|
78
|
+
failure_messages << "expected that transition :#{transition.keys.first} => :#{transition.values.first} would have finalizer #{finalizer.name}"
|
79
|
+
result = false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
result
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
include EndStateMatchers
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: end_state
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alexpeachey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- Rakefile
|
85
85
|
- end_state.gemspec
|
86
86
|
- examples/example1.rb
|
87
|
+
- examples/machine_spec.rb
|
87
88
|
- lib/end_state.rb
|
88
89
|
- lib/end_state/action.rb
|
89
90
|
- lib/end_state/errors.rb
|
@@ -94,6 +95,7 @@ files:
|
|
94
95
|
- lib/end_state/state_machine.rb
|
95
96
|
- lib/end_state/transition.rb
|
96
97
|
- lib/end_state/version.rb
|
98
|
+
- lib/end_state_matchers.rb
|
97
99
|
- spec/end_state/action_spec.rb
|
98
100
|
- spec/end_state/finalizers/persistence_spec.rb
|
99
101
|
- spec/end_state/state_machine_spec.rb
|