state-fu 0.13.1 → 0.13.3

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.
@@ -25,6 +25,7 @@ module Vizier #:nodoc:all
25
25
  end
26
26
 
27
27
  def legal?( str )
28
+ str = str.to_s
28
29
  str =~ /^[#{LEGAL_CHARS}]+$/ && str == str.split
29
30
  end
30
31
 
@@ -47,29 +47,29 @@ describe StateFu::Plotter do
47
47
  describe ".generate" do
48
48
 
49
49
  it "should call generate_dot!" do
50
- mock( @plotter ).generate_dot!() { "dot" }
50
+ @plotter.should_receive(:generate_dot!).and_return("dot")
51
51
  @plotter.generate
52
52
  end
53
53
 
54
54
  it "should store the result in the dot attribute" do
55
- mock( @plotter).generate_dot!() { "dot" }
55
+ @plotter.should_receive(:generate_dot!).and_return("dot")
56
56
  @plotter.generate
57
57
  @plotter.dot.should == "dot"
58
58
  end
59
59
 
60
60
  describe ".save_as(filename)" do
61
61
  it "should save the string to a file" do
62
- mock( File).open( 'filename', 'w' ).yields( @fh = Object.new() )
63
- mock( @fh ).write( @plotter.output )
62
+ File.should_receive(:open).with('filename', 'w').and_yield(@fh = Object.new())
63
+ @fh.should_receive(:write).with(@plotter.output)
64
64
  @plotter.output.save_as( 'filename' )
65
65
  end
66
66
  end
67
67
 
68
68
  describe ".save!" do
69
69
  it "should save the string in a tempfile and return the path" do
70
- mock(@tempfile = Object.new).path {"path"}.subject
71
- mock(Tempfile).new(['state_fu_graph','.dot']).yields( @fh = Object.new() ) { @tempfile }
72
- mock( @fh ).write( @plotter.output )
70
+ (@tempfile = Object.new).should_receive(:path).and_return('path')
71
+ (@fh = Object.new()).should_receive(:write).with(@plotter.output)
72
+ Tempfile.should_receive(:new).with(['state_fu_graph','.dot']).and_yield(@fh).and_return(@tempfile)
73
73
  @plotter.output.save!.should == 'path'
74
74
  end
75
75
  end
data/spec/helper.rb CHANGED
@@ -1,13 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
  require File.join(File.dirname(__FILE__),'spec_helper')
3
3
 
4
- begin
5
- require 'rr'
6
- rescue LoadError => e
7
- STDERR.puts "The '#{gem_name}' gem is required to run StateFu's specs. Please install it by running (as root):\ngem install #{gem_name}\n\n"
8
- exit 1;
9
- end
10
-
11
- Spec::Runner.configure do |config|
12
- config.mock_with :rr
13
- end
@@ -111,22 +111,14 @@ describe "an ActiveRecord model with StateFu included:" do
111
111
  # satisfy the not null constraint
112
112
  describe "automagic state_fu! before_save filter and validations" do
113
113
 
114
- it "should call state_fu! before a record is created" do
115
- @ex.should be_new_record
116
- mock.proxy( @ex ).state_fu!.at_least( 1 ) { }
117
- @ex.save!
118
- end
119
-
120
- it "should call state_fu! before a record is updated" do
121
- @ex.should be_new_record
122
- mock.proxy( @ex ).state_fu!.at_least( 1 ) { }
123
- @ex.save!
124
- end
114
+ it "should call state_fu! before a record is created"
115
+ it "should call state_fu! before a record is updated"
125
116
 
126
117
  it "should create a record given only a name, with the field set to the initial state" do
127
118
  ex = ExampleRecord.new( :name => "exemplar" )
128
- ex.should be_valid
129
119
  ex.state_fu_field.should == nil
120
+ ex.should be_valid
121
+ ex.state_fu_field.should == 'initial'
130
122
  ex.save!
131
123
  ex.should_not be_new_record
132
124
  ex.state_fu_field.should == 'initial'
@@ -89,7 +89,7 @@ describe "Document" do
89
89
  end
90
90
 
91
91
  it "should call update_rss when publish! is called" do
92
- mock( @doc ).update_rss(anything) {}
92
+ @doc.should_receive(:update_rss)
93
93
  @doc.status.publish!
94
94
  end
95
95
 
@@ -125,7 +125,7 @@ describe "Document" do
125
125
  describe "delete!" do
126
126
 
127
127
  it "should execute destroy()" do
128
- mock( @doc ).destroy(anything) {}
128
+ @doc.should_receive(:destroy)
129
129
  @doc.status.delete!
130
130
  end
131
131
 
@@ -49,7 +49,8 @@ describe String do
49
49
  it "should call sanitize_for_shell! when shell.escape! is called, and be clean afterwards " do
50
50
  @str.should be_dirty
51
51
  @str.should_not be_clean
52
- mock( @str ).sanitize_for_shell!(anything) {}
52
+ @str.should_receive(:sanitize_for_shell!)
53
+
53
54
  @str.shell.escape!
54
55
  @str.should_not be_dirty
55
56
  @str.should be_clean
@@ -51,18 +51,14 @@ describe "Transition requirement reflection" do
51
51
 
52
52
  describe "transition.valid? / transition.requirements_met?" do
53
53
  it "should be true if all requirements are met (return truth)" do
54
- #@obj.state_fu.next_states[:moon].entry_requirements.should == [:spacesuit?]
55
-
56
- #@obj.state_fu.fireable?([:fly_spaceship,:moon]).should == true
54
+ @obj.state_fu.next_states[:moon].entry_requirements.should == [:spacesuit?]
57
55
  @obj.can_fly_spaceship?(:moon).should == true
58
- #@obj.fly_spaceship(:moon).requirements_met?.should == true
59
- #@obj.fly_spaceship(:moon).should be_valid
56
+ @obj.fly_spaceship(:moon).should be_valid
60
57
  end
61
58
 
62
59
  it "should be false if not all requirements are met" do
63
- stub( @obj ).spacesuit?() { false }
60
+ @obj.should_receive(:spacesuit?).any_number_of_times.and_return(false)
64
61
  @obj.state_fu.next_states[:moon].entry_requirements.should == [:spacesuit?]
65
- # @obj.state_fu.evaluate(:spacesuit?).should == false
66
62
  @obj.can_fly_spaceship?(:moon).should == false
67
63
  @obj.fly_spaceship(:moon).requirements_met?.should == false
68
64
  @obj.fly_spaceship(:moon).should_not be_valid
@@ -71,11 +67,9 @@ describe "Transition requirement reflection" do
71
67
 
72
68
  describe "flying from russia to america without one's affairs in order while wearing a turban" do
73
69
  before do
74
- mock( @obj ).us_visa?(anything) { false }
75
- mock( @obj ).no_turban?(anything) { false }
76
- mock( @obj ).no_arrest_warrant?(anything) { false }
77
- mock( @obj ).money_for_bribe?(anything) { false }
78
- mock( @obj ).papers_in_order?(anything) { false }
70
+ %w(us_visa? no_turban? no_arrest_warrant? money_for_bribe? papers_in_order?).each do |meth|
71
+ @obj.should_receive(meth).any_number_of_times.and_return(false)
72
+ end
79
73
  end
80
74
 
81
75
  describe "when no messages are supplied for the requirements" do
@@ -136,9 +130,9 @@ describe "Transition requirement reflection" do
136
130
  end
137
131
 
138
132
  describe "when a message is supplied for the requirement" do
139
- it "should contain a list of the requirement failure messages as strings" do
140
- mock( @obj ).spacesuit?(anything) { false }
141
- mock( @obj ).fuel?(anything) { false }
133
+ it "should contain a list of the requirement failure messages as strings" do
134
+ @obj.should_receive(:spacesuit?).and_return(false)
135
+ @obj.should_receive(:fuel?).and_return(false)
142
136
  @obj.state_fu.fly_spaceship(:moon).unmet_requirements.should == [:spacesuit?, :fuel?]
143
137
  end
144
138
  end
@@ -148,8 +142,8 @@ describe "Transition requirement reflection" do
148
142
  describe "transition.unmet_requirement_messages" do
149
143
  describe "when a string message is defined for one of two unmet_requirements" do
150
144
  before do
151
- stub( @obj ).spacesuit?() { false }
152
- stub( @obj ).fuel?() { false }
145
+ @obj.should_receive(:spacesuit?).and_return(false)
146
+ @obj.should_receive(:fuel?).and_return(false)
153
147
  @msg = "You got no spacesuit."
154
148
  @machine.requirement_messages[:spacesuit?] = @msg
155
149
  end
@@ -169,8 +163,8 @@ describe "Transition requirement reflection" do
169
163
 
170
164
  describe "when a proc message is defined for one of two unmet_requirements" do
171
165
  before do
172
- stub( @obj ).spacesuit?() { false }
173
- stub( @obj ).fuel?() { false }
166
+ @obj.should_receive(:spacesuit?).any_number_of_times.and_return(false)
167
+ @obj.should_receive(:fuel?).any_number_of_times.and_return(false)
174
168
  end
175
169
 
176
170
  describe "when the arity of the proc is 1" do
@@ -239,9 +233,7 @@ describe "Transition requirement reflection" do
239
233
  it "should call t.evaluate_named_proc_or_method(:no_spacesuit_msg_method)" do
240
234
  t = @obj.state_fu.fly_spaceship(:moon)
241
235
  t.unmet_requirements.length.should == 2
242
- stub( t ).evaluate( anything) { false }
243
-
244
- mock( t ).evaluate(:no_spacesuit_msg_method){ ":)" }
236
+ t.should_receive(:evaluate).with(:no_spacesuit_msg_method).and_return ':)'
245
237
  messages = t.unmet_requirement_messages
246
238
  messages.should include( ":)" )
247
239
  end
data/spec/spec_helper.rb CHANGED
@@ -20,16 +20,16 @@ module MySpecHelper
20
20
 
21
21
  def prepare_active_record( options={}, &migration )
22
22
  if skip_slow_specs?
23
- skip_slow_specs and return false
23
+ skip_slow_specs and return
24
24
  end
25
25
 
26
26
  begin
27
- require 'activesupport'
27
+ require 'active_support'
28
28
  require 'active_record'
29
29
  require 'sqlite3'
30
30
  rescue LoadError => e
31
31
  pending "skipping specifications due to load error: #{e}"
32
- return false
32
+ return
33
33
  end
34
34
 
35
35
  options.symbolize_keys!
@@ -219,7 +219,7 @@ describe "A door which opens and shuts:" do
219
219
  rescue StateFu::RequirementError => e
220
220
  e.to_a.should == ["Sorry, it's locked."]
221
221
  e.to_h.should == {:not_locked? => "Sorry, it's locked."}
222
- e.to_enum.should be_kind_of(Enumerable::Enumerator)
222
+ e.should respond_to(:to_enum)
223
223
  e.should_not be_empty
224
224
  e.length.should == 1
225
225
  e.each do |requirement, message|
@@ -27,11 +27,9 @@ describe StateFu::Binding do
27
27
 
28
28
  describe "constructor" do
29
29
  before do
30
- mock(Klass).state_fu_options.at_most(3) do
31
- {
32
- :example => {:field_name => :example_field}
33
- }
34
- end
30
+ Klass.should_receive(:state_fu_options).at_most(3).times.and_return({
31
+ :example => {:field_name => :example_field}
32
+ })
35
33
  end
36
34
 
37
35
  it "should create a new Binding given valid arguments" do
@@ -61,12 +59,12 @@ describe StateFu::Binding do
61
59
  describe "when StateFu::Persistence.active_record_column? is true" do
62
60
 
63
61
  before do
64
- mock( StateFu::Persistence ).active_record_column?(Klass, :example_field).times(1) { true }
65
- mock( Klass ).before_create( :state_fu!) { }
62
+ StateFu::Persistence.should_receive(:active_record_column?).with(Klass, :example_field).and_return true
63
+ Klass.should_receive(:before_validation_on_create).with(:state_fu!)
66
64
  end
67
65
 
68
66
  it "should get an ActiveRecord persister" do
69
- mock( StateFu::Persistence::ActiveRecord ).new( anything, :example_field ) { @p }
67
+ StateFu::Persistence::ActiveRecord.should_receive(:new).with(anything, :example_field).and_return(@p)
70
68
  b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example )
