rr 0.1.15 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,12 @@
1
+ * 0.2.1
2
+ - Added mock_probe
3
+ - Added stub_probe
4
+ - Probe returns the return value of the passed in block, instead of ignoring its return value
5
+ - Scenario#after_call returns the return value of the passed in block
6
+ - Not using method aliasing to store original method
7
+ - Renamed DoubleMethods to InstanceMethods
8
+ - Added InstanceMethods#mock_probe
9
+
1
10
  * 0.1.15
2
11
  - Fixed [#12333] Rebinding original_methods causes blocks not to work
3
12
 
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ def run_suite
21
21
  end
22
22
 
23
23
  PKG_NAME = "rr"
24
- PKG_VERSION = "0.1.15"
24
+ PKG_VERSION = "0.2.1"
25
25
  PKG_FILES = FileList[
26
26
  '[A-Z]*',
27
27
  '*.rb',
@@ -2,7 +2,7 @@ dir = File.dirname(__FILE__)
2
2
  require "#{dir}/environment_fixture_setup"
3
3
  require "examples/rr/space/space_helper"
4
4
  require "examples/rr/expectations/times_called_expectation/times_called_expectation_helper"
5
- require "examples/rr/extensions/double_methods_example_helper"
5
+ require "examples/rr/extensions/instance_methods_example_helper"
6
6
 
7
7
  require "rr/adapters/rspec"
8
8
  Spec::Runner.configure do |config|
@@ -4,7 +4,7 @@ require "#{dir}/example_helper"
4
4
  describe "RR", :shared => true do
5
5
  before(:each) do
6
6
  @obj = Object.new
7
- extend RR::Extensions::DoubleMethods
7
+ extend RR::Extensions::InstanceMethods
8
8
  end
9
9
 
10
10
  after(:each) do
@@ -28,12 +28,6 @@ describe Double, "#bind with an existing method" do
28
28
  @object.foobar
29
29
  rr_foobar_called.should == true
30
30
  end
31
-
32
- it "stores original method in __rr__original_method_name" do
33
- @double.bind
34
- @object.respond_to?(:__rr__original_foobar).should == true
35
- @object.method(:__rr__original_foobar).should == @original_method
36
- end
37
31
  end
38
32
 
39
33
  describe Double, "#bind without an existing method" do
@@ -61,10 +55,5 @@ describe Double, "#bind without an existing method" do
61
55
  @object.foobar
62
56
  rr_foobar_called.should == true
63
57
  end
64
-
65
- it "stores original method in __rr__original_method_name" do
66
- @double.bind
67
- @object.respond_to?(:__rr__original_foobar).should == false
68
- end
69
58
  end
70
59
  end
@@ -55,14 +55,6 @@ describe Double, "#reset when method exists" do
55
55
  @object.methods.should include(@method_name.to_s)
56
56
  @object.foobar.should == :original_foobar
57
57
  end
58
-
59
- it "cleans up by removing the __rr__original_method" do
60
- @double.bind
61
- @object.methods.should include("__rr__foobar")
62
-
63
- @double.reset
64
- @object.methods.should_not include("__rr__foobar")
65
- end
66
58
  end
67
59
 
68
60
  describe Double, "#reset when method with block exists" do
@@ -2,8 +2,8 @@ require "examples/example_helper"
2
2
 
3
3
  module RR
4
4
  module Extensions
5
- describe DoubleMethods, "#anything" do
6
- it_should_behave_like "RR::Extensions::DoubleMethods"
5
+ describe InstanceMethods, "#anything" do
6
+ it_should_behave_like "RR::Extensions::InstanceMethods"
7
7
 
8
8
  it "returns an Anything matcher" do
9
9
  anything.should == WildcardMatchers::Anything.new
@@ -14,8 +14,8 @@ module Extensions
14
14
  end
15
15
  end
16
16
 
17
- describe DoubleMethods, "#is_a" do
18
- it_should_behave_like "RR::Extensions::DoubleMethods"
17
+ describe InstanceMethods, "#is_a" do
18
+ it_should_behave_like "RR::Extensions::InstanceMethods"
19
19
 
20
20
  it "returns an IsA matcher" do
21
21
  is_a(Integer).should == WildcardMatchers::IsA.new(Integer)
@@ -26,8 +26,8 @@ module Extensions
26
26
  end
27
27
  end
28
28
 
29
- describe DoubleMethods, "#numeric" do
30
- it_should_behave_like "RR::Extensions::DoubleMethods"
29
+ describe InstanceMethods, "#numeric" do
30
+ it_should_behave_like "RR::Extensions::InstanceMethods"
31
31
 
32
32
  it "returns an Numeric matcher" do
33
33
  numeric.should == WildcardMatchers::Numeric.new
@@ -38,8 +38,8 @@ module Extensions
38
38
  end
39
39
  end
40
40
 
41
- describe DoubleMethods, "#boolean" do
42
- it_should_behave_like "RR::Extensions::DoubleMethods"
41
+ describe InstanceMethods, "#boolean" do
42
+ it_should_behave_like "RR::Extensions::InstanceMethods"
43
43
 
44
44
  it "returns an Boolean matcher" do
45
45
  boolean.should == WildcardMatchers::Boolean.new
@@ -50,8 +50,8 @@ module Extensions
50
50
  end
51
51
  end
52
52
 
53
- describe DoubleMethods, "#duck_type" do
54
- it_should_behave_like "RR::Extensions::DoubleMethods"
53
+ describe InstanceMethods, "#duck_type" do
54
+ it_should_behave_like "RR::Extensions::InstanceMethods"
55
55
 
56
56
  it "returns a DuckType matcher" do
57
57
  duck_type(:one, :two).should == WildcardMatchers::DuckType.new(:one, :two)
@@ -2,8 +2,8 @@ require "examples/example_helper"
2
2
 
3
3
  module RR
4
4
  module Extensions
5
- describe DoubleMethods, "#mock" do
6
- it_should_behave_like "RR::Extensions::DoubleMethods"
5
+ describe InstanceMethods, "#mock" do
6
+ it_should_behave_like "RR::Extensions::InstanceMethods"
7
7
 
8
8
  before do
9
9
  @subject = Object.new
@@ -33,8 +33,8 @@ module Extensions
33
33
  end
34
34
  end
35
35
 
36
- describe DoubleMethods, "#stub" do
37
- it_should_behave_like "RR::Extensions::DoubleMethods"
36
+ describe InstanceMethods, "#stub" do
37
+ it_should_behave_like "RR::Extensions::InstanceMethods"
38
38
 
39
39
  before do
40
40
  @subject = Object.new
@@ -62,22 +62,26 @@ module Extensions
62
62
  end
63
63
  end
64
64
 
65
- describe DoubleMethods, "#probe" do
66
- it_should_behave_like "RR::Extensions::DoubleMethods"
65
+ describe InstanceMethods, "#probe and #mock_probe" do
66
+ it_should_behave_like "RR::Extensions::InstanceMethods"
67
67
 
68
68
  before do
69
69
  @subject = Object.new
70
70
  end
71
71
 
72
72
  it "sets up the RR probe call chain" do
73
- should_create_probe_call_chain probe(@subject)
73
+ should_create_mock_probe_call_chain probe(@subject)
74
+ end
75
+
76
+ it "sets up the RR probe call chain" do
77
+ should_create_mock_probe_call_chain mock_probe(@subject)
74
78
  end
75
79
 
76
80
  it "sets up the RR probe call chain with rr_probe" do
77
- should_create_probe_call_chain rr_probe(@subject)
81
+ should_create_mock_probe_call_chain rr_mock_probe(@subject)
78
82
  end
79
83
 
80
- def should_create_probe_call_chain(creator)
84
+ def should_create_mock_probe_call_chain(creator)
81
85
  class << @subject
82
86
  def foobar(*args)
83
87
  :original_value
@@ -93,8 +97,38 @@ module Extensions
93
97
  end
94
98
  end
95
99
 
96
- describe DoubleMethods, "#do_not_allow" do
97
- it_should_behave_like "RR::Extensions::DoubleMethods"
100
+ describe InstanceMethods, "#stub_probe" do
101
+ it_should_behave_like "RR::Extensions::InstanceMethods"
102
+
103
+ before do
104
+ @subject = Object.new
105
+ end
106
+
107
+ it "sets up the RR probe call chain" do
108
+ should_create_stub_probe_call_chain stub_probe(@subject)
109
+ end
110
+
111
+ it "sets up the RR probe call chain" do
112
+ should_create_stub_probe_call_chain rr_stub_probe(@subject)
113
+ end
114
+
115
+ def should_create_stub_probe_call_chain(creator)
116
+ class << @subject
117
+ def foobar(*args)
118
+ :original_value
119
+ end
120
+ end
121
+
122
+ scenario = creator.foobar
123
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
124
+ scenario.argument_expectation.class.should == RR::Expectations::AnyArgumentExpectation
125
+
126
+ @subject.foobar(:something).should == :original_value
127
+ end
128
+ end
129
+
130
+ describe InstanceMethods, "#do_not_allow" do
131
+ it_should_behave_like "RR::Extensions::InstanceMethods"
98
132
 
99
133
  before do
100
134
  @subject = Object.new
@@ -2,9 +2,9 @@ require "examples/example_helper"
2
2
 
3
3
  module RR
4
4
  module Extensions
5
- describe DoubleMethods, :shared => true do
5
+ describe InstanceMethods, :shared => true do
6
6
  before do
7
- extend RR::Extensions::DoubleMethods
7
+ extend RR::Extensions::InstanceMethods
8
8
  end
9
9
  end
10
10
  end
@@ -2,8 +2,8 @@ require "examples/example_helper"
2
2
 
3
3
  module RR
4
4
  module Extensions
5
- describe DoubleMethods, "#any_times" do
6
- it_should_behave_like "RR::Extensions::DoubleMethods"
5
+ describe InstanceMethods, "#any_times" do
6
+ it_should_behave_like "RR::Extensions::InstanceMethods"
7
7
 
8
8
  it "returns an AnyTimesMatcher" do
9
9
  any_times.should == TimesCalledMatchers::AnyTimesMatcher.new
@@ -1,7 +1,7 @@
1
1
  require "examples/example_helper"
2
2
 
3
3
  module RR
4
- describe ProbeCreator, :shared => true do
4
+ describe MockProbeCreator, :shared => true do
5
5
  before(:each) do
6
6
  @space = Space.new
7
7
  @subject = Object.new
@@ -15,15 +15,15 @@ describe ProbeCreator, :shared => true do
15
15
  end
16
16
  end
17
17
 
18
- describe ProbeCreator, ".new without block" do
19
- it_should_behave_like "RR::ProbeCreator"
18
+ describe MockProbeCreator, ".new without block" do
19
+ it_should_behave_like "RR::MockProbeCreator"
20
20
 
21
21
  before do
22
- @creator = ProbeCreator.new(@space, @subject)
22
+ @creator = MockProbeCreator.new(@space, @subject)
23
23
  end
24
24
 
25
25
  it "clears out all methods from creator" do
26
- creator_subclass = Class.new(ProbeCreator) do
26
+ creator_subclass = Class.new(MockProbeCreator) do
27
27
  def i_should_be_a_scenario
28
28
  end
29
29
  end
@@ -34,14 +34,14 @@ describe ProbeCreator, ".new without block" do
34
34
  end
35
35
  end
36
36
 
37
- describe ProbeCreator, ".new with block" do
38
- it_should_behave_like "RR::ProbeCreator"
37
+ describe MockProbeCreator, ".new with block" do
38
+ it_should_behave_like "RR::MockProbeCreator"
39
39
 
40
40
  before do
41
41
  def @subject.foobar(*args)
42
42
  :original_foobar
43
43
  end
44
- @creator = ProbeCreator.new(@space, @subject) do |c|
44
+ @creator = MockProbeCreator.new(@space, @subject) do |c|
45
45
  c.foobar(1, 2)
46
46
  c.foobar(1)
47
47
  c.foobar.with_any_args
@@ -56,7 +56,7 @@ describe ProbeCreator, ".new with block" do
56
56
  end
57
57
 
58
58
  it "clears out all methods from creator" do
59
- creator_subclass = Class.new(ProbeCreator) do
59
+ creator_subclass = Class.new(MockProbeCreator) do
60
60
  def i_should_be_a_scenario
61
61
  end
62
62
  end
@@ -68,14 +68,14 @@ describe ProbeCreator, ".new with block" do
68
68
  end
69
69
  end
70
70
 
71
- describe ProbeCreator, ".new where method takes a block" do
72
- it_should_behave_like "RR::ProbeCreator"
71
+ describe MockProbeCreator, ".new where method takes a block" do
72
+ it_should_behave_like "RR::MockProbeCreator"
73
73
 
74
74
  before do
75
75
  def @subject.foobar(*args, &block)
76
76
  yield(*args)
77
77
  end
78
- @creator = ProbeCreator.new(@space, @subject)
78
+ @creator = MockProbeCreator.new(@space, @subject)
79
79
  end
80
80
 
81
81
  it "calls the block" do
@@ -85,12 +85,12 @@ describe ProbeCreator, ".new where method takes a block" do
85
85
  end
86
86
 
87
87
 
88
- describe ProbeCreator, "#method_missing" do
89
- it_should_behave_like "RR::ProbeCreator"
88
+ describe MockProbeCreator, "#method_missing" do
89
+ it_should_behave_like "RR::MockProbeCreator"
90
90
 
91
91
  before do
92
92
  @subject = Object.new
93
- @creator = ProbeCreator.new(@space, @subject)
93
+ @creator = MockProbeCreator.new(@space, @subject)
94
94
  end
95
95
 
96
96
  it "sets expectations on the subject while calling the original method" do
@@ -106,7 +106,10 @@ describe ProbeCreator, "#method_missing" do
106
106
  (class << @subject; self; end).class_eval do
107
107
  define_method(:foobar) {real_value}
108
108
  end
109
- @creator.foobar(1, 2) {|value| mock(value).a_method {99}}
109
+ @creator.foobar(1, 2) do |value|
110
+ mock(value).a_method {99}
111
+ value
112
+ end
110
113
 
111
114
  return_value = @subject.foobar(1, 2)
112
115
  return_value.should === return_value
@@ -262,10 +262,11 @@ describe Scenario, "#after_call" do
262
262
  @scenario.after_call {}.should === @scenario
263
263
  end
264
264
 
265
- it "receives the return value in the block" do
265
+ it "sends return value of Scenario implementation to after_call" do
266
266
  return_value = {}
267
267
  @scenario.returns(return_value).after_call do |value|
268
268
  value[:foo] = :bar
269
+ value
269
270
  end
270
271
 
271
272
  actual_value = @scenario.call
@@ -273,10 +274,21 @@ describe Scenario, "#after_call" do
273
274
  actual_value.should == {:foo => :bar}
274
275
  end
275
276
 
277
+ it "receives the return value in the after_call callback" do
278
+ return_value = :returns_value
279
+ @scenario.returns(return_value).after_call do |value|
280
+ :after_call_value
281
+ end
282
+
283
+ actual_value = @scenario.call
284
+ actual_value.should == :after_call_value
285
+ end
286
+
276
287
  it "allows after_call to mock the return value" do
277
288
  return_value = Object.new
278
289
  @scenario.with_any_args.returns(return_value).after_call do |value|
279
290
  mock(value).inner_method(1) {:baz}
291
+ value
280
292
  end
281
293
 
282
294
  @object.foobar.inner_method(1).should == :baz
@@ -343,11 +355,18 @@ end
343
355
  describe Scenario, "#call implemented by a proc" do
344
356
  it_should_behave_like "RR::Scenario"
345
357
 
346
- it "calls the return proc when scheduled to call a proc" do
358
+ it "calls the return proc when implemented by a proc" do
347
359
  @scenario.returns {|arg| "returning #{arg}"}
348
360
  @scenario.call(:foobar).should == "returning foobar"
349
361
  end
350
362
 
363
+ it "calls and returns the after_call when after_call is set" do
364
+ @scenario.returns {|arg| "returning #{arg}"}.after_call do |value|
365
+ "#{value} after call"
366
+ end
367
+ @scenario.call(:foobar).should == "returning foobar after call"
368
+ end
369
+
351
370
  it "returns nil when to returns is not set" do
352
371
  @scenario.call.should be_nil
353
372
  end
@@ -53,7 +53,7 @@ describe Space, "#create_stub_creator" do
53
53
  end
54
54
  end
55
55
 
56
- describe Space, "#create_probe_creator" do
56
+ describe Space, "#create_mock_probe_creator" do
57
57
  it_should_behave_like "RR::Space"
58
58
 
59
59
  before do
@@ -65,15 +65,15 @@ describe Space, "#create_probe_creator" do
65
65
  end
66
66
  end
67
67
 
68
- it "creates a ProbeCreator" do
69
- creator = @space.create_probe_creator(@object)
68
+ it "creates a MockProbeCreator" do
69
+ creator = @space.create_mock_probe_creator(@object)
70
70
  creator.foobar(1)
71
71
  @object.foobar(1).should == :original_foobar
72
72
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
73
73
  end
74
74
 
75
75
  it "uses block definition when passed a block" do
76
- creator = @space.create_probe_creator(@object) do |c|
76
+ creator = @space.create_mock_probe_creator(@object) do |c|
77
77
  c.foobar(1)
78
78
  end
79
79
  @object.foobar(1).should == :original_foobar
@@ -81,6 +81,34 @@ describe Space, "#create_probe_creator" do
81
81
  end
82
82
  end
83
83
 
84
+ describe Space, "#create_stub_probe_creator" do
85
+ it_should_behave_like "RR::Space"
86
+
87
+ before do
88
+ @space = Space.new
89
+ @object = Object.new
90
+ @method_name = :foobar
91
+ def @object.foobar(*args)
92
+ :original_foobar
93
+ end
94
+ end
95
+
96
+ it "creates a StubProbeCreator" do
97
+ creator = @space.create_stub_probe_creator(@object)
98
+ creator.foobar
99
+ @object.foobar(1).should == :original_foobar
100
+ @object.foobar(1).should == :original_foobar
101
+ end
102
+
103
+ it "uses block definition when passed a block" do
104
+ creator = @space.create_stub_probe_creator(@object) do |c|
105
+ c.foobar(1)
106
+ end
107
+ @object.foobar(1).should == :original_foobar
108
+ @object.foobar(1).should == :original_foobar
109
+ end
110
+ end
111
+
84
112
  describe Space, "#create_do_not_allow_creator" do
85
113
  it_should_behave_like "RR::Space"
86
114
 
@@ -0,0 +1,127 @@
1
+ require "examples/example_helper"
2
+
3
+ module RR
4
+ describe StubProbeCreator, :shared => true do
5
+ before(:each) do
6
+ @space = Space.new
7
+ @subject = Object.new
8
+ end
9
+
10
+ it "initializes creator with passed in object" do
11
+ class << @creator
12
+ attr_reader :subject
13
+ end
14
+ @creator.subject.should === @subject
15
+ end
16
+ end
17
+
18
+ describe StubProbeCreator, ".new without block" do
19
+ it_should_behave_like "RR::StubProbeCreator"
20
+
21
+ before do
22
+ @creator = StubProbeCreator.new(@space, @subject)
23
+ end
24
+
25
+ it "clears out all methods from creator" do
26
+ creator_subclass = Class.new(StubProbeCreator) do
27
+ def i_should_be_a_scenario
28
+ end
29
+ end
30
+ creator_subclass.instance_methods.should include('i_should_be_a_scenario')
31
+
32
+ creator = creator_subclass.new(@space, @subject)
33
+ creator.i_should_be_a_scenario.should be_instance_of(Scenario)
34
+ end
35
+ end
36
+
37
+ describe StubProbeCreator, ".new with block" do
38
+ it_should_behave_like "RR::StubProbeCreator"
39
+
40
+ before do
41
+ def @subject.foobar(*args)
42
+ :original_foobar
43
+ end
44
+ @creator = StubProbeCreator.new(@space, @subject) do |c|
45
+ c.foobar
46
+ end
47
+ end
48
+
49
+ it "creates doubles" do
50
+ @subject.foobar(1, 2).should == :original_foobar
51
+ @subject.foobar(1, 2).should == :original_foobar
52
+ @subject.foobar(1).should == :original_foobar
53
+ @subject.foobar(1).should == :original_foobar
54
+ @subject.foobar(:something).should == :original_foobar
55
+ @subject.foobar(:something).should == :original_foobar
56
+ end
57
+
58
+ it "clears out all methods from creator" do
59
+ creator_subclass = Class.new(StubProbeCreator) do
60
+ def i_should_be_a_scenario
61
+ end
62
+ end
63
+ creator_subclass.instance_methods.should include('i_should_be_a_scenario')
64
+
65
+ creator_subclass.new(@space, @subject) do |m|
66
+ m.i_should_be_a_scenario.should be_instance_of(Scenario)
67
+ end
68
+ end
69
+ end
70
+
71
+ describe StubProbeCreator, ".new where method takes a block" do
72
+ it_should_behave_like "RR::StubProbeCreator"
73
+
74
+ before do
75
+ def @subject.foobar(*args, &block)
76
+ yield(*args)
77
+ end
78
+ @creator = StubProbeCreator.new(@space, @subject)
79
+ end
80
+
81
+ it "calls the block" do
82
+ @creator.foobar(1, 2)
83
+ @subject.foobar(1, 2) {|arg1, arg2| [arg2, arg1]}.should == [2, 1]
84
+ end
85
+ end
86
+
87
+ describe StubProbeCreator, "#method_missing" do
88
+ it_should_behave_like "RR::StubProbeCreator"
89
+
90
+ before do
91
+ @subject = Object.new
92
+ @creator = StubProbeCreator.new(@space, @subject)
93
+ end
94
+
95
+ it "sets up a scenario with passed in arguments" do
96
+ def @subject.foobar(*args); :baz; end
97
+ @creator.foobar(1, 2)
98
+ proc do
99
+ @subject.foobar
100
+ end.should raise_error(Errors::ScenarioNotFoundError)
101
+ end
102
+
103
+ it "sets expectations on the subject while calling the original method" do
104
+ def @subject.foobar(*args); :baz; end
105
+ @creator.foobar(1, 2) {:new_value}
106
+ 10.times do
107
+ @subject.foobar(1, 2).should == :new_value
108
+ end
109
+ end
110
+
111
+ it "sets after_call on the scenario when passed a block" do
112
+ real_value = Object.new
113
+ (class << @subject; self; end).class_eval do
114
+ define_method(:foobar) {real_value}
115
+ end
116
+ @creator.foobar(1, 2) do |value|
117
+ mock(value).a_method {99}
118
+ value
119
+ end
120
+
121
+ return_value = @subject.foobar(1, 2)
122
+ return_value.should === return_value
123
+ return_value.a_method.should == 99
124
+ end
125
+ end
126
+
127
+ end
data/lib/rr.rb CHANGED
@@ -4,7 +4,8 @@ require "rr/double"
4
4
  require "rr/creator"
5
5
  require "rr/mock_creator"
6
6
  require "rr/stub_creator"
7
- require "rr/probe_creator"
7
+ require "rr/mock_probe_creator"
8
+ require "rr/stub_probe_creator"
8
9
  require "rr/do_not_allow_creator"
9
10
 
10
11
  require "rr/scenario"
@@ -38,4 +39,4 @@ require "rr/times_called_matchers/proc_matcher"
38
39
  require "rr/times_called_matchers/at_least_matcher"
39
40
  require "rr/times_called_matchers/at_most_matcher"
40
41
 
41
- require "rr/extensions/double_methods"
42
+ require "rr/extensions/instance_methods"
@@ -4,7 +4,7 @@ patterns.push(RR::Errors::BACKTRACE_IDENTIFIER)
4
4
  module RR
5
5
  module Adapters
6
6
  module Rspec
7
- include RR::Extensions::DoubleMethods
7
+ include RR::Extensions::InstanceMethods
8
8
  def setup_mocks_for_rspec
9
9
  RR::Space.instance.reset
10
10
  end
@@ -3,7 +3,7 @@ RR::Space.trim_backtrace = true
3
3
  module RR
4
4
  module Adapters
5
5
  module TestUnit
6
- include RR::Extensions::DoubleMethods
6
+ include RR::Extensions::InstanceMethods
7
7
  def self.included(mod)
8
8
  mod.class_eval do
9
9
  alias_method :setup_without_rr, :setup
@@ -4,15 +4,13 @@ module RR
4
4
  # has Argument Expectations and Times called Expectations.
5
5
  class Double
6
6
  MethodArguments = Struct.new(:arguments, :block)
7
- attr_reader :space, :object, :method_name, :scenarios
7
+ attr_reader :space, :object, :method_name, :original_method, :scenarios
8
8
 
9
9
  def initialize(space, object, method_name)
10
10
  @space = space
11
11
  @object = object
12
12
  @method_name = method_name.to_sym
13
- if @object.respond_to?(method_name)
14
- meta.send(:alias_method, original_method_name, method_name)
15
- end
13
+ @original_method = object.method(method_name) if @object.methods.include?(method_name.to_s)
16
14
  @scenarios = []
17
15
  end
18
16
 
@@ -49,21 +47,13 @@ module RR
49
47
  # if one exists.
50
48
  def reset
51
49
  meta.send(:remove_method, placeholder_name)
52
- if original_method
53
- meta.send(:alias_method, @method_name, original_method_name)
54
- meta.send(:remove_method, original_method_name)
50
+ if @original_method
51
+ meta.send(:define_method, @method_name, @original_method)
55
52
  else
56
53
  meta.send(:remove_method, @method_name)
57
54
  end
58
55
  end
59
56
 
60
- # The original method of the object. It returns nil if the object
61
- # does not have an original method.
62
- def original_method
63
- return nil unless @object.respond_to?(original_method_name)
64
- return @object.method(original_method_name)
65
- end
66
-
67
57
  protected
68
58
  def define_implementation_placeholder
69
59
  me = self
@@ -79,6 +69,7 @@ module RR
79
69
  scenario_not_found_error(*args)
80
70
  end
81
71
 
72
+ protected
82
73
  def find_scenario_to_attempt(args)
83
74
  matches = ScenarioMatches.new(@scenarios).find_all_matches!(args)
84
75
 
@@ -115,10 +106,6 @@ module RR
115
106
  def placeholder_name
116
107
  "__rr__#{@method_name}"
117
108
  end
118
-
119
- def original_method_name
120
- "__rr__original_#{@method_name}"
121
- end
122
109
 
123
110
  def meta
124
111
  (class << @object; self; end)
@@ -1,6 +1,6 @@
1
1
  module RR
2
2
  module Extensions
3
- module DoubleMethods
3
+ module InstanceMethods
4
4
  # Sets up a MockCreator that generates a Double Scenario that
5
5
  # acts like a mock.
6
6
  # mock(object).method_name(arg1, arg2) {return_value}
@@ -15,13 +15,45 @@ module Extensions
15
15
  RR::Space.instance.create_stub_creator(subject, &definition)
16
16
  end
17
17
 
18
- # Sets up a ProbeCreator that generates a Double Scenario that
19
- # acts like a probe.
20
- # probe(controller.template).render(:partial => "my/socks")
21
- def probe(subject, &definition)
22
- RR::Space.instance.create_probe_creator(subject, &definition)
18
+ # Sets up a MockProbeCreator that generates a Double Scenario that
19
+ # acts like mock verifications while calling the actual method.
20
+ #
21
+ # mock_probe(controller.template).render(:partial => "my/socks")
22
+ #
23
+ # Passing a block allows you to intercept the return value.
24
+ # The return value can be modified, validated, and/or overridden by
25
+ # passing in a block. The return value of the block will replace
26
+ # the actual return value.
27
+ #
28
+ # mock_probe(controller.template).render(:partial => "my/socks") do |html|
29
+ # html.should include("My socks are wet")
30
+ # "My new return value"
31
+ # end
32
+ def mock_probe(subject, &definition)
33
+ RR::Space.instance.create_mock_probe_creator(subject, &definition)
23
34
  end
24
35
 
36
+ # Sets up a StubProbeCreator that generates a Double Scenario that
37
+ # acts like mock verifications while calling the actual method.
38
+ #
39
+ # mock_probe(User).new {|user| my_user}
40
+ #
41
+ # Passing a block allows you to intercept the return value.
42
+ # The return value can be modified, validated, and/or overridden by
43
+ # passing in a block. The return value of the block will replace
44
+ # the actual return value.
45
+ #
46
+ # mock_probe(controller.template).render(:partial => "my/socks") do |html|
47
+ # html.should include("My socks are wet")
48
+ # "My new return value"
49
+ # end
50
+ def stub_probe(subject, &definition)
51
+ RR::Space.instance.create_stub_probe_creator(subject, &definition)
52
+ end
53
+
54
+ # Same as mock_probe
55
+ alias_method :probe, :mock_probe
56
+
25
57
  # Sets up a DoNotAllowCreator that generates a Double Scenario that
26
58
  # expects never to be called.
27
59
  # do_not_allow(object).method_name
@@ -0,0 +1,36 @@
1
+ module RR
2
+ # RR::MockProbeCreator uses RR::MockProbeCreator#method_missing to create
3
+ # a Scenario that acts like a mock with probing capabilities.
4
+ #
5
+ # Passing a block allows you to intercept the return value.
6
+ # The return value can be modified, validated, and/or overridden by
7
+ # passing in a block. The return value of the block will replace
8
+ # the actual return value.
9
+ #
10
+ # probe(subject).method_name(arg1, arg2) do |return_value|
11
+ # return_value.method_name.should == :return_value
12
+ # my_return_value
13
+ # end
14
+ #
15
+ # probe(User) do |m|
16
+ # m.find('4') do |user|
17
+ # mock(user).valid? {false}
18
+ # user
19
+ # end
20
+ # end
21
+ #
22
+ # user = User.find('4')
23
+ # user.valid? # false
24
+ class MockProbeCreator < Creator
25
+ module InstanceMethods
26
+ protected
27
+ def method_missing(method_name, *args, &after_call)
28
+ double = @space.create_double(@subject, method_name)
29
+ scenario = @space.create_scenario(double)
30
+ scenario.with(*args).once.implemented_by(double.original_method)
31
+ scenario.after_call(&after_call) if after_call
32
+ scenario
33
+ end
34
+ end
35
+ end
36
+ end
@@ -243,8 +243,8 @@ module RR
243
243
  # exceeds the expected TimesCalledExpectation.
244
244
  def call(*args, &block)
245
245
  return_value = call_implementation(*args, &block)
246
- @after_call.call(return_value) if @after_call
247
- return_value
246
+ return return_value unless @after_call
247
+ @after_call.call(return_value)
248
248
  end
249
249
 
250
250
  def call_implementation(*args, &block)
@@ -33,9 +33,14 @@ module RR
33
33
  StubCreator.new(self, subject, &definition)
34
34
  end
35
35
 
36
- # Creates a ProbeCreator.
37
- def create_probe_creator(subject, &definition)
38
- ProbeCreator.new(self, subject, &definition)
36
+ # Creates a MockProbeCreator.
37
+ def create_mock_probe_creator(subject, &definition)
38
+ MockProbeCreator.new(self, subject, &definition)
39
+ end
40
+
41
+ # Creates a StubProbeCreator.
42
+ def create_stub_probe_creator(subject, &definition)
43
+ StubProbeCreator.new(self, subject, &definition)
39
44
  end
40
45
 
41
46
  # Creates a DoNotAllowCreator.
@@ -0,0 +1,42 @@
1
+ module RR
2
+ # RR::StubProbeCreator uses RR::StubProbeCreator#method_missing to create
3
+ # a Scenario that acts like a stub with probing capabilities.
4
+ #
5
+ # Passing a block allows you to intercept the return value.
6
+ # The return value can be modified, validated, and/or overridden by
7
+ # passing in a block. The return value of the block will replace
8
+ # the actual return value.
9
+ #
10
+ # probe_stub(subject).method_name(arg1, arg2) do |return_value|
11
+ # return_value.method_name.should == :return_value
12
+ # my_return_value
13
+ # end
14
+ #
15
+ # probe_stub(User) do |m|
16
+ # m.find do |user|
17
+ # mock(user).valid? {false}
18
+ # user
19
+ # end
20
+ # end
21
+ #
22
+ # user = User.find('4')
23
+ # user.valid? # false
24
+ class StubProbeCreator < Creator
25
+ module InstanceMethods
26
+ protected
27
+ def method_missing(method_name, *args, &after_call)
28
+ double = @space.create_double(@subject, method_name)
29
+ scenario = @space.create_scenario(double)
30
+ scenario.implemented_by(double.original_method)
31
+ scenario.any_number_of_times
32
+ if args.empty?
33
+ scenario.with_any_args
34
+ else
35
+ scenario.with(*args)
36
+ end
37
+ scenario.after_call(&after_call) if after_call
38
+ scenario
39
+ end
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.3
3
3
  specification_version: 1
4
4
  name: rr
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.15
6
+ version: 0.2.1
7
7
  date: 2007-07-17 00:00:00 -07:00
8
8
  summary: RR (Double Ruby) is a double framework that features a rich selection of double techniques and a terse syntax. http://xunitpatterns.com/Test%20Double.html
9
9
  require_paths:
@@ -33,15 +33,16 @@ files:
33
33
  - CHANGES
34
34
  - README
35
35
  - lib/rr.rb
36
+ - lib/rr/mock_probe_creator.rb
36
37
  - lib/rr/scenario.rb
37
38
  - lib/rr/creator.rb
39
+ - lib/rr/stub_probe_creator.rb
38
40
  - lib/rr/scenario_matches.rb
39
41
  - lib/rr/stub_creator.rb
40
42
  - lib/rr/space.rb
41
43
  - lib/rr/double.rb
42
44
  - lib/rr/do_not_allow_creator.rb
43
45
  - lib/rr/mock_creator.rb
44
- - lib/rr/probe_creator.rb
45
46
  - lib/rr/times_called_matchers/any_times_matcher.rb
46
47
  - lib/rr/times_called_matchers/at_most_matcher.rb
47
48
  - lib/rr/times_called_matchers/times_called_matcher.rb
@@ -68,18 +69,19 @@ files:
68
69
  - lib/rr/wildcard_matchers/numeric.rb
69
70
  - lib/rr/wildcard_matchers/is_a.rb
70
71
  - lib/rr/wildcard_matchers/anything.rb
71
- - lib/rr/extensions/double_methods.rb
72
+ - lib/rr/extensions/instance_methods.rb
72
73
  - examples/environment_fixture_setup.rb
73
74
  - examples/rspec_example_suite.rb
74
75
  - examples/example_suite.rb
75
76
  - examples/example_helper.rb
76
77
  - examples/test_unit_example_suite.rb
77
78
  - examples/high_level_example.rb
78
- - examples/rr/probe_creator_example.rb
79
+ - examples/rr/stub_probe_creator_example.rb
79
80
  - examples/rr/mock_creator_example.rb
80
81
  - examples/rr/stub_creator_example.rb
81
82
  - examples/rr/scenario_example.rb
82
83
  - examples/rr/do_not_allow_creator_example.rb
84
+ - examples/rr/mock_probe_creator_example.rb
83
85
  - examples/rr/rspec/rspec_backtrace_tweaking_example.rb
84
86
  - examples/rr/rspec/rspec_adapter_example.rb
85
87
  - examples/rr/rspec/rspec_usage_example.rb
@@ -123,10 +125,10 @@ files:
123
125
  - examples/rr/test_unit/test_unit_backtrace_test.rb
124
126
  - examples/rr/test_unit/test_helper.rb
125
127
  - examples/rr/test_unit/test_unit_integration_test.rb
126
- - examples/rr/extensions/double_methods_creator_example.rb
127
- - examples/rr/extensions/double_methods_argument_matcher_example.rb
128
- - examples/rr/extensions/double_methods_example_helper.rb
129
- - examples/rr/extensions/double_methods_times_matcher_example.rb
128
+ - examples/rr/extensions/instance_methods_argument_matcher_example.rb
129
+ - examples/rr/extensions/instance_methods_times_matcher_example.rb
130
+ - examples/rr/extensions/instance_methods_creator_example.rb
131
+ - examples/rr/extensions/instance_methods_example_helper.rb
130
132
  test_files: []
131
133
 
132
134
  rdoc_options:
@@ -1,36 +0,0 @@
1
- module RR
2
- # RR::ProbeCreator uses RR::ProbeCreator#method_missing to create
3
- # a Scenario that acts like a probe.
4
- #
5
- # The following example probes method_name with arg1 and arg2
6
- # returning the actual value of the method. The block is an after callback
7
- # that intercepts the return value. Mocks or other modifications can
8
- # be done to the return value.
9
- #
10
- # probe(subject).method_name(arg1, arg2) { |return_value| }
11
- #
12
- # The ProbeCreator also supports a block sytnax. The block accepts
13
- # a after_call callback, instead of a return value as with MockCreator
14
- # and StubCreator.
15
- #
16
- # probe(User) do |m|
17
- # m.find('4') do |user|
18
- # mock(user).valid? {false}
19
- # end
20
- # end
21
- #
22
- # user = User.find('4')
23
- # user.valid? # false
24
- class ProbeCreator < Creator
25
- module InstanceMethods
26
- protected
27
- def method_missing(method_name, *args, &after_call)
28
- double = @space.create_double(@subject, method_name)
29
- scenario = @space.create_scenario(double)
30
- scenario.with(*args).once.implemented_by(double.original_method)
31
- scenario.after_call(&after_call) if after_call
32
- scenario
33
- end
34
- end
35
- end
36
- end