Skip to content

Commit

Permalink
Prevent infinite loop when interpolating a null double as an integer …
Browse files Browse the repository at this point in the history
…into a string.

Closes rspec#154.
  • Loading branch information
myronmarston committed Jun 18, 2012
1 parent 980b98f commit f8cae14
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Bug fixes
* Infinite loop generated by using `any_instance` and `dup`. (Sidu Ponnappa @kaiwren)
* `double.should_receive(:foo).at_least(:once).and_return(a)` always returns a
even if `:foo` is already stubbed.
* Prevent infinite loop when interpolating a null double into a string
as an integer (`"%i" % double.as_null_object`). (Myron Marston)

### 2.10.1 / 2012-05-05
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.10.0...v2.10.1)
Expand Down
4 changes: 3 additions & 1 deletion lib/rspec/mocks/test_double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ def __initialize_as_test_double(name=nil, stubs_and_options={})
end

def method_missing(message, *args, &block)
raise NoMethodError if message == :to_ary
raise NoMethodError if message == :to_ary
return 0 if message == :to_int && __mock_proxy.null_object?
__mock_proxy.record_message_received(message, *args, &block)

begin
__mock_proxy.null_object? ? self : super
rescue NameError
Expand Down
16 changes: 16 additions & 0 deletions spec/rspec/mocks/null_object_mock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ module Mocks
@double.stub(:foo)
@double.should respond_to(:foo)
end

it "raises an error when interpolated in a string as an integer" do
expected_error = RUBY_VERSION =~ /1.8/ ?
TypeError :
RSpec::Mocks::MockExpectationError

expect { "%i" % @double }.to raise_error(expected_error)
end
end

describe "a double acting as a null object" do
Expand Down Expand Up @@ -59,6 +67,14 @@ module Mocks
@double.message(:expected_arg)
@double.message(:unexpected_arg)
end

it "can be interpolated in a string as an integer" do
# This form of string interpolation calls
# @double.to_int.to_int.to_int...etc until it gets an integer,
# and thus gets stuck in an infinite loop unless our double
# returns an int value from #to_int.
("%i" % @double).should eq("0")
end
end

describe "#as_null_object" do
Expand Down

0 comments on commit f8cae14

Please sign in to comment.