71
69
  b.persister.should == @p
72
70
  end
@@ -74,10 +72,10 @@ describe StateFu::Binding do
74
72
 
75
73
  describe "when StateFu::Persistence.active_record_column? is false" do
76
74
  before do
77
- mock( StateFu::Persistence ).active_record_column?(Klass, :example_field) { false }
75
+ StateFu::Persistence.should_receive(:active_record_column?).with(Klass, :example_field).and_return false
78
76
  end
79
77
  it "should get an Attribute persister" do
80
- mock( StateFu::Persistence::Attribute ).new( anything, :example_field ) { @p }
78
+ StateFu::Persistence::Attribute.should_receive(:new).with(anything, :example_field).and_return @p
81
79
  b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example )
82
80
  b.persister.should == @p
83
81
  end
@@ -173,6 +171,7 @@ describe StateFu::Binding do
173
171
  describe "when called with additional arguments after the destination event/state" do
174
172
 
175
173
  # This would make very little sense to someone trying to understand how to use the library.
174
+ # figure out how to spec it properly.
176
175
  it "should pass the arguments to any requirements to determine transition availability" do
177
176
  pending
178
177
  t = nil
@@ -8,7 +8,7 @@ describe StateFu::Event do
8
8
  include MySpecHelper
9
9
  before do
