rr 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGES +5 -0
  2. data/README +20 -9
  3. data/Rakefile +1 -1
  4. data/examples/example_suite.rb +1 -1
  5. data/examples/high_level_example.rb +4 -4
  6. data/examples/rr/double/double_dispatching_example.rb +41 -41
  7. data/examples/rr/double/double_verify_example.rb +1 -1
  8. data/examples/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +2 -2
  9. data/examples/rr/extensions/instance_methods_creator_example.rb +48 -38
  10. data/examples/rr/extensions/instance_methods_space_example.rb +8 -8
  11. data/examples/rr/rspec/rspec_adapter_example.rb +9 -9
  12. data/examples/rr/rspec/rspec_usage_example.rb +16 -2
  13. data/examples/rr/scenario_creator_example.rb +400 -0
  14. data/examples/rr/scenario_example.rb +19 -4
  15. data/examples/rr/scenario_method_proxy_example.rb +71 -0
  16. data/examples/rr/space/space_create_example.rb +93 -106
  17. data/examples/rr/space/space_example.rb +2 -2
  18. data/examples/rr/space/space_register_example.rb +3 -3
  19. data/examples/rr/space/space_reset_example.rb +11 -11
  20. data/examples/rr/space/space_verify_example.rb +12 -12
  21. data/examples/rr/test_unit/test_unit_integration_test.rb +11 -2
  22. data/lib/rr.rb +10 -12
  23. data/lib/rr/errors/scenario_definition_error.rb +6 -0
  24. data/lib/rr/extensions/instance_methods.rb +84 -84
  25. data/lib/rr/scenario.rb +18 -3
  26. data/lib/rr/scenario_creator.rb +233 -0
  27. data/lib/rr/scenario_method_proxy.rb +19 -0
  28. data/lib/rr/space.rb +12 -32
  29. metadata +7 -13
  30. data/examples/rr/do_not_allow_creator_example.rb +0 -111
  31. data/examples/rr/mock_creator_example.rb +0 -87
  32. data/examples/rr/mock_probe_creator_example.rb +0 -120
  33. data/examples/rr/stub_creator_example.rb +0 -96
  34. data/examples/rr/stub_probe_creator_example.rb +0 -127
  35. data/lib/rr/creator.rb +0 -16
  36. data/lib/rr/do_not_allow_creator.rb +0 -33
  37. data/lib/rr/mock_creator.rb +0 -26
  38. data/lib/rr/mock_probe_creator.rb +0 -36
  39. data/lib/rr/stub_creator.rb +0 -30
  40. data/lib/rr/stub_probe_creator.rb +0 -42
@@ -5,9 +5,11 @@ describe Scenario, :shared => true do
5
5
  before do
6
6
  @space = Space.new
7
7
  @object = Object.new
8
- @method_name = :foobar
9
- @double = @space.create_double(@object, @method_name)
10
- @scenario = @space.create_scenario(@double)
8
+ def @object.foobar(a, b)
9
+ [b, a]
10
+ end
11
+ @double = @space.double(@object, :foobar)
12
+ @scenario = @space.scenario(@double)
11
13
  end
12
14
  end
13
15
 
@@ -352,6 +354,19 @@ describe Scenario, "#implemented_by" do
352
354
  end
353
355
  end
354
356
 
357
+ describe Scenario, "#implemented_by_original_method" do
358
+ it_should_behave_like "RR::Scenario"
359
+
360
+ it "returns the scenario object" do
361
+ @scenario.implemented_by_original_method.should === @scenario
362
+ end
363
+
364
+ it "sets the implementation to the original method" do
365
+ @scenario.implemented_by_original_method
366
+ @scenario.call(1, 2).should == [2, 1]
367
+ end
368
+ end
369
+
355
370
  describe Scenario, "#call implemented by a proc" do
356
371
  it_should_behave_like "RR::Scenario"
357
372
 
@@ -386,7 +401,7 @@ describe Scenario, "#call implemented by a proc" do
386
401
 
387
402
  it "raises ScenarioOrderError when ordered and called out of order" do
388
403
  scenario1 = @scenario
389
- scenario2 = @space.create_scenario(@double)
404
+ scenario2 = @space.scenario(@double)
390
405
 
