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

Commit 7820eb2

Browse files
committed
Workaround buggy Method#parameters in JRuby
JRuby apparently only supports #arity (and not #parameters) for Java proxy methods (see jruby/jruby#2817), but verifying doubles in RSpec use #parameters to infer required method parameters, and thus doesn't work for Java classes. Given that this issue affects every release of JRuby so far with support for #parameters, it seemed reasonable to replace the previous workaround that only affected JRuby 1.7.0 through 1.7.19.
1 parent c41e875 commit 7820eb2

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/rspec/support/method_signature_verifier.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,26 @@ def classify_parameters
154154
INFINITY = 1 / 0.0
155155
end
156156

157-
# Some versions of JRuby have a nasty bug we have to work around :(.
158-
# https://github.com/jruby/jruby/issues/2816
157+
# JRuby has only partial support for UnboundMethod#parameters, so we fall back on using #arity
158+
# https://github.com/jruby/jruby/issues/2816 and https://github.com/jruby/jruby/issues/2817
159159
if RSpec::Support::Ruby.jruby? &&
160160
RubyFeatures.optional_and_splat_args_supported? &&
161-
Class.new { attr_writer :foo }.instance_method(:foo=).parameters == []
161+
Java::JavaLang::String.instance_method(:char_at).parameters == []
162162

163163
class MethodSignature < remove_const(:MethodSignature)
164164
private
165165

166166
def classify_parameters
167167
super
168-
return unless @method.parameters == [] && @method.arity == 1
169-
@max_non_kw_args = @min_non_kw_args = 1
168+
if (arity = @method.arity) != 0 && @method.parameters.empty?
169+
if arity < 0
170+
@min_non_kw_args = ~arity
171+
@max_non_kw_args = INFINITY
172+
else
173+
@min_non_kw_args = arity
174+
@max_non_kw_args = arity
175+
end
176+
end
170177
end
171178
end
172179
end

spec/rspec/support/method_signature_verifier_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,16 @@ def arity_block(_, &block); end
719719
expect(valid_non_kw_args?(2)).to eq false
720720
end
721721
end
722+
723+
if Ruby.jruby?
724+
describe 'a single-argument Java method' do
725+
let(:test_method) { Java::JavaLang::String.instance_method(:char_at) }
726+
727+
it 'validates against a single argument' do
728+
expect(valid_non_kw_args?(1)).to eq true
729+
end
730+
end
731+
end
722732
end
723733

724734
let(:fake_matcher) { Object.new }

0 commit comments

Comments
 (0)