Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special case for Class.new method signature that should use initialize signature instead #419

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/rspec/support/method_signature_verifier.rb
Expand Up @@ -12,7 +12,7 @@ class MethodSignature # rubocop:disable ClassLength
attr_reader :min_non_kw_args, :max_non_kw_args, :optional_kw_args, :required_kw_args

def initialize(method)
@method = method
@method = method_or_class_initialize(method)
@optional_kw_args = []
@required_kw_args = []
classify_parameters
Expand Down Expand Up @@ -158,6 +158,15 @@ def unlimited_args?
end

INFINITY = 1 / 0.0

private

def method_or_class_initialize(method)
# Support only not redefined Class.new
return method if !method.respond_to?(:owner) || method.owner != Class
return method if !method.respond_to?(:name) || method.name != :new
Copy link
Member

Choose a reason for hiding this comment

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

What are the cases when methods don't respond to owner or name?

Copy link
Author

Choose a reason for hiding this comment

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

At least one of the tests are passing Proc, which fails without this check.

method.receiver.instance_method(:initialize)
end
end

if RSpec::Support::Ruby.jruby?
Expand Down
57 changes: 57 additions & 0 deletions spec/rspec/support/method_signature_verifier_spec.rb
Expand Up @@ -817,6 +817,63 @@ def arity_block(_, &block); end
end
end

describe 'an `.new` method' do
old_verbose, $VERBOSE = $VERBOSE, false
class TestInitializeClass; def initialize(arg1, arg2); end; end
class TestNewClass; def self.new(arg1, arg2, arg3); super; end; end
$VERBOSE = old_verbose

describe 'for pure Class' do
let(:test_method) { Class.method(:new) }

it 'match unlimited arguments' do
expect(validate_expectation :unlimited_args).to eq(true)
end

it 'validates against 2 arguments' do
expect(valid_non_kw_args?(2)).to eq true
end

it 'validates against 2 arguments' do
expect(valid_non_kw_args?(3)).to eq true
end
end

describe 'for class with initialize method' do


let(:test_method) { TestInitializeClass.method(:new) }

it 'does not match unlimited arguments' do
expect(validate_expectation :unlimited_args).to eq(false)
end

it 'validates against 2 arguments' do
expect(valid_non_kw_args?(2)).to eq true
end

it 'fails validation against 3 arguments' do
expect(valid_non_kw_args?(3)).to eq false
end
end

describe 'for class with owerwritten new method' do
let(:test_method) { TestNewClass.method(:new) }

it 'does not match unlimited arguments' do
expect(validate_expectation :unlimited_args).to eq(false)
end

it 'fails validation against 2 arguments' do
expect(valid_non_kw_args?(2)).to eq false
end

it 'validates against 3 arguments' do
expect(valid_non_kw_args?(3)).to eq true
end
end
end

if Ruby.jruby?
describe 'a single-argument Java method' do
let(:test_method) { Java::JavaLang::String.instance_method(:char_at) }
Expand Down