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

Add specs for MachData#deconstruct_keys and #deconstruct #1030

Merged
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
13 changes: 2 additions & 11 deletions core/matchdata/captures_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/captures'

describe "MatchData#captures" do
it "returns an array of the match captures" do
/(.)(.)(\d+)(\d)/.match("THX1138.").captures.should == ["H","X","113","8"]
end

ruby_version_is "3.0" do
it "returns instances of String when given a String subclass" do
str = MatchDataSpecs::MyString.new("THX1138: The Movie")
/(.)(.)(\d+)(\d)/.match(str).captures.each { |c| c.should be_an_instance_of(String) }
end
end
it_behaves_like :matchdata_captures, :captures
end
65 changes: 65 additions & 0 deletions core/matchdata/deconstruct_keys_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require_relative '../../spec_helper'

describe "MatchData#deconstruct_keys" do
ruby_version_is "3.2" do
it "returns whole hash for nil as an argument" do
m = /(?<f>foo)(?<b>bar)/.match("foobar")

m.deconstruct_keys(nil).should == { f: "foo", b: "bar" }
end

it "returns only specified keys" do
m = /(?<f>foo)(?<b>bar)/.match("foobar")

m.deconstruct_keys([:f]).should == { f: "foo" }
end

it "requires one argument" do
m = /l/.match("l")

-> {
m.deconstruct_keys
}.should raise_error(ArgumentError, "wrong number of arguments (given 0, expected 1)")
end

it "it raises error when argument is neither nil nor array" do
m = /(?<f>foo)(?<b>bar)/.match("foobar")

-> { m.deconstruct_keys(1) }.should raise_error(TypeError, "wrong argument type Integer (expected Array)")
-> { m.deconstruct_keys("asd") }.should raise_error(TypeError, "wrong argument type String (expected Array)")
-> { m.deconstruct_keys(:x) }.should raise_error(TypeError, "wrong argument type Symbol (expected Array)")
-> { m.deconstruct_keys({}) }.should raise_error(TypeError, "wrong argument type Hash (expected Array)")
end

it "returns {} when passed []" do
m = /(?<f>foo)(?<b>bar)/.match("foobar")

m.deconstruct_keys([]).should == {}
end

it "does not accept non-Symbol keys" do
m = /(?<f>foo)(?<b>bar)/.match("foobar")

-> {
m.deconstruct_keys(['year', :foo])
}.should raise_error(TypeError, "wrong argument type String (expected Symbol)")
end

it "process keys till the first non-existing one" do
m = /(?<f>foo)(?<b>bar)(?<c>baz)/.match("foobarbaz")

m.deconstruct_keys([:f, :a, :b]).should == { f: "foo" }
end

it "returns {} when there are no named captured groups at all" do
m = /foo.+/.match("foobar")

m.deconstruct_keys(nil).should == {}
end

it "returns {} when passed more keys than named captured groups" do
m = /(?<f>foo)(?<b>bar)/.match("foobar")
m.deconstruct_keys([:f, :b, :c]).should == {}
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved
end
end
8 changes: 8 additions & 0 deletions core/matchdata/deconstruct_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative '../../spec_helper'
require_relative 'shared/captures'

describe "MatchData#deconstruct" do
ruby_version_is "3.2" do
it_behaves_like :matchdata_captures, :deconstruct
end
end
15 changes: 15 additions & 0 deletions core/matchdata/shared/captures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'

describe :matchdata_captures, shared: true do
it "returns an array of the match captures" do
/(.)(.)(\d+)(\d)/.match("THX1138.").send(@method).should == ["H","X","113","8"]
end

ruby_version_is "3.0" do
it "returns instances of String when given a String subclass" do
str = MatchDataSpecs::MyString.new("THX1138: The Movie")
/(.)(.)(\d+)(\d)/.match(str).send(@method).each { |c| c.should be_an_instance_of(String) }
end
end
end