391
406
  scenario1.with(1).returns {:return_1}.ordered.once
392
407
  scenario2.with(2).returns {:return_2}.ordered.once
@@ -0,0 +1,71 @@
1
+ require "examples/example_helper"
2
+
3
+ module RR
4
+ describe ScenarioMethodProxy, :shared => true do
5
+ before(:each) do
6
+ @space = Space.new
7
+ @subject = Object.new
8
+ @creator = @space.scenario_creator
9
+ @creator.mock
10
+ end
11
+
12
+ it "initializes proxy with passed in creator" do
13
+ class << @proxy
14
+ attr_reader :creator
15
+ end
16
+ @proxy.creator.should === @creator
17
+ end
18
+ end
19
+
20
+ describe ScenarioMethodProxy, ".new without block" do
21
+ it_should_behave_like "RR::ScenarioMethodProxy"
22
+
23
+ before do
24
+ @proxy = ScenarioMethodProxy.new(@space, @creator, @subject)
25
+ end
26
+
27
+ it "clears out all methods from proxy" do
28
+ proxy_subclass = Class.new(ScenarioMethodProxy) do
29
+ def i_should_be_a_scenario
30
+ end
31
+ end
32
+ proxy_subclass.instance_methods.should include('i_should_be_a_scenario')
33
+
34
+ proxy = proxy_subclass.new(@space, @creator, @subject)
35
+ proxy.i_should_be_a_scenario.should be_instance_of(Scenario)
36
+ end
37
+ end
38
+
39
+ describe ScenarioMethodProxy, ".new with block" do
40
+ it_should_behave_like "RR::ScenarioMethodProxy"
41
+
42
+ before do
43
+ @proxy = ScenarioMethodProxy.new(@space, @creator, @subject) do |b|
44
+ b.foobar(1, 2) {:one_two}
45
+ b.foobar(1) {:one}
46
+ b.foobar.with_any_args {:default}
47
+ b.baz() {:baz_result}
48
+ end
49
+ end
50
+
51
+ it "creates doubles" do
52
+ @subject.foobar(1, 2).should == :one_two
53
+ @subject.foobar(1).should == :one
54
+ @subject.foobar(:something).should == :default
55
+ @subject.baz.should == :baz_result
56
+ end
57
+
58
+ it "clears out all methods from proxy" do
59
+ proxy_subclass = Class.new(ScenarioMethodProxy) do
60
+ def i_should_be_a_scenario
61
+ end
62
+ end
63
+ proxy_subclass.instance_methods.should include('i_should_be_a_scenario')
64
+
65
+ proxy_subclass.new(@space, @creator, @subject) do |m|
66
+ m.i_should_be_a_scenario.should be_instance_of(Scenario)
67
+ end
68
+ end
69
+ end
70
+
71
+ end
@@ -1,7 +1,7 @@
1
1
  require "examples/example_helper"
2
2
 
3
3
  module RR
4
- describe Space, "#mock_creator" do
4
+ describe Space, "#scenario_method_proxy", :shared => true do
5
5
  it_should_behave_like "RR::Space"
6
6
 
7
7
  before do
@@ -9,27 +9,50 @@ describe Space, "#mock_creator" do
9
9
  @object = Object.new
10
10
  end
11
11
 
12
- it "creates a MockCreator" do
13
- creator = @space.mock_creator(@object)
14
- creator.foobar(1) {:baz}
15
- @object.foobar(1).should == :baz
16
- proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
12
+ it "creates a ScenarioMethodProxy" do
13
+ proxy = @space.scenario_method_proxy(@creator, @object)
14
+ proxy.should be_instance_of(ScenarioMethodProxy)
17
15
  end
18
16
 
19
- it "creates a mock Scenario for method when passed a second argument" do
20
- creator = @space.mock_creator(@object, :foobar).with(1) {:baz}
21
- @object.foobar(1).should == :baz
22
- proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
17
+ it "sets space to self" do
18
+ proxy = @space.scenario_method_proxy(@creator, @object)
19
+ class << proxy
20
+ attr_reader :space
21
+ end
22
+ proxy.space.should === @space
23
+ end
24
+
25
+ it "sets creator to passed in creator" do
26
+ proxy = @space.scenario_method_proxy(@creator, @object)
27
+ class << proxy
28
+ attr_reader :creator
29
+ end
30
+ proxy.creator.should === @creator
23
31
  end
