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

Add tests and fix for ruby 3 keyword arguments issues #1407

Merged
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
1 change: 1 addition & 0 deletions lib/rspec/mocks/any_instance/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def observe!(method_name)
recorder.playback!(self, method_name)
__send__(method_name, *args, &blk)
end
@klass.__send__(:ruby2_keywords, method_name) if @klass.respond_to?(:ruby2_keywords, true)
end

def mark_invoked!(method_name)
Expand Down
48 changes: 48 additions & 0 deletions spec/rspec/mocks/and_call_original_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ def meth_2(x)
yield x, :additional_yielded_arg
end

if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-RUBY, __FILE__, __LINE__)
def meth_3(**kwargs)
kwargs
end

def meth_4(x: 1)
x
end
RUBY
end

if RSpec::Support::RubyFeatures.required_kw_args_supported?
binding.eval(<<-RUBY, __FILE__, __LINE__)
def meth_5(x:)
x
end
RUBY
end

def self.new_instance
new
end
Expand Down Expand Up @@ -125,6 +145,34 @@ def inst.foo; :bar; end
expect(klass.new.meth_1).to eq(:original)
end

if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-RUBY, __FILE__, __LINE__)
it 'works for instance methods that use double splat' do
expect_any_instance_of(klass).to receive(:meth_3).and_call_original
expect(klass.new.meth_3(x: :kwarg)).to eq({x: :kwarg})
end

it 'works for instance methods that use optional keyword arguments' do
expect_any_instance_of(klass).to receive(:meth_4).and_call_original
expect(klass.new.meth_4).to eq(1)
end

it 'works for instance methods that use optional keyword arguments with an argument supplied' do
expect_any_instance_of(klass).to receive(:meth_4).and_call_original
expect(klass.new.meth_4(x: :kwarg)).to eq(:kwarg)
end
RUBY
end

if RSpec::Support::RubyFeatures.required_kw_args_supported?
binding.eval(<<-RUBY, __FILE__, __LINE__)
it 'works for instance methods that use required keyword arguments' do
expect_any_instance_of(klass).to receive(:meth_5).and_call_original
expect(klass.new.meth_5(x: :kwarg)).to eq(:kwarg)
end
RUBY
end

it 'works for instance methods defined on the superclass of the class' do
subclass = Class.new(klass)
expect_any_instance_of(subclass).to receive(:meth_1).and_call_original
Expand Down
59 changes: 55 additions & 4 deletions spec/rspec/mocks/matchers/receive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,63 @@ module Mocks
receiver.foo(1.1)
end

it 'allows a `do...end` block implementation to be provided' do
wrapped.to receive(:foo) do
4
context 'without yielding receiver' do
# when `yield_receiver_to_any_instance_implementation_blocks` is `true`
# the block arguments are different for `expect` and `expect_any_instance_of`
around do |example|
previous_value = RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks?
RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false
example.run
RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = previous_value
end

it 'allows a `do...end` block implementation to be provided' do
wrapped.to receive(:foo) do
4
end

expect(receiver.foo).to eq(4)
end

expect(receiver.foo).to eq(4)
if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-RUBY, __FILE__, __LINE__)
it 'allows a `do...end` block implementation with keyword args to be provided' do
wrapped.to receive(:foo) do |**kwargs|
kwargs[:kw]
end

expect(receiver.foo(kw: :arg)).to eq(:arg)
end

it 'allows a `do...end` block implementation with optional keyword args to be provided' do
wrapped.to receive(:foo) do |kw: :arg|
kw
end

expect(receiver.foo(kw: 1)).to eq(1)
end

it 'allows a `do...end` block implementation with optional keyword args to be provided' do
wrapped.to receive(:foo) do |kw: :arg|
kw
end

expect(receiver.foo).to eq(:arg)
end
RUBY
end

if RSpec::Support::RubyFeatures.required_kw_args_supported?
binding.eval(<<-RUBY, __FILE__, __LINE__)
it 'allows a `do...end` block implementation with required keyword args' do
wrapped.to receive(:foo) do |kw:|
kw
end

expect(receiver.foo(kw: :arg)).to eq(:arg)
end
RUBY
end
end

it 'allows chaining off a `do...end` block implementation to be provided' do
Expand Down