10
10
  @machine = Object.new
11
- stub(@machine).tools() { [].extend( StateFu::ToolArray ) }
11
+ @machine.should_receive(:tools).any_number_of_times.and_return [].extend(StateFu::ToolArray)
12
12
  end
13
13
 
14
14
  describe "Instance methods" do
@@ -30,29 +30,29 @@ describe StateFu::Event do
30
30
 
31
31
  describe "target" do
32
32
  it "should be nil if targets is nil" do
33
- stub( @event ).targets() { nil }
33
+ @event.should_receive(:targets).and_return nil
34
34
  @event.target.should == nil
35
35
  end
36
36
 
37
37
  it "should be nil if targets has more than one state" do
38
- stub( @event ).targets() { [@state_a, @state_b] }
38
+ @event.should_receive(:targets).at_least(1).times.and_return [@state_a, @state_b]
39
39
  @event.target.should == nil
40
40
  end
41
41
 
42
42
  it "should be the sole state if targets is set and there is only one" do
43
- stub( @event ).targets() { [@state_a] }
43
+ @event.should_receive(:targets).at_least(1).times.and_return [@state_a]
44
44
  @event.target.should == @state_a
45
45
  end
46
46
  end
47
47
 
48
48
  describe 'origins=' do
49
49
  it "should call get_states_list_by_name with its argument" do