24
32
 
25
33
  it "raises error if passed a method name and a block" do
26
34
  proc do
27
- @space.mock_creator(@object, :foobar) {}
35
+ @space.scenario_method_proxy(@creator, @object, :foobar) {}
28
36
  end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
29
37
  end
38
+ end
39
+
40
+ describe Space, "#scenario_method_proxy with a Mock strategy" do
41
+ it_should_behave_like "RR::Space#scenario_method_proxy"
42
+
43
+ before do
44
+ @creator = @space.scenario_creator
45
+ @creator.mock
46
+ end
47
+
48
+ it "creates a mock Scenario for method when passed a second argument" do
49
+ @space.scenario_method_proxy(@creator, @object, :foobar).with(1) {:baz}
50
+ @object.foobar(1).should == :baz
51
+ proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
52
+ end
30
53
 
31
54
  it "uses block definition when passed a block" do
32
- creator = @space.mock_creator(@object) do |c|
55
+ @space.scenario_method_proxy(@creator, @object) do |c|
33
56
  c.foobar(1) {:baz}
34
57
  end
35
58
  @object.foobar(1).should == :baz
@@ -37,36 +60,22 @@ describe Space, "#mock_creator" do
37
60
  end
38
61
  end
39
62
 
40
- describe Space, "#stub_creator" do
41
- it_should_behave_like "RR::Space"
63
+ describe Space, "#scenario_method_proxy with a Stub strategy" do
64
+ it_should_behave_like "RR::Space#scenario_method_proxy"
42
65
 
43
66
  before do
44
- @space = Space.new
45
- @object = Object.new
46
- @method_name = :foobar
47
- end
48
-
49
- it "creates a StubCreator" do
50
- creator = @space.stub_creator(@object)
51
- creator.foobar {:baz}
52
- @object.foobar.should == :baz
53
- @object.foobar.should == :baz
67
+ @creator = @space.scenario_creator
68
+ @creator.stub
54
69
  end
55
70
 
56
71
  it "creates a stub Scenario for method when passed a second argument" do
57
- creator = @space.stub_creator(@object, :foobar).with(1) {:baz}
72
+ @space.scenario_method_proxy(@creator, @object, :foobar).with(1) {:baz}
58
73
  @object.foobar(1).should == :baz
59
74
  @object.foobar(1).should == :baz
60
75
  end
61
76
 
62
- it "raises error if passed a method name and a block" do
63
- proc do
64
- @space.stub_creator(@object, :foobar) {}
65
- end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
66
- end
67
-
68
77
  it "uses block definition when passed a block" do
69
- creator = @space.stub_creator(@object) do |c|
78
+ @space.scenario_method_proxy(@creator, @object) do |c|
70
79
  c.foobar(1) {:return_value}
71
80
  c.foobar.with_any_args {:default}
72
81
  c.baz(1) {:baz_value}
@@ -77,39 +86,25 @@ describe Space, "#stub_creator" do
77
86
  end
78
87
  end
79
88
 
80
- describe Space, "#mock_probe_creator" do
81
- it_should_behave_like "RR::Space"
89
+ describe Space, "#scenario_method_proxy with a Mock Probe strategy" do
90
+ it_should_behave_like "RR::Space#scenario_method_proxy"
82
91
 
83
92
  before do
84
- @space = Space.new
85
- @object = Object.new
86
- @method_name = :foobar
93
+ @creator = @space.scenario_creator
94
+ @creator.mock.probe
87
95
  def @object.foobar(*args)
88
96
  :original_foobar
89
97
  end
90
98
  end
91
99
 
92
- it "creates a MockProbeCreator" do
93
- creator = @space.mock_probe_creator(@object)
94
- creator.foobar(1)
95
- @object.foobar(1).should == :original_foobar
96
- proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
97
- end
98
-
99
100
  it "creates a mock probe Scenario for method when passed a second argument" do
