diff options
Diffstat (limited to 'spec/ruby/core/env')
36 files changed, 268 insertions, 165 deletions
diff --git a/spec/ruby/core/env/assoc_spec.rb b/spec/ruby/core/env/assoc_spec.rb index c7a388db75..b81be7ddf2 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, "no implicit conversion of Object into String") + -> { ENV.assoc(Object.new) }.should.raise(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 48b034ba1d..c0d20193ad 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.should equal(ENV) + 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..bb3c7ff2f8 --- /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(ArgumentError) + end + + it "raises ArgumentError when keyword argument is not 'freeze'" do + -> { + ENV.clone(foo: nil) + }.should.raise(ArgumentError) + end + + it "raises TypeError" do + -> { + ENV.clone + }.should.raise(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 d2de51c225..4b05064eba 100644 --- a/spec/ruby/core/env/delete_if_spec.rb +++ b/spec/ruby/core/env/delete_if_spec.rb @@ -22,15 +22,15 @@ describe "ENV.delete_if" do end it "returns ENV when block given" do - ENV.delete_if { |k, v| ["foo", "bar"].include?(k) }.should equal(ENV) + 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 equal(ENV) + ENV.delete_if { false }.should.equal?(ENV) end it "returns an Enumerator if no block given" do - ENV.delete_if.should be_an_instance_of(Enumerator) + ENV.delete_if.should.instance_of?(Enumerator) end it "deletes pairs through enumerator" do @@ -42,12 +42,12 @@ describe "ENV.delete_if" do it "returns ENV from enumerator" do enum = ENV.delete_if - enum.each { |k, v| ["foo", "bar"].include?(k) }.should equal(ENV) + 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) + 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 5e7891f74d..db6d07b057 100644 --- a/spec/ruby/core/env/delete_spec.rb +++ b/spec/ruby/core/env/delete_spec.rb @@ -30,11 +30,9 @@ describe "ENV.delete" do ScratchPad.recorded.should == "foo" end - ruby_version_is "3.0" do - 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 "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 @@ -43,7 +41,15 @@ describe "ENV.delete" do 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") + -> { ENV.delete(Object.new) }.should.raise(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..d8cfac891d --- /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(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 0efcb09900..ad2cb560a0 100644 --- a/spec/ruby/core/env/each_key_spec.rb +++ b/spec/ruby/core/env/each_key_spec.rb @@ -10,9 +10,9 @@ describe "ENV.each_key" do ENV.clear ENV["1"] = "3" ENV["2"] = "4" - ENV.each_key { |k| e << k }.should equal(ENV) - e.should include("1") - e.should include("2") + ENV.each_key { |k| e << k }.should.equal?(ENV) + e.should.include?("1") + e.should.include?("2") ensure ENV.replace orig end @@ -20,7 +20,7 @@ describe "ENV.each_key" do it "returns an Enumerator if called without a block" do enum = ENV.each_key - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == ENV.keys end diff --git a/spec/ruby/core/env/each_value_spec.rb b/spec/ruby/core/env/each_value_spec.rb index cc3c9ebfb8..6f65e923e6 100644 --- a/spec/ruby/core/env/each_value_spec.rb +++ b/spec/ruby/core/env/each_value_spec.rb @@ -10,9 +10,9 @@ describe "ENV.each_value" do ENV.clear ENV["1"] = "3" ENV["2"] = "4" - ENV.each_value { |v| e << v }.should equal(ENV) - e.should include("3") - e.should include("4") + ENV.each_value { |v| e << v }.should.equal?(ENV) + e.should.include?("3") + e.should.include?("4") ensure ENV.replace orig end @@ -20,7 +20,7 @@ describe "ENV.each_value" do it "returns an Enumerator if called without a block" do enum = ENV.each_value - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == ENV.values end diff --git a/spec/ruby/core/env/element_reference_spec.rb b/spec/ruby/core/env/element_reference_spec.rb index 560c127a9c..e3ac979418 100644 --- a/spec/ruby/core/env/element_reference_spec.rb +++ b/spec/ruby/core/env/element_reference_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' require_relative 'fixtures/common' @@ -28,7 +28,7 @@ describe "ENV.[]" do 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") + -> { ENV[Object.new] }.should.raise(TypeError, "no implicit conversion of Object into String") end platform_is :windows do @@ -71,6 +71,6 @@ describe "ENV.[]" do ENV[@variable] = "" Encoding.default_internal = Encoding::IBM437 - ENV[@variable].encoding.should equal(Encoding::IBM437) + ENV[@variable].encoding.should.equal?(Encoding::IBM437) end end diff --git a/spec/ruby/core/env/except_spec.rb b/spec/ruby/core/env/except_spec.rb index cfe5865abe..fb8f3b7536 100644 --- a/spec/ruby/core/env/except_spec.rb +++ b/spec/ruby/core/env/except_spec.rb @@ -1,36 +1,34 @@ require_relative 'spec_helper' require_relative 'shared/to_hash' -ruby_version_is "3.0" do - describe "ENV.except" do - before do - @orig_hash = ENV.to_hash - end +describe "ENV.except" do + before do + @orig_hash = ENV.to_hash + end - after do - ENV.replace @orig_hash - end + after do + ENV.replace @orig_hash + end - # Testing the method without arguments is covered via - it_behaves_like :env_to_hash, :except + # 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 + it "returns a hash without the requested subset" do + ENV.clear - ENV['one'] = '1' - ENV['two'] = '2' - ENV['three'] = '3' + ENV['one'] = '1' + ENV['two'] = '2' + ENV['three'] = '3' - ENV.except('one', 'three').should == { 'two' => '2' } - end + ENV.except('one', 'three').should == { 'two' => '2' } + end - it "ignores keys not present in the original hash" do - ENV.clear + it "ignores keys not present in the original hash" do + ENV.clear - ENV['one'] = '1' - ENV['two'] = '2' + ENV['one'] = '1' + ENV['two'] = '2' - ENV.except('one', 'three').should == { 'two' => '2' } - end + 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 b2e7a88cab..a2ec79c62b 100644 --- a/spec/ruby/core/env/fetch_spec.rb +++ b/spec/ruby/core/env/fetch_spec.rb @@ -16,7 +16,7 @@ describe "ENV.fetch" do end it "raises a TypeError if the key is not a String" do - -> { ENV.fetch Object.new }.should raise_error(TypeError, "no implicit conversion of Object into String") + -> { ENV.fetch Object.new }.should.raise(TypeError, "no implicit conversion of Object into String") end context "when the key is not found" do @@ -25,7 +25,7 @@ describe "ENV.fetch" do it "formats the object with #inspect in the KeyError message" do -> { ENV.fetch('foo') - }.should raise_error(KeyError, 'key not found: "foo"') + }.should.raise(KeyError, 'key not found: "foo"') end end @@ -47,7 +47,7 @@ describe "ENV.fetch" do it "warns on block and default parameter given" do -> do - ENV.fetch("foo", "default") { "bar" }.should == "bar" + ENV.fetch("foo", "default") { "bar" }.should == "bar" end.should complain(/block supersedes default value argument/) end diff --git a/spec/ruby/core/env/fetch_values_spec.rb b/spec/ruby/core/env/fetch_values_spec.rb new file mode 100644 index 0000000000..302cde2fd1 --- /dev/null +++ b/spec/ruby/core/env/fetch_values_spec.rb @@ -0,0 +1,51 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/common' + +ruby_version_is "4.1" do + describe "ENV.fetch_values" do + before :each do + @saved_foo = ENV["foo"] + @saved_bar = ENV["bar"] + ENV.delete("foo") + ENV.delete("bar") + end + + after :each do + ENV["foo"] = @saved_foo + ENV["bar"] = @saved_bar + end + + it "returns an array of the values corresponding to the given keys" do + ENV["foo"] = "oof" + ENV["bar"] = "rab" + ENV.fetch_values("bar", "foo").should == ["rab", "oof"] + end + + it "returns the default value from block" do + ENV["foo"] = "oof" + ENV.fetch_values("bar") { |key| "`#{key}' is not found" }.should == ["`bar' is not found"] + ENV.fetch_values("bar", "foo") { |key| "`#{key}' is not found" }.should == ["`bar' is not found", "oof"] + end + + it "returns an empty array if no keys specified" do + ENV.fetch_values.should == [] + end + + it "raises KeyError when there is no matching key" do + ENV["foo"] = "oof" + ENV["bar"] = "rab" + -> { + ENV.fetch_values("bar", "y", "foo", "z") + }.should.raise(KeyError, 'key not found: "y"') + end + + it "uses the locale encoding" do + ENV.fetch_values(ENV.keys.first).first.encoding.should == ENVSpecs.encoding + end + + it "raises TypeError when a key is not coercible to String" do + ENV["foo"] = "oof" + -> { ENV.fetch_values("foo", Object.new) }.should.raise(TypeError, "no implicit conversion of Object into String") + end + 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 301a66ab4e..0000000000 --- a/spec/ruby/core/env/index_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/key' - -ruby_version_is ''...'3.0' do - 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 -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 64b6a207d0..f24981e216 100644 --- a/spec/ruby/core/env/keep_if_spec.rb +++ b/spec/ruby/core/env/keep_if_spec.rb @@ -22,15 +22,15 @@ describe "ENV.keep_if" do end it "returns ENV when block given" do - ENV.keep_if { |k, v| !["foo", "bar"].include?(k) }.should equal(ENV) + 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 equal(ENV) + ENV.keep_if { true }.should.equal?(ENV) end it "returns an Enumerator if no block given" do - ENV.keep_if.should be_an_instance_of(Enumerator) + ENV.keep_if.should.instance_of?(Enumerator) end it "deletes pairs through enumerator" do @@ -42,12 +42,12 @@ describe "ENV.keep_if" do it "returns ENV from enumerator" do enum = ENV.keep_if - enum.each { |k, v| !["foo", "bar"].include?(k) }.should equal(ENV) + 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) + 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..677cf35216 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 == 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(TypeError, "no implicit conversion of Object into String") + end end 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/rassoc_spec.rb b/spec/ruby/core/env/rassoc_spec.rb index ab9fe68088..e4ac5ce5e2 100644 --- a/spec/ruby/core/env/rassoc_spec.rb +++ b/spec/ruby/core/env/rassoc_spec.rb @@ -22,7 +22,7 @@ describe "ENV.rassoc" do [ ["foo", "bar"], ["baz", "bar"], - ].should include(ENV.rassoc("bar")) + ].should.include?(ENV.rassoc("bar")) end it "returns nil if no environment variable with the given value exists" do diff --git a/spec/ruby/core/env/reject_spec.rb b/spec/ruby/core/env/reject_spec.rb index 6a9794925d..4df864493d 100644 --- a/spec/ruby/core/env/reject_spec.rb +++ b/spec/ruby/core/env/reject_spec.rb @@ -25,15 +25,15 @@ 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 equal(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["foo"] = "bar" enum = ENV.reject! - enum.should be_an_instance_of(Enumerator) - enum.each { |k, v| k == "foo" }.should equal(ENV) + enum.should.instance_of?(Enumerator) + enum.each { |k, v| k == "foo" }.should.equal?(ENV) ENV["foo"].should == nil end @@ -41,7 +41,7 @@ describe "ENV.reject!" do orig = ENV.to_hash begin ENV.clear - -> { ENV.reject! }.should_not raise_error(LocalJumpError) + -> { ENV.reject! }.should_not.raise(LocalJumpError) ensure ENV.replace orig end @@ -76,13 +76,13 @@ describe "ENV.reject" do end it "returns a Hash" do - ENV.reject { false }.should be_kind_of(Hash) + ENV.reject { false }.should.is_a?(Hash) end it "returns an Enumerator if called without a block" do ENV["foo"] = "bar" enum = ENV.reject - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each { |k, v| k == "foo"} ENV["foo"] = nil end @@ -91,7 +91,7 @@ describe "ENV.reject" do orig = ENV.to_hash begin ENV.clear - -> { ENV.reject }.should_not raise_error(LocalJumpError) + -> { ENV.reject }.should_not.raise(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 9fc67643d1..27eb3e45dd 100644 --- a/spec/ruby/core/env/replace_spec.rb +++ b/spec/ruby/core/env/replace_spec.rb @@ -11,41 +11,41 @@ describe "ENV.replace" do end it "replaces ENV with a Hash" do - ENV.replace("foo" => "0", "bar" => "1").should equal(ENV) + 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.replace(Object.new) }.should.raise(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.replace(Object.new => "0") }.should.raise(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.replace("foo" => Object.new) }.should.raise(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) + -> { ENV.replace("foo=" =>"bar") }.should.raise(Errno::EINVAL) end it "raises Errno::EINVAL when the key is an empty string" do - -> { ENV.replace("" => "bar") }.should raise_error(Errno::EINVAL) + -> { ENV.replace("" => "bar") }.should.raise(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") + -> { ENV.replace("foo" => "1", Object.new => Object.new) }.should.raise(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.replace(Object.new => Object.new, "foo" => "0") }.should.raise(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 d901b854c4..0661ca924c 100644 --- a/spec/ruby/core/env/shared/each.rb +++ b/spec/ruby/core/env/shared/each.rb @@ -8,9 +8,9 @@ describe :env_each, shared: true do ENV.clear ENV["foo"] = "bar" ENV["baz"] = "boo" - ENV.send(@method) { |k, v| e << [k, v] }.should equal(ENV) - e.should include(["foo", "bar"]) - e.should include(["baz", "boo"]) + ENV.send(@method) { |k, v| e << [k, v] }.should.equal?(ENV) + e.should.include?(["foo", "bar"]) + e.should.include?(["baz", "boo"]) ensure ENV.replace orig end @@ -18,7 +18,7 @@ describe :env_each, shared: true do it "returns an Enumerator if called without a block" do enum = ENV.send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each do |name, value| ENV[name].should == value end @@ -55,9 +55,9 @@ describe :env_each, shared: true do Encoding.default_internal = internal = Encoding::IBM437 ENV.send(@method) do |key, value| - key.encoding.should equal(internal) + key.encoding.should.equal?(internal) if value.ascii_only? - value.encoding.should equal(internal) + value.encoding.should.equal?(internal) end end end diff --git a/spec/ruby/core/env/shared/include.rb b/spec/ruby/core/env/shared/include.rb index 3efcd523d6..ceca02e3eb 100644 --- a/spec/ruby/core/env/shared/include.rb +++ b/spec/ruby/core/env/shared/include.rb @@ -17,7 +17,14 @@ describe :env_include, shared: true do 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") + -> { ENV.send(@method, Object.new) }.should.raise(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 93396d2aca..0000000000 --- a/spec/ruby/core/env/shared/key.rb +++ /dev/null @@ -1,31 +0,0 @@ -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" - suppress_warning { - ENV.send(@method, "bar").should == "foo" - } - end - - it "returns nil if the passed value is not found" do - ENV.delete("foo") - suppress_warning { - 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 - -> { - suppress_warning { - 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 75ba112a32..8ec648a637 100644 --- a/spec/ruby/core/env/shared/select.rb +++ b/spec/ruby/core/env/shared/select.rb @@ -14,7 +14,7 @@ describe :env_select, shared: true do it "returns an Enumerator when no block is given" do enum = ENV.send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) end it "selects via the enumerator" do @@ -49,7 +49,7 @@ describe :env_select!, shared: true do end it "returns an Enumerator if called without a block" do - ENV.send(@method).should be_an_instance_of(Enumerator) + ENV.send(@method).should.instance_of?(Enumerator) end it "selects via the enumerator" do diff --git a/spec/ruby/core/env/shared/store.rb b/spec/ruby/core/env/shared/store.rb index d6265c66a5..388208a8ac 100644 --- a/spec/ruby/core/env/shared/store.rb +++ b/spec/ruby/core/env/shared/store.rb @@ -14,13 +14,13 @@ describe :env_store, shared: true do it "returns the value" do value = "bar" - ENV.send(@method, "foo", value).should equal(value) + ENV.send(@method, "foo", value).should.equal?(value) end it "deletes the environment variable when the value is nil" do ENV["foo"] = "bar" ENV.send(@method, "foo", nil) - ENV.key?("foo").should be_false + ENV.key?("foo").should == false end it "coerces the key argument with #to_str" do @@ -38,23 +38,23 @@ 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, "no implicit conversion of Object into String") + -> { ENV.send(@method, Object.new, "bar") }.should.raise(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, "no implicit conversion of Object into String") + -> { ENV.send(@method, "foo", Object.new) }.should.raise(TypeError, "no implicit conversion of Object into String") end it "raises Errno::EINVAL when the key contains the '=' character" do - -> { ENV.send(@method, "foo=", "bar") }.should raise_error(Errno::EINVAL) + -> { ENV.send(@method, "foo=", "bar") }.should.raise(Errno::EINVAL) end it "raises Errno::EINVAL when the key is an empty string" do - -> { ENV.send(@method, "", "bar") }.should raise_error(Errno::EINVAL) + -> { ENV.send(@method, "", "bar") }.should.raise(Errno::EINVAL) end it "does nothing when the key is not a valid environment variable key and the value is nil" do ENV.send(@method, "foo=", nil) - ENV.key?("foo=").should be_false + ENV.key?("foo=").should == false end end diff --git a/spec/ruby/core/env/shared/to_hash.rb b/spec/ruby/core/env/shared/to_hash.rb index a0d4d7ce69..7868690c38 100644 --- a/spec/ruby/core/env/shared/to_hash.rb +++ b/spec/ruby/core/env/shared/to_hash.rb @@ -10,7 +10,7 @@ describe :env_to_hash, shared: true do it "returns the ENV as a hash" do ENV["foo"] = "bar" h = ENV.send(@method) - h.should be_an_instance_of(Hash) + h.should.instance_of?(Hash) h["foo"].should == "bar" end @@ -24,7 +24,7 @@ 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.should_not.equal? ENV h.size.should == ENV.size h.each_pair do |k, v| ENV[k].should == v diff --git a/spec/ruby/core/env/shared/update.rb b/spec/ruby/core/env/shared/update.rb index 7d4799955b..112cc2505d 100644 --- a/spec/ruby/core/env/shared/update.rb +++ b/spec/ruby/core/env/shared/update.rb @@ -10,21 +10,19 @@ describe :env_update, shared: true do end it "adds the parameter hash to ENV, returning ENV" do - ENV.send(@method, "foo" => "0", "bar" => "1").should equal(ENV) + ENV.send(@method, "foo" => "0", "bar" => "1").should.equal?(ENV) ENV["foo"].should == "0" ENV["bar"].should == "1" end - ruby_version_is "3.2" do - 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 "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) + 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 @@ -65,23 +63,23 @@ describe :env_update, shared: true do end it "returns ENV when block given" do - ENV.send(@method, {"foo" => "0", "bar" => "1"}){}.should equal(ENV) + 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") + -> { ENV.send @method, Object.new => "0" }.should.raise(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") + -> { ENV.send @method, "foo" => Object.new }.should.raise(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) + -> { ENV.send(@method, "foo=" => "bar") }.should.raise(Errno::EINVAL) end it "raises Errno::EINVAL when a name is an empty string" do - -> { ENV.send(@method, "" => "bar") }.should raise_error(Errno::EINVAL) + -> { ENV.send(@method, "" => "bar") }.should.raise(Errno::EINVAL) end it "updates good data preceding an error" do diff --git a/spec/ruby/core/env/shared/value.rb b/spec/ruby/core/env/shared/value.rb index bef96b5fef..c2b5025465 100644 --- a/spec/ruby/core/env/shared/value.rb +++ b/spec/ruby/core/env/shared/value.rb @@ -16,6 +16,13 @@ describe :env_value, shared: true do 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 diff --git a/spec/ruby/core/env/shift_spec.rb b/spec/ruby/core/env/shift_spec.rb index 1b92e5d1e4..52bd0f9139 100644 --- a/spec/ruby/core/env/shift_spec.rb +++ b/spec/ruby/core/env/shift_spec.rb @@ -33,15 +33,15 @@ describe "ENV.shift" do Encoding.default_internal = nil pair = ENV.shift - pair.first.encoding.should equal(ENVSpecs.encoding) - pair.last.encoding.should equal(ENVSpecs.encoding) + 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 Encoding.default_internal = Encoding::IBM437 pair = ENV.shift - pair.first.encoding.should equal(Encoding::IBM437) - pair.last.encoding.should equal(Encoding::IBM437) + pair.first.encoding.should.equal?(Encoding::IBM437) + pair.last.encoding.should.equal?(Encoding::IBM437) end end 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 index e3b6020391..4c0416547d 100644 --- a/spec/ruby/core/env/slice_spec.rb +++ b/spec/ruby/core/env/slice_spec.rb @@ -21,7 +21,17 @@ describe "ENV.slice" 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") + -> { ENV.slice(Object.new) }.should.raise(TypeError, "no implicit conversion of Object into String") end end diff --git a/spec/ruby/core/env/to_a_spec.rb b/spec/ruby/core/env/to_a_spec.rb index 39e3877b48..2b1649281f 100644 --- a/spec/ruby/core/env/to_a_spec.rb +++ b/spec/ruby/core/env/to_a_spec.rb @@ -6,7 +6,10 @@ describe "ENV.to_a" do a = ENV.to_a a.is_a?(Array).should == true a.size.should == ENV.size - ENV.each_pair { |k, v| a.should include([k, v])} + 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 diff --git a/spec/ruby/core/env/to_h_spec.rb b/spec/ruby/core/env/to_h_spec.rb index 3c4a92aa57..7e0fef2120 100644 --- a/spec/ruby/core/env/to_h_spec.rb +++ b/spec/ruby/core/env/to_h_spec.rb @@ -18,6 +18,18 @@ describe "ENV.to_h" do 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 } @@ -26,17 +38,17 @@ describe "ENV.to_h" do 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/) + end.should.raise(ArgumentError, /element has wrong array length/) -> do ENV.to_h { |k, v| [k] } - end.should raise_error(ArgumentError, /element has wrong array length/) + end.should.raise(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.should.raise(TypeError, /wrong element type String/) end it "coerces returned pair to Array with #to_ary" do @@ -52,7 +64,7 @@ describe "ENV.to_h" do -> do ENV.to_h { |k| x } - end.should raise_error(TypeError, /wrong element type MockObject/) + end.should.raise(TypeError, /wrong element type MockObject/) end end end diff --git a/spec/ruby/core/env/values_at_spec.rb b/spec/ruby/core/env/values_at_spec.rb index 338680e820..4a733f4ed5 100644 --- a/spec/ruby/core/env/values_at_spec.rb +++ b/spec/ruby/core/env/values_at_spec.rb @@ -33,6 +33,6 @@ describe "ENV.values_at" do 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") + -> { ENV.values_at("foo", Object.new) }.should.raise(TypeError, "no implicit conversion of Object into String") end end |
