Skip to content
Prev Previous commit
Add specs for method signature verifier with blocks and keywords
  • Loading branch information
JonRowe committed Aug 17, 2025
commit fbb5720e234ed889b58d10d852a30ef28aa968b7
66 changes: 66 additions & 0 deletions rspec-support/spec/rspec/support/method_signature_verifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def signature_description
signature.description
end

def validate(target, *args)
target_signature = MethodSignature.new(target)
described_class.new(target_signature, args).valid?
end
ruby2_keywords(:validate) if respond_to?(:ruby2_keywords, true)

def validate_expectation(*args)
obj = MethodSignatureExpectation.new

Expand All @@ -43,6 +49,7 @@ def validate_expectation(*args)

described_class.new(signature).with_expectation(obj).valid?
end
ruby2_keywords(:validate_expectation) if respond_to?(:ruby2_keywords, true)

shared_context 'a method verifier' do
describe 'with a method with arguments' do
Expand Down Expand Up @@ -292,6 +299,10 @@ def arity_kw(x, y:1, z:2); end
expect(valid?(nil, :a => 1)).to eq(false)
end

it 'does not treat a last-arg hash as kw args' do
expect(valid?({ "1" => 1 })).to eq(true)
end

it 'mentions the invalid keyword args in the error', :pending => RSpec::Support::Ruby.jruby? && !RSpec::Support::Ruby.jruby_9000? do
expect(error_for(nil, :a => 0, :b => 1)).to \
eq("Invalid keyword arguments provided: a, b")
Expand Down Expand Up @@ -905,6 +916,61 @@ def arity_block(_, &block); end
end
end
end

describe 'a proc' do
it 'will match partial args' do
a_proc = proc { |_a, _b| }
expect(validate a_proc, 1).to be(true)
expect(validate a_proc, 2, 2).to be(true)
expect(validate a_proc, 3, 3, 3).to be(false)
end

if RubyFeatures.kw_args_supported?
it 'will not match keyword args when not defined' do
eval <<-RUBY
a_proc = proc { |_a| }
expect(validate a_proc, :arg, opts: []).to eq(false)
RUBY
end

it 'will match keyword args' do
eval <<-RUBY
a_proc = proc { |_a, opts: []| }
expect(validate a_proc, :arg, opts: [:value]).to eq(true)
RUBY
end

it 'allows a hash to be distinct from optional keyword arguments' do
eval <<-RUBY
a_proc = proc { |_a, opts: []| }
expect(validate a_proc, {1 => "not_opts"}).to eq(true)
RUBY
end
end

if RubyFeatures.required_kw_args_supported?
it 'will not match required keyword args when not defined' do
eval <<-RUBY
a_proc = proc { |_a| }
expect(validate a_proc, :arg, required: true).to eq(false)
RUBY
end

it 'will match required keyword args' do
eval <<-RUBY
a_proc = proc { |_a, required:| }
expect(validate a_proc, :arg, required: true).to eq(true)
RUBY
end

it 'allows a hash to be distinct from required keyword arguments' do
eval <<-RUBY
a_proc = proc { |_a, required:| }
expect(validate a_proc, {1 => "not_opts"}, required: true).to eq(true)
RUBY
end
end
end
end

let(:fake_matcher) { Object.new }
Expand Down