50
- mock( @machine ).find_or_create_states_by_name( :initial ) { }
50
+ @machine.should_receive(:find_or_create_states_by_name).with(:initial).and_return []
51
51
  @event.origins= :initial
52
52
  end
53
53
 
54
54
  it "should set @origin to the result" do
55
- mock( @machine ).find_or_create_states_by_name( :initial ) { [:result] }
55
+ @machine.should_receive(:find_or_create_states_by_name).with(:initial).and_return [:result]
56
56
  @event.origins= :initial
57
57
  @event.origins.should == [:result]
58
58
  end
@@ -61,12 +61,12 @@ describe StateFu::Event do
61
61
 
62
62
  describe 'targets=' do
63
63
  it "should call get_states_list_by_name with its argument" do
64
- mock( @machine ).find_or_create_states_by_name( :initial ) { [] }
64
+ @machine.should_receive(:find_or_create_states_by_name).with(:initial).and_return []
65
65
  @event.targets= :initial
66
66
  end
67
67
 
68
68
  it "should set @target to the result" do
69
- mock( @machine ).find_or_create_states_by_name( :initial ) { [:result] }
69
+ @machine.should_receive(:find_or_create_states_by_name).with(:initial).and_return [:result]
70
70
  @event.targets= :initial
71
71
  @event.targets.should == [:result]
72
72
  end
@@ -95,9 +95,12 @@ describe StateFu::Event do
95
95
  describe "given @event.from :initial, :to => :final" do
96
96
  describe "setting attributes" do
97
97
  before do
98
- stub( @machine ).find_or_create_states_by_name( anything ) { |*a| raise(a.inspect) }
99
- stub( @machine ).find_or_create_states_by_name( :initial ) { [@initial] }
100
- stub( @machine ).find_or_create_states_by_name( :final ) { [@final] }
98
+ @machine.should_receive(:find_or_create_states_by_name).with(:initial).and_return [@initial]
99
+ @machine.should_receive(:find_or_create_states_by_name).with(:final).and_return [@final]
100
+
101
+ # stub( @machine ).find_or_create_states_by_name( anything ) { |*a| raise(a.inspect) }
102
+ # stub( @machine ).find_or_create_states_by_name( :initial ) { [@initial] }
103
+ # stub( @machine ).find_or_create_states_by_name( :final ) { [@final] }
101
104
  end
102
105
 
103
106
  it "should call @machine.find_or_create_states_by_name() with :initial and :final" do
@@ -118,23 +121,15 @@ describe StateFu::Event do
118
121
 
119
122
  describe "given @event.from <Array>, :to => <Array>" do
