-
-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Ruby 2.7] Add specs for pattern matching #751
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c6ce00d
Add specs for pattern matching
andrykonchin 939d629
Wrap specs with eval method calls
andrykonchin 9935693
Add specs for Array#deconstruct
andrykonchin 884e323
Add specs for Hash#deconstruct_keys
andrykonchin 4a39267
Add specs for Struct#deconstruct_keys
andrykonchin 9cfe5b8
Add specs for Struct#deconstruct
andrykonchin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require_relative '../../spec_helper' | ||
|
||
ruby_version_is "2.7" do | ||
describe "Array#deconstruct" do | ||
it "returns self" do | ||
array = [1] | ||
|
||
array.deconstruct.should equal array | ||
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,25 @@ | ||
require_relative '../../spec_helper' | ||
|
||
ruby_version_is "2.7" do | ||
describe "Hash#deconstruct_keys" do | ||
it "returns self" do | ||
hash = {a: 1, b: 2} | ||
|
||
hash.deconstruct_keys([:a, :b]).should equal hash | ||
end | ||
|
||
it "requires one argument" do | ||
-> { | ||
{a: 1}.deconstruct_keys | ||
}.should raise_error(ArgumentError, /wrong number of arguments \(given 0, expected 1\)/) | ||
end | ||
|
||
it "ignores argument" do | ||
hash = {a: 1, b: 2} | ||
|
||
hash.deconstruct_keys([:a]).should == {a: 1, b: 2} | ||
hash.deconstruct_keys(0 ).should == {a: 1, b: 2} | ||
hash.deconstruct_keys('' ).should == {a: 1, b: 2} | ||
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,50 @@ | ||
require_relative '../../spec_helper' | ||
|
||
ruby_version_is "2.7" do | ||
describe "Struct#deconstruct_keys" do | ||
it "returns a hash of attributes" do | ||
struct = Struct.new(:x, :y) | ||
s = struct.new(1, 2) | ||
|
||
s.deconstruct_keys([:x, :y]).should == {x: 1, y: 2} | ||
end | ||
|
||
it "requires one argument" do | ||
-> { | ||
Struct.new(:x).new(1).deconstruct_keys | ||
}.should raise_error(ArgumentError, /wrong number of arguments \(given 0, expected 1\)/) | ||
end | ||
|
||
it "returns only specified keys" do | ||
struct = Struct.new(:x, :y, :z) | ||
s = struct.new(1, 2, 3) | ||
|
||
s.deconstruct_keys([:x, :y]).should == {x: 1, y: 2} | ||
s.deconstruct_keys([:x] ).should == {x: 1} | ||
s.deconstruct_keys([] ).should == {} | ||
end | ||
|
||
it "accepts string attribute names" do | ||
struct = Struct.new(:x, :y) | ||
s = struct.new(1, 2) | ||
|
||
s.deconstruct_keys(['x', 'y']).should == {'x' => 1, 'y' => 2} | ||
end | ||
|
||
it "accepts argument position number as well but returns them as keys" do | ||
struct = Struct.new(:x, :y, :z) | ||
s = struct.new(10, 20, 30) | ||
|
||
s.deconstruct_keys([0, 1, 2]).should == {0 => 10, 1 => 20, 2 => 30} | ||
s.deconstruct_keys([0, 1] ).should == {0 => 10, 1 => 20} | ||
s.deconstruct_keys([0] ).should == {0 => 10} | ||
end | ||
|
||
it "ignores not existing attribute names" do | ||
struct = Struct.new(:x, :y) | ||
s = struct.new(1, 2) | ||
|
||
s.deconstruct_keys([:a, :b, :c]).should == {} | ||
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,12 @@ | ||
require_relative '../../spec_helper' | ||
|
||
ruby_version_is "2.7" do | ||
describe "Struct#deconstruct" do | ||
it "returns an array of attribute values" do | ||
struct = Struct.new(:x, :y) | ||
s = struct.new(1, 2) | ||
|
||
s.deconstruct.should == [1, 2] | ||
end | ||
end | ||
end |
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.
Do you know if this is intended behavior?
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.
@eregon
Looks like so - https://github.com/ruby/ruby/blob/master/hash.c#L4591-L4595
Keys passed as argument are just a hint to optimize performance.
Struct#deconstruct_keys
uses keys... Hash doesn't. At least for now. Hope it will be changed in the next release.