diff options
Diffstat (limited to 'spec/ruby/core/env')
23 files changed, 373 insertions, 399 deletions
diff --git a/spec/ruby/core/env/each_pair_spec.rb b/spec/ruby/core/env/each_pair_spec.rb index 2d7ed5faa0..1acd2fbb00 100644 --- a/spec/ruby/core/env/each_pair_spec.rb +++ b/spec/ruby/core/env/each_pair_spec.rb @@ -1,6 +1,63 @@ require_relative 'spec_helper' -require_relative 'shared/each' +require_relative '../enumerable/shared/enumeratorized' describe "ENV.each_pair" do - it_behaves_like :env_each, :each_pair + it "returns each pair" do + orig = ENV.to_hash + e = [] + begin + ENV.clear + ENV["foo"] = "bar" + ENV["baz"] = "boo" + ENV.each_pair { |k, v| e << [k, v] }.should.equal?(ENV) + e.should.include?(["foo", "bar"]) + e.should.include?(["baz", "boo"]) + ensure + ENV.replace orig + end + end + + it "returns an Enumerator if called without a block" do + enum = ENV.each_pair + enum.should.instance_of?(Enumerator) + enum.each do |name, value| + ENV[name].should == value + end + end + + it_behaves_like :enumeratorized_with_origin_size, :each_pair, ENV + + describe "with encoding" do + before :each do + @external = Encoding.default_external + @internal = Encoding.default_internal + + Encoding.default_external = Encoding::BINARY + end + + after :each do + Encoding.default_external = @external + Encoding.default_internal = @internal + end + + it "uses the locale encoding when Encoding.default_internal is nil" do + Encoding.default_internal = nil + + ENV.each_pair do |key, value| + key.should.be_locale_env + value.should.be_locale_env + end + end + + it "transcodes from the locale encoding to Encoding.default_internal if set" do + Encoding.default_internal = internal = Encoding::IBM437 + + ENV.each_pair do |key, value| + key.encoding.should.equal?(internal) + if value.ascii_only? + value.encoding.should.equal?(internal) + end + end + end + end end diff --git a/spec/ruby/core/env/each_spec.rb b/spec/ruby/core/env/each_spec.rb index d1e06f55b6..166a0b4fc8 100644 --- a/spec/ruby/core/env/each_spec.rb +++ b/spec/ruby/core/env/each_spec.rb @@ -1,6 +1,7 @@ require_relative 'spec_helper' -require_relative 'shared/each' describe "ENV.each" do - it_behaves_like :env_each, :each + it "is an alias of ENV.each_pair" do + ENV.method(:each).should == ENV.method(:each_pair) + end end diff --git a/spec/ruby/core/env/element_set_spec.rb b/spec/ruby/core/env/element_set_spec.rb index 26dfee1ade..ca5d468009 100644 --- a/spec/ruby/core/env/element_set_spec.rb +++ b/spec/ruby/core/env/element_set_spec.rb @@ -1,6 +1,62 @@ require_relative '../../spec_helper' -require_relative 'shared/store' describe "ENV.[]=" do - it_behaves_like :env_store, :[]= + before :each do + @saved_foo = ENV["foo"] + end + + after :each do + ENV["foo"] = @saved_foo + end + + it "sets the environment variable to the given value" do + ENV["foo"] = "bar" + ENV["foo"].should == "bar" + end + + it "returns the value" do + value = "bar" + ENV.send(:[]=, "foo", value).should.equal?(value) + end + + it "deletes the environment variable when the value is nil" do + ENV["foo"] = "bar" + ENV["foo"] = nil + ENV.key?("foo").should == false + end + + it "coerces the key argument with #to_str" do + k = mock("key") + k.should_receive(:to_str).and_return("foo") + ENV[k] = "bar" + ENV["foo"].should == "bar" + end + + it "coerces the value argument with #to_str" do + v = mock("value") + v.should_receive(:to_str).and_return("bar") + ENV["foo"] = v + ENV["foo"].should == "bar" + end + + it "raises TypeError when the key is not coercible to String" do + -> { ENV[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["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["foo="] = "bar" }.should.raise(Errno::EINVAL) + end + + it "raises Errno::EINVAL when the key is an empty string" do + -> { ENV[""] = "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["foo="] = nil + ENV.key?("foo=").should == false + end end diff --git a/spec/ruby/core/env/filter_spec.rb b/spec/ruby/core/env/filter_spec.rb index 52f8b79a0b..54997bfda1 100644 --- a/spec/ruby/core/env/filter_spec.rb +++ b/spec/ruby/core/env/filter_spec.rb @@ -1,13 +1,13 @@ require_relative '../../spec_helper' -require_relative '../enumerable/shared/enumeratorized' -require_relative 'shared/select' describe "ENV.filter!" do - it_behaves_like :env_select!, :filter! - it_behaves_like :enumeratorized_with_origin_size, :filter!, ENV + it "is an alias of ENV.select!" do + ENV.method(:filter!).should == ENV.method(:select!) + end end describe "ENV.filter" do - it_behaves_like :env_select, :filter - it_behaves_like :enumeratorized_with_origin_size, :filter, ENV + it "is an alias of ENV.select" do + ENV.method(:filter).should == ENV.method(:select) + end end diff --git a/spec/ruby/core/env/has_key_spec.rb b/spec/ruby/core/env/has_key_spec.rb index 798668105d..7feeec8dfa 100644 --- a/spec/ruby/core/env/has_key_spec.rb +++ b/spec/ruby/core/env/has_key_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/include' describe "ENV.has_key?" do - it_behaves_like :env_include, :has_key? + it "is an alias of ENV.include?" do + ENV.method(:has_key?).should == ENV.method(:include?) + end end diff --git a/spec/ruby/core/env/has_value_spec.rb b/spec/ruby/core/env/has_value_spec.rb index a2bf3eb877..b76ec0dc5d 100644 --- a/spec/ruby/core/env/has_value_spec.rb +++ b/spec/ruby/core/env/has_value_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/value' describe "ENV.has_value?" do - it_behaves_like :env_value, :has_value? + it "is an alias of ENV.value?" do + ENV.method(:has_value?).should == ENV.method(:value?) + end end diff --git a/spec/ruby/core/env/include_spec.rb b/spec/ruby/core/env/include_spec.rb index 3975f095ac..8e68aa3bfd 100644 --- a/spec/ruby/core/env/include_spec.rb +++ b/spec/ruby/core/env/include_spec.rb @@ -1,6 +1,32 @@ require_relative '../../spec_helper' -require_relative 'shared/include' describe "ENV.include?" do - it_behaves_like :env_include, :include? + 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.include?( "foo").should == true + end + + it "returns false if ENV doesn't include the key" do + ENV.delete("foo") + ENV.include?( "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.include?( k).should == true + end + + it "raises TypeError if the argument is not a String and does not respond to #to_str" do + -> { ENV.include?( Object.new) }.should.raise(TypeError, "no implicit conversion of Object into String") + end end diff --git a/spec/ruby/core/env/key_spec.rb b/spec/ruby/core/env/key_spec.rb index 677cf35216..56f5f609a7 100644 --- a/spec/ruby/core/env/key_spec.rb +++ b/spec/ruby/core/env/key_spec.rb @@ -1,8 +1,9 @@ require_relative '../../spec_helper' -require_relative 'shared/include' describe "ENV.key?" do - it_behaves_like :env_include, :key? + it "is an alias of ENV.include?" do + ENV.method(:key?).should == ENV.method(:include?) + end end describe "ENV.key" do diff --git a/spec/ruby/core/env/length_spec.rb b/spec/ruby/core/env/length_spec.rb index c6f9062892..6baac6f7a4 100644 --- a/spec/ruby/core/env/length_spec.rb +++ b/spec/ruby/core/env/length_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/length' describe "ENV.length" do - it_behaves_like :env_length, :length + it "is an alias of ENV.size" do + ENV.method(:length).should == ENV.method(:size) + end end diff --git a/spec/ruby/core/env/member_spec.rb b/spec/ruby/core/env/member_spec.rb index 9119022ae5..f062d41190 100644 --- a/spec/ruby/core/env/member_spec.rb +++ b/spec/ruby/core/env/member_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/include' describe "ENV.member?" do - it_behaves_like :env_include, :member? + it "is an alias of ENV.include?" do + ENV.method(:member?).should == ENV.method(:include?) + end end diff --git a/spec/ruby/core/env/merge_spec.rb b/spec/ruby/core/env/merge_spec.rb index f10662cf79..78231afbb2 100644 --- a/spec/ruby/core/env/merge_spec.rb +++ b/spec/ruby/core/env/merge_spec.rb @@ -1,6 +1,106 @@ require_relative '../../spec_helper' -require_relative 'shared/update' describe "ENV.merge!" do - it_behaves_like :env_update, :merge! + 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.merge!("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.merge!({"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.merge!({"foo" => "0", "bar" => "1"}).should.equal?(ENV) + end + + it "yields key, the old value and the new value when replacing an entry" do + ENV.merge!({"foo" => "0", "bar" => "3"}) + a = [] + ENV.merge!({"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.merge!({"foo" => "0", "bar" => "3"}) + ENV.merge!({"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.merge!({"foo" => "0"}) + ENV.merge!("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.merge!({"foo" => "0"}) + ENV.merge!("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.merge!({"foo" => "0", "bar" => "1"}){}.should.equal?(ENV) + end + + it "raises TypeError when a name is not coercible to String" do + -> { ENV.merge!(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.merge!("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.merge!("foo=" => "bar") }.should.raise(Errno::EINVAL) + end + + it "raises Errno::EINVAL when a name is an empty string" do + -> { ENV.merge!("" => "bar") }.should.raise(Errno::EINVAL) + end + + it "updates good data preceding an error" do + ENV["foo"] = "0" + begin + ENV.merge!({"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.merge!({Object.new => "1", "foo" => "2"}) + rescue TypeError + ensure + ENV["foo"].should == "0" + end + end end diff --git a/spec/ruby/core/env/select_spec.rb b/spec/ruby/core/env/select_spec.rb index c3a76f4434..2b92d61551 100644 --- a/spec/ruby/core/env/select_spec.rb +++ b/spec/ruby/core/env/select_spec.rb @@ -1,13 +1,68 @@ require_relative '../../spec_helper' require_relative '../enumerable/shared/enumeratorized' -require_relative 'shared/select' describe "ENV.select!" do - it_behaves_like :env_select!, :select! it_behaves_like :enumeratorized_with_origin_size, :select!, ENV + + 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 false" do + ENV["foo"] = "bar" + ENV.select! { |k, v| k != "foo" } + ENV["foo"].should == nil + end + + it "returns self if any changes were made" do + ENV["foo"] = "bar" + (ENV.select! { |k, v| k != "foo" }).should == ENV + end + + it "returns nil if no changes were made" do + (ENV.select! { true }).should == nil + end + + it "returns an Enumerator if called without a block" do + ENV.select!.should.instance_of?(Enumerator) + end + + it "selects via the enumerator" do + enum = ENV.select! + ENV["foo"] = "bar" + enum.each { |k, v| k != "foo" } + ENV["foo"].should == nil + end end describe "ENV.select" do - it_behaves_like :env_select, :select it_behaves_like :enumeratorized_with_origin_size, :select, ENV + + 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 returns true" do + ENV["foo"] = "bar" + (ENV.select { |k, v| k == "foo" }).should == { "foo" => "bar" } + end + + it "returns an Enumerator when no block is given" do + enum = ENV.select + enum.should.instance_of?(Enumerator) + end + + it "selects via the enumerator" do + enum = ENV.select + ENV["foo"] = "bar" + enum.each { |k, v| k == "foo" }.should == { "foo" => "bar"} + end end diff --git a/spec/ruby/core/env/shared/each.rb b/spec/ruby/core/env/shared/each.rb deleted file mode 100644 index 0661ca924c..0000000000 --- a/spec/ruby/core/env/shared/each.rb +++ /dev/null @@ -1,65 +0,0 @@ -require_relative '../../enumerable/shared/enumeratorized' - -describe :env_each, shared: true do - it "returns each pair" do - orig = ENV.to_hash - e = [] - begin - 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"]) - ensure - ENV.replace orig - end - end - - it "returns an Enumerator if called without a block" do - enum = ENV.send(@method) - enum.should.instance_of?(Enumerator) - enum.each do |name, value| - ENV[name].should == value - end - end - - before :all do - @object = ENV - end - it_should_behave_like :enumeratorized_with_origin_size - - describe "with encoding" do - before :each do - @external = Encoding.default_external - @internal = Encoding.default_internal - - Encoding.default_external = Encoding::BINARY - end - - after :each do - Encoding.default_external = @external - Encoding.default_internal = @internal - end - - it "uses the locale encoding when Encoding.default_internal is nil" do - Encoding.default_internal = nil - - ENV.send(@method) do |key, value| - key.should.be_locale_env - value.should.be_locale_env - end - end - - it "transcodes from the locale encoding to Encoding.default_internal if set" do - Encoding.default_internal = internal = Encoding::IBM437 - - ENV.send(@method) do |key, value| - key.encoding.should.equal?(internal) - if value.ascii_only? - value.encoding.should.equal?(internal) - end - end - end - end -end diff --git a/spec/ruby/core/env/shared/include.rb b/spec/ruby/core/env/shared/include.rb deleted file mode 100644 index ceca02e3eb..0000000000 --- a/spec/ruby/core/env/shared/include.rb +++ /dev/null @@ -1,30 +0,0 @@ -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 - end - - it "returns false if ENV doesn't include the key" do - 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(TypeError, "no implicit conversion of Object into String") - end -end diff --git a/spec/ruby/core/env/shared/length.rb b/spec/ruby/core/env/shared/length.rb deleted file mode 100644 index 6d788a3f4a..0000000000 --- a/spec/ruby/core/env/shared/length.rb +++ /dev/null @@ -1,13 +0,0 @@ -describe :env_length, shared: true do - it "returns the number of ENV entries" do - orig = ENV.to_hash - begin - ENV.clear - ENV["foo"] = "bar" - ENV["baz"] = "boo" - ENV.send(@method).should == 2 - ensure - ENV.replace orig - end - end -end diff --git a/spec/ruby/core/env/shared/select.rb b/spec/ruby/core/env/shared/select.rb deleted file mode 100644 index 8ec648a637..0000000000 --- a/spec/ruby/core/env/shared/select.rb +++ /dev/null @@ -1,61 +0,0 @@ -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" } - end - - it "returns an Enumerator when no block is given" do - enum = ENV.send(@method) - enum.should.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" } - ENV["foo"].should == nil - end - - it "returns self if any changes were made" do - ENV["foo"] = "bar" - (ENV.send(@method) { |k, v| k != "foo" }).should == ENV - end - - it "returns nil if no changes were made" do - (ENV.send(@method) { true }).should == nil - end - - it "returns an Enumerator if called without a block" do - ENV.send(@method).should.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 deleted file mode 100644 index 388208a8ac..0000000000 --- a/spec/ruby/core/env/shared/store.rb +++ /dev/null @@ -1,60 +0,0 @@ -describe :env_store, shared: true do - before :each do - @saved_foo = ENV["foo"] - end - - after :each do - ENV["foo"] = @saved_foo - end - - it "sets the environment variable to the given value" do - ENV.send(@method, "foo", "bar") - ENV["foo"].should == "bar" - end - - it "returns the value" do - value = "bar" - 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 == false - end - - it "coerces the key argument with #to_str" do - k = mock("key") - k.should_receive(:to_str).and_return("foo") - ENV.send(@method, k, "bar") - ENV["foo"].should == "bar" - end - - it "coerces the value argument with #to_str" do - v = mock("value") - v.should_receive(:to_str).and_return("bar") - ENV.send(@method, "foo", v) - ENV["foo"].should == "bar" - end - - it "raises TypeError when the key is not coercible to String" do - -> { 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(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(Errno::EINVAL) - end - - it "raises Errno::EINVAL when the key is an empty string" do - -> { 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 == false - end -end diff --git a/spec/ruby/core/env/shared/update.rb b/spec/ruby/core/env/shared/update.rb deleted file mode 100644 index 112cc2505d..0000000000 --- a/spec/ruby/core/env/shared/update.rb +++ /dev/null @@ -1,104 +0,0 @@ -describe :env_update, shared: true do - before :each do - @saved_foo = ENV["foo"] - @saved_bar = ENV["bar"] - end - - after :each do - ENV["foo"] = @saved_foo - ENV["bar"] = @saved_bar - end - - it "adds the parameter hash to ENV, 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(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(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(Errno::EINVAL) - end - - it "raises Errno::EINVAL when a name is an empty string" do - -> { ENV.send(@method, "" => "bar") }.should.raise(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 - end -end diff --git a/spec/ruby/core/env/shared/value.rb b/spec/ruby/core/env/shared/value.rb deleted file mode 100644 index c2b5025465..0000000000 --- a/spec/ruby/core/env/shared/value.rb +++ /dev/null @@ -1,29 +0,0 @@ -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 - end - - it "returns false if ENV doesn't have the value" 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 -end diff --git a/spec/ruby/core/env/size_spec.rb b/spec/ruby/core/env/size_spec.rb index 7c8072481e..9c6de20df6 100644 --- a/spec/ruby/core/env/size_spec.rb +++ b/spec/ruby/core/env/size_spec.rb @@ -1,6 +1,15 @@ require_relative '../../spec_helper' -require_relative 'shared/length' describe "ENV.size" do - it_behaves_like :env_length, :size + it "returns the number of ENV entries" do + orig = ENV.to_hash + begin + ENV.clear + ENV["foo"] = "bar" + ENV["baz"] = "boo" + ENV.size.should == 2 + ensure + ENV.replace orig + end + end end diff --git a/spec/ruby/core/env/store_spec.rb b/spec/ruby/core/env/store_spec.rb index b4700e0a96..a5fd7e1e26 100644 --- a/spec/ruby/core/env/store_spec.rb +++ b/spec/ruby/core/env/store_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/store' describe "ENV.store" do - it_behaves_like :env_store, :store + it "is an alias of ENV.[]=" do + ENV.method(:store).should == ENV.method(:[]=) + end end diff --git a/spec/ruby/core/env/update_spec.rb b/spec/ruby/core/env/update_spec.rb index 95a8a2eb49..44d05d617f 100644 --- a/spec/ruby/core/env/update_spec.rb +++ b/spec/ruby/core/env/update_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/update' describe "ENV.update" do - it_behaves_like :env_update, :update + it "is an alias of ENV.merge!" do + ENV.method(:update).should == ENV.method(:merge!) + end end diff --git a/spec/ruby/core/env/value_spec.rb b/spec/ruby/core/env/value_spec.rb index 906e86ab39..c732cfbd15 100644 --- a/spec/ruby/core/env/value_spec.rb +++ b/spec/ruby/core/env/value_spec.rb @@ -1,6 +1,31 @@ require_relative '../../spec_helper' -require_relative 'shared/value' describe "ENV.value?" do - it_behaves_like :env_value, :value? + 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.value?("bar").should == true + end + + it "returns false if ENV doesn't have the value" do + ENV.value?("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.value?(v).should == true + end + + it "returns nil if the argument is not a String and does not respond to #to_str" do + ENV.value?(Object.new).should == nil + end end |