120
123
  it "should call @machine.find_or_create_states_by_name() with both arrays" do
121
- stub( @machine ).find_or_create_states_by_name(:initial, :start) do
122
- [@initial, @start]
123
- end
124
- stub( @machine ).find_or_create_states_by_name(:final, :end) do
125
- [@final, @end]
126
- end
127
-
124
+ @machine.should_receive(:find_or_create_states_by_name).with(:initial, :start).and_return [@initial, @start]
125
+ @machine.should_receive(:find_or_create_states_by_name).with(:final, :end).and_return [@final, @end]
128
126
  @event.from( [:initial, :start], :to => [:final, :end] )
129
127
  end
130
128
  end
131
129
 
132
130
  describe "given @event.from :ALL, :to => :ALL" do
133
131
  it "should set origins and targets to @machine.states" do
134
- stub( @machine ).states() { [:all, :of, :them ] }
135
- stub( @machine ).find_or_create_states_by_name(anything) do |x|
136
- x
137
- end
132
+ @machine.should_receive(:states).any_number_of_times.and_return [:all, :of, :them]
138
133
  @event.from( :ALL, :to => :ALL )
139
134
  @event.origins.should == [:all, :of, :them ]
140
135
  @event.targets.should == [:all, :of, :them ]
@@ -146,7 +141,7 @@ describe StateFu::Event do
146
141
  describe '.to()' do
147
142
  describe "given :final" do
148
143
  it "should set @event.target to machine.find_or_create_states_by_name( :final )" do
149
- mock( @machine ).find_or_create_states_by_name(:final) { [@final] }
144
+ @machine.should_receive(:find_or_create_states_by_name).with(:final).and_return [@final]
150
145
  @event.to :final
151
146
  @event.targets.should == [@final]
152
147
  end
@@ -157,16 +152,16 @@ describe StateFu::Event do
157
152
 
158
153
  describe 'origin_names' do
159
154
  it "should return an array of state names in origin when origin is not nil" do
160
- mock( @machine ).find_or_create_states_by_name(:initial) { [@initial] }
161
- mock( @machine ).find_or_create_states_by_name(:final) { [@final] }
155
+ @machine.should_receive(:find_or_create_states_by_name).with(:initial).and_return [@initial]
156
+ @machine.should_receive(:find_or_create_states_by_name).with(:final).and_return [@final]
162
157
  @event.from :initial, :to => :final
163
158
  @event.origin.should == @initial
164
- mock( @initial ).to_sym().times(any_times) { :initial }
159
+ @initial.should_receive(:to_sym).any_number_of_times.and_return(:initial)
165
160
  @event.origin_names.should == [:initial]
166
161
  end
167
162
 
168
163
  it "should return nil when origin is nil" do
169
- mock( @event ).origins().times(any_times) { nil }
164
+ @event.should_receive(:origins).any_number_of_times.and_return nil
170
165
  @event.origin_names.should == nil
171
166
  end
172
167
 
@@ -174,37 +169,37 @@ describe StateFu::Event do
174
169
 
175
170
  describe 'target_names' do
176
171
  it "should return an array of state names in target when target is not nil" do
177
- mock( @event ).targets.times( any_times ) { [@final] }
178
- mock( @final ).to_sym { :final }
172
+ @event.should_receive(:targets).any_number_of_times.and_return [@final]
173
+ @final.should_receive(:to_sym).any_number_of_times.and_return(:final)
179
174
  @event.target_names.should == [:final]
180
175
  end
181
176
 
182
177
  it "should return nil when target is nil" do
183
- mock( @event ).targets().times(any_times) { nil }
178
+ @event.should_receive(:targets).any_number_of_times.and_return nil
184
179
  @event.target_names.should == nil
185
180
  end
186
181
  end
187
182
 
188
183
  describe 'to?' do
189
184
  it "should return true given a symbol which is the name of a state in @target" do
190
- mock( @event ).targets.times(any_times) { [StateFu::State.new(@machine,:a)] }
185
+ @event.should_receive(:targets).any_number_of_times.and_return [StateFu::State.new(@machine,:a)]
191
186
  @event.to?( :a ).should == true
192
187
  end
193
188
 
194
189
  it "should return false given a symbol which is not the name of a state in @target" do
195
- mock( @event ).targets.times(any_times) { [StateFu::State.new(@machine,:a)] }
190
+ @event.should_receive(:targets).any_number_of_times.and_return [StateFu::State.new(@machine,:a)]
196
191
  @event.to?( :b ).should == false
