Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Differentiate between block args with defaults and without. #83

Merged
merged 1 commit into from
Jun 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 3.0.1 Development
[Full Changelog](http://github.com/rspec/rspec-support/compare/v3.0.0...3-0-maintenance)

* Fix `BlockSignature` so that it correctly differentiates between
required and optional block args. (Myron Marston, rspec-mocks#714)

### 3.0.0 / 2014-06-01
[Full Changelog](http://github.com/rspec/rspec-support/compare/v3.0.0.rc1...v3.0.0)

Expand Down
21 changes: 21 additions & 0 deletions lib/rspec/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,26 @@ def self.method_handle_for(object, method_name)
end
end
end

if RUBY_PLATFORM == 'java'
def self.proc_to_lambda(block)
return block if block.lambda?
lambda(&block)
end
elsif respond_to?(:define_singleton_method)
def self.proc_to_lambda(block)
return block if block.lambda?

obj = Object.new
obj.define_singleton_method(:to_lambda, &block)
obj.method(:to_lambda).to_proc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_lambda to_proc? Can you elaborate how this works?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods and lambdas share semantics in terms of how return works and how they handle arguments. To convert a proc to a lambda on MRI, we need to define a method using the proc, and then, use Method#to_proc to get back proc that's a lambda proc.

I just chose to_lambda as the name for the method but it doesn't really matter what it's named.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, so to_proc actually returns a lambda ok, thanks :)

end
else # 1.8.7
def self.proc_to_lambda(block)
obj = Object.new
(class << obj; self; end).__send__(:define_method, :to_lambda, &block)
obj.method(:to_lambda).to_proc
end
end
end
end
7 changes: 2 additions & 5 deletions lib/rspec/support/method_signature_verifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,8 @@ def classify_parameters
#
# @api private
class BlockSignature < MethodSignature
if RubyFeatures.optional_and_splat_args_supported?
def classify_parameters
super
@min_non_kw_args = @max_non_kw_args unless @max_non_kw_args == INFINITY
end
def initialize(block)
super(Support.proc_to_lambda(block))
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/rspec/support/method_signature_verifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@

module RSpec
module Support
describe BlockSignature do
it 'differentiates optional vs required args', :if => RubyFeatures.optional_and_splat_args_supported? do
bs = BlockSignature.new(Proc.new { |a, b| })
expect(bs.min_non_kw_args).to eq(2)
expect(bs.max_non_kw_args).to eq(2)

bs = eval("BlockSignature.new(Proc.new { |a, b=2| })")
expect(bs.min_non_kw_args).to eq(1)
expect(bs.max_non_kw_args).to eq(2)
end
end

describe MethodSignatureVerifier do
describe '#verify!' do
let(:signature) { MethodSignature.new(test_method) }
Expand Down
15 changes: 15 additions & 0 deletions spec/rspec/support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,20 @@ def method_missing(name, *args, &block)
end
end
end

describe ".proc_to_lambda", :if => Proc.method_defined?(:lambda?) do
it "converts a proc to a lambda" do
p = Proc.new { 47 }
expect(p).not_to be_lambda
l = Support.proc_to_lambda(p)
expect(l).to be_lambda
expect(l.call).to eq(47)
end

it 'returns a lambda unchanged' do
l = lambda { }
expect(Support.proc_to_lambda(l)).to be(l)
end
end
end
end