100
- creator = @space.mock_probe_creator(@object, :foobar).with(1)
101
+ @space.scenario_method_proxy(@creator, @object, :foobar).with(1)
101
102
  @object.foobar(1).should == :original_foobar
102
103
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
103
104
  end
104
105
 
105
- it "raises error if passed a method name and a block" do
106
- proc do
107
- @space.mock_probe_creator(@object, :foobar) {}
108
- end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
109
- end
110
-
111
106
  it "uses block definition when passed a block" do
112
- creator = @space.mock_probe_creator(@object) do |c|
107
+ @space.scenario_method_proxy(@creator, @object) do |c|
113
108
  c.foobar(1)
114
109
  end
115
110
  @object.foobar(1).should == :original_foobar
@@ -117,39 +112,25 @@ describe Space, "#mock_probe_creator" do
117
112
  end
118
113
  end
119
114
 
120
- describe Space, "#stub_probe_creator" do
121
- it_should_behave_like "RR::Space"
115
+ describe Space, "#scenario_method_proxy with a Stub Probe strategy" do
116
+ it_should_behave_like "RR::Space#scenario_method_proxy"
122
117
 
123
118
  before do
124
- @space = Space.new
125
- @object = Object.new
126
- @method_name = :foobar
119
+ @creator = @space.scenario_creator
120
+ @creator.stub.probe
127
121
  def @object.foobar(*args)
128
122
  :original_foobar
129
123
  end
130
124
  end
131
125
 
132
- it "creates a StubProbeCreator" do
133
- creator = @space.stub_probe_creator(@object)
134
- creator.foobar
135
- @object.foobar(1).should == :original_foobar
136
- @object.foobar(1).should == :original_foobar
137
- end
138
-
139
126
  it "creates a stub probe Scenario for method when passed a second argument" do
140
- creator = @space.stub_probe_creator(@object, :foobar)
127
+ @space.scenario_method_proxy(@creator, @object, :foobar)
141
128
  @object.foobar(1).should == :original_foobar
142
129
  @object.foobar(1).should == :original_foobar
143
130
  end
144
131
 
145
- it "raises error if passed a method name and a block" do
146
- proc do
147
- @space.stub_probe_creator(@object, :foobar) {}
148
- end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
149
- end
150
-
151
132
  it "uses block definition when passed a block" do
152
- creator = @space.stub_probe_creator(@object) do |c|
133
+ @space.scenario_method_proxy(@creator, @object) do |c|
153
134
  c.foobar(1)
154
135
  end
155
136
  @object.foobar(1).should == :original_foobar
@@ -157,40 +138,46 @@ describe Space, "#stub_probe_creator" do
157
138
  end
158
139
  end
159
140
 
160
- describe Space, "#do_not_allow_creator" do
161
- it_should_behave_like "RR::Space"
141
+ describe Space, "#scenario_method_proxy with a Do Not Allow strategy" do
142
+ it_should_behave_like "RR::Space#scenario_method_proxy"
162
143
 
163
144
  before do
164
- @space = Space.new
165
- @object = Object.new
166
- end
167
-
168
- it "creates a MockCreator" do
169
- creator = @space.do_not_allow_creator(@object)
170
- creator.foobar(1)
171
- proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
145
+ @creator = @space.scenario_creator
146
+ @creator.do_not_call
172
147
  end
173
148
 
174
149
  it "creates a do not allow Scenario for method when passed a second argument" do
175
- creator = @space.do_not_allow_creator(@object, :foobar).with(1)
150
+ @space.scenario_method_proxy(@creator, @object, :foobar).with(1)
176
151
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
177
152
  end
178
153
 
179
- it "raises error if passed a method name and a block" do
180
- proc do
181
- @space.do_not_allow_creator(@object, :foobar) {}
182
- end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
183
- end
184
-
185
154
  it "uses block definition when passed a block" do
186
- creator = @space.do_not_allow_creator(@object) do |c|
155
+ @space.scenario_method_proxy(@creator, @object) do |c|
187
156
  c.foobar(1)
188
157
  end
189
158
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
190
159
  end
191
160
  end
192
161
 