197
192
  end
198
193
  end
199
194
 
200
195
  describe 'from?' do
201
196
  it "should return true given a symbol which is the name of a state in @origin" do
202
- mock( @event ).origins.times(any_times) { [StateFu::State.new(@machine,:a)] }
197
+ @event.should_receive(:origins).any_number_of_times.and_return [StateFu::State.new(@machine,:a)]
203
198
  @event.from?( :a ).should == true
204
199
  end
205
200
 
206
201
  it "should return nil given a symbol which is not the name of a state in @origin" do
207
- mock( @event ).origins().times(any_times) { [StateFu::State.new(@machine,:a)] }
202
+ @event.should_receive(:origins).any_number_of_times.and_return [StateFu::State.new(@machine,:a)]
208
203
  @event.from?( :b ).should == nil
209
204
  end
210
205
  end
@@ -10,12 +10,12 @@ describe StateFu::Lathe do
10
10
  @state = Object.new()
11
11
  @event = Object.new()
12
12
 
13
- stub(@machine).tools() { [].extend( StateFu::ToolArray ) }
13
+ @machine.should_receive(:tools).any_number_of_times.and_return([].extend( StateFu::ToolArray ))
14
14
  @lathe = StateFu::Lathe.new( @machine )
15
- @states = [].extend StateFu::StateArray
16
- stub( @machine ).states() { @states }
15
+ @states = [].extend StateFu::StateArray
16
+ @machine.should_receive(:states).any_number_of_times.and_return(@states)
17
17
  @events = [].extend StateFu::EventArray
18
- stub( @machine ).events() { @events }
18
+ @machine.should_receive(:events).any_number_of_times.and_return(@events)
19
19
  end
20
20
 
21
21
  describe "constructor" do
@@ -29,7 +29,7 @@ describe StateFu::Lathe do
29
29
 
30
30
  it "should accept a state_or_event (state / event ) and if given one, be a child" do
31
31
  options = {}
32
- mock( @state ).apply!( options ) {}
32
+ @state.should_receive(:apply!).with(options)
33
33
  lathe = StateFu::Lathe.new( @machine, @state )
34
34
  lathe.should be_kind_of( StateFu::Lathe )
35
35
  lathe.machine.should == @machine
@@ -63,10 +63,9 @@ describe StateFu::Lathe do
63
63
  options = {:banana => :flower}
64
64
  @state = Object.new()
65
65
  @child = Object.new()
66
- # can't mock the block :(
67
- mock( StateFu::State ).new( @machine, :wobble, options ) { @state }
68
- mock( StateFu::Lathe ).new( @machine, @state, options ) { @child }
69
- mock( @child )
66
+ StateFu::State.should_receive(:new).with(@machine, :wobble, options).and_return(@state)
67
+ StateFu::Lathe.should_receive(:new).with(@machine, @state, options).and_return(@child)
68
+ # TODO mock the block
70
69
  @lathe.state( :wobble, options )
71
70
  end
72
71
 
@@ -183,10 +182,9 @@ describe StateFu::Lathe do
183
182
  options = {:banana => :flower}
184
183
  @event = Object.new()
185
184
  @child = Object.new()
186
- # can't mock the block :(
187
- mock( StateFu::Event ).new( @machine, :wobble, options ) { @event }
188
- mock( StateFu::Lathe ).new( @machine, @event, options ) { @child }
189
- mock( @child )
185
+ # TODO mock the block
186
+ StateFu::Event.should_receive(:new).with(@machine, :wobble, options).and_return(@event)
187
+ StateFu::Lathe.should_receive(:new).with(@machine, @event, options).and_return(@child)
190
188
  @lathe.event( :wobble, options )
191
189
  end
192
190
 
@@ -327,7 +325,7 @@ describe StateFu::Lathe do
327
325
 
328
326
  describe "helper" do
329
327
  it "should call machine.helper *args" do
330
- mock( @machine ).helper( :fee, :fi, :fo, :fum )
328
+ @machine.should_receive(:helper).with( :fee, :fi, :fo, :fum )
331
329
  @lathe.helper( :fee, :fi, :fo, :fum )
332
330
  end
333
331
  end
@@ -380,7 +378,7 @@ describe StateFu::Lathe do
380
378
 
381
379
  describe ".event(:name)" do
382
380
  before do
