-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specs for
MachData#deconstruct_keys
and #deconstruct
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
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,52 @@ | ||
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 | ||
-> { | ||
/l/.match("l").deconstruct_keys | ||
}.should raise_error(ArgumentError) | ||
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) | ||
end | ||
|
||
it "ignores not existing Symbol keys" do | ||
m = /(?<f>foo)(?<b>bar)/.match("foobar") | ||
|
||
m.deconstruct_keys([:f, :a]).should == { f: "foo" } | ||
end | ||
end | ||
end |