davidlee-state-fu 0.3.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +124 -34
- data/Rakefile +36 -30
- data/lib/no_stdout.rb +1 -1
- data/lib/state-fu.rb +9 -8
- data/lib/state_fu/active_support_lite/array/access.rb +12 -5
- data/lib/state_fu/active_support_lite/array/conversions.rb +10 -4
- data/lib/state_fu/active_support_lite/array/extract_options.rb +5 -4
- data/lib/state_fu/active_support_lite/array/grouping.rb +7 -4
- data/lib/state_fu/active_support_lite/array/random_access.rb +4 -3
- data/lib/state_fu/active_support_lite/array/wrapper.rb +4 -3
- data/lib/state_fu/active_support_lite/array.rb +3 -1
- data/lib/state_fu/active_support_lite/blank.rb +18 -9
- data/lib/state_fu/active_support_lite/cattr_reader.rb +4 -1
- data/lib/state_fu/active_support_lite/keys.rb +8 -3
- data/lib/state_fu/active_support_lite/misc.rb +6 -4
- data/lib/state_fu/active_support_lite/module/delegation.rb +130 -0
- data/lib/state_fu/active_support_lite/module.rb +1 -0
- data/lib/state_fu/active_support_lite/object.rb +5 -2
- data/lib/state_fu/active_support_lite/string.rb +6 -1
- data/lib/state_fu/active_support_lite/symbol.rb +2 -1
- data/lib/state_fu/applicable.rb +41 -0
- data/lib/state_fu/{helper.rb → arrays.rb} +45 -121
- data/lib/state_fu/binding.rb +136 -159
- data/lib/state_fu/core_ext.rb +78 -10
- data/lib/state_fu/event.rb +112 -48
- data/lib/state_fu/exceptions.rb +80 -34
- data/lib/state_fu/executioner.rb +149 -0
- data/lib/state_fu/has_options.rb +16 -0
- data/lib/state_fu/hooks.rb +21 -16
- data/lib/state_fu/interface.rb +80 -83
- data/lib/state_fu/lathe.rb +361 -148
- data/lib/state_fu/logger.rb +122 -45
- data/lib/state_fu/machine.rb +60 -32
- data/lib/state_fu/method_factory.rb +180 -72
- data/lib/state_fu/methodical.rb +17 -0
- data/lib/state_fu/persistence/active_record.rb +6 -1
- data/lib/state_fu/persistence/attribute.rb +1 -0
- data/lib/state_fu/persistence/base.rb +8 -6
- data/lib/state_fu/persistence.rb +94 -23
- data/lib/state_fu/sprocket.rb +26 -11
- data/lib/state_fu/state.rb +8 -27
- data/lib/state_fu/transition.rb +207 -98
- data/lib/state_fu/transition_query.rb +214 -0
- data/lib/state_fu.rb +1 -0
- data/lib/tasks/spec_last.rake +46 -0
- data/lib/tasks/state_fu.rake +57 -0
- data/lib/vizier.rb +61 -61
- data/spec/custom_formatter.rb +49 -0
- data/spec/features/binding_and_transition_helper_mixin_spec.rb +2 -2
- data/spec/features/method_missing_only_once_spec.rb +28 -0
- data/spec/features/not_requirements_spec.rb +83 -46
- data/spec/features/plotter_spec.rb +97 -0
- data/spec/features/shared_log_spec.rb +7 -0
- data/spec/features/singleton_machine_spec.rb +39 -0
- data/spec/features/state_and_array_options_accessor_spec.rb +1 -1
- data/spec/features/{transition_boolean_comparison.rb → transition_boolean_comparison_spec.rb} +29 -18
- data/spec/helper.rb +6 -117
- data/spec/integration/active_record_persistence_spec.rb +18 -4
- data/spec/integration/binding_extension_spec.rb +1 -1
- data/spec/integration/class_accessor_spec.rb +49 -59
- data/spec/integration/event_definition_spec.rb +20 -20
- data/spec/integration/example_01_document_spec.rb +13 -8
- data/spec/integration/example_02_string_spec.rb +3 -2
- data/spec/integration/instance_accessor_spec.rb +16 -19
- data/spec/integration/lathe_extension_spec.rb +2 -2
- data/spec/integration/machine_duplication_spec.rb +59 -37
- data/spec/integration/relaxdb_persistence_spec.rb +6 -3
- data/spec/integration/requirement_reflection_spec.rb +66 -57
- data/spec/integration/state_definition_spec.rb +72 -66
- data/spec/integration/transition_spec.rb +169 -173
- data/spec/spec.opts +5 -3
- data/spec/spec_helper.rb +132 -0
- data/spec/state_fu_spec.rb +870 -0
- data/spec/units/binding_spec.rb +33 -22
- data/spec/units/event_spec.rb +3 -22
- data/spec/units/exceptions_spec.rb +7 -0
- data/spec/units/lathe_spec.rb +7 -7
- data/spec/units/machine_spec.rb +67 -75
- data/spec/units/method_factory_spec.rb +55 -48
- data/spec/units/sprocket_spec.rb +5 -7
- data/spec/units/state_spec.rb +33 -24
- metadata +31 -19
- data/lib/state_fu/active_support_lite/inheritable_attributes.rb +0 -1
- data/lib/state_fu/fu_space.rb +0 -51
- data/lib/state_fu/mock_transition.rb +0 -38
- data/spec/BDD/plotter_spec.rb +0 -115
- data/spec/integration/dynamic_requirement_spec.rb +0 -160
- data/spec/integration/ex_machine_for_accounts_spec.rb +0 -79
- data/spec/integration/sanity_spec.rb +0 -31
- data/spec/units/fu_space_spec.rb +0 -95
data/spec/units/binding_spec.rb
CHANGED
|
@@ -27,29 +27,27 @@ describe StateFu::Binding do
|
|
|
27
27
|
before do
|
|
28
28
|
reset!
|
|
29
29
|
make_pristine_class('Klass')
|
|
30
|
-
Klass.
|
|
30
|
+
Klass.state_fu_machine(){}
|
|
31
31
|
@obj = Klass.new()
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
describe "constructor" do
|
|
35
35
|
before do
|
|
36
|
-
mock(
|
|
37
|
-
{
|
|
38
|
-
Klass => { :example => :example_field }
|
|
39
|
-
}
|
|
36
|
+
mock(Klass).state_fu_field_names.at_most(1) do
|
|
37
|
+
{ :example => :example_field }
|
|
40
38
|
end
|
|
41
39
|
end
|
|
42
40
|
|
|
43
41
|
it "should create a new Binding given valid arguments" do
|
|
44
|
-
b = StateFu::Binding.new( Klass.
|
|
42
|
+
b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example )
|
|
45
43
|
b.should be_kind_of( StateFu::Binding )
|
|
46
44
|
b.object.should == @obj
|
|
47
|
-
b.machine.should == Klass.
|
|
45
|
+
b.machine.should == Klass.state_fu_machine
|
|
48
46
|
b.method_name.should == :example
|
|
49
47
|
end
|
|
50
48
|
|
|
51
49
|
it "should add any options supplied to the binding" do
|
|
52
|
-
b = StateFu::Binding.new( Klass.
|
|
50
|
+
b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example,
|
|
53
51
|
:colour => :red,
|
|
54
52
|
:style => [:robust, :fruity] )
|
|
55
53
|
b.options.should == { :colour => :red, :style => [:robust, :fruity] }
|
|
@@ -66,23 +64,23 @@ describe StateFu::Binding do
|
|
|
66
64
|
|
|
67
65
|
describe "when StateFu::Persistence.active_record_column? is true" do
|
|
68
66
|
before do
|
|
69
|
-
mock( StateFu::Persistence ).active_record_column?(Klass, :example_field).times(
|
|
70
|
-
mock( Klass ).
|
|
67
|
+
mock( StateFu::Persistence ).active_record_column?(Klass, :example_field).times(1) { true }
|
|
68
|
+
mock( Klass ).before_create( :state_fu!) { }
|
|
71
69
|
end
|
|
72
70
|
it "should get an ActiveRecord persister" do
|
|
73
71
|
mock( StateFu::Persistence::ActiveRecord ).new( anything, :example_field ) { @p }
|
|
74
|
-
b = StateFu::Binding.new( Klass.
|
|
72
|
+
b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example )
|
|
75
73
|
b.persister.should == @p
|
|
76
74
|
end
|
|
77
75
|
end
|
|
78
76
|
|
|
79
77
|
describe "when StateFu::Persistence.active_record_column? is false" do
|
|
80
78
|
before do
|
|
81
|
-
mock( StateFu::Persistence ).active_record_column?(Klass, :example_field)
|
|
79
|
+
mock( StateFu::Persistence ).active_record_column?(Klass, :example_field) { false }
|
|
82
80
|
end
|
|
83
81
|
it "should get an Attribute persister" do
|
|
84
82
|
mock( StateFu::Persistence::Attribute ).new( anything, :example_field ) { @p }
|
|
85
|
-
b = StateFu::Binding.new( Klass.
|
|
83
|
+
b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example )
|
|
86
84
|
b.persister.should == @p
|
|
87
85
|
end
|
|
88
86
|
end
|
|
@@ -93,10 +91,10 @@ describe StateFu::Binding do
|
|
|
93
91
|
it "should create a new StateFu::Binding with default method-name & field_name" do
|
|
94
92
|
b = @obj.state_fu()
|
|
95
93
|
b.should be_kind_of( StateFu::Binding )
|
|
96
|
-
b.machine.should
|
|
97
|
-
b.object.should
|
|
98
|
-
b.method_name.should
|
|
99
|
-
b.field_name.should
|
|
94
|
+
b.machine.should == Klass.state_fu_machine
|
|
95
|
+
b.object.should == @obj
|
|
96
|
+
b.method_name.should == StateFu::DEFAULT
|
|
97
|
+
b.field_name.to_sym.should == StateFu::DEFAULT_FIELD
|
|
100
98
|
end
|
|
101
99
|
end
|
|
102
100
|
|
|
@@ -104,13 +102,13 @@ describe StateFu::Binding do
|
|
|
104
102
|
before do
|
|
105
103
|
reset!
|
|
106
104
|
make_pristine_class('Klass')
|
|
107
|
-
Klass.
|
|
105
|
+
Klass.state_fu_machine do
|
|
108
106
|
state :new do
|
|
109
107
|
event :age, :to => :old
|
|
110
108
|
end
|
|
111
109
|
state :old
|
|
112
110
|
end
|
|
113
|
-
@machine = Klass.
|
|
111
|
+
@machine = Klass.state_fu_machine()
|
|
114
112
|
@object = Klass.new()
|
|
115
113
|
@binding = @object.state_fu()
|
|
116
114
|
end
|
|
@@ -144,9 +142,14 @@ describe StateFu::Binding do
|
|
|
144
142
|
before do
|
|
145
143
|
reset!
|
|
146
144
|
make_pristine_class("Klass")
|
|
147
|
-
|
|
145
|
+
Klass.class_eval do
|
|
146
|
+
def tissue?(*args); "o_O"; end
|
|
147
|
+
end
|
|
148
|
+
@machine = Klass.state_fu_machine do
|
|
148
149
|
state :snoo do
|
|
149
|
-
event :am_fireable, :to => :wizz
|
|
150
|
+
event :am_fireable, :to => :wizz do
|
|
151
|
+
requires :tissue?
|
|
152
|
+
end
|
|
150
153
|
end
|
|
151
154
|
state :wizz do
|
|
152
155
|
event :not_fireable, :to => :pong
|
|
@@ -171,7 +174,15 @@ describe StateFu::Binding do
|
|
|
171
174
|
end
|
|
172
175
|
|
|
173
176
|
describe "when called with additional arguments after the destination event/state" do
|
|
174
|
-
|
|
177
|
+
|
|
178
|
+
# This would make very little sense to someone trying to understand how to use the library.
|
|
179
|
+
it "should pass the arguments to any requirements to determine transition availability" do
|
|
180
|
+
pending
|
|
181
|
+
mock(@obj).tissue?() do
|
|
182
|
+
current_transition.should be_kind_of(StateFu::Transition)
|
|
183
|
+
end #{|tr| tr.args.should == [:a,:b] }
|
|
184
|
+
@obj.state_fu.am_fireable?(:a, :b)
|
|
185
|
+
end
|
|
175
186
|
end
|
|
176
187
|
|
|
177
188
|
end
|
data/spec/units/event_spec.rb
CHANGED
|
@@ -86,7 +86,7 @@ describe StateFu::Event do
|
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
it "should have the event as the sprocket" do
|
|
89
|
-
@lathe.
|
|
89
|
+
@lathe.state_or_event.should == @event
|
|
90
90
|
end
|
|
91
91
|
|
|
92
92
|
end
|
|
@@ -203,31 +203,12 @@ describe StateFu::Event do
|
|
|
203
203
|
@event.from?( :a ).should == true
|
|
204
204
|
end
|
|
205
205
|
|
|
206
|
-
it "should return
|
|
206
|
+
it "should return nil given a symbol which is not the name of a state in @origin" do
|
|
207
207
|
mock( @event ).origins().times(any_times) { [StateFu::State.new(@machine,:a)] }
|
|
208
|
-
@event.from?( :b ).should ==
|
|
208
|
+
@event.from?( :b ).should == nil
|
|
209
209
|
end
|
|
210
210
|
end
|
|
211
211
|
|
|
212
|
-
describe 'complete?' do
|
|
213
|
-
it "should be false if either origin / target are nil" do
|
|
214
|
-
@event.complete?.should == false
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
it "should be true when origin / target are both not nil" do
|
|
218
|
-
mock( @event ).origins { [:a] }
|
|
219
|
-
mock( @event ).targets { [:b] }
|
|
220
|
-
@event.complete?.should == true
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
it "should be false when either origin / target are nil" do
|
|
224
|
-
mock( @event ).origins { [:a] }
|
|
225
|
-
mock( @event ).targets { nil }
|
|
226
|
-
@event.complete?.should == false
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
end
|
|
230
|
-
|
|
231
212
|
end # describe instance methods
|
|
232
213
|
end # describe StateFu::Event
|
|
233
214
|
end
|
|
@@ -18,11 +18,13 @@ describe StateFu::TransitionHalted do
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
it "should create a TransitionHalted given a transition" do
|
|
21
|
+
pending
|
|
21
22
|
e = StateFu::TransitionHalted.new( @transition )
|
|
22
23
|
e.should be_kind_of( StateFu::TransitionHalted )
|
|
23
24
|
end
|
|
24
25
|
|
|
25
26
|
it "should allow a custom message" do
|
|
27
|
+
pending
|
|
26
28
|
msg = 'helo'
|
|
27
29
|
e = StateFu::TransitionHalted.new( @transition, msg )
|
|
28
30
|
e.should be_kind_of( StateFu::TransitionHalted )
|
|
@@ -30,12 +32,14 @@ describe StateFu::TransitionHalted do
|
|
|
30
32
|
end
|
|
31
33
|
|
|
32
34
|
it "should allow a message to be omitted" do
|
|
35
|
+
pending
|
|
33
36
|
e = StateFu::TransitionHalted.new( @transition )
|
|
34
37
|
e.should be_kind_of( StateFu::TransitionHalted )
|
|
35
38
|
e.message.should == StateFu::TransitionHalted::DEFAULT_MESSAGE
|
|
36
39
|
end
|
|
37
40
|
|
|
38
41
|
it "should allow access to the transition" do
|
|
42
|
+
pending
|
|
39
43
|
e = StateFu::TransitionHalted.new( @transition )
|
|
40
44
|
e.transition.should == @transition
|
|
41
45
|
end
|
|
@@ -52,12 +56,14 @@ describe StateFu::InvalidTransition do
|
|
|
52
56
|
|
|
53
57
|
describe "constructor" do
|
|
54
58
|
it "should create an InvalidTransition given a binding, event, origin & target" do
|
|
59
|
+
pending
|
|
55
60
|
e = StateFu::InvalidTransition.new( @binding, @event, @origin, @target )
|
|
56
61
|
e.should be_kind_of( StateFu::InvalidTransition )
|
|
57
62
|
e.message.should == StateFu::InvalidTransition::DEFAULT_MESSAGE
|
|
58
63
|
end
|
|
59
64
|
|
|
60
65
|
it "should allow a custom message" do
|
|
66
|
+
pending
|
|
61
67
|
msg = 'helo'
|
|
62
68
|
e = StateFu::InvalidTransition.new( @binding, @event, @origin, @target, msg )
|
|
63
69
|
e.should be_kind_of( StateFu::InvalidTransition )
|
|
@@ -65,6 +71,7 @@ describe StateFu::InvalidTransition do
|
|
|
65
71
|
end
|
|
66
72
|
|
|
67
73
|
it "should allow access to the binding, event, origin, and target" do
|
|
74
|
+
pending
|
|
68
75
|
e = StateFu::InvalidTransition.new( @binding, @event, @origin, @target )
|
|
69
76
|
e.binding.should == @binding
|
|
70
77
|
e.event.should == @event
|
data/spec/units/lathe_spec.rb
CHANGED
|
@@ -6,7 +6,7 @@ describe StateFu::Lathe do
|
|
|
6
6
|
before do
|
|
7
7
|
reset!
|
|
8
8
|
make_pristine_class('Klass')
|
|
9
|
-
@machine =
|
|
9
|
+
@machine = StateFu::Machine.new()
|
|
10
10
|
@state = Object.new()
|
|
11
11
|
@event = Object.new()
|
|
12
12
|
|
|
@@ -23,23 +23,23 @@ describe StateFu::Lathe do
|
|
|
23
23
|
lathe = StateFu::Lathe.new( @machine )
|
|
24
24
|
lathe.should be_kind_of( StateFu::Lathe )
|
|
25
25
|
lathe.machine.should == @machine
|
|
26
|
-
lathe.
|
|
26
|
+
lathe.state_or_event.should == nil
|
|
27
27
|
lathe.options.should == {}
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
it "should accept a
|
|
30
|
+
it "should accept a state_or_event (state / event ) and if given one, be a child" do
|
|
31
31
|
options = {}
|
|
32
32
|
mock( @state ).apply!( options ) {}
|
|
33
33
|
lathe = StateFu::Lathe.new( @machine, @state )
|
|
34
34
|
lathe.should be_kind_of( StateFu::Lathe )
|
|
35
35
|
lathe.machine.should == @machine
|
|
36
|
-
lathe.
|
|
36
|
+
lathe.state_or_event.should == @state
|
|
37
37
|
lathe.options.should == {}
|
|
38
38
|
lathe.should be_child
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
-
describe "lathe instance with no
|
|
42
|
+
describe "lathe instance with no state_or_event (master lathe for a machine)" do
|
|
43
43
|
before do
|
|
44
44
|
end
|
|
45
45
|
|
|
@@ -351,7 +351,7 @@ describe StateFu::Lathe do
|
|
|
351
351
|
@lathe = StateFu::Lathe.new( @machine, @state )
|
|
352
352
|
end
|
|
353
353
|
|
|
354
|
-
it "should create a named event from and to the lathe's
|
|
354
|
+
it "should create a named event from and to the lathe's state_or_event (state)" do
|
|
355
355
|
|
|
356
356
|
@machine.events.should be_empty
|
|
357
357
|
@machine.states.length.should == 1
|
|
@@ -380,7 +380,7 @@ describe StateFu::Lathe do
|
|
|
380
380
|
|
|
381
381
|
describe ".event(:name)" do
|
|
382
382
|
before do
|
|
383
|
-
mock( @machine ).find_or_create_states_by_name( @lathe.
|
|
383
|
+
mock( @machine ).find_or_create_states_by_name( @lathe.state_or_event ).at_least(1) { @lathe.state_or_event }
|
|
384
384
|
end
|
|
385
385
|
|
|
386
386
|
it "should create the named event if it does not exist" do
|
data/spec/units/machine_spec.rb
CHANGED
|
@@ -16,17 +16,17 @@ describe StateFu::Machine do
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
describe "Machine.for_class" do
|
|
19
|
-
describe "when there's no
|
|
19
|
+
describe "when there's no machine defined for the class" do
|
|
20
20
|
before do
|
|
21
21
|
reset!
|
|
22
22
|
make_pristine_class 'Klass'
|
|
23
|
-
|
|
23
|
+
# mock( Klass ).machines() { {} }
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
it "should create a new machine and bind! it" do
|
|
27
27
|
@machine = Object.new
|
|
28
28
|
mock( @machine ).bind!( Klass, :moose, nil )
|
|
29
|
-
mock( StateFu::Machine ).new(
|
|
29
|
+
mock( StateFu::Machine ).new( {} ) { @machine }
|
|
30
30
|
StateFu::Machine.for_class( Klass, :moose )
|
|
31
31
|
end
|
|
32
32
|
|
|
@@ -34,15 +34,8 @@ describe StateFu::Machine do
|
|
|
34
34
|
@m = StateFu::Machine.for_class( Klass, :snoo ) do
|
|
35
35
|
state :porpoise
|
|
36
36
|
end
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
describe "when there's a matching machine in FuSpace" do
|
|
42
|
-
it "should retrieve the previously created machine" do
|
|
43
|
-
@machine = Object.new
|
|
44
|
-
mock( StateFu::FuSpace ).class_machines() { { Klass => { :moose => @machine } } }
|
|
45
|
-
StateFu::Machine.for_class( Klass, :moose ).should == @machine
|
|
37
|
+
# mock( Klass ).machines() { {} }
|
|
38
|
+
# @m.states.map(&:name).should == [:porpoise]
|
|
46
39
|
end
|
|
47
40
|
end
|
|
48
41
|
|
|
@@ -56,24 +49,23 @@ describe StateFu::Machine do
|
|
|
56
49
|
before do
|
|
57
50
|
reset!
|
|
58
51
|
make_pristine_class 'Klass'
|
|
59
|
-
@
|
|
52
|
+
@m = StateFu::Machine.new
|
|
60
53
|
end
|
|
61
54
|
|
|
62
55
|
describe "helper" do
|
|
63
56
|
it "should add its arguments to the @@helpers array" do
|
|
64
57
|
module Foo; FOO = :foo; end
|
|
65
58
|
module Bar; BAR = :bar; end
|
|
66
|
-
@
|
|
67
|
-
@
|
|
59
|
+
@m.helper Foo, Bar
|
|
60
|
+
@m.helpers.should == [Foo, Bar]
|
|
68
61
|
end
|
|
69
62
|
|
|
70
63
|
end
|
|
71
64
|
|
|
72
65
|
describe ".initialize" do
|
|
73
|
-
it "should
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
end.should raise_error( ArgumentError )
|
|
66
|
+
it "should apply options to the machine" do
|
|
67
|
+
@m = StateFu::Machine.new( :colour => "blue")
|
|
68
|
+
@m.options.should == {:colour => "blue" }
|
|
77
69
|
end
|
|
78
70
|
end
|
|
79
71
|
|
|
@@ -82,42 +74,42 @@ describe StateFu::Machine do
|
|
|
82
74
|
end
|
|
83
75
|
|
|
84
76
|
describe ".bind!" do
|
|
85
|
-
it "should call StateFu::
|
|
77
|
+
it "should call StateFu::Machine.bind! with itself and its arguments" do
|
|
86
78
|
field_name = :my_field_name
|
|
87
|
-
mock( StateFu::
|
|
88
|
-
@
|
|
79
|
+
mock( StateFu::Machine ).bind!( @m, Klass, :newname, field_name ) {}
|
|
80
|
+
@m.bind!( Klass, :newname, field_name )
|
|
89
81
|
end
|
|
90
82
|
|
|
91
83
|
it "should generate a field name if none is given" do
|
|
92
84
|
klass = Klass
|
|
93
85
|
name = :StinkJuice
|
|
94
86
|
field_name = 'stink_juice_field'
|
|
95
|
-
|
|
96
|
-
|
|
87
|
+
@m.bind!( Klass, name )
|
|
88
|
+
Klass.state_fu_field_names[name].should == 'stink_juice_field'
|
|
97
89
|
end
|
|
98
90
|
end
|
|
99
91
|
|
|
100
92
|
describe ".initial_state=" do
|
|
101
93
|
|
|
102
94
|
it "should set @initial_state given a String, Symbol or State for an existing state" do
|
|
103
|
-
state = StateFu::State.new( @
|
|
104
|
-
@
|
|
105
|
-
@
|
|
106
|
-
@
|
|
95
|
+
state = StateFu::State.new( @m, :wizzle )
|
|
96
|
+
@m.states << state
|
|
97
|
+
@m.initial_state = state
|
|
98
|
+
@m.initial_state.should == state
|
|
107
99
|
end
|
|
108
100
|
|
|
109
101
|
it "should create the state if it doesnt exist" do
|
|
110
|
-
@
|
|
111
|
-
@
|
|
112
|
-
@
|
|
113
|
-
@
|
|
102
|
+
@m.initial_state = :snoo
|
|
103
|
+
@m.initial_state.should be_kind_of( StateFu::State )
|
|
104
|
+
@m.initial_state.name.should == :snoo
|
|
105
|
+
@m.states.should include( @m.initial_state )
|
|
114
106
|
end
|
|
115
107
|
|
|
116
108
|
it "should raise an ArgumentError given a number or an Array" do
|
|
117
|
-
lambda do @
|
|
109
|
+
lambda do @m.initial_state = 6
|
|
118
110
|
end.should raise_error( ArgumentError )
|
|
119
111
|
|
|
120
|
-
lambda do @
|
|
112
|
+
lambda do @m.initial_state = [:ping]
|
|
121
113
|
end.should raise_error( ArgumentError )
|
|
122
114
|
end
|
|
123
115
|
|
|
@@ -125,110 +117,110 @@ describe StateFu::Machine do
|
|
|
125
117
|
|
|
126
118
|
describe ".initial_state" do
|
|
127
119
|
it "should return nil if there are no states and initial_state= has not been called" do
|
|
128
|
-
@
|
|
129
|
-
@
|
|
120
|
+
@m.states.should == []
|
|
121
|
+
@m.initial_state.should == nil
|
|
130
122
|
end
|
|
131
123
|
|
|
132
124
|
it "should return the first state if one exists" do
|
|
133
|
-
stub( @
|
|
134
|
-
@
|
|
125
|
+
stub( @m ).states() { [:a, :b, :c] }
|
|
126
|
+
@m.initial_state.should == :a
|
|
135
127
|
end
|
|
136
128
|
|
|
137
129
|
end
|
|
138
130
|
|
|
139
131
|
describe ".states" do
|
|
140
132
|
it "should return an array extended with StateFu::StateArray" do
|
|
141
|
-
@
|
|
142
|
-
@
|
|
133
|
+
@m.states.should be_kind_of( Array )
|
|
134
|
+
@m.states.extended_by.should include( StateFu::StateArray )
|
|
143
135
|
end
|
|
144
136
|
end
|
|
145
137
|
|
|
146
138
|
describe ".state_names" do
|
|
147
139
|
it "should return a list of symbols of state names" do
|
|
148
|
-
@
|
|
149
|
-
@
|
|
150
|
-
@
|
|
140
|
+
@m.states << StateFu::State.new( @m, :a )
|
|
141
|
+
@m.states << StateFu::State.new( @m, :b )
|
|
142
|
+
@m.state_names.should == [:a, :b ]
|
|
151
143
|
end
|
|
152
144
|
end
|
|
153
145
|
|
|
154
146
|
describe ".events" do
|
|
155
147
|
it "should return an array extended with StateFu::EventArray" do
|
|
156
|
-
@
|
|
157
|
-
@
|
|
148
|
+
@m.events.should be_kind_of( Array )
|
|
149
|
+
@m.events.extended_by.should include( StateFu::EventArray )
|
|
158
150
|
end
|
|
159
151
|
end
|
|
160
152
|
|
|
161
153
|
describe ".event_names" do
|
|
162
154
|
it "should return a list of symbols of event names" do
|
|
163
|
-
@
|
|
164
|
-
@
|
|
165
|
-
@
|
|
155
|
+
@m.events << StateFu::Event.new( @m, :a )
|
|
156
|
+
@m.events << StateFu::Event.new( @m, :b )
|
|
157
|
+
@m.event_names.should == [:a, :b ]
|
|
166
158
|
end
|
|
167
159
|
end
|
|
168
160
|
|
|
169
161
|
describe ".find_or_create_states_by_name" do
|
|
170
162
|
describe "given an array of symbols" do
|
|
171
163
|
it "should return the states named by the symbols if they exist" do
|
|
172
|
-
a = StateFu::State.new( @
|
|
173
|
-
b = StateFu::State.new( @
|
|
174
|
-
@
|
|
175
|
-
@
|
|
176
|
-
@
|
|
177
|
-
@
|
|
164
|
+
a = StateFu::State.new( @m, :a )
|
|
165
|
+
b = StateFu::State.new( @m, :b )
|
|
166
|
+
@m.states << a
|
|
167
|
+
@m.states << b
|
|
168
|
+
@m.find_or_create_states_by_name( :a, :b ).should == [a, b]
|
|
169
|
+
@m.find_or_create_states_by_name( [:a, :b] ).should == [a, b]
|
|
178
170
|
end
|
|
179
171
|
|
|
180
172
|
it "should return the states named by the symbols and create them if they don't exist" do
|
|
181
|
-
@
|
|
182
|
-
res = @
|
|
173
|
+
@m.states.should == []
|
|
174
|
+
res = @m.find_or_create_states_by_name( :a, :b )
|
|
183
175
|
res.should be_kind_of( Array )
|
|
184
176
|
res.length.should == 2
|
|
185
177
|
res.all? { |e| e.class == StateFu::State }.should be_true
|
|
186
178
|
res.map(&:name).should == [ :a, :b ]
|
|
187
|
-
@
|
|
179
|
+
@m.find_or_create_states_by_name( :a, :b ).should == res
|
|
188
180
|
end
|
|
189
181
|
end # arr symbols
|
|
190
182
|
|
|
191
183
|
describe "given an array of states" do
|
|
192
184
|
it "should return the states if they're in the machine's states array" do
|
|
193
|
-
a = StateFu::State.new( @
|
|
194
|
-
b = StateFu::State.new( @
|
|
195
|
-
@
|
|
196
|
-
@
|
|
197
|
-
@
|
|
198
|
-
@
|
|
199
|
-
@
|
|
185
|
+
a = StateFu::State.new( @m, :a )
|
|
186
|
+
b = StateFu::State.new( @m, :b )
|
|
187
|
+
@m.states << a
|
|
188
|
+
@m.states << b
|
|
189
|
+
@m.find_or_create_states_by_name( a, b ).should == [a, b]
|
|
190
|
+
@m.find_or_create_states_by_name( [a, b] ).should == [a, b]
|
|
191
|
+
@m.find_or_create_states_by_name( [[a, b]] ).should == [a, b]
|
|
200
192
|
end
|
|
201
193
|
|
|
202
194
|
it "should add the states to the machine's states array if they're absent" do
|
|
203
|
-
a = StateFu::State.new( @
|
|
204
|
-
b = StateFu::State.new( @
|
|
205
|
-
@
|
|
206
|
-
@
|
|
207
|
-
@
|
|
195
|
+
a = StateFu::State.new( @m, :a )
|
|
196
|
+
b = StateFu::State.new( @m, :b )
|
|
197
|
+
@m.find_or_create_states_by_name( a, b ).should == [a, b]
|
|
198
|
+
@m.find_or_create_states_by_name( [a, b] ).should == [a, b]
|
|
199
|
+
@m.find_or_create_states_by_name( [[a, b]] ).should == [a, b]
|
|
208
200
|
end
|
|
209
201
|
end # arr states
|
|
210
202
|
end # find_or_create_states_by_name
|
|
211
203
|
|
|
212
204
|
describe "requirement_messages" do
|
|
213
205
|
it "should be a hash" do
|
|
214
|
-
@
|
|
215
|
-
@
|
|
206
|
+
@m.should respond_to(:requirement_messages)
|
|
207
|
+
@m.requirement_messages.should be_kind_of( Hash )
|
|
216
208
|
end
|
|
217
209
|
|
|
218
210
|
it "should be empty by default" do
|
|
219
|
-
@
|
|
211
|
+
@m.requirement_messages.should be_empty
|
|
220
212
|
end
|
|
221
213
|
|
|
222
214
|
end # requirement_messages
|
|
223
215
|
|
|
224
216
|
describe "named_procs" do
|
|
225
217
|
it "should be a hash" do
|
|
226
|
-
@
|
|
227
|
-
@
|
|
218
|
+
@m.should respond_to(:named_procs)
|
|
219
|
+
@m.named_procs.should be_kind_of( Hash )
|
|
228
220
|
end
|
|
229
221
|
|
|
230
222
|
it "should be empty by default" do
|
|
231
|
-
@
|
|
223
|
+
@m.named_procs.should be_empty
|
|
232
224
|
end
|
|
233
225
|
|
|
234
226
|
end # named_procs
|