383
- mock( @machine ).find_or_create_states_by_name( @lathe.state_or_event ).at_least(1) { @lathe.state_or_event }
381
+ @machine.should_receive(:find_or_create_states_by_name).with(@lathe.state_or_event).at_least(1).times.and_return(@lathe.state_or_event)
384
382
  end
385
383
 
386
384
  it "should create the named event if it does not exist" do
@@ -477,25 +475,23 @@ describe StateFu::Lathe do
477
475
  @master = @lathe
478
476
  @event = @lathe.event( :go )
479
477
  @lathe = StateFu::Lathe.new( @machine, @event )
480
- stub( @machine ).find_or_create_states_by_name(:a) { [:a] }
481
- stub( @machine ).find_or_create_states_by_name(:b) { [:b] }
482
478
  end
483
479
 
484
480
  describe ".from" do
485
481
  it "should create any states mentioned which do not exist" do
486
- mock( @machine ).find_or_create_states_by_name(:a, :b) { [:a, :b] }
482
+ @machine.should_receive(:find_or_create_states_by_name).with(:a, :b).and_return([:a, :b])
487
483
  @lathe.from( :a, :b )
488
484
  end
489
485
 
490
486
  it "should set the origins to the result of machine.find_or_create_states_by_name" do
491
- mock( @machine ).find_or_create_states_by_name(:a, :b) { [:a, :b] }
487
+ @machine.should_receive(:find_or_create_states_by_name).with(:a, :b).and_return([:a, :b])
492
488
  @lathe.from( :a, :b )
493
489
  @event.origins.should == [:a, :b]
494
490
  end
495
491
 
496
492
  it "should accumulate @origins on successive invocations" do
497
- mock( @machine ).find_or_create_states_by_name(:a, :b) { [:a, :b] }
498
- mock( @machine ).find_or_create_states_by_name(:x, :y) { [:x, :y] }
493
+ @machine.should_receive(:find_or_create_states_by_name).with(:a, :b).and_return([:a, :b])
494
+ @machine.should_receive(:find_or_create_states_by_name).with(:x, :y).and_return([:x, :y])
499
495
  @lathe.from( :a, :b )
500
496
  @event.origins.should == [:a, :b]
501
497
  @lathe.from( :x, :y )
@@ -503,10 +499,10 @@ describe StateFu::Lathe do
503
499
  end
504
500
 
505
501
  it "should set / update both origin and target if a hash is given" do
506
- mock( @machine ).find_or_create_states_by_name(:a) { [:a] }
507
- mock( @machine ).find_or_create_states_by_name(:b) { [:b] }
508
- mock( @machine ).find_or_create_states_by_name(:a, :b) { [:a, :b] }
509
- mock( @machine ).find_or_create_states_by_name(:x, :y) { [:x, :y] }
502
+ @machine.should_receive(:find_or_create_states_by_name).with(:a).and_return [:a]
503
+ @machine.should_receive(:find_or_create_states_by_name).with(:b).and_return [:b]
504
+ @machine.should_receive(:find_or_create_states_by_name).with(:a, :b).and_return([:a, :b])
505
+ @machine.should_receive(:find_or_create_states_by_name).with(:x, :y).and_return([:x, :y])
510
506
  @lathe.from( :a => :b )
511
507
  @event.origin.should == :a
512
508
  @event.target.should == :b
@@ -519,20 +515,20 @@ describe StateFu::Lathe do
519
515
  end
520
516
 
521
517
  describe ".to" do
522
- it "should create any states mentioned which do not exist" do
523
- mock( @machine ).find_or_create_states_by_name(:a, :b) { [:a, :b] }
518
+ it "should create any states mentioned which do not exist" do
519
+ @machine.should_receive(:find_or_create_states_by_name).with(:a, :b).and_return([:a, :b])
524
520
  @lathe.to( :a, :b )
525
521
  end
526
522
 
527
523
  it "should set the targets to the result of machine.find_or_create_states_by_name" do
528
- mock( @machine ).find_or_create_states_by_name(:a, :b) { [:a, :b] }
524
+ @machine.should_receive(:find_or_create_states_by_name).with(:a, :b).and_return([:a, :b])
529
525
  @lathe.to( :a, :b )
530
526
  @event.targets.should == [:a, :b]
531
527
  end
532
528
 
533
529
  it "should update @origins on successive invocations" do
534
- mock( @machine ).find_or_create_states_by_name(:a, :b) { [:a, :b] }
535
- mock( @machine ).find_or_create_states_by_name(:x, :y) { [:x, :y] }
530
+ @machine.should_receive(:find_or_create_states_by_name).with(:a, :b).and_return([:a, :b])
531
+ @machine.should_receive(:find_or_create_states_by_name).with(:x, :y).and_return([:x, :y])
536
532
  @lathe.to( :a, :b )
