This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 103
Allow string keys for method kwarg splats #591
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bd26d93
Allow string keys for method kwarg splats
malcolmohare ab28606
refactor kwargs filtering code path
malcolmohare a150ae3
remove kwargs parameter that had a hash as a key as its not valid for…
malcolmohare 7f30ba3
version gate the change and the specs
malcolmohare 30dea7d
make impl cleaner
malcolmohare 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 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
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 |
---|---|---|
|
@@ -384,17 +384,21 @@ def arity_kw(x, y = {}, z:2); end | |
expect(valid?(nil, :a => 1)).to eq(false) | ||
end | ||
|
||
it 'treats symbols as keyword arguments and the rest as optional argument' do | ||
expect(valid?(nil, 'a' => 1)).to eq(true) | ||
expect(valid?(nil, 'a' => 1, :z => 3)).to eq(true) | ||
expect(valid?(nil, 'a' => 1, :b => 3)).to eq(false) | ||
expect(valid?(nil, 'a' => 1, :b => 2, :z => 3)).to eq(false) | ||
it 'treats symbols as keyword arguments' do | ||
expect(valid?(nil, :z => 3)).to eq(true) | ||
expect(valid?(nil, :b => 3)).to eq(false) | ||
expect(valid?(nil, :b => 2, :z => 3)).to eq(false) | ||
end | ||
|
||
it 'treats string keys as invalid keyword arguments' do | ||
expect(valid?(nil, 'a' => 1)).to eq(false) | ||
expect(valid?(nil, 'a' => 1, :z => 3)).to eq(false) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be changed to conditonally apply the new behaviour, the old spec was valid for Ruby 2.x Typically we'd define whole different specs for different Rubies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, added this as a RubyFeatures method and applied the change and the specs based on that flag. |
||
|
||
it 'mentions the invalid keyword args in the error', :pending => RSpec::Support::Ruby.jruby? && !RSpec::Support::Ruby.jruby_9000? do | ||
expect(error_for(1, 2, :a => 0)).to eq("Invalid keyword arguments provided: a") | ||
expect(error_for(1, :a => 0)).to eq("Invalid keyword arguments provided: a") | ||
expect(error_for(1, 'a' => 0, :b => 0)).to eq("Invalid keyword arguments provided: b") | ||
expect(error_for(1, 'a' => 0, :b => 0)).to eq("Invalid keyword arguments provided: a, b") | ||
malcolmohare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
it 'describes invalid arity precisely' do | ||
|
@@ -656,6 +660,53 @@ def arity_required_kw_splat(w, *x, y:, z:, a: 'default'); end | |
end | ||
end | ||
|
||
describe 'a method with only a keyword arg splat' do | ||
eval <<-RUBY | ||
def arity_kw_arg_splat(**rest); end | ||
RUBY | ||
|
||
let(:test_method) { method(:arity_kw_arg_splat) } | ||
|
||
it 'allows undeclared keyword args' do | ||
expect(valid?(:x => 1)).to eq(true) | ||
expect(valid?(:x => 1, 'y' => 2)).to eq(true) | ||
expect(valid?('y' => 2)).to eq(true) | ||
end | ||
|
||
it 'does not allow kw args of only unsupported types' do | ||
expect(valid?(3 => 1)).to eq(false) | ||
expect(valid?({"a":"b"} => 1)).to eq(false) | ||
end | ||
|
||
it "ignores kwargs with invalid types if there are any with valid types" do | ||
expect(valid?(:x => 1, 3 => 10)).to eq(true) | ||
expect(valid?(:x => 1, 'y' => 2, 3 => 10)).to eq(true) | ||
expect(valid?('y' => 2, 3 => 10)).to eq(true) | ||
end | ||
|
||
it 'mentions the required kw args and keyword splat in the description' do | ||
expect(signature_description).to \ | ||
eq("any additional keyword args") | ||
end | ||
|
||
describe 'with an expectation object' do | ||
it 'allows zero non-kw args' do | ||
expect(validate_expectation 0).to eq(true) | ||
expect(validate_expectation 1).to eq(false) | ||
expect(validate_expectation 0, 0).to eq(true) | ||
expect(validate_expectation 0, 1).to eq(false) | ||
end | ||
|
||
it 'does not match unlimited arguments' do | ||
expect(validate_expectation :unlimited_args).to eq(false) | ||
end | ||
|
||
it 'matches arbitrary keywords' do | ||
expect(validate_expectation :arbitrary_kw_args).to eq(true) | ||
end | ||
end | ||
end | ||
|
||
describe 'a method with required keyword arguments and a keyword arg splat' do | ||
eval <<-RUBY | ||
def arity_kw_arg_splat(x:, **rest); end | ||
|
@@ -666,6 +717,7 @@ def arity_kw_arg_splat(x:, **rest); end | |
it 'allows extra undeclared keyword args' do | ||
expect(valid?(:x => 1)).to eq(true) | ||
expect(valid?(:x => 1, :y => 2)).to eq(true) | ||
expect(valid?(:x => 1, :y => 2, 'z' => 3)).to eq(true) | ||
end | ||
|
||
it 'mentions missing required keyword args in the error' do | ||
|
@@ -730,7 +782,9 @@ def arity_kw_arg_splat(x, **rest); end | |
it 'allows a single arg and any number of keyword args' do | ||
expect(valid?(nil)).to eq(true) | ||
expect(valid?(nil, :x => 1)).to eq(true) | ||
expect(valid?(nil, 'x' => 1)).to eq(true) | ||
expect(valid?(nil, :x => 1, :y => 2)).to eq(true) | ||
expect(valid?(nil, :x => 1, :y => 2, 'z' => 3)).to eq(true) | ||
expect(valid?(:x => 1)).to eq(true) | ||
expect(valid?(nil, {})).to eq(true) | ||
|
||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an alternative version of this function, let me know which is preferred.