This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 102
Add helper for running class_exec with keywords when needed #407
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.