rr 0.4.10 → 0.6.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/CHANGES +14 -0
- data/README.rdoc +67 -13
- data/Rakefile +1 -1
- data/lib/rr.rb +29 -9
- data/lib/rr/adapters/rr_methods.rb +38 -158
- data/lib/rr/double.rb +46 -41
- data/lib/rr/double_definitions/child_double_definition_creator.rb +23 -0
- data/lib/rr/double_definitions/double_definition.rb +212 -0
- data/lib/rr/double_definitions/double_definition_creator.rb +153 -0
- data/lib/rr/double_definitions/double_definition_creator_proxy.rb +25 -0
- data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +15 -0
- data/lib/rr/double_definitions/strategies/implementation/proxy.rb +62 -0
- data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
- data/lib/rr/double_definitions/strategies/scope/instance.rb +15 -0
- data/lib/rr/double_definitions/strategies/scope/instance_of_class.rb +43 -0
- data/lib/rr/double_definitions/strategies/scope/scope_strategy.rb +15 -0
- data/lib/rr/double_definitions/strategies/strategy.rb +70 -0
- data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +34 -0
- data/lib/rr/double_definitions/strategies/verification/mock.rb +44 -0
- data/lib/rr/double_definitions/strategies/verification/stub.rb +45 -0
- data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +15 -0
- data/lib/rr/double_injection.rb +21 -15
- data/lib/rr/expectations/argument_equality_expectation.rb +2 -1
- data/lib/rr/space.rb +23 -22
- data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
- data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
- data/spec/high_level_spec.rb +111 -64
- data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +1 -1
- data/spec/rr/adapters/rr_methods_creator_spec.rb +99 -315
- data/spec/rr/adapters/rr_methods_space_spec.rb +90 -109
- data/spec/rr/adapters/rr_methods_spec_helper.rb +1 -1
- data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +1 -1
- data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +103 -0
- data/spec/rr/double_definitions/double_definition_creator_proxy_spec.rb +83 -0
- data/spec/rr/double_definitions/double_definition_creator_spec.rb +495 -0
- data/spec/rr/double_definitions/double_definition_spec.rb +1116 -0
- data/spec/rr/double_injection/double_injection_bind_spec.rb +111 -0
- data/spec/rr/double_injection/double_injection_dispatching_spec.rb +245 -0
- data/spec/rr/{double → double_injection}/double_injection_has_original_method_spec.rb +9 -9
- data/spec/rr/double_injection/double_injection_reset_spec.rb +90 -0
- data/spec/rr/double_injection/double_injection_spec.rb +77 -0
- data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
- data/spec/rr/double_spec.rb +156 -136
- data/spec/rr/errors/rr_error_spec.rb +1 -1
- data/spec/rr/expectations/any_argument_expectation_spec.rb +1 -1
- data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +6 -30
- data/spec/rr/expectations/argument_equality_expectation_spec.rb +35 -18
- data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +22 -41
- data/spec/rr/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
- data/spec/rr/expectations/hash_including_spec.rb +17 -0
- data/spec/rr/expectations/satisfy_argument_equality_expectation_spec.rb +59 -0
- data/spec/rr/expectations/satisfy_spec.rb +14 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +30 -28
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +55 -54
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +49 -48
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +9 -7
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +77 -76
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +58 -57
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +59 -58
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +25 -24
- data/spec/rr/rspec/rspec_adapter_spec.rb +12 -11
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +10 -8
- data/spec/rr/rspec/rspec_usage_spec.rb +1 -1
- data/spec/rr/space/hash_with_object_id_key_spec.rb +1 -1
- data/spec/rr/space/space_spec.rb +330 -192
- data/spec/rr/test_unit/test_helper.rb +1 -2
- data/spec/rr/test_unit/test_unit_backtrace_test.rb +1 -2
- data/spec/rr/test_unit/test_unit_integration_test.rb +1 -2
- data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/integer_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/proc_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/range_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +1 -1
- data/spec/rr/wildcard_matchers/anything_spec.rb +24 -0
- data/spec/rr/wildcard_matchers/boolean_spec.rb +36 -0
- data/spec/rr/wildcard_matchers/duck_type_spec.rb +52 -0
- data/spec/rr/wildcard_matchers/is_a_spec.rb +32 -0
- data/spec/rr/wildcard_matchers/numeric_spec.rb +32 -0
- data/spec/rr/wildcard_matchers/range_spec.rb +35 -0
- data/spec/rr/wildcard_matchers/regexp_spec.rb +43 -0
- data/spec/rr_spec.rb +28 -0
- data/spec/spec_helper.rb +84 -0
- metadata +43 -29
- data/lib/rr/double_creator.rb +0 -271
- data/lib/rr/double_definition.rb +0 -179
- data/lib/rr/double_definition_builder.rb +0 -44
- data/lib/rr/double_definition_creator.rb +0 -156
- data/lib/rr/double_definition_creator_proxy.rb +0 -20
- data/spec/rr/double/double_injection_bind_spec.rb +0 -105
- data/spec/rr/double/double_injection_dispatching_spec.rb +0 -228
- data/spec/rr/double/double_injection_reset_spec.rb +0 -86
- data/spec/rr/double/double_injection_spec.rb +0 -72
- data/spec/rr/double/double_injection_verify_spec.rb +0 -24
- data/spec/rr/double_definition_creator_proxy_spec.rb +0 -85
- data/spec/rr/double_definition_creator_spec.rb +0 -496
- data/spec/rr/double_definition_spec.rb +0 -815
- data/spec/rr/expectations/anything_spec.rb +0 -14
- data/spec/rr/expectations/boolean_spec.rb +0 -14
- data/spec/rr/expectations/duck_type_argument_equality_expectation_spec.rb +0 -71
- data/spec/rr/expectations/duck_type_spec.rb +0 -14
- data/spec/rr/expectations/is_a_argument_equality_expectation_spec.rb +0 -51
- data/spec/rr/expectations/is_a_spec.rb +0 -14
- data/spec/rr/expectations/numeric_argument_equality_expectation_spec.rb +0 -47
- data/spec/rr/expectations/numeric_spec.rb +0 -14
- data/spec/rr/expectations/range_argument_equality_expectation_spec.rb +0 -59
- data/spec/rr/expectations/range_spec.rb +0 -10
- data/spec/rr/expectations/regexp_argument_equality_expectation_spec.rb +0 -72
- data/spec/rr/expectations/regexp_spec.rb +0 -10
@@ -0,0 +1,111 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
module RR
|
4
|
+
module DoubleDefinitions
|
5
|
+
describe DoubleInjection do
|
6
|
+
describe "#bind" do
|
7
|
+
context "with an existing method" do
|
8
|
+
before do
|
9
|
+
@subject = Object.new
|
10
|
+
@method_name = :foobar
|
11
|
+
def @subject.foobar;
|
12
|
+
:original_foobar;
|
13
|
+
end
|
14
|
+
@original_method = @subject.method(@method_name)
|
15
|
+
@subject.methods.should include(@method_name.to_s)
|
16
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "overrides the original method with the double_injection's dispatching methods" do
|
20
|
+
@subject.respond_to?(:__rr__foobar).should == false
|
21
|
+
@double_injection.bind
|
22
|
+
@subject.respond_to?(:__rr__foobar).should == true
|
23
|
+
|
24
|
+
rr_foobar_called = false
|
25
|
+
(class << @subject; self; end).class_eval do
|
26
|
+
define_method :__rr__foobar do
|
27
|
+
rr_foobar_called = true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
rr_foobar_called.should == false
|
32
|
+
@subject.foobar
|
33
|
+
rr_foobar_called.should == true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "stores original method in __rr__original_method_name" do
|
37
|
+
@double_injection.bind
|
38
|
+
@subject.respond_to?(:__rr__original_foobar).should == true
|
39
|
+
@subject.method(:__rr__original_foobar).should == @original_method
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "without an existing method" do
|
44
|
+
before do
|
45
|
+
@subject = Object.new
|
46
|
+
@method_name = :foobar
|
47
|
+
@subject.methods.should_not include(@method_name.to_s)
|
48
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "creates a new method with the double_injection's dispatching methods" do
|
52
|
+
@subject.respond_to?(:__rr__foobar).should == false
|
53
|
+
@double_injection.bind
|
54
|
+
@subject.respond_to?(:__rr__foobar).should == true
|
55
|
+
|
56
|
+
rr_foobar_called = false
|
57
|
+
(class << @subject; self; end).class_eval do
|
58
|
+
define_method :__rr__foobar do
|
59
|
+
rr_foobar_called = true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
rr_foobar_called.should == false
|
64
|
+
@subject.foobar
|
65
|
+
rr_foobar_called.should == true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "does not create method __rr__original_method_name" do
|
69
|
+
@double_injection.bind
|
70
|
+
@subject.respond_to?(:__rr__original_foobar).should == false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "with #==" do
|
75
|
+
before do
|
76
|
+
@subject = Object.new
|
77
|
+
@method_name = :'=='
|
78
|
+
@subject.should respond_to(@method_name)
|
79
|
+
@original_method = @subject.method(@method_name)
|
80
|
+
@subject.methods.should include(@method_name.to_s)
|
81
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "overrides the original method with the double_injection's dispatching methods" do
|
85
|
+
@subject.respond_to?(:"__rr__#{@method_name}").should == false
|
86
|
+
@double_injection.bind
|
87
|
+
@subject.respond_to?(:"__rr__#{@method_name}").should == true
|
88
|
+
|
89
|
+
override_called = false
|
90
|
+
method_name = @method_name
|
91
|
+
(class << @subject; self; end).class_eval do
|
92
|
+
define_method :"__rr__#{method_name}" do
|
93
|
+
override_called = true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
override_called.should == false
|
98
|
+
@subject == 1
|
99
|
+
override_called.should == true
|
100
|
+
end
|
101
|
+
|
102
|
+
it "stores original method in __rr__original_method_name" do
|
103
|
+
@double_injection.bind
|
104
|
+
@subject.respond_to?(:"__rr__original_#{@method_name}").should == true
|
105
|
+
@subject.method(:"__rr__original_#{@method_name}").should == @original_method
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,245 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInjection do
|
5
|
+
attr_reader :space, :subject, :double_injection
|
6
|
+
it_should_behave_like "Swapped Space"
|
7
|
+
before do
|
8
|
+
@subject = Object.new
|
9
|
+
subject.methods.should_not include(method_name.to_s)
|
10
|
+
@double_injection = space.double_injection(subject, method_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def new_double
|
14
|
+
Double.new(
|
15
|
+
double_injection,
|
16
|
+
DoubleDefinitions::DoubleDefinition.new(
|
17
|
+
DoubleDefinitions::DoubleDefinitionCreator.new,
|
18
|
+
subject
|
19
|
+
)
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "methods whose name do not contain ! or ?" do
|
24
|
+
def method_name
|
25
|
+
:foobar
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when the original method uses the passed-in block" do
|
29
|
+
it "executes the passed-in block" do
|
30
|
+
method_fixture = Object.new
|
31
|
+
class << method_fixture
|
32
|
+
def method_with_block(a, b)
|
33
|
+
yield(a, b)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
double = new_double
|
37
|
+
double.with(1, 2).implemented_by(method_fixture.method(:method_with_block))
|
38
|
+
subject.foobar(1, 2) {|a, b| [b, a]}.should == [2, 1]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when no other Double with duplicate ArgumentExpectations exists" do
|
43
|
+
it "dispatches to Double that have an exact match" do
|
44
|
+
double1_with_exact_match = new_double
|
45
|
+
double1_with_exact_match.with(:exact_match_1).returns {:return_1}
|
46
|
+
double_with_no_match = new_double
|
47
|
+
double_with_no_match.with("nothing that matches").returns {:no_match}
|
48
|
+
double2_with_exact_match = new_double
|
49
|
+
double2_with_exact_match.with(:exact_match_2).returns {:return_2}
|
50
|
+
|
51
|
+
subject.foobar(:exact_match_1).should == :return_1
|
52
|
+
subject.foobar(:exact_match_2).should == :return_2
|
53
|
+
end
|
54
|
+
|
55
|
+
it "dispatches to Double that have a wildcard match" do
|
56
|
+
double_with_wildcard_match = new_double
|
57
|
+
double_with_wildcard_match.with_any_args.returns {:wild_card_value}
|
58
|
+
double_with_no_match = new_double
|
59
|
+
double_with_no_match.with("nothing that matches").returns {:no_match}
|
60
|
+
|
61
|
+
subject.foobar(:wildcard_match_1).should == :wild_card_value
|
62
|
+
subject.foobar(:wildcard_match_2, 3).should == :wild_card_value
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when other Doubles exists but none of them match the passed-in arguments" do
|
67
|
+
it "raises DoubleNotFoundError error when arguments do not match a double" do
|
68
|
+
double_1 = new_double
|
69
|
+
double_1.with(1, 2)
|
70
|
+
|
71
|
+
double_2 = new_double
|
72
|
+
double_2.with(3)
|
73
|
+
|
74
|
+
error = nil
|
75
|
+
begin
|
76
|
+
subject.foobar(:arg1, :arg2)
|
77
|
+
viotated "Error should have been raised"
|
78
|
+
rescue Errors::DoubleNotFoundError => e
|
79
|
+
error = e
|
80
|
+
end
|
81
|
+
error.message.should include("On subject #<Object")
|
82
|
+
expected_double_message_part = "unexpected method invocation:\n" <<
|
83
|
+
" foobar(:arg1, :arg2)\n"
|
84
|
+
"expected invocations:\n"
|
85
|
+
"- foobar(1, 2)\n" <<
|
86
|
+
"- foobar(3)"
|
87
|
+
error.message.should include(expected_double_message_part)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "when at least one Double with NonTerminal TimesCalledMatchers exits" do
|
92
|
+
it "dispatches to Double with exact match" do
|
93
|
+
double = new_double(1, 2) {:return_value}
|
94
|
+
subject.foobar(1, 2).should == :return_value
|
95
|
+
end
|
96
|
+
|
97
|
+
it "matches to the last Double that was registered with an exact match" do
|
98
|
+
double_1 = new_double(1, 2) {:value_1}
|
99
|
+
double_2 = new_double(1, 2) {:value_2}
|
100
|
+
|
101
|
+
subject.foobar(1, 2).should == :value_2
|
102
|
+
end
|
103
|
+
|
104
|
+
it "dispatches to Double with wildcard match" do
|
105
|
+
double = new_double(anything) {:return_value}
|
106
|
+
subject.foobar(:dont_care).should == :return_value
|
107
|
+
end
|
108
|
+
|
109
|
+
it "matches to the last Double that was registered with a wildcard match" do
|
110
|
+
double_1 = new_double(anything) {:value_1}
|
111
|
+
double_2 = new_double(anything) {:value_2}
|
112
|
+
|
113
|
+
subject.foobar(:dont_care).should == :value_2
|
114
|
+
end
|
115
|
+
|
116
|
+
it "matches to Double with exact match Double even when a Double with wildcard match was registered later" do
|
117
|
+
exact_double_registered_first = new_double(1, 2) {:exact_first}
|
118
|
+
wildcard_double_registered_last = new_double(anything, anything) {:wildcard_last}
|
119
|
+
|
120
|
+
subject.foobar(1, 2).should == :exact_first
|
121
|
+
end
|
122
|
+
|
123
|
+
def new_double(*arguments, &return_value)
|
124
|
+
double = super()
|
125
|
+
double.with(*arguments).any_number_of_times.returns(&return_value)
|
126
|
+
double.should_not be_terminal
|
127
|
+
double
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "when two or more Terminal Doubles with duplicate Exact Match ArgumentExpectations exists" do
|
132
|
+
it "dispatches to Double that have an exact match" do
|
133
|
+
double1_with_exact_match = new_double(:exact_match) {:return_1}
|
134
|
+
|
135
|
+
subject.foobar(:exact_match).should == :return_1
|
136
|
+
end
|
137
|
+
|
138
|
+
it "dispatches to the first Double that have an exact match" do
|
139
|
+
double1_with_exact_match = new_double(:exact_match) {:return_1}
|
140
|
+
double2_with_exact_match = new_double(:exact_match) {:return_2}
|
141
|
+
|
142
|
+
subject.foobar(:exact_match).should == :return_1
|
143
|
+
end
|
144
|
+
|
145
|
+
it "dispatches the second Double with an exact match
|
146
|
+
when the first double's Times Called expectation is satisfied" do
|
147
|
+
double1_with_exact_match = new_double(:exact_match) {:return_1}
|
148
|
+
double2_with_exact_match = new_double(:exact_match) {:return_2}
|
149
|
+
|
150
|
+
subject.foobar(:exact_match)
|
151
|
+
subject.foobar(:exact_match).should == :return_2
|
152
|
+
end
|
153
|
+
|
154
|
+
it "raises TimesCalledError when all of the doubles Times Called expectation is satisfied" do
|
155
|
+
double1_with_exact_match = new_double(:exact_match) {:return_1}
|
156
|
+
double2_with_exact_match = new_double(:exact_match) {:return_2}
|
157
|
+
|
158
|
+
subject.foobar(:exact_match)
|
159
|
+
subject.foobar(:exact_match)
|
160
|
+
lambda do
|
161
|
+
subject.foobar(:exact_match)
|
162
|
+
end.should raise_error(Errors::TimesCalledError)
|
163
|
+
end
|
164
|
+
|
165
|
+
def new_double(*arguments, &return_value)
|
166
|
+
double = super()
|
167
|
+
double.with(*arguments).once.returns(&return_value)
|
168
|
+
double.should be_terminal
|
169
|
+
double
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context "when two or more Doubles with duplicate Wildcard Match ArgumentExpectations exists" do
|
174
|
+
it "dispatches to Double that have a wildcard match" do
|
175
|
+
double_1 = new_double {:return_1}
|
176
|
+
|
177
|
+
subject.foobar(:anything).should == :return_1
|
178
|
+
end
|
179
|
+
|
180
|
+
it "dispatches to the first Double that has a wildcard match" do
|
181
|
+
double_1 = new_double {:return_1}
|
182
|
+
double_2 = new_double {:return_2}
|
183
|
+
|
184
|
+
subject.foobar(:anything).should == :return_1
|
185
|
+
end
|
186
|
+
|
187
|
+
it "dispatches the second Double with a wildcard match
|
188
|
+
when the first double's Times Called expectation is satisfied" do
|
189
|
+
double_1 = new_double {:return_1}
|
190
|
+
double_2 = new_double {:return_2}
|
191
|
+
|
192
|
+
subject.foobar(:anything)
|
193
|
+
subject.foobar(:anything).should == :return_2
|
194
|
+
end
|
195
|
+
|
196
|
+
it "raises TimesCalledError when all of the doubles Times Called expectation is satisfied" do
|
197
|
+
double_1 = new_double {:return_1}
|
198
|
+
double_2 = new_double {:return_2}
|
199
|
+
|
200
|
+
subject.foobar(:anything)
|
201
|
+
subject.foobar(:anything)
|
202
|
+
lambda do
|
203
|
+
subject.foobar(:anything)
|
204
|
+
end.should raise_error(Errors::TimesCalledError)
|
205
|
+
end
|
206
|
+
|
207
|
+
def new_double(&return_value)
|
208
|
+
double = super
|
209
|
+
double.with_any_args.once.returns(&return_value)
|
210
|
+
double.should be_terminal
|
211
|
+
double
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe "method names with !" do
|
217
|
+
def method_name
|
218
|
+
:foobar!
|
219
|
+
end
|
220
|
+
|
221
|
+
context "when the original method uses the passed-in block" do
|
222
|
+
it "executes the block" do
|
223
|
+
double = new_double
|
224
|
+
double.with(1, 2) {:return_value}
|
225
|
+
subject.foobar!(1, 2).should == :return_value
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
describe "method names with ?" do
|
232
|
+
def method_name
|
233
|
+
:foobar?
|
234
|
+
end
|
235
|
+
|
236
|
+
context "when the original method uses the passed-in block" do
|
237
|
+
it "executes the block" do
|
238
|
+
double = new_double
|
239
|
+
double.with(1, 2) {:return_value}
|
240
|
+
subject.foobar?(1, 2).should == :return_value
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
require "
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
2
|
|
3
3
|
module RR
|
4
4
|
describe DoubleInjection, "#object_has_original_method?" do
|
5
5
|
before do
|
6
|
-
@
|
6
|
+
@subject = Object.new
|
7
7
|
@method_name = :to_s
|
8
|
-
@double_injection = DoubleInjection.new(@
|
8
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
9
9
|
class << @double_injection
|
10
10
|
public :original_method_name
|
11
11
|
end
|
@@ -18,10 +18,10 @@ module RR
|
|
18
18
|
|
19
19
|
it "returns true when respond_to is true and methods include method" do
|
20
20
|
@double_injection.bind
|
21
|
-
def @
|
21
|
+
def @subject.methods
|
22
22
|
[:__rr_original_to_s]
|
23
23
|
end
|
24
|
-
def @
|
24
|
+
def @subject.respond_to?(value)
|
25
25
|
true
|
26
26
|
end
|
27
27
|
|
@@ -30,10 +30,10 @@ module RR
|
|
30
30
|
|
31
31
|
it "returns true when respond_to is true and methods do not include method" do
|
32
32
|
@double_injection.bind
|
33
|
-
def @
|
33
|
+
def @subject.methods
|
34
34
|
[]
|
35
35
|
end
|
36
|
-
def @
|
36
|
+
def @subject.respond_to?(value)
|
37
37
|
true
|
38
38
|
end
|
39
39
|
|
@@ -42,10 +42,10 @@ module RR
|
|
42
42
|
|
43
43
|
it "returns false when respond_to is false and methods do not include method" do
|
44
44
|
@double_injection.bind
|
45
|
-
def @
|
45
|
+
def @subject.methods
|
46
46
|
[]
|
47
47
|
end
|
48
|
-
def @
|
48
|
+
def @subject.respond_to?(value)
|
49
49
|
false
|
50
50
|
end
|
51
51
|
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInjection do
|
5
|
+
macro("cleans up by removing the __rr__method") do
|
6
|
+
it "cleans up by removing the __rr__method" do
|
7
|
+
@double_injection.bind
|
8
|
+
@subject.methods.should include("__rr__foobar")
|
9
|
+
|
10
|
+
@double_injection.reset
|
11
|
+
@subject.methods.should_not include("__rr__foobar")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#reset" do
|
16
|
+
context "when method does not exist" do
|
17
|
+
send("cleans up by removing the __rr__method")
|
18
|
+
|
19
|
+
before do
|
20
|
+
@subject = Object.new
|
21
|
+
@method_name = :foobar
|
22
|
+
@subject.methods.should_not include(@method_name.to_s)
|
23
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "removes the method" do
|
27
|
+
@double_injection.bind
|
28
|
+
@subject.methods.should include(@method_name.to_s)
|
29
|
+
|
30
|
+
@double_injection.reset
|
31
|
+
@subject.methods.should_not include(@method_name.to_s)
|
32
|
+
lambda {@subject.foobar}.should raise_error(NoMethodError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when method exists" do
|
37
|
+
send("cleans up by removing the __rr__method")
|
38
|
+
|
39
|
+
before do
|
40
|
+
@subject = Object.new
|
41
|
+
@method_name = :foobar
|
42
|
+
def @subject.foobar
|
43
|
+
:original_foobar
|
44
|
+
end
|
45
|
+
@subject.methods.should include(@method_name.to_s)
|
46
|
+
@original_method = @subject.method(@method_name)
|
47
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
48
|
+
|
49
|
+
@double_injection.bind
|
50
|
+
@subject.methods.should include(@method_name.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "rebind original method" do
|
54
|
+
@double_injection.reset
|
55
|
+
@subject.methods.should include(@method_name.to_s)
|
56
|
+
@subject.foobar.should == :original_foobar
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when method with block exists" do
|
61
|
+
send("cleans up by removing the __rr__method")
|
62
|
+
|
63
|
+
before do
|
64
|
+
@subject = Object.new
|
65
|
+
@method_name = :foobar
|
66
|
+
def @subject.foobar
|
67
|
+
yield(:original_argument)
|
68
|
+
end
|
69
|
+
@subject.methods.should include(@method_name.to_s)
|
70
|
+
@original_method = @subject.method(@method_name)
|
71
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
72
|
+
|
73
|
+
@double_injection.bind
|
74
|
+
@subject.methods.should include(@method_name.to_s)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "rebinds original method with block" do
|
78
|
+
@double_injection.reset
|
79
|
+
@subject.methods.should include(@method_name.to_s)
|
80
|
+
|
81
|
+
original_argument = nil
|
82
|
+
@subject.foobar do |arg|
|
83
|
+
original_argument = arg
|
84
|
+
end
|
85
|
+
original_argument.should == :original_argument
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|