rr 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,9 +1,12 @@
1
+ * 0.1.15
2
+ - Fixed [#12333] Rebinding original_methods causes blocks not to work
3
+
1
4
  * 0.1.14
2
5
  - Introduced concept of Terminal and NonTerminal TimesCalledMatchers
3
6
  - Doubles that can be called many times can be replaced
4
7
  - Terminal Scenarios are called before NonTerminal Scenarios
5
8
  - Error message tweaking
6
- - Raise error when making a Scenarios with NonDeterministic TimesMatcher Ordered
9
+ - Raise error when making a Scenarios with NonTerminal TimesMatcher Ordered
7
10
 
8
11
  * 0.1.13
9
12
  - Fixed [#12290] Scenario#returns with false causes a return value of nil
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.14"
24
+ PKG_VERSION = "0.1.15"
25
25
  PKG_FILES = FileList[
26
26
  '[A-Z]*',
27
27
  '*.rb',
@@ -7,6 +7,7 @@ describe Double, "#bind with an existing method" do
7
7
  @object = Object.new
8
8
  @method_name = :foobar
9
9
  def @object.foobar; :original_foobar; end
10
+ @original_method = @object.method(:foobar)
10
11
  @object.methods.should include(@method_name.to_s)
11
12
  @double = Double.new(@space, @object, @method_name)
12
13
  end
@@ -27,5 +28,43 @@ describe Double, "#bind with an existing method" do
27
28
  @object.foobar
28
29
  rr_foobar_called.should == true
29
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
+ end
38
+
39
+ describe Double, "#bind without an existing method" do
40
+ before do
41
+ @space = Space.new
42
+ @object = Object.new
43
+ @method_name = :foobar
44
+ @object.methods.should_not include(@method_name.to_s)
45
+ @double = Double.new(@space, @object, @method_name)
46
+ end
47
+
48
+ it "overrides the original method with the double's dispatching methods" do
49
+ @object.respond_to?(:__rr__foobar).should == false
50
+ @double.bind
51
+ @object.respond_to?(:__rr__foobar).should == true
52
+
53
+ rr_foobar_called = false
54
+ (class << @object; self; end).class_eval do
55
+ define_method :__rr__foobar do
56
+ rr_foobar_called = true
57
+ end
58
+ end
59
+
60
+ rr_foobar_called.should == false
61
+ @object.foobar
62
+ rr_foobar_called.should == true
63
+ 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
30
69
  end
31
70
  end
@@ -2,7 +2,7 @@ require "examples/example_helper"
2
2
 
3
3
  module RR
4
4
  describe Double, "#reset", :shared => true do
5
- it "cleans up by removing the __rr__ method" do
5
+ it "cleans up by removing the __rr__method" do
6
6
  @double.bind
7
7
  @object.methods.should include("__rr__foobar")
8
8
 
@@ -45,15 +45,53 @@ describe Double, "#reset when method exists" do
45
45
  @object.methods.should include(@method_name.to_s)
46
46
  @original_method = @object.method(@method_name)
47
47
  @double = Double.new(@space, @object, @method_name)
48
- end
49
48
 
50
- it "removes the method" do
51
49
  @double.bind
52
50
  @object.methods.should include(@method_name.to_s)
51
+ end
53
52
 
53
+ it "rebind original method" do
54
54
  @double.reset
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
+ end
67
+
68
+ describe Double, "#reset when method with block exists" do
69
+ it_should_behave_like "RR::Double#reset"
70
+
71
+ before do
72
+ @space = Space.new
73
+ @object = Object.new
74
+ @method_name = :foobar
75
+ def @object.foobar
76
+ yield(:original_argument)
77
+ end
78
+ @object.methods.should include(@method_name.to_s)
79
+ @original_method = @object.method(@method_name)
80
+ @double = Double.new(@space, @object, @method_name)
81
+
82
+ @double.bind
83
+ @object.methods.should include(@method_name.to_s)
84
+ end
85
+
86
+ it "rebinds original method with block" do
87
+ @double.reset
88
+ @object.methods.should include(@method_name.to_s)
89
+
90
+ original_argument = nil
91
+ @object.foobar do |arg|
92
+ original_argument = arg
93
+ end
94
+ original_argument.should == :original_argument
95
+ end
58
96
  end
59
97
  end
data/lib/rr/double.rb CHANGED
@@ -4,13 +4,15 @@ 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, :original_method, :scenarios
7
+ attr_reader :space, :object, :method_name, :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
- @original_method = object.method(method_name) if @object.methods.include?(method_name.to_s)
13
+ if @object.respond_to?(method_name)
14
+ meta.send(:alias_method, original_method_name, method_name)
15
+ end
14
16
  @scenarios = []
15
17
  end
16
18
 
@@ -47,13 +49,21 @@ module RR
47
49
  # if one exists.
48
50
  def reset
49
51
  meta.send(:remove_method, placeholder_name)
50
- if @original_method
51
- meta.send(:define_method, @method_name, &@original_method)
52
+ if original_method
53
+ meta.send(:alias_method, @method_name, original_method_name)
54
+ meta.send(:remove_method, original_method_name)
52
55
  else
53
56
  meta.send(:remove_method, @method_name)
54
57
  end
55
58
  end
56
59
 
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
+
57
67
  protected
58
68
  def define_implementation_placeholder
59
69
  me = self
@@ -69,7 +79,6 @@ module RR
69
79
  scenario_not_found_error(*args)
70
80
  end
71
81
 
72
- protected
73
82
  def find_scenario_to_attempt(args)
74
83
  matches = ScenarioMatches.new(@scenarios).find_all_matches!(args)
75
84
 
@@ -106,6 +115,10 @@ module RR
106
115
  def placeholder_name
107
116
  "__rr__#{@method_name}"
108
117
  end
118
+
119
+ def original_method_name
120
+ "__rr__original_#{@method_name}"
121
+ end
109
122
 
110
123
  def meta
111
124
  (class << @object; self; end)
metadata CHANGED
@@ -3,8 +3,8 @@ 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.14
7
- date: 2007-07-16 00:00:00 -07:00
6
+ version: 0.1.15
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:
10
10
  - lib