diff options
Diffstat (limited to 'spec/ruby/core/env')
46 files changed, 756 insertions, 219 deletions
diff --git a/spec/ruby/core/env/assoc_spec.rb b/spec/ruby/core/env/assoc_spec.rb index 853eca79a5..c7a388db75 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, "no implicit conversion of Object into String") + 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/clone_spec.rb b/spec/ruby/core/env/clone_spec.rb new file mode 100644 index 0000000000..01a29c6ab4 --- /dev/null +++ b/spec/ruby/core/env/clone_spec.rb @@ -0,0 +1,21 @@ +require_relative '../../spec_helper' + +describe "ENV#clone" do + it "raises ArgumentError when keyword argument 'freeze' is neither nil nor boolean" do + -> { + ENV.clone(freeze: 1) + }.should raise_error(ArgumentError) + end + + it "raises ArgumentError when keyword argument is not 'freeze'" do + -> { + ENV.clone(foo: nil) + }.should raise_error(ArgumentError) + end + + it "raises TypeError" do + -> { + ENV.clone + }.should raise_error(TypeError, /Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash/) + end +end 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..f28ac97911 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,37 @@ 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 "returns the result of given block if the named environment variable does not exist" do + ENV.delete("foo") + ENV.delete("foo") { |name| "bar" }.should == "bar" + end + + it "does not evaluate the block if the environment variable exists" do + ENV["foo"] = "bar" + ENV.delete("foo") { |name| fail "Should not happen" } + ENV["foo"].should == nil + end + + it "removes the variable coerced with #to_str" do + ENV["foo"] = "bar" + k = mock('key') + k.should_receive(:to_str).and_return("foo") + ENV.delete(k) + 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 end diff --git a/spec/ruby/core/env/dup_spec.rb b/spec/ruby/core/env/dup_spec.rb new file mode 100644 index 0000000000..ac66b455cd --- /dev/null +++ b/spec/ruby/core/env/dup_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' + +describe "ENV#dup" do + it "raises TypeError" do + -> { + ENV.dup + }.should raise_error(TypeError, /Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash/) + end +end diff --git a/spec/ruby/core/env/each_key_spec.rb b/spec/ruby/core/env/each_key_spec.rb index 294bf39912..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 @@ -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_pair_spec.rb b/spec/ruby/core/env/each_pair_spec.rb index 01082dd419..2d7ed5faa0 100644 --- a/spec/ruby/core/env/each_pair_spec.rb +++ b/spec/ruby/core/env/each_pair_spec.rb @@ -1,4 +1,4 @@ -require_relative '../../spec_helper' +require_relative 'spec_helper' require_relative 'shared/each' describe "ENV.each_pair" do diff --git a/spec/ruby/core/env/each_spec.rb b/spec/ruby/core/env/each_spec.rb index 06d8be3124..d1e06f55b6 100644 --- a/spec/ruby/core/env/each_spec.rb +++ b/spec/ruby/core/env/each_spec.rb @@ -1,4 +1,4 @@ -require_relative '../../spec_helper' +require_relative 'spec_helper' require_relative 'shared/each' describe "ENV.each" do diff --git a/spec/ruby/core/env/each_value_spec.rb b/spec/ruby/core/env/each_value_spec.rb index 88f4bc49da..cc3c9ebfb8 100644 --- a/spec/ruby/core/env/each_value_spec.rb +++ b/spec/ruby/core/env/each_value_spec.rb @@ -1,4 +1,4 @@ -require_relative '../../spec_helper' +require_relative 'spec_helper' require_relative '../enumerable/shared/enumeratorized' describe "ENV.each_value" do @@ -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 @@ -19,12 +19,14 @@ 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 ENV.each_value do |value| - value.encoding.should == Encoding.find('locale') + value.should.be_locale_env end end diff --git a/spec/ruby/core/env/element_reference_spec.rb b/spec/ruby/core/env/element_reference_spec.rb index 59b53fc4b1..66a9bc9690 100644 --- a/spec/ruby/core/env/element_reference_spec.rb +++ b/spec/ruby/core/env/element_reference_spec.rb @@ -1,5 +1,6 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "ENV.[]" do before :each do @@ -16,7 +17,18 @@ describe "ENV.[]" do it "returns only frozen values" do ENV[@variable] = "a non-frozen string" - ENV[@variable].frozen?.should == true + ENV[@variable].should.frozen? + 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 @@ -47,7 +59,7 @@ describe "ENV.[]" do it "uses the locale encoding if Encoding.default_internal is nil" do Encoding.default_internal = nil - locale = Encoding.find('locale') + locale = ENVSpecs.encoding locale = Encoding::BINARY if locale == Encoding::US_ASCII ENV[@variable] = "\xC3\xB8" ENV[@variable].encoding.should == locale diff --git a/spec/ruby/core/env/empty_spec.rb b/spec/ruby/core/env/empty_spec.rb index 7ef17d244f..afeb406a9e 100644 --- a/spec/ruby/core/env/empty_spec.rb +++ b/spec/ruby/core/env/empty_spec.rb @@ -4,12 +4,12 @@ describe "ENV.empty?" do it "returns true if the Environment is empty" do if ENV.keys.size > 0 - ENV.empty?.should == false + ENV.should_not.empty? end orig = ENV.to_hash begin ENV.clear - ENV.empty?.should == true + ENV.should.empty? ensure ENV.replace orig end @@ -17,7 +17,7 @@ describe "ENV.empty?" do it "returns false if not empty" do if ENV.keys.size > 0 - ENV.empty?.should == false + ENV.should_not.empty? end end end diff --git a/spec/ruby/core/env/except_spec.rb b/spec/ruby/core/env/except_spec.rb new file mode 100644 index 0000000000..fb8f3b7536 --- /dev/null +++ b/spec/ruby/core/env/except_spec.rb @@ -0,0 +1,34 @@ +require_relative 'spec_helper' +require_relative 'shared/to_hash' + +describe "ENV.except" do + before do + @orig_hash = ENV.to_hash + end + + after do + ENV.replace @orig_hash + end + + # Testing the method without arguments is covered via + it_behaves_like :env_to_hash, :except + + it "returns a hash without the requested subset" do + ENV.clear + + ENV['one'] = '1' + ENV['two'] = '2' + ENV['three'] = '3' + + ENV.except('one', 'three').should == { 'two' => '2' } + end + + it "ignores keys not present in the original hash" do + ENV.clear + + ENV['one'] = '1' + ENV['two'] = '2' + + ENV.except('one', 'three').should == { 'two' => '2' } + end +end diff --git a/spec/ruby/core/env/fetch_spec.rb b/spec/ruby/core/env/fetch_spec.rb index c8a11430ab..2c5d7cc3a0 100644 --- a/spec/ruby/core/env/fetch_spec.rb +++ b/spec/ruby/core/env/fetch_spec.rb @@ -1,19 +1,26 @@ require_relative '../../spec_helper' require_relative '../../shared/hash/key_error' +require_relative 'fixtures/common' 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 - lambda { 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 - it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, ENV + it_behaves_like :key_error, -> obj, key { obj.fetch(key) }, ENV it "formats the object with #inspect in the KeyError message" do -> { @@ -23,20 +30,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 - lambda do - ENV.fetch("should_never_be_set", "default") { 1 }.should == 1 + -> do + 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 == ENVSpecs.encoding end end diff --git a/spec/ruby/core/env/filter_spec.rb b/spec/ruby/core/env/filter_spec.rb index ba18a3b33b..52f8b79a0b 100644 --- a/spec/ruby/core/env/filter_spec.rb +++ b/spec/ruby/core/env/filter_spec.rb @@ -2,14 +2,12 @@ require_relative '../../spec_helper' require_relative '../enumerable/shared/enumeratorized' require_relative 'shared/select' -ruby_version_is "2.6" do - describe "ENV.filter!" do - it_behaves_like :env_select!, :filter! - it_behaves_like :enumeratorized_with_origin_size, :filter!, ENV - end +describe "ENV.filter!" do + it_behaves_like :env_select!, :filter! + it_behaves_like :enumeratorized_with_origin_size, :filter!, ENV +end - describe "ENV.filter" do - it_behaves_like :env_select, :filter - it_behaves_like :enumeratorized_with_origin_size, :filter, ENV - end +describe "ENV.filter" do + it_behaves_like :env_select, :filter + it_behaves_like :enumeratorized_with_origin_size, :filter, ENV end diff --git a/spec/ruby/core/env/fixtures/common.rb b/spec/ruby/core/env/fixtures/common.rb new file mode 100644 index 0000000000..8d5057614d --- /dev/null +++ b/spec/ruby/core/env/fixtures/common.rb @@ -0,0 +1,9 @@ +module ENVSpecs + def self.encoding + locale = Encoding.find('locale') + if ruby_version_is '3' and platform_is :windows + locale = Encoding::UTF_8 + end + locale + end +end diff --git a/spec/ruby/core/env/index_spec.rb b/spec/ruby/core/env/index_spec.rb deleted file mode 100644 index 04986a0421..0000000000 --- a/spec/ruby/core/env/index_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/key' - -describe "ENV.index" do - it_behaves_like :env_key, :index -end diff --git a/spec/ruby/core/env/indexes_spec.rb b/spec/ruby/core/env/indexes_spec.rb deleted file mode 100644 index e724feaa39..0000000000 --- a/spec/ruby/core/env/indexes_spec.rb +++ /dev/null @@ -1 +0,0 @@ -require_relative '../../spec_helper' diff --git a/spec/ruby/core/env/indices_spec.rb b/spec/ruby/core/env/indices_spec.rb deleted file mode 100644 index e724feaa39..0000000000 --- a/spec/ruby/core/env/indices_spec.rb +++ /dev/null @@ -1 +0,0 @@ -require_relative '../../spec_helper' diff --git a/spec/ruby/core/env/inspect_spec.rb b/spec/ruby/core/env/inspect_spec.rb index 3c611c24a1..7dd92b120f 100644 --- a/spec/ruby/core/env/inspect_spec.rb +++ b/spec/ruby/core/env/inspect_spec.rb @@ -4,7 +4,7 @@ describe "ENV.inspect" do it "returns a String that looks like a Hash with real data" do ENV["foo"] = "bar" - ENV.inspect.should =~ /\{.*"foo"=>"bar".*\}/ + ENV.inspect.should =~ /\{.*"foo" *=> *"bar".*\}/ ENV.delete "foo" 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/key_spec.rb b/spec/ruby/core/env/key_spec.rb index 82cfbefa39..cf70286409 100644 --- a/spec/ruby/core/env/key_spec.rb +++ b/spec/ruby/core/env/key_spec.rb @@ -1,11 +1,39 @@ require_relative '../../spec_helper' require_relative 'shared/include' -require_relative 'shared/key' describe "ENV.key?" do it_behaves_like :env_include, :key? end describe "ENV.key" do - it_behaves_like :env_key, :key + 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.key("bar").should == "foo" + end + + it "returns nil if the passed value is not found" do + ENV.delete("foo") + ENV.key("foo").should be_nil + end + + it "coerces the key element with #to_str" do + ENV["foo"] = "bar" + k = mock('key') + k.should_receive(:to_str).and_return("bar") + ENV.key(k).should == "foo" + end + + it "raises TypeError if the argument is not a String and does not respond to #to_str" do + -> { + ENV.key(Object.new) + }.should raise_error(TypeError, "no implicit conversion of Object into String") + 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/length_spec.rb b/spec/ruby/core/env/length_spec.rb index 536af9edf5..c6f9062892 100644 --- a/spec/ruby/core/env/length_spec.rb +++ b/spec/ruby/core/env/length_spec.rb @@ -2,5 +2,5 @@ require_relative '../../spec_helper' require_relative 'shared/length' describe "ENV.length" do - it_behaves_like :env_length, :length + it_behaves_like :env_length, :length end diff --git a/spec/ruby/core/env/merge_spec.rb b/spec/ruby/core/env/merge_spec.rb index b418cd11f4..f10662cf79 100644 --- a/spec/ruby/core/env/merge_spec.rb +++ b/spec/ruby/core/env/merge_spec.rb @@ -1,8 +1,6 @@ require_relative '../../spec_helper' require_relative 'shared/update' -ruby_version_is "2.7" do - describe "ENV.merge!" do - it_behaves_like :env_update, :merge! - end +describe "ENV.merge!" do + it_behaves_like :env_update, :merge! end 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/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/reject_spec.rb b/spec/ruby/core/env/reject_spec.rb index 409efa1385..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,19 +25,23 @@ 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 orig = ENV.to_hash begin ENV.clear - lambda { ENV.reject! }.should_not raise_error(LocalJumpError) + -> { ENV.reject! }.should_not raise_error(LocalJumpError) ensure ENV.replace orig end @@ -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,14 +80,18 @@ 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 orig = ENV.to_hash begin ENV.clear - lambda { ENV.reject }.should_not raise_error(LocalJumpError) + -> { ENV.reject }.should_not raise_error(LocalJumpError) ensure ENV.replace orig 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/each.rb b/spec/ruby/core/env/shared/each.rb index 8a262e4862..d901b854c4 100644 --- a/spec/ruby/core/env/shared/each.rb +++ b/spec/ruby/core/env/shared/each.rb @@ -8,7 +8,7 @@ describe :env_each, shared: true do ENV.clear ENV["foo"] = "bar" ENV["baz"] = "boo" - ENV.send(@method) { |k, v| e << [k, v] } + ENV.send(@method) { |k, v| e << [k, v] }.should equal(ENV) e.should include(["foo", "bar"]) e.should include(["baz", "boo"]) ensure @@ -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 @@ -31,8 +35,6 @@ describe :env_each, shared: true do @internal = Encoding.default_internal Encoding.default_external = Encoding::BINARY - - @locale_encoding = Encoding.find "locale" end after :each do @@ -44,8 +46,8 @@ describe :env_each, shared: true do Encoding.default_internal = nil ENV.send(@method) do |key, value| - key.encoding.should equal(@locale_encoding) - value.encoding.should equal(@locale_encoding) + key.should.be_locale_env + value.should.be_locale_env end end diff --git a/spec/ruby/core/env/shared/include.rb b/spec/ruby/core/env/shared/include.rb index 8d8311dcf2..70aa555301 100644 --- a/spec/ruby/core/env/shared/include.rb +++ b/spec/ruby/core/env/shared/include.rb @@ -1,11 +1,30 @@ 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 "coerces the key with #to_str" do + ENV["foo"] = "bar" + k = mock('key') + k.should_receive(:to_str).and_return("foo") + ENV.send(@method, k).should == true + 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 deleted file mode 100644 index 43299beadb..0000000000 --- a/spec/ruby/core/env/shared/key.rb +++ /dev/null @@ -1,11 +0,0 @@ -describe :env_key, shared: true do - 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 - 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 4949ca8c73..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,19 +38,19 @@ describe :env_store, shared: true do end it "raises TypeError when the key is not coercible to String" do - lambda { 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 - lambda { 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 - lambda { ENV.send(@method, "foo=", "bar") }.should raise_error(Errno::EINVAL) + -> { ENV.send(@method, "foo=", "bar") }.should raise_error(Errno::EINVAL) end it "raises Errno::EINVAL when the key is an empty string" do - lambda { ENV.send(@method, "", "bar") }.should raise_error(Errno::EINVAL) + -> { ENV.send(@method, "", "bar") }.should raise_error(Errno::EINVAL) end it "does nothing when the key is not a valid environment variable key and the value is nil" do diff --git a/spec/ruby/core/env/shared/to_hash.rb b/spec/ruby/core/env/shared/to_hash.rb index 254054c14d..a0d4d7ce69 100644 --- a/spec/ruby/core/env/shared/to_hash.rb +++ b/spec/ruby/core/env/shared/to_hash.rb @@ -1,22 +1,33 @@ 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 - ENV.send(@method).keys.all? {|k| k.encoding == Encoding.find('locale') }.should be_true + ENV.send(@method).keys.each {|k| k.should.be_locale_env } end it "uses the locale encoding for values" do - ENV.send(@method).values.all? {|v| v.encoding == Encoding.find('locale') }.should be_true + ENV.send(@method).values.each {|k| k.should.be_locale_env } end 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 cd09877243..e1b1c9c290 100644 --- a/spec/ruby/core/env/shared/update.rb +++ b/spec/ruby/core/env/shared/update.rb @@ -1,21 +1,104 @@ describe :env_update, shared: true do - it "adds the parameter hash to ENV" do - ENV["foo"].should == nil - ENV.send @method, "foo" => "bar" - ENV["foo"].should == "bar" - ENV.delete "foo" - 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" + 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, returning ENV" do + ENV.send(@method, "foo" => "0", "bar" => "1").should equal(ENV) + ENV["foo"].should == "0" + ENV["bar"].should == "1" + end + + it "adds the multiple parameter hashes to ENV, returning ENV" do + ENV.send(@method, {"foo" => "multi1"}, {"bar" => "multi2"}).should equal(ENV) + ENV["foo"].should == "multi1" + ENV["bar"].should == "multi2" + 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 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" + end + + # 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 + + 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 "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 + 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..c2b5025465 100644 --- a/spec/ruby/core/env/shared/value.rb +++ b/spec/ruby/core/env/shared/value.rb @@ -1,11 +1,29 @@ 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 "coerces the value element with #to_str" do + ENV["foo"] = "bar" + v = mock('value') + v.should_receive(:to_str).and_return("bar") + ENV.send(@method, v).should == true + 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/shift_spec.rb b/spec/ruby/core/env/shift_spec.rb index c03b5d50c5..1b92e5d1e4 100644 --- a/spec/ruby/core/env/shift_spec.rb +++ b/spec/ruby/core/env/shift_spec.rb @@ -1,28 +1,5 @@ require_relative '../../spec_helper' - -describe "ENV.shift" do - it "returns a pair and deletes it" do - ENV.empty?.should == false - orig = ENV.to_hash - begin - pair = ENV.shift - ENV.has_key?(pair.first).should == false - ensure - ENV.replace orig - end - ENV.has_key?(pair.first).should == true - end - - it "returns nil if ENV.empty?" do - orig = ENV.to_hash - begin - ENV.clear - ENV.shift.should == nil - ensure - ENV.replace orig - end - end -end +require_relative 'fixtures/common' describe "ENV.shift" do before :each do @@ -31,6 +8,7 @@ describe "ENV.shift" do @internal = Encoding.default_internal Encoding.default_external = Encoding::BINARY + ENV.replace({"FOO"=>"BAR"}) end after :each do @@ -39,12 +17,24 @@ describe "ENV.shift" do ENV.replace @orig end + it "returns a pair and deletes it" do + ENV.should.has_key?("FOO") + pair = ENV.shift + pair.should == ["FOO", "BAR"] + ENV.should_not.has_key?("FOO") + end + + it "returns nil if ENV.empty?" do + ENV.clear + ENV.shift.should == nil + end + it "uses the locale encoding if Encoding.default_internal is nil" do Encoding.default_internal = nil pair = ENV.shift - pair.first.encoding.should equal(Encoding.find("locale")) - pair.last.encoding.should equal(Encoding.find("locale")) + pair.first.encoding.should equal(ENVSpecs.encoding) + pair.last.encoding.should equal(ENVSpecs.encoding) end it "transcodes from the locale encoding to Encoding.default_internal if set" do diff --git a/spec/ruby/core/env/size_spec.rb b/spec/ruby/core/env/size_spec.rb index f050e9e5a9..7c8072481e 100644 --- a/spec/ruby/core/env/size_spec.rb +++ b/spec/ruby/core/env/size_spec.rb @@ -2,5 +2,5 @@ require_relative '../../spec_helper' require_relative 'shared/length' describe "ENV.size" do - it_behaves_like :env_length, :size + it_behaves_like :env_length, :size 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..959239d2b2 --- /dev/null +++ b/spec/ruby/core/env/slice_spec.rb @@ -0,0 +1,37 @@ +require_relative '../../spec_helper' + +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 "returns the values for the keys coerced with #to_str, but keeps the original objects as result keys" do + foo = mock('key 1') + foo.should_receive(:to_str).and_return("foo") + boo = mock('key 2') + boo.should_receive(:to_str).and_return("boo") + bar = mock('key 3') + bar.should_receive(:to_str).and_return("bar") + 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 diff --git a/spec/ruby/core/env/spec_helper.rb b/spec/ruby/core/env/spec_helper.rb new file mode 100644 index 0000000000..470ffa58bc --- /dev/null +++ b/spec/ruby/core/env/spec_helper.rb @@ -0,0 +1,26 @@ +require_relative '../../spec_helper' + +locale_env_matcher = Class.new do + def initialize(name = 'locale') + encoding = Encoding.find(name) + @encodings = (encoding = Encoding::US_ASCII) ? + [encoding, Encoding::ASCII_8BIT] : [encoding] + end + + def matches?(actual) + @actual = actual = actual.encoding + @encodings.include?(actual) + end + + def failure_message + ["Expected #{@actual} to be #{@encodings.join(' or ')}"] + end + + def negative_failure_message + ["Expected #{@actual} not to be #{@encodings.join(' or ')}"] + end +end + +String.__send__(:define_method, :be_locale_env) do |expected = 'locale'| + locale_env_matcher.new(expected) +end diff --git a/spec/ruby/core/env/to_a_spec.rb b/spec/ruby/core/env/to_a_spec.rb index ed290a48a5..2b1649281f 100644 --- a/spec/ruby/core/env/to_a_spec.rb +++ b/spec/ruby/core/env/to_a_spec.rb @@ -1,19 +1,21 @@ -require_relative '../../spec_helper' +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 + a.each { |k,v| ENV[k].should == v } + + a.first.should.is_a?(Array) + a.first.size.should == 2 end it "returns the entries in the locale encoding" do ENV.to_a.each do |key, value| - key.encoding.should == Encoding.find('locale') - value.encoding.should == Encoding.find('locale') + key.should.be_locale_env + value.should.be_locale_env end end end diff --git a/spec/ruby/core/env/to_h_spec.rb b/spec/ruby/core/env/to_h_spec.rb index 81a17700e9..58ea2d2030 100644 --- a/spec/ruby/core/env/to_h_spec.rb +++ b/spec/ruby/core/env/to_h_spec.rb @@ -1,55 +1,70 @@ -require_relative '../../spec_helper' +require_relative 'spec_helper' require_relative 'shared/to_hash' describe "ENV.to_h" do it_behaves_like :env_to_hash, :to_h - ruby_version_is "2.6" do - context "with block" do - before do - @orig_hash = ENV.to_hash - end - - after do - ENV.replace @orig_hash - end - - it "converts [key, value] pairs returned by the block to a hash" do - ENV.replace("a" => "b", "c" => "d") - ENV.to_h { |k, v| [k, v.upcase] }.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] } - end.should raise_error(ArgumentError, /element has wrong array length/) - - -> do - ENV.to_h { |k, v| [k] } - end.should raise_error(ArgumentError, /element has wrong array length/) - end - - it "raises TypeError if block returns something other than Array" do - -> do - ENV.to_h { |k, v| "not-array" } - end.should raise_error(TypeError, /wrong element type String/) - end - - it "coerces returned pair to Array with #to_ary" do - x = mock('x') - x.stub!(:to_ary).and_return([:b, 'b']) - - ENV.to_h { |k| x }.should == { :b => 'b' } - end - - it "does not coerce returned pair to Array with #to_a" do - x = mock('x') - x.stub!(:to_a).and_return([:b, 'b']) - - -> do - ENV.to_h { |k| x } - end.should raise_error(TypeError, /wrong element type MockObject/) - end + context "with block" do + before do + @orig_hash = ENV.to_hash + end + + after do + ENV.replace @orig_hash + end + + it "converts [key, value] pairs returned by the block to a hash" do + ENV.replace("a" => "b", "c" => "d") + ENV.to_h { |k, v| [k, v.upcase] }.should == { 'a' => "B", 'c' => "D" } + end + + it "passes to a block each pair's key and value as separate arguments" do + ENV.replace("a" => "b", "c" => "d") + + ScratchPad.record [] + ENV.to_h { |k, v| ScratchPad << [k, v]; [k, v] } + ScratchPad.recorded.sort.should == [["a", "b"], ["c", "d"]] + + ScratchPad.record [] + ENV.to_h { |*args| ScratchPad << args; [args[0], args[1]] } + ScratchPad.recorded.sort.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] } + end.should raise_error(ArgumentError, /element has wrong array length/) + + -> do + ENV.to_h { |k, v| [k] } + end.should raise_error(ArgumentError, /element has wrong array length/) + end + + it "raises TypeError if block returns something other than Array" do + -> do + ENV.to_h { |k, v| "not-array" } + end.should raise_error(TypeError, /wrong element type String/) + end + + it "coerces returned pair to Array with #to_ary" do + x = mock('x') + x.stub!(:to_ary).and_return([:b, 'b']) + + ENV.to_h { |k| x }.should == { :b => 'b' } + end + + it "does not coerce returned pair to Array with #to_a" do + x = mock('x') + x.stub!(:to_a).and_return([:b, 'b']) + + -> do + ENV.to_h { |k| x } + end.should raise_error(TypeError, /wrong element type MockObject/) end end end diff --git a/spec/ruby/core/env/to_hash_spec.rb b/spec/ruby/core/env/to_hash_spec.rb index 4de0e077bb..306572c353 100644 --- a/spec/ruby/core/env/to_hash_spec.rb +++ b/spec/ruby/core/env/to_hash_spec.rb @@ -1,4 +1,4 @@ -require_relative '../../spec_helper' +require_relative 'spec_helper' require_relative 'shared/to_hash' describe "ENV.to_hash" do diff --git a/spec/ruby/core/env/values_at_spec.rb b/spec/ruby/core/env/values_at_spec.rb index 906d8b01f2..338680e820 100644 --- a/spec/ruby/core/env/values_at_spec.rb +++ b/spec/ruby/core/env/values_at_spec.rb @@ -1,17 +1,38 @@ require_relative '../../spec_helper' +require_relative 'fixtures/common' 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') + ENV.values_at(ENV.keys.first).first.encoding.should == ENVSpecs.encoding + 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..71bc877d31 100644 --- a/spec/ruby/core/env/values_spec.rb +++ b/spec/ruby/core/env/values_spec.rb @@ -1,21 +1,14 @@ -require_relative '../../spec_helper' +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 ENV.values.each do |value| - value.encoding.should == Encoding.find('locale') + value.should.be_locale_env end end end |