537
533
  @event.targets.should == [:a, :b]
538
534
  @lathe.to( :x, :y )
@@ -24,8 +24,8 @@ describe StateFu::Machine do
24
24
 
25
25
  it "should create a new machine and bind! it" do
26
26
  @machine = Object.new
27
- mock(@machine).bind!(Klass, :moose, {})
28
- mock(StateFu::Machine).new({}) { @machine }
27
+ @machine.should_receive(:bind!).with(Klass, :moose, {})
28
+ StateFu::Machine.should_receive(:new).and_return @machine
29
29
  StateFu::Machine.for_class Klass, :moose
30
30
  end
31
31
 
@@ -73,7 +73,7 @@ describe StateFu::Machine do
73
73
  describe ".bind!" do
74
74
  it "should call StateFu::Machine.bind! with itself and its arguments" do
75
75
  field_name = :my_field_name
76
- mock( StateFu::Machine ).bind!( @m, Klass, :newname, field_name ) {}
76
+ StateFu::Machine.should_receive(:bind!).with @m, Klass, :newname, field_name
77
77
  @m.bind!( Klass, :newname, field_name )
78
78
  end
79
79
 
@@ -119,7 +119,7 @@ describe StateFu::Machine do
119
119
  end
120
120
 
121
121
  it "should return the first state if one exists" do
122
- stub( @m ).states() { [:a, :b, :c] }
122
+ @m.should_receive(:states).and_return [:a, :b, :c]
123
123
  @m.initial_state.should == :a
124
124
  end
125
125
 
@@ -34,18 +34,21 @@ describe StateFu::MethodFactory do
34
34
  end
35
35
  it "should call the original method_missing on an unexpected method call" do
36
36
  @k = Klass.new
37
- mock(@k).callme
37
+ @k.should_receive(:callme)
38
38
  @k.whut?
39
39
  end
40
40
  end
41
41
 
42
42
  describe "event creation methods" do
43
43
  it "should call method_missing" do
44
+ pending "RSpec mocking makes this tricky ..."
45
+ @obj.should_receive(:method_missing).with(:simple_event!)
44
46
  mock( @obj ).method_missing( :simple_event! )
45
47
  @obj.simple_event!
46
48
  end
47
49
 
48
50
  it "should call state_fu!" do
51
+ pending "RSpec mocking makes this tricky ..."
49
52
  mock.proxy( StateFu::Binding ).new( Klass.state_fu_machine, @obj, StateFu::DEFAULT )
50
53
  @obj
51
54
  @obj.private_methods.map(&:to_sym).should include(:state_fu_field)
@@ -61,6 +64,8 @@ describe StateFu::MethodFactory do
61
64
  end
62
65
 
63
66
  it "should call binding.fire!( :simple_event ... ) with any specified args" do
67
+ pending "RSpec mocking makes this tricky ..."
68
+
64
69
  mock.instance_of( StateFu::Binding ).fire_transition!( is_a(StateFu::Event), is_a(StateFu::State), :aa, :bb, {:cc => "dd"} )
65
70
  t = @obj.simple_event!( :aa, :bb, :cc => "dd" )
66
71
  end
@@ -127,7 +132,7 @@ describe StateFu::MethodFactory do
127
132
  end
128
133
 
129
134
  it "should be false when the binding says it\'s not can_transition?" do
130
- mock( @binding ).can_transition?( is_a(StateFu::Event), is_a(StateFu::State) ) { false }
135
+ @binding.should_receive(:can_transition?).with(@machine.events[:simple_event], @machine.states[:targ]).and_return false
131
136
  @binding.can_simple_event?.should == false
132
137
  end
133
138
  end # can_transition?
@@ -48,8 +48,8 @@ describe StateFu::State do
48
48
 
49
49
  it "should call machine.events.from(self)" do
50
50
  machine_events = Object.new
51
- mock( @machine ).events { machine_events }
52
- mock( machine_events ).from( @state ) { nil }
51
+ @machine.should_receive(:events).and_return machine_events
52
+ machine_events.should_receive(:from).with(@state).and_return nil
53
53
  @state.events
54
54
  end
55
55
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state-fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.1
4
+ version: 0.13.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Lee
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-18 00:00:00 +11:00
12
+ date: 2009-12-13 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15