diff options
Diffstat (limited to 'spec/ruby/core/env')
-rw-r--r-- | spec/ruby/core/env/assoc_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/env/delete_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/env/each_key_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/env/each_value_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/env/index_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/env/keys_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/env/rehash_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/env/replace_spec.rb | 50 | ||||
-rw-r--r-- | spec/ruby/core/env/shared/to_hash.rb | 13 | ||||
-rw-r--r-- | spec/ruby/core/env/shared/update.rb | 44 | ||||
-rw-r--r-- | spec/ruby/core/env/to_a_spec.rb | 5 | ||||
-rw-r--r-- | spec/ruby/core/env/to_h_spec.rb | 5 | ||||
-rw-r--r-- | spec/ruby/core/env/values_at_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/env/values_spec.rb | 9 |
14 files changed, 149 insertions, 33 deletions
diff --git a/spec/ruby/core/env/assoc_spec.rb b/spec/ruby/core/env/assoc_spec.rb index 9946e328a9..c7a388db75 100644 --- a/spec/ruby/core/env/assoc_spec.rb +++ b/spec/ruby/core/env/assoc_spec.rb @@ -26,6 +26,6 @@ describe "ENV.assoc" do 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) + -> { ENV.assoc(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") end end diff --git a/spec/ruby/core/env/delete_spec.rb b/spec/ruby/core/env/delete_spec.rb index e875df4aeb..b7fe1ee675 100644 --- a/spec/ruby/core/env/delete_spec.rb +++ b/spec/ruby/core/env/delete_spec.rb @@ -30,6 +30,12 @@ describe "ENV.delete" do ScratchPad.recorded.should == "foo" end + it "does not evaluate the block if the envirionment variable exists" do + ENV["foo"] = "bar" + ENV.delete("foo") { |name| fail "Should not happen" } + ENV["foo"].should == nil + 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 diff --git a/spec/ruby/core/env/each_key_spec.rb b/spec/ruby/core/env/each_key_spec.rb index 5c5cf4f80e..0efcb09900 100644 --- a/spec/ruby/core/env/each_key_spec.rb +++ b/spec/ruby/core/env/each_key_spec.rb @@ -10,7 +10,7 @@ describe "ENV.each_key" do ENV.clear ENV["1"] = "3" ENV["2"] = "4" - ENV.each_key { |k| e << k } + ENV.each_key { |k| e << k }.should equal(ENV) e.should include("1") e.should include("2") ensure diff --git a/spec/ruby/core/env/each_value_spec.rb b/spec/ruby/core/env/each_value_spec.rb index ea29b3a0d7..60d9f605d8 100644 --- a/spec/ruby/core/env/each_value_spec.rb +++ b/spec/ruby/core/env/each_value_spec.rb @@ -10,7 +10,7 @@ describe "ENV.each_value" do ENV.clear ENV["1"] = "3" ENV["2"] = "4" - ENV.each_value { |v| e << v } + ENV.each_value { |v| e << v }.should equal(ENV) e.should include("3") e.should include("4") ensure diff --git a/spec/ruby/core/env/index_spec.rb b/spec/ruby/core/env/index_spec.rb index 04986a0421..43875f5a50 100644 --- a/spec/ruby/core/env/index_spec.rb +++ b/spec/ruby/core/env/index_spec.rb @@ -3,4 +3,10 @@ require_relative 'shared/key' describe "ENV.index" do it_behaves_like :env_key, :index + + it "warns about deprecation" do + -> do + ENV.index("foo") + end.should complain(/warning: ENV.index is deprecated; use ENV.key/) + end end diff --git a/spec/ruby/core/env/keys_spec.rb b/spec/ruby/core/env/keys_spec.rb index 3699b2c225..b074a8f7c7 100644 --- a/spec/ruby/core/env/keys_spec.rb +++ b/spec/ruby/core/env/keys_spec.rb @@ -2,8 +2,8 @@ require_relative '../../spec_helper' describe "ENV.keys" do - it "returns all the keys" do - ENV.keys.sort.should == ENV.to_hash.keys.sort + it "returns an array of the keys" do + ENV.keys.should == ENV.to_hash.keys end it "returns the keys in the locale encoding" do diff --git a/spec/ruby/core/env/rehash_spec.rb b/spec/ruby/core/env/rehash_spec.rb index e724feaa39..3782e4b727 100644 --- a/spec/ruby/core/env/rehash_spec.rb +++ b/spec/ruby/core/env/rehash_spec.rb @@ -1 +1,7 @@ require_relative '../../spec_helper' + +describe "ENV.rehash" do + it "returns nil" do + ENV.rehash.should == nil + end +end diff --git a/spec/ruby/core/env/replace_spec.rb b/spec/ruby/core/env/replace_spec.rb index 8837deea4a..9fc67643d1 100644 --- a/spec/ruby/core/env/replace_spec.rb +++ b/spec/ruby/core/env/replace_spec.rb @@ -1,15 +1,51 @@ require_relative '../../spec_helper' describe "ENV.replace" do + before :each do + @orig = ENV.to_hash + ENV.delete("foo") + end + + after :each do + ENV.replace(@orig) + end it "replaces ENV with a Hash" do - ENV["foo"] = "bar" - e = ENV.reject { |k, v| k == "foo" } - e["baz"] = "bam" - ENV.replace e - ENV["foo"].should == nil - ENV["baz"].should == "bam" - ENV.delete "baz" + ENV.replace("foo" => "0", "bar" => "1").should equal(ENV) + ENV.size.should == 2 + ENV["foo"].should == "0" + ENV["bar"].should == "1" + end + + it "raises TypeError if the argument is not a Hash" do + -> { ENV.replace(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into Hash") + ENV.to_hash.should == @orig + end + + it "raises TypeError if a key is not a String" do + -> { ENV.replace(Object.new => "0") }.should raise_error(TypeError, "no implicit conversion of Object into String") + ENV.to_hash.should == @orig + end + + it "raises TypeError if a value is not a String" do + -> { ENV.replace("foo" => Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") + ENV.to_hash.should == @orig + end + + it "raises Errno::EINVAL when the key contains the '=' character" do + -> { ENV.replace("foo=" =>"bar") }.should raise_error(Errno::EINVAL) + end + + it "raises Errno::EINVAL when the key is an empty string" do + -> { ENV.replace("" => "bar") }.should raise_error(Errno::EINVAL) + end + + it "does not accept good data preceding an error" do + -> { ENV.replace("foo" => "1", Object.new => Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") end + it "does not accept good data following an error" do + -> { ENV.replace(Object.new => Object.new, "foo" => "0") }.should raise_error(TypeError, "no implicit conversion of Object into String") + ENV.to_hash.should == @orig + end end diff --git a/spec/ruby/core/env/shared/to_hash.rb b/spec/ruby/core/env/shared/to_hash.rb index 254054c14d..bfa5699e8d 100644 --- a/spec/ruby/core/env/shared/to_hash.rb +++ b/spec/ruby/core/env/shared/to_hash.rb @@ -1,10 +1,17 @@ describe :env_to_hash, shared: true do + before :each do + @saved_foo = ENV["foo"] + end + + after :each do + ENV["foo"]= @saved_foo + end + it "returns the ENV as a hash" do ENV["foo"] = "bar" h = ENV.send(@method) h.should be_an_instance_of(Hash) h["foo"].should == "bar" - ENV.delete "foo" end it "uses the locale encoding for keys" do @@ -18,5 +25,9 @@ describe :env_to_hash, shared: true do it "duplicates the ENV when converting to a Hash" do h = ENV.send(@method) h.should_not equal ENV + h.size.should == ENV.size + h.each_pair do |k, v| + ENV[k].should == v + end end end diff --git a/spec/ruby/core/env/shared/update.rb b/spec/ruby/core/env/shared/update.rb index 430cda5185..129a56544c 100644 --- a/spec/ruby/core/env/shared/update.rb +++ b/spec/ruby/core/env/shared/update.rb @@ -9,8 +9,8 @@ describe :env_update, shared: true do ENV["bar"] = @saved_bar end - it "adds the parameter hash to ENV" do - ENV.send @method, {"foo" => "0", "bar" => "1"} + it "adds the parameter hash to ENV, returning ENV" do + ENV.send(@method, "foo" => "0", "bar" => "1").should equal(ENV) ENV["foo"].should == "0" ENV["bar"].should == "1" end @@ -19,17 +19,43 @@ describe :env_update, shared: true 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 + it "yields key, the old value and the new value when replacing an entry" do ENV.send @method, {"foo" => "0", "bar" => "3"} a = [] ENV.send @method, {"foo" => "1", "bar" => "4"} do |key, old, new| a << [key, old, new] + new + end + a[0].should == ["foo", "0", "1"] + a[1].should == ["bar", "3", "4"] + end + + it "yields key, the old value and the new value when replacing an entry" do + ENV.send @method, {"foo" => "0", "bar" => "3"} + ENV.send @method, {"foo" => "1", "bar" => "4"} do |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 + + ruby_version_is "2.7" do + # BUG: https://bugs.ruby-lang.org/issues/16192 + it "does not evaluate the block when the name is new" do + ENV.delete("bar") + ENV.send @method, {"foo" => "0"} + ENV.send(@method, "bar" => "1") { |key, old, new| fail "Should not get here" } + ENV["bar"].should == "1" + end + + # BUG: https://bugs.ruby-lang.org/issues/16192 + it "does not use the block's return value as the value when the name is new" do + ENV.delete("bar") + ENV.send @method, {"foo" => "0"} + ENV.send(@method, "bar" => "1") { |key, old, new| "Should not use this value" } + ENV["foo"].should == "0" + ENV["bar"].should == "1" + end end it "returns ENV when block given" do @@ -44,6 +70,14 @@ describe :env_update, shared: true do -> { ENV.send @method, "foo" => Object.new }.should raise_error(TypeError, "no implicit conversion of Object into String") end + it "raises Errno::EINVAL when a name contains the '=' character" do + -> { ENV.send(@method, "foo=" => "bar") }.should raise_error(Errno::EINVAL) + end + + it "raises Errno::EINVAL when a name is an empty string" do + -> { ENV.send(@method, "" => "bar") }.should raise_error(Errno::EINVAL) + end + it "updates good data preceding an error" do ENV["foo"] = "0" begin diff --git a/spec/ruby/core/env/to_a_spec.rb b/spec/ruby/core/env/to_a_spec.rb index ed290a48a5..25441eb029 100644 --- a/spec/ruby/core/env/to_a_spec.rb +++ b/spec/ruby/core/env/to_a_spec.rb @@ -3,11 +3,10 @@ require_relative '../../spec_helper' describe "ENV.to_a" do it "returns the ENV as an array" do - ENV["foo"] = "bar" a = ENV.to_a a.is_a?(Array).should == true - a.find { |e| e.first == "foo" }.should == ["foo", "bar"] - ENV.delete "foo" + a.size.should == ENV.size + ENV.each_pair { |k, v| a.should include([k, v])} end it "returns the entries in the locale encoding" do diff --git a/spec/ruby/core/env/to_h_spec.rb b/spec/ruby/core/env/to_h_spec.rb index 81a17700e9..822167aa2b 100644 --- a/spec/ruby/core/env/to_h_spec.rb +++ b/spec/ruby/core/env/to_h_spec.rb @@ -19,6 +19,11 @@ describe "ENV.to_h" do ENV.to_h { |k, v| [k, v.upcase] }.should == { 'a' => "B", 'c' => "D" } end + it "does not require the array elements to be strings" do + ENV.replace("a" => "b", "c" => "d") + ENV.to_h { |k, v| [k.to_sym, v.to_sym] }.should == { :a => :b, :c => :d } + end + it "raises ArgumentError if block returns longer or shorter array" do -> do ENV.to_h { |k, v| [k, v.upcase, 1] } diff --git a/spec/ruby/core/env/values_at_spec.rb b/spec/ruby/core/env/values_at_spec.rb index 906d8b01f2..ee970e5f65 100644 --- a/spec/ruby/core/env/values_at_spec.rb +++ b/spec/ruby/core/env/values_at_spec.rb @@ -1,17 +1,37 @@ require_relative '../../spec_helper' describe "ENV.values_at" 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 "returns an array of the values referenced by the parameters as keys" do + it "returns an array of the values corresponding to the given keys" do ENV["foo"] = "oof" ENV["bar"] = "rab" - ENV.values_at.should == [] ENV.values_at("bar", "foo").should == ["rab", "oof"] - ENV.delete "foo" - ENV.delete "bar" + end + + it "returns an empty array if no keys specified" do + ENV.values_at.should == [] + end + + it "returns nil for each key that is not a name" do + ENV["foo"] = "oof" + ENV["bar"] = "rab" + ENV.values_at("x", "bar", "y", "foo", "z").should == [nil, "rab", nil, "oof", nil] end it "uses the locale encoding" do ENV.values_at(ENV.keys.first).first.encoding.should == Encoding.find('locale') end + + it "raises TypeError when a key is not coercible to String" do + -> { ENV.values_at("foo", Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") + end end diff --git a/spec/ruby/core/env/values_spec.rb b/spec/ruby/core/env/values_spec.rb index 2e4e69c23b..919d7ffb7c 100644 --- a/spec/ruby/core/env/values_spec.rb +++ b/spec/ruby/core/env/values_spec.rb @@ -3,14 +3,7 @@ require_relative '../../spec_helper' describe "ENV.values" do it "returns an array of the values" do - orig = ENV.to_hash - begin - ENV.replace "a" => "b", "c" => "d" - a = ENV.values - a.sort.should == ["b", "d"] - ensure - ENV.replace orig - end + ENV.values.should == ENV.to_hash.values end it "uses the locale encoding" do |