jferris-rr 0.7.1.0.1239654108
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +196 -0
- data/README.rdoc +329 -0
- data/Rakefile +77 -0
- data/lib/rr.rb +84 -0
- data/lib/rr/adapters/rr_methods.rb +122 -0
- data/lib/rr/adapters/rspec.rb +58 -0
- data/lib/rr/adapters/test_unit.rb +29 -0
- data/lib/rr/double.rb +212 -0
- data/lib/rr/double_definitions/child_double_definition_creator.rb +27 -0
- data/lib/rr/double_definitions/double_definition.rb +346 -0
- data/lib/rr/double_definitions/double_definition_creator.rb +167 -0
- data/lib/rr/double_definitions/double_definition_creator_proxy.rb +37 -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/implementation/strongly_typed_reimplementation.rb +17 -0
- data/lib/rr/double_definitions/strategies/scope/instance.rb +15 -0
- data/lib/rr/double_definitions/strategies/scope/instance_of_class.rb +46 -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 +143 -0
- data/lib/rr/double_matches.rb +51 -0
- data/lib/rr/errors/argument_equality_error.rb +6 -0
- data/lib/rr/errors/double_definition_error.rb +6 -0
- data/lib/rr/errors/double_not_found_error.rb +6 -0
- data/lib/rr/errors/double_order_error.rb +6 -0
- data/lib/rr/errors/rr_error.rb +20 -0
- data/lib/rr/errors/spy_verification_errors/double_injection_not_found_error.rb +8 -0
- data/lib/rr/errors/spy_verification_errors/invocation_count_error.rb +8 -0
- data/lib/rr/errors/spy_verification_errors/spy_verification_error.rb +8 -0
- data/lib/rr/errors/subject_does_not_implement_method_error.rb +6 -0
- data/lib/rr/errors/subject_has_different_arity_error.rb +6 -0
- data/lib/rr/errors/times_called_error.rb +6 -0
- data/lib/rr/expectations/any_argument_expectation.rb +21 -0
- data/lib/rr/expectations/argument_equality_expectation.rb +41 -0
- data/lib/rr/expectations/times_called_expectation.rb +57 -0
- data/lib/rr/hash_with_object_id_key.rb +41 -0
- data/lib/rr/recorded_calls.rb +103 -0
- data/lib/rr/space.rb +123 -0
- data/lib/rr/spy_verification.rb +48 -0
- data/lib/rr/spy_verification_proxy.rb +18 -0
- data/lib/rr/times_called_matchers/any_times_matcher.rb +18 -0
- data/lib/rr/times_called_matchers/at_least_matcher.rb +15 -0
- data/lib/rr/times_called_matchers/at_most_matcher.rb +23 -0
- data/lib/rr/times_called_matchers/integer_matcher.rb +19 -0
- data/lib/rr/times_called_matchers/non_terminal.rb +27 -0
- data/lib/rr/times_called_matchers/proc_matcher.rb +11 -0
- data/lib/rr/times_called_matchers/range_matcher.rb +21 -0
- data/lib/rr/times_called_matchers/terminal.rb +20 -0
- data/lib/rr/times_called_matchers/times_called_matcher.rb +44 -0
- data/lib/rr/wildcard_matchers/anything.rb +18 -0
- data/lib/rr/wildcard_matchers/boolean.rb +23 -0
- data/lib/rr/wildcard_matchers/duck_type.rb +32 -0
- data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
- data/lib/rr/wildcard_matchers/is_a.rb +25 -0
- data/lib/rr/wildcard_matchers/numeric.rb +13 -0
- data/lib/rr/wildcard_matchers/range.rb +7 -0
- data/lib/rr/wildcard_matchers/regexp.rb +7 -0
- data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
- data/spec/core_spec_suite.rb +19 -0
- data/spec/environment_fixture_setup.rb +6 -0
- data/spec/high_level_spec.rb +368 -0
- data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
- data/spec/rr/adapters/rr_methods_creator_spec.rb +149 -0
- data/spec/rr/adapters/rr_methods_space_spec.rb +115 -0
- data/spec/rr/adapters/rr_methods_spec_helper.rb +11 -0
- data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +17 -0
- data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +112 -0
- data/spec/rr/double_definitions/double_definition_creator_proxy_spec.rb +155 -0
- data/spec/rr/double_definitions/double_definition_creator_spec.rb +502 -0
- data/spec/rr/double_definitions/double_definition_spec.rb +1159 -0
- data/spec/rr/double_injection/double_injection_bind_spec.rb +111 -0
- data/spec/rr/double_injection/double_injection_dispatching_spec.rb +244 -0
- data/spec/rr/double_injection/double_injection_has_original_method_spec.rb +55 -0
- 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 +352 -0
- data/spec/rr/errors/rr_error_spec.rb +67 -0
- data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
- data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +14 -0
- data/spec/rr/expectations/argument_equality_expectation_spec.rb +135 -0
- data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +34 -0
- 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 +46 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +69 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +71 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +23 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +104 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +81 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +83 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +38 -0
- data/spec/rr/rspec/invocation_matcher_spec.rb +279 -0
- data/spec/rr/rspec/rspec_adapter_spec.rb +66 -0
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +31 -0
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec_fixture.rb +11 -0
- data/spec/rr/rspec/rspec_usage_spec.rb +86 -0
- data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
- data/spec/rr/space/space_spec.rb +542 -0
- data/spec/rr/test_unit/test_helper.rb +7 -0
- data/spec/rr/test_unit/test_unit_backtrace_test.rb +35 -0
- data/spec/rr/test_unit/test_unit_integration_test.rb +57 -0
- data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
- data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
- data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
- data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
- data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
- data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
- data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
- 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/rspec_spec_suite.rb +16 -0
- data/spec/spec_helper.rb +107 -0
- data/spec/spec_suite.rb +27 -0
- data/spec/spy_verification_spec.rb +129 -0
- data/spec/test_unit_spec_suite.rb +21 -0
- metadata +187 -0
@@ -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,244 @@
|
|
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
|
+
).any_number_of_times
|
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.definition.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.definition.with(:exact_match_1).returns {:return_1}
|
46
|
+
double_with_no_match = new_double
|
47
|
+
double_with_no_match.definition.with("nothing that matches").returns {:no_match}
|
48
|
+
double2_with_exact_match = new_double
|
49
|
+
double2_with_exact_match.definition.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.definition.with_any_args.returns {:wild_card_value}
|
58
|
+
double_with_no_match = new_double
|
59
|
+
double_with_no_match.definition.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.definition.with(1, 2)
|
70
|
+
|
71
|
+
double_2 = new_double
|
72
|
+
double_2.definition.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.definition.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.definition.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.definition.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.definition.with(1, 2) {:return_value}
|
225
|
+
subject.foobar!(1, 2).should == :return_value
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "method names with ?" do
|
231
|
+
def method_name
|
232
|
+
:foobar?
|
233
|
+
end
|
234
|
+
|
235
|
+
context "when the original method uses the passed-in block" do
|
236
|
+
it "executes the block" do
|
237
|
+
double = new_double
|
238
|
+
double.definition.with(1, 2) {:return_value}
|
239
|
+
subject.foobar?(1, 2).should == :return_value
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInjection, "#object_has_original_method?" do
|
5
|
+
before do
|
6
|
+
@subject = Object.new
|
7
|
+
@method_name = :to_s
|
8
|
+
@double_injection = DoubleInjection.new(@subject, @method_name)
|
9
|
+
class << @double_injection
|
10
|
+
public :original_method_name
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns true when method is still in object" do
|
15
|
+
@double_injection.bind
|
16
|
+
@double_injection.object_has_original_method?.should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns true when respond_to is true and methods include method" do
|
20
|
+
@double_injection.bind
|
21
|
+
def @subject.methods
|
22
|
+
[:__rr_original_to_s]
|
23
|
+
end
|
24
|
+
def @subject.respond_to?(value)
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
@double_injection.object_has_original_method?.should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns true when respond_to is true and methods do not include method" do
|
32
|
+
@double_injection.bind
|
33
|
+
def @subject.methods
|
34
|
+
[]
|
35
|
+
end
|
36
|
+
def @subject.respond_to?(value)
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
@double_injection.object_has_original_method?.should be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns false when respond_to is false and methods do not include method" do
|
44
|
+
@double_injection.bind
|
45
|
+
def @subject.methods
|
46
|
+
[]
|
47
|
+
end
|
48
|
+
def @subject.respond_to?(value)
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
@double_injection.object_has_original_method?.should be_false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -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
|