Skip to content
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 6 commits into from
Jan 17, 2020
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
11 changes: 11 additions & 0 deletions core/array/deconstruct_spec.rb
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
25 changes: 25 additions & 0 deletions core/hash/deconstruct_keys_spec.rb
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}
Copy link
Member

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?

Copy link
Member Author

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.

end
end
end
50 changes: 50 additions & 0 deletions core/struct/deconstruct_keys_spec.rb
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
12 changes: 12 additions & 0 deletions core/struct/deconstruct_spec.rb
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
Loading