Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper for running class_exec with keywords when needed
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
RSpec::Support.require_rspec_support("method_signature_verifier") | ||
|
||
module RSpec | ||
module Support | ||
module WithKeywordsWhenNeeded | ||
# This module adds keyword sensitive support for core ruby methods | ||
# where we cannot use `ruby2_keywords` directly. | ||
|
||
module_function | ||
|
||
if RSpec::Support::RubyFeatures.kw_args_supported? | ||
# Remove this in RSpec 4 in favour of explictly passed in kwargs where | ||
# this is used. Works around a warning in Ruby 2.7 | ||
|
||
def class_exec(klass, *args, &block) | ||
if MethodSignature.new(block).has_kw_args_in?(args) | ||
binding.eval(<<-CODE, __FILE__, __LINE__) | ||
kwargs = args.pop | ||
klass.class_exec(*args, **kwargs, &block) | ||
CODE | ||
else | ||
klass.class_exec(*args, &block) | ||
end | ||
end | ||
ruby2_keywords :class_exec if respond_to?(:ruby2_keywords, true) | ||
else | ||
def class_exec(klass, *args, &block) | ||
klass.class_exec(*args, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'rspec/support/with_keywords_when_needed' | ||
|
||
module RSpec::Support | ||
RSpec.describe "WithKeywordsWhenNeeded" do | ||
|
||
describe ".class_exec" do | ||
extend RubyFeatures | ||
|
||
let(:klass) do | ||
Class.new do | ||
def self.check_argument(argument) | ||
raise ArgumentError unless argument == 42 | ||
end | ||
end | ||
end | ||
|
||
def run(klass, *args, &block) | ||
WithKeywordsWhenNeeded.class_exec(klass, *args, &block) | ||
end | ||
|
||
it "will run a block without keyword arguments" do | ||
run(klass, 42) { |arg| check_argument(arg) } | ||
end | ||
|
||
it "will run a block with a hash with out keyword arguments" do | ||
run(klass, "value" => 42) { |arg| check_argument(arg["value"]) } | ||
end | ||
|
||
it "will run a block with optional keyword arguments when none are provided", :if => kw_args_supported? do | ||
binding.eval(<<-CODE, __FILE__, __LINE__) | ||
run(klass, 42) { |arg, val: nil| check_argument(arg) } | ||
CODE | ||
end | ||
|
||
it "will run a block with optional keyword arguments when they are provided", :if => required_kw_args_supported? do | ||
binding.eval(<<-CODE, __FILE__, __LINE__) | ||
run(klass, val: 42) { |val: nil| check_argument(val) } | ||
CODE | ||
end | ||
|
||
it "will run a block with required keyword arguments", :if => required_kw_args_supported? do | ||
binding.eval(<<-CODE, __FILE__, __LINE__) | ||
run(klass, val: 42) { |val:| check_argument(val) } | ||
CODE | ||
end | ||
end | ||
end | ||
end |