aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/env
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/env')
-rw-r--r--spec/ruby/core/env/assoc_spec.rb10
-rw-r--r--spec/ruby/core/env/clear_spec.rb2
-rw-r--r--spec/ruby/core/env/delete_if_spec.rb37
-rw-r--r--spec/ruby/core/env/delete_spec.rb14
-rw-r--r--spec/ruby/core/env/each_key_spec.rb4
-rw-r--r--spec/ruby/core/env/each_value_spec.rb4
-rw-r--r--spec/ruby/core/env/element_reference_spec.rb11
-rw-r--r--spec/ruby/core/env/fetch_spec.rb32
-rw-r--r--spec/ruby/core/env/keep_if_spec.rb31
-rw-r--r--spec/ruby/core/env/rassoc_spec.rb21
-rw-r--r--spec/ruby/core/env/reject_spec.rb30
-rw-r--r--spec/ruby/core/env/shared/each.rb6
-rw-r--r--spec/ruby/core/env/shared/include.rb16
-rw-r--r--spec/ruby/core/env/shared/key.rb16
-rw-r--r--spec/ruby/core/env/shared/select.rb33
-rw-r--r--spec/ruby/core/env/shared/store.rb10
-rw-r--r--spec/ruby/core/env/shared/update.rb71
-rw-r--r--spec/ruby/core/env/shared/value.rb15
-rw-r--r--spec/ruby/core/env/slice_spec.rb29
19 files changed, 342 insertions, 50 deletions
diff --git a/spec/ruby/core/env/assoc_spec.rb b/spec/ruby/core/env/assoc_spec.rb
index 853eca79a5..9946e328a9 100644
--- a/spec/ruby/core/env/assoc_spec.rb
+++ b/spec/ruby/core/env/assoc_spec.rb
@@ -1,8 +1,12 @@
require_relative '../../spec_helper'
describe "ENV.assoc" do
+ before :each do
+ @foo = ENV["foo"]
+ end
+
after :each do
- ENV.delete("foo")
+ ENV["foo"] = @foo
end
it "returns an array of the key and value of the environment variable with the given key" do
@@ -20,4 +24,8 @@ describe "ENV.assoc" do
k.should_receive(:to_str).and_return("foo")
ENV.assoc(k).should == ["foo", "bar"]
end
+
+ it "raises TypeError if the argument is not a String and does not respond to #to_str" do
+ -> { ENV.assoc(Object.new) }.should raise_error(TypeError)
+ end
end
diff --git a/spec/ruby/core/env/clear_spec.rb b/spec/ruby/core/env/clear_spec.rb
index fd0984a220..48b034ba1d 100644
--- a/spec/ruby/core/env/clear_spec.rb
+++ b/spec/ruby/core/env/clear_spec.rb
@@ -4,7 +4,7 @@ describe "ENV.clear" do
it "deletes all environment variables" do
orig = ENV.to_hash
begin
- ENV.clear
+ ENV.clear.should equal(ENV)
# This used 'env' the helper before. That shells out to 'env' which
# itself sets up certain environment variables before it runs, because
diff --git a/spec/ruby/core/env/delete_if_spec.rb b/spec/ruby/core/env/delete_if_spec.rb
index d64443194e..d2de51c225 100644
--- a/spec/ruby/core/env/delete_if_spec.rb
+++ b/spec/ruby/core/env/delete_if_spec.rb
@@ -2,14 +2,31 @@ require_relative '../../spec_helper'
require_relative '../enumerable/shared/enumeratorized'
describe "ENV.delete_if" do
+ before :each do
+ @foo = ENV["foo"]
+ @bar = ENV["bar"]
+
+ ENV["foo"] = "0"
+ ENV["bar"] = "1"
+ end
+
+ after :each do
+ ENV["foo"] = @foo
+ ENV["bar"] = @bar
+ end
+
it "deletes pairs if the block returns true" do
- ENV["foo"] = "bar"
- ENV.delete_if { |k, v| k == "foo" }
+ ENV.delete_if { |k, v| ["foo", "bar"].include?(k) }
ENV["foo"].should == nil
+ ENV["bar"].should == nil
+ end
+
+ it "returns ENV when block given" do
+ ENV.delete_if { |k, v| ["foo", "bar"].include?(k) }.should equal(ENV)
end
it "returns ENV even if nothing deleted" do
- ENV.delete_if { false }.should_not == nil
+ ENV.delete_if { false }.should equal(ENV)
end
it "returns an Enumerator if no block given" do
@@ -17,10 +34,20 @@ describe "ENV.delete_if" do
end
it "deletes pairs through enumerator" do
- ENV["foo"] = "bar"
enum = ENV.delete_if
- enum.each { |k, v| k == "foo" }
+ enum.each { |k, v| ["foo", "bar"].include?(k) }
ENV["foo"].should == nil
+ ENV["bar"].should == nil
+ end
+
+ it "returns ENV from enumerator" do
+ enum = ENV.delete_if
+ enum.each { |k, v| ["foo", "bar"].include?(k) }.should equal(ENV)
+ end
+
+ it "returns ENV from enumerator even if nothing deleted" do
+ enum = ENV.delete_if
+ enum.each { false }.should equal(ENV)
end
it_behaves_like :enumeratorized_with_origin_size, :delete_if, ENV
diff --git a/spec/ruby/core/env/delete_spec.rb b/spec/ruby/core/env/delete_spec.rb
index 1e677fb252..e875df4aeb 100644
--- a/spec/ruby/core/env/delete_spec.rb
+++ b/spec/ruby/core/env/delete_spec.rb
@@ -1,8 +1,11 @@
require_relative '../../spec_helper'
describe "ENV.delete" do
+ before :each do
+ @saved_foo = ENV["foo"]
+ end
after :each do
- ENV.delete("foo")
+ ENV["foo"] = @saved_foo
end
it "removes the variable from the environment" do
@@ -16,9 +19,18 @@ describe "ENV.delete" do
ENV.delete("foo").should == "bar"
end
+ it "returns nil if the named environment variable does not exist and no block given" do
+ ENV.delete("foo")
+ ENV.delete("foo").should == nil
+ end
+
it "yields the name to the given block if the named environment variable does not exist" do
ENV.delete("foo")
ENV.delete("foo") { |name| ScratchPad.record name }
ScratchPad.recorded.should == "foo"
end
+
+ it "raises TypeError if the argument is not a String and does not respond to #to_str" do
+ -> { ENV.delete(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
+ end
end
diff --git a/spec/ruby/core/env/each_key_spec.rb b/spec/ruby/core/env/each_key_spec.rb
index 294bf39912..5c5cf4f80e 100644
--- a/spec/ruby/core/env/each_key_spec.rb
+++ b/spec/ruby/core/env/each_key_spec.rb
@@ -19,7 +19,9 @@ describe "ENV.each_key" do
end
it "returns an Enumerator if called without a block" do
- ENV.each_key.should be_an_instance_of(Enumerator)
+ enum = ENV.each_key
+ enum.should be_an_instance_of(Enumerator)
+ enum.to_a.should == ENV.keys
end
it "returns keys in the locale encoding" do
diff --git a/spec/ruby/core/env/each_value_spec.rb b/spec/ruby/core/env/each_value_spec.rb
index 88f4bc49da..ea29b3a0d7 100644
--- a/spec/ruby/core/env/each_value_spec.rb
+++ b/spec/ruby/core/env/each_value_spec.rb
@@ -19,7 +19,9 @@ describe "ENV.each_value" do
end
it "returns an Enumerator if called without a block" do
- ENV.each_value.should be_an_instance_of(Enumerator)
+ enum = ENV.each_value
+ enum.should be_an_instance_of(Enumerator)
+ enum.to_a.should == ENV.values
end
it "uses the locale encoding" do
diff --git a/spec/ruby/core/env/element_reference_spec.rb b/spec/ruby/core/env/element_reference_spec.rb
index 59b53fc4b1..7d2a2d78e3 100644
--- a/spec/ruby/core/env/element_reference_spec.rb
+++ b/spec/ruby/core/env/element_reference_spec.rb
@@ -19,6 +19,17 @@ describe "ENV.[]" do
ENV[@variable].frozen?.should == true
end
+ it "coerces a non-string name with #to_str" do
+ ENV[@variable] = "bar"
+ k = mock('key')
+ k.should_receive(:to_str).and_return(@variable)
+ ENV[k].should == "bar"
+ end
+
+ it "raises TypeError if the argument is not a String and does not respond to #to_str" do
+ -> { ENV[Object.new] }.should raise_error(TypeError, "no implicit conversion of Object into String")
+ end
+
platform_is :windows do
it "looks up values case-insensitively" do
ENV[@variable] = "bar"
diff --git a/spec/ruby/core/env/fetch_spec.rb b/spec/ruby/core/env/fetch_spec.rb
index eeaf290cf0..ef8f0a4ed3 100644
--- a/spec/ruby/core/env/fetch_spec.rb
+++ b/spec/ruby/core/env/fetch_spec.rb
@@ -2,14 +2,20 @@ require_relative '../../spec_helper'
require_relative '../../shared/hash/key_error'
describe "ENV.fetch" do
+ before :each do
+ @foo_saved = ENV.delete("foo")
+ end
+ after :each do
+ ENV["foo"] = @saved_foo
+ end
+
it "returns a value" do
ENV["foo"] = "bar"
ENV.fetch("foo").should == "bar"
- ENV.delete "foo"
end
it "raises a TypeError if the key is not a String" do
- -> { ENV.fetch :should_never_be_set }.should raise_error(TypeError)
+ -> { ENV.fetch Object.new }.should raise_error(TypeError, "no implicit conversion of Object into String")
end
context "when the key is not found" do
@@ -23,20 +29,34 @@ describe "ENV.fetch" do
end
it "provides the given default parameter" do
- ENV.fetch("should_never_be_set", "default").should == "default"
+ ENV.fetch("foo", "default").should == "default"
+ end
+
+ it "does not insist that the default be a String" do
+ ENV.fetch("foo", :default).should == :default
end
it "provides a default value from a block" do
- ENV.fetch("should_never_be_set") { |k| "wanted #{k}" }.should == "wanted should_never_be_set"
+ ENV.fetch("foo") { |k| "wanted #{k}" }.should == "wanted foo"
+ end
+
+ it "does not insist that the block return a String" do
+ ENV.fetch("foo") { |k| k.to_sym }.should == :foo
end
it "warns on block and default parameter given" do
-> do
- ENV.fetch("should_never_be_set", "default") { 1 }.should == 1
+ ENV.fetch("foo", "default") { "bar" }.should == "bar"
end.should complain(/block supersedes default value argument/)
end
+ it "does not evaluate the block when key found" do
+ ENV["foo"] = "bar"
+ ENV.fetch("foo") { fail "should not get here"}.should == "bar"
+ end
+
it "uses the locale encoding" do
- ENV.fetch(ENV.keys.first).encoding.should == Encoding.find('locale')
+ ENV["foo"] = "bar"
+ ENV.fetch("foo").encoding.should == Encoding.find('locale')
end
end
diff --git a/spec/ruby/core/env/keep_if_spec.rb b/spec/ruby/core/env/keep_if_spec.rb
index cf8e27936e..64b6a207d0 100644
--- a/spec/ruby/core/env/keep_if_spec.rb
+++ b/spec/ruby/core/env/keep_if_spec.rb
@@ -3,20 +3,30 @@ require_relative '../enumerable/shared/enumeratorized'
describe "ENV.keep_if" do
before :each do
- ENV["foo"] = "bar"
+ @foo = ENV["foo"]
+ @bar = ENV["bar"]
+
+ ENV["foo"] = "0"
+ ENV["bar"] = "1"
end
after :each do
- ENV.delete "foo"
+ ENV["foo"] = @foo
+ ENV["bar"] = @bar
end
it "deletes pairs if the block returns false" do
- ENV.keep_if { |k, v| k != "foo" }
+ ENV.keep_if { |k, v| !["foo", "bar"].include?(k) }
ENV["foo"].should == nil
+ ENV["bar"].should == nil
+ end
+
+ it "returns ENV when block given" do
+ ENV.keep_if { |k, v| !["foo", "bar"].include?(k) }.should equal(ENV)
end
it "returns ENV even if nothing deleted" do
- ENV.keep_if { true }.should_not == nil
+ ENV.keep_if { true }.should equal(ENV)
end
it "returns an Enumerator if no block given" do
@@ -25,8 +35,19 @@ describe "ENV.keep_if" do
it "deletes pairs through enumerator" do
enum = ENV.keep_if
- enum.each { |k, v| k != "foo" }
+ enum.each { |k, v| !["foo", "bar"].include?(k) }
ENV["foo"].should == nil
+ ENV["bar"].should == nil
+ end
+
+ it "returns ENV from enumerator" do
+ enum = ENV.keep_if
+ enum.each { |k, v| !["foo", "bar"].include?(k) }.should equal(ENV)
+ end
+
+ it "returns ENV from enumerator even if nothing deleted" do
+ enum = ENV.keep_if
+ enum.each { true }.should equal(ENV)
end
it_behaves_like :enumeratorized_with_origin_size, :keep_if, ENV
diff --git a/spec/ruby/core/env/rassoc_spec.rb b/spec/ruby/core/env/rassoc_spec.rb
index 4b839c15c7..ab9fe68088 100644
--- a/spec/ruby/core/env/rassoc_spec.rb
+++ b/spec/ruby/core/env/rassoc_spec.rb
@@ -1,8 +1,14 @@
require_relative '../../spec_helper'
describe "ENV.rassoc" do
+ before :each do
+ @foo = ENV["foo"]
+ @baz = ENV["baz"]
+ end
+
after :each do
- ENV.delete("foo")
+ ENV["foo"] = @foo
+ ENV["baz"] = @baz
end
it "returns an array of the key and value of the environment variable with the given value" do
@@ -10,6 +16,15 @@ describe "ENV.rassoc" do
ENV.rassoc("bar").should == ["foo", "bar"]
end
+ it "returns a single array even if there are multiple such environment variables" do
+ ENV["foo"] = "bar"
+ ENV["baz"] = "bar"
+ [
+ ["foo", "bar"],
+ ["baz", "bar"],
+ ].should include(ENV.rassoc("bar"))
+ end
+
it "returns nil if no environment variable with the given value exists" do
ENV.rassoc("bar").should == nil
end
@@ -20,4 +35,8 @@ describe "ENV.rassoc" do
v.should_receive(:to_str).and_return("bar")
ENV.rassoc(v).should == ["foo", "bar"]
end
+
+ it "returns nil if the argument is not a String and does not respond to #to_str" do
+ ENV.rassoc(Object.new).should == nil
+ end
end
diff --git a/spec/ruby/core/env/reject_spec.rb b/spec/ruby/core/env/reject_spec.rb
index 1026c2f451..6a9794925d 100644
--- a/spec/ruby/core/env/reject_spec.rb
+++ b/spec/ruby/core/env/reject_spec.rb
@@ -2,6 +2,14 @@ require_relative '../../spec_helper'
require_relative '../enumerable/shared/enumeratorized'
describe "ENV.reject!" do
+ before :each do
+ @foo = ENV["foo"]
+ end
+
+ after :each do
+ ENV["foo"] = @foo
+ end
+
it "rejects entries based on key" do
ENV["foo"] = "bar"
ENV.reject! { |k, v| k == "foo" }
@@ -17,12 +25,16 @@ describe "ENV.reject!" do
it "returns itself or nil" do
ENV.reject! { false }.should == nil
ENV["foo"] = "bar"
- ENV.reject! { |k, v| k == "foo" }.should == ENV
+ ENV.reject! { |k, v| k == "foo" }.should equal(ENV)
ENV["foo"].should == nil
end
it "returns an Enumerator if called without a block" do
- ENV.reject!.should be_an_instance_of(Enumerator)
+ ENV["foo"] = "bar"
+ enum = ENV.reject!
+ enum.should be_an_instance_of(Enumerator)
+ enum.each { |k, v| k == "foo" }.should equal(ENV)
+ ENV["foo"].should == nil
end
it "doesn't raise if empty" do
@@ -39,6 +51,14 @@ describe "ENV.reject!" do
end
describe "ENV.reject" do
+ before :each do
+ @foo = ENV["foo"]
+ end
+
+ after :each do
+ ENV["foo"] = @foo
+ end
+
it "rejects entries based on key" do
ENV["foo"] = "bar"
e = ENV.reject { |k, v| k == "foo" }
@@ -60,7 +80,11 @@ describe "ENV.reject" do
end
it "returns an Enumerator if called without a block" do
- ENV.reject.should be_an_instance_of(Enumerator)
+ ENV["foo"] = "bar"
+ enum = ENV.reject
+ enum.should be_an_instance_of(Enumerator)
+ enum.each { |k, v| k == "foo"}
+ ENV["foo"] = nil
end
it "doesn't raise if empty" do
diff --git a/spec/ruby/core/env/shared/each.rb b/spec/ruby/core/env/shared/each.rb
index 261ad3a2a6..eb375046d6 100644
--- a/spec/ruby/core/env/shared/each.rb
+++ b/spec/ruby/core/env/shared/each.rb
@@ -17,7 +17,11 @@ describe :env_each, shared: true do
end
it "returns an Enumerator if called without a block" do
- ENV.send(@method).should be_an_instance_of(Enumerator)
+ enum = ENV.send(@method)
+ enum.should be_an_instance_of(Enumerator)
+ enum.each do |name, value|
+ ENV[name].should == value
+ end
end
before :all do
diff --git a/spec/ruby/core/env/shared/include.rb b/spec/ruby/core/env/shared/include.rb
index 8d8311dcf2..3efcd523d6 100644
--- a/spec/ruby/core/env/shared/include.rb
+++ b/spec/ruby/core/env/shared/include.rb
@@ -1,11 +1,23 @@
describe :env_include, shared: true do
+ before :each do
+ @saved_foo = ENV["foo"]
+ end
+
+ after :each do
+ ENV["foo"] = @saved_foo
+ end
+
it "returns true if ENV has the key" do
ENV["foo"] = "bar"
ENV.send(@method, "foo").should == true
- ENV.delete "foo"
end
it "returns false if ENV doesn't include the key" do
- ENV.send(@method, "should_never_be_set").should == false
+ ENV.delete("foo")
+ ENV.send(@method, "foo").should == false
+ end
+
+ it "raises TypeError if the argument is not a String and does not respond to #to_str" do
+ -> { ENV.send(@method, Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
end
end
diff --git a/spec/ruby/core/env/shared/key.rb b/spec/ruby/core/env/shared/key.rb
index 43299beadb..fcb3a9b8c5 100644
--- a/spec/ruby/core/env/shared/key.rb
+++ b/spec/ruby/core/env/shared/key.rb
@@ -1,11 +1,23 @@
describe :env_key, shared: true do
+ before :each do
+ @saved_foo = ENV["foo"]
+ end
+
+ after :each do
+ ENV["foo"] = @saved_foo
+ end
+
it "returns the index associated with the passed value" do
ENV["foo"] = "bar"
ENV.send(@method, "bar").should == "foo"
- ENV.delete "foo"
end
it "returns nil if the passed value is not found" do
- ENV.send(@method, "should_never_be_set").should be_nil
+ ENV.delete("foo")
+ ENV.send(@method, "foo").should be_nil
+ end
+
+ it "raises TypeError if the argument is not a String and does not respond to #to_str" do
+ -> { ENV.send(@method, Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
end
end
diff --git a/spec/ruby/core/env/shared/select.rb b/spec/ruby/core/env/shared/select.rb
index a0b46a775a..75ba112a32 100644
--- a/spec/ruby/core/env/shared/select.rb
+++ b/spec/ruby/core/env/shared/select.rb
@@ -1,16 +1,38 @@
describe :env_select, shared: true do
+ before :each do
+ @saved_foo = ENV["foo"]
+ end
+
+ after :each do
+ ENV["foo"] = @saved_foo
+ end
+
it "returns a Hash of names and values for which block return true" do
ENV["foo"] = "bar"
(ENV.send(@method) { |k, v| k == "foo" }).should == { "foo" => "bar" }
- ENV.delete "foo"
end
it "returns an Enumerator when no block is given" do
- ENV.send(@method).should be_an_instance_of(Enumerator)
+ enum = ENV.send(@method)
+ enum.should be_an_instance_of(Enumerator)
+ end
+
+ it "selects via the enumerator" do
+ enum = ENV.send(@method)
+ ENV["foo"] = "bar"
+ enum.each { |k, v| k == "foo" }.should == { "foo" => "bar"}
end
end
describe :env_select!, shared: true do
+ before :each do
+ @saved_foo = ENV["foo"]
+ end
+
+ after :each do
+ ENV["foo"] = @saved_foo
+ end
+
it "removes environment variables for which the block returns true" do
ENV["foo"] = "bar"
ENV.send(@method) { |k, v| k != "foo" }
@@ -29,4 +51,11 @@ describe :env_select!, shared: true do
it "returns an Enumerator if called without a block" do
ENV.send(@method).should be_an_instance_of(Enumerator)
end
+
+ it "selects via the enumerator" do
+ enum = ENV.send(@method)
+ ENV["foo"] = "bar"
+ enum.each { |k, v| k != "foo" }
+ ENV["foo"].should == nil
+ end
end
diff --git a/spec/ruby/core/env/shared/store.rb b/spec/ruby/core/env/shared/store.rb
index 6ae91ef8fc..d6265c66a5 100644
--- a/spec/ruby/core/env/shared/store.rb
+++ b/spec/ruby/core/env/shared/store.rb
@@ -1,6 +1,10 @@
describe :env_store, shared: true do
+ before :each do
+ @saved_foo = ENV["foo"]
+ end
+
after :each do
- ENV.delete("foo")
+ ENV["foo"] = @saved_foo
end
it "sets the environment variable to the given value" do
@@ -34,11 +38,11 @@ describe :env_store, shared: true do
end
it "raises TypeError when the key is not coercible to String" do
- -> { ENV.send(@method, Object.new, "bar") }.should raise_error(TypeError)
+ -> { ENV.send(@method, Object.new, "bar") }.should raise_error(TypeError, "no implicit conversion of Object into String")
end
it "raises TypeError when the value is not coercible to String" do
- -> { ENV.send(@method, "foo", Object.new) }.should raise_error(TypeError)
+ -> { ENV.send(@method, "foo", Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
end
it "raises Errno::EINVAL when the key contains the '=' character" do
diff --git a/spec/ruby/core/env/shared/update.rb b/spec/ruby/core/env/shared/update.rb
index cd09877243..430cda5185 100644
--- a/spec/ruby/core/env/shared/update.rb
+++ b/spec/ruby/core/env/shared/update.rb
@@ -1,21 +1,66 @@
describe :env_update, shared: true do
+ before :each do
+ @saved_foo = ENV["foo"]
+ @saved_bar = ENV["bar"]
+ end
+
+ after :each do
+ ENV["foo"] = @saved_foo
+ ENV["bar"] = @saved_bar
+ end
+
it "adds the parameter hash to ENV" do
- ENV["foo"].should == nil
- ENV.send @method, "foo" => "bar"
- ENV["foo"].should == "bar"
- ENV.delete "foo"
+ ENV.send @method, {"foo" => "0", "bar" => "1"}
+ ENV["foo"].should == "0"
+ ENV["bar"].should == "1"
+ end
+
+ it "returns ENV when no block given" do
+ ENV.send(@method, {"foo" => "0", "bar" => "1"}).should equal(ENV)
end
it "yields key, the old value and the new value when replacing entries" do
- ENV.send @method, "foo" => "bar"
- ENV["foo"].should == "bar"
- ENV.send(@method, "foo" => "boo") do |key, old, new|
- key.should == "foo"
- old.should == "bar"
- new.should == "boo"
- "rab"
+ ENV.send @method, {"foo" => "0", "bar" => "3"}
+ a = []
+ ENV.send @method, {"foo" => "1", "bar" => "4"} do |key, old, new|
+ a << [key, old, new]
+ (new.to_i + 1).to_s
+ end
+ ENV["foo"].should == "2"
+ ENV["bar"].should == "5"
+ a[0].should == ["foo", "0", "1"]
+ a[1].should == ["bar", "3", "4"]
+ end
+
+ it "returns ENV when block given" do
+ ENV.send(@method, {"foo" => "0", "bar" => "1"}){}.should equal(ENV)
+ end
+
+ it "raises TypeError when a name is not coercible to String" do
+ -> { ENV.send @method, Object.new => "0" }.should raise_error(TypeError, "no implicit conversion of Object into String")
+ end
+
+ it "raises TypeError when a value is not coercible to String" do
+ -> { ENV.send @method, "foo" => Object.new }.should raise_error(TypeError, "no implicit conversion of Object into String")
+ end
+
+ it "updates good data preceding an error" do
+ ENV["foo"] = "0"
+ begin
+ ENV.send @method, {"foo" => "2", Object.new => "1"}
+ rescue TypeError
+ ensure
+ ENV["foo"].should == "2"
+ end
+ end
+
+ it "does not update good data following an error" do
+ ENV["foo"] = "0"
+ begin
+ ENV.send @method, {Object.new => "1", "foo" => "2"}
+ rescue TypeError
+ ensure
+ ENV["foo"].should == "0"
end
- ENV["foo"].should == "rab"
- ENV.delete "foo"
end
end
diff --git a/spec/ruby/core/env/shared/value.rb b/spec/ruby/core/env/shared/value.rb
index d9ee90f12d..bef96b5fef 100644
--- a/spec/ruby/core/env/shared/value.rb
+++ b/spec/ruby/core/env/shared/value.rb
@@ -1,11 +1,22 @@
describe :env_value, shared: true do
+ before :each do
+ @saved_foo = ENV["foo"]
+ end
+
+ after :each do
+ ENV["foo"] = @saved_foo
+ end
+
it "returns true if ENV has the value" do
ENV["foo"] = "bar"
ENV.send(@method, "bar").should == true
- ENV["foo"] = nil
end
it "returns false if ENV doesn't have the value" do
- ENV.send(@method, "this_value_should_never_exist").should == false
+ ENV.send(@method, "foo").should == false
+ end
+
+ it "returns nil if the argument is not a String and does not respond to #to_str" do
+ ENV.send(@method, Object.new).should == nil
end
end
diff --git a/spec/ruby/core/env/slice_spec.rb b/spec/ruby/core/env/slice_spec.rb
new file mode 100644
index 0000000000..1ac8a303f8
--- /dev/null
+++ b/spec/ruby/core/env/slice_spec.rb
@@ -0,0 +1,29 @@
+require_relative '../../spec_helper'
+
+ruby_version_is "2.6" do
+ describe "ENV.slice" do
+ before :each do
+ @saved_foo = ENV["foo"]
+ @saved_bar = ENV["bar"]
+ ENV["foo"] = "0"
+ ENV["bar"] = "1"
+ end
+
+ after :each do
+ ENV["foo"] = @saved_foo
+ ENV["bar"] = @saved_bar
+ end
+
+ it "returns a hash of the given environment variable names and their values" do
+ ENV.slice("foo", "bar").should == {"foo" => "0", "bar" => "1"}
+ end
+
+ it "ignores each String that is not an environment variable name" do
+ ENV.slice("foo", "boo", "bar").should == {"foo" => "0", "bar" => "1"}
+ end
+
+ it "raises TypeError if any argument is not a String and does not respond to #to_str" do
+ -> { ENV.slice(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
+ end
+ end
+end