Skip to content

Commit af627d6

Browse files
Earlopainandrykonchin
authored andcommitted
[Feature #21781] Add ENV.fetch_values
Specs/Implementation inspired by methods on hash/env that already exist.
1 parent 3c45817 commit af627d6

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

core/env/fetch_values_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'fixtures/common'
3+
4+
ruby_version_is "4.1" do
5+
describe "ENV.fetch_values" do
6+
before :each do
7+
@saved_foo = ENV["foo"]
8+
@saved_bar = ENV["bar"]
9+
ENV.delete("foo")
10+
ENV.delete("bar")
11+
end
12+
13+
after :each do
14+
ENV["foo"] = @saved_foo
15+
ENV["bar"] = @saved_bar
16+
end
17+
18+
it "returns an array of the values corresponding to the given keys" do
19+
ENV["foo"] = "oof"
20+
ENV["bar"] = "rab"
21+
ENV.fetch_values("bar", "foo").should == ["rab", "oof"]
22+
end
23+
24+
it "returns the default value from block" do
25+
ENV["foo"] = "oof"
26+
ENV.fetch_values("bar") { |key| "`#{key}' is not found" }.should == ["`bar' is not found"]
27+
ENV.fetch_values("bar", "foo") { |key| "`#{key}' is not found" }.should == ["`bar' is not found", "oof"]
28+
end
29+
30+
it "returns an empty array if no keys specified" do
31+
ENV.fetch_values.should == []
32+
end
33+
34+
it "raises KeyError when there is no matching key" do
35+
ENV["foo"] = "oof"
36+
ENV["bar"] = "rab"
37+
-> {
38+
ENV.fetch_values("bar", "y", "foo", "z")
39+
}.should raise_error(KeyError, 'key not found: "y"')
40+
end
41+
42+
it "uses the locale encoding" do
43+
ENV.fetch_values(ENV.keys.first).first.encoding.should == ENVSpecs.encoding
44+
end
45+
46+
it "raises TypeError when a key is not coercible to String" do
47+
ENV["foo"] = "oof"
48+
-> { ENV.fetch_values("foo", Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)