193
- describe Space, "#create_scenario" do
162
+ describe Space, "#scenario_creator" do
163
+ it_should_behave_like "RR::Space"
164
+
165
+ before do
166
+ @space = Space.new
167
+ @object = Object.new
168
+ @creator = @space.scenario_creator
169
+ end
170
+
171
+ it "sets the space" do
172
+ @creator.space.should === @space
173
+ end
174
+
175
+ it "creates a ScenarioCreator" do
176
+ @creator.should be_instance_of(ScenarioCreator)
177
+ end
178
+ end
179
+
180
+ describe Space, "#scenario" do
194
181
  it_should_behave_like "RR::Space"
195
182
 
196
183
  before do
@@ -200,17 +187,17 @@ describe Space, "#create_scenario" do
200
187
  end
201
188
 
202
189
  it "creates a Scenario and registers it to the double" do
203
- double = @space.create_double(@object, @method_name)
190
+ double = @space.double(@object, @method_name)
204
191
  def double.scenarios
205
192
  @scenarios
206
193
  end
207
194
 
208
- scenario = @space.create_scenario(double)
195
+ scenario = @space.scenario(double)
209
196
  double.scenarios.should include(scenario)
210
197
  end
211
198
  end
212
199
 
213
- describe Space, "#create_double" do
200
+ describe Space, "#double" do
214
201
  it_should_behave_like "RR::Space"
215
202
 
216
203
  before do
@@ -223,14 +210,14 @@ describe Space, "#create_double" do
223
210
  (object1 === object2).should be_true
224
211
  object1.__id__.should_not == object2.__id__
225
212
 
226
- double1 = @space.create_double(object1, :foobar)
227
- double2 = @space.create_double(object2, :foobar)
213
+ double1 = @space.double(object1, :foobar)
214
+ double2 = @space.double(object2, :foobar)
228
215
 
229
216
  double1.should_not == double2
230
217
  end
231
218
  end
232
219
 
233
- describe Space, "#create_double when double does not exist" do
220
+ describe Space, "#double when double does not exist" do
234
221
  it_should_behave_like "RR::Space"
235
222
 
236
223
  before do
@@ -243,28 +230,28 @@ describe Space, "#create_double when double does not exist" do
243
230
  end
244
231
 
245
232
  it "returns double and adds double to double list when method_name is a symbol" do
246
- double = @space.create_double(@object, @method_name)
247
- @space.doubles[@object][@method_name].should === double
233
+ double = @space.double(@object, @method_name)
234
+ @space.double(@object, @method_name).should === double
248
235
  double.space.should === @space
249
236
  double.object.should === @object
250
237
  double.method_name.should === @method_name
251
238
  end
252
239
 
253
240
  it "returns double and adds double to double list when method_name is a string" do
254
- double = @space.create_double(@object, 'foobar')
255
- @space.doubles[@object][@method_name].should === double
241
+ double = @space.double(@object, 'foobar')
242
+ @space.double(@object, @method_name).should === double
256
243
  double.space.should === @space
257
244
  double.object.should === @object
258
245
  double.method_name.should === @method_name
259
246
  end
260
247
 
261
248
  it "overrides the method when passing a block" do
262
- double = @space.create_double(@object, @method_name)
249
+ double = @space.double(@object, @method_name)
263
250
  @object.methods.should include("__rr__#{@method_name}")
264
251
  end
265
252
  end
266
253
 
267
- describe Space, "#create_double when double exists" do
254
+ describe Space, "#double when double exists" do
268
255
  it_should_behave_like "RR::Space"
269
256
 
270
257
  before do
@@ -278,14 +265,14 @@ describe Space, "#create_double when double exists" do
278
265
 
279
266
  it "returns the existing double" do
280
267
  original_foobar_method = @object.method(:foobar)
281
- double = @space.create_double(@object, 'foobar')
268
+ double = @space.double(@object, 'foobar')
282
269
 
283
270
  double.original_method.should == original_foobar_method
284
271
 
285
- @space.create_double(@object, 'foobar').should === double
272
+ @space.double(@object, 'foobar').should === double
286
273
 
287
274
  double.reset
288
275
  @object.foobar.should == :original_foobar
289
276
  end
290
277
  end
291
- end
278
+ end