diff options
Diffstat (limited to 'spec/ruby/core/env')
54 files changed, 1563 insertions, 0 deletions
diff --git a/spec/ruby/core/env/assoc_spec.rb b/spec/ruby/core/env/assoc_spec.rb new file mode 100644 index 0000000000..c7a388db75 --- /dev/null +++ b/spec/ruby/core/env/assoc_spec.rb @@ -0,0 +1,31 @@ +require_relative '../../spec_helper' + +describe "ENV.assoc" do + before :each do + @foo = ENV["foo"] + end + + after :each do + ENV["foo"] = @foo + end + + it "returns an array of the key and value of the environment variable with the given key" do + ENV["foo"] = "bar" + ENV.assoc("foo").should == ["foo", "bar"] + end + + it "returns nil if no environment variable with the given key exists" do + ENV.assoc("foo").should == nil + end + + it "returns the key element coerced with #to_str" do + ENV["foo"] = "bar" + k = mock('key') + 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 new file mode 100644 index 0000000000..48b034ba1d --- /dev/null +++ b/spec/ruby/core/env/clear_spec.rb @@ -0,0 +1,20 @@ +require_relative '../../spec_helper' + +describe "ENV.clear" do + it "deletes all environment variables" do + orig = ENV.to_hash + begin + 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 + # the shell sets them up before it runs any command. + # + # Thusly, you can ONLY test this by asking through ENV itself. + ENV.size.should == 0 + ensure + ENV.replace orig + end + end + +end 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 new file mode 100644 index 0000000000..d2de51c225 --- /dev/null +++ b/spec/ruby/core/env/delete_if_spec.rb @@ -0,0 +1,54 @@ +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.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 equal(ENV) + end + + it "returns an Enumerator if no block given" do + ENV.delete_if.should be_an_instance_of(Enumerator) + end + + it "deletes pairs through enumerator" do + enum = ENV.delete_if + 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 +end diff --git a/spec/ruby/core/env/delete_spec.rb b/spec/ruby/core/env/delete_spec.rb new file mode 100644 index 0000000000..f28ac97911 --- /dev/null +++ b/spec/ruby/core/env/delete_spec.rb @@ -0,0 +1,55 @@ +require_relative '../../spec_helper' + +describe "ENV.delete" do + before :each do + @saved_foo = ENV["foo"] + end + after :each do + ENV["foo"] = @saved_foo + end + + it "removes the variable from the environment" do + ENV["foo"] = "bar" + ENV.delete("foo") + ENV["foo"].should == nil + end + + it "returns the previous value" do + ENV["foo"] = "bar" + 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 new file mode 100644 index 0000000000..0efcb09900 --- /dev/null +++ b/spec/ruby/core/env/each_key_spec.rb @@ -0,0 +1,34 @@ +require_relative '../../spec_helper' +require_relative '../enumerable/shared/enumeratorized' + +describe "ENV.each_key" do + + it "returns each key" do + e = [] + orig = ENV.to_hash + begin + 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") + ensure + ENV.replace orig + end + end + + it "returns an Enumerator if called without a block" do + 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 + ENV.each_key do |key| + key.encoding.should == Encoding.find('locale') + end + end + + it_behaves_like :enumeratorized_with_origin_size, :each_key, ENV +end diff --git a/spec/ruby/core/env/each_pair_spec.rb b/spec/ruby/core/env/each_pair_spec.rb new file mode 100644 index 0000000000..2d7ed5faa0 --- /dev/null +++ b/spec/ruby/core/env/each_pair_spec.rb @@ -0,0 +1,6 @@ +require_relative 'spec_helper' +require_relative 'shared/each' + +describe "ENV.each_pair" do + it_behaves_like :env_each, :each_pair +end diff --git a/spec/ruby/core/env/each_spec.rb b/spec/ruby/core/env/each_spec.rb new file mode 100644 index 0000000000..d1e06f55b6 --- /dev/null +++ b/spec/ruby/core/env/each_spec.rb @@ -0,0 +1,6 @@ +require_relative 'spec_helper' +require_relative 'shared/each' + +describe "ENV.each" do + it_behaves_like :env_each, :each +end diff --git a/spec/ruby/core/env/each_value_spec.rb b/spec/ruby/core/env/each_value_spec.rb new file mode 100644 index 0000000000..cc3c9ebfb8 --- /dev/null +++ b/spec/ruby/core/env/each_value_spec.rb @@ -0,0 +1,34 @@ +require_relative 'spec_helper' +require_relative '../enumerable/shared/enumeratorized' + +describe "ENV.each_value" do + + it "returns each value" do + e = [] + orig = ENV.to_hash + begin + 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") + ensure + ENV.replace orig + end + end + + it "returns an Enumerator if called without a block" do + 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.should.be_locale_env + end + end + + it_behaves_like :enumeratorized_with_origin_size, :each_value, ENV +end diff --git a/spec/ruby/core/env/element_reference_spec.rb b/spec/ruby/core/env/element_reference_spec.rb new file mode 100644 index 0000000000..66a9bc9690 --- /dev/null +++ b/spec/ruby/core/env/element_reference_spec.rb @@ -0,0 +1,76 @@ +# encoding: binary +require_relative '../../spec_helper' +require_relative 'fixtures/common' + +describe "ENV.[]" do + before :each do + @variable = "returns_only_frozen_values" + end + + after :each do + ENV.delete @variable + end + + it "returns nil if the variable isn't found" do + ENV["this_var_is_never_set"].should == nil + end + + it "returns only frozen values" do + ENV[@variable] = "a non-frozen string" + 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 + it "looks up values case-insensitively" do + ENV[@variable] = "bar" + ENV[@variable.upcase].should == "bar" + end + end +end + +describe "ENV.[]" do + before :each do + @variable = "env_element_reference_encoding_specs" + + @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 + + ENV.delete @variable + end + + it "uses the locale encoding if Encoding.default_internal is nil" do + Encoding.default_internal = nil + + locale = ENVSpecs.encoding + locale = Encoding::BINARY if locale == Encoding::US_ASCII + ENV[@variable] = "\xC3\xB8" + ENV[@variable].encoding.should == locale + end + + it "transcodes from the locale encoding to Encoding.default_internal if set" do + # We cannot reliably know the locale encoding, so we merely check that + # the result string has the expected encoding. + ENV[@variable] = "" + Encoding.default_internal = Encoding::IBM437 + + ENV[@variable].encoding.should equal(Encoding::IBM437) + end +end diff --git a/spec/ruby/core/env/element_set_spec.rb b/spec/ruby/core/env/element_set_spec.rb new file mode 100644 index 0000000000..26dfee1ade --- /dev/null +++ b/spec/ruby/core/env/element_set_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/store' + +describe "ENV.[]=" do + it_behaves_like :env_store, :[]= +end diff --git a/spec/ruby/core/env/empty_spec.rb b/spec/ruby/core/env/empty_spec.rb new file mode 100644 index 0000000000..afeb406a9e --- /dev/null +++ b/spec/ruby/core/env/empty_spec.rb @@ -0,0 +1,23 @@ +require_relative '../../spec_helper' + +describe "ENV.empty?" do + + it "returns true if the Environment is empty" do + if ENV.keys.size > 0 + ENV.should_not.empty? + end + orig = ENV.to_hash + begin + ENV.clear + ENV.should.empty? + ensure + ENV.replace orig + end + end + + it "returns false if not empty" do + if ENV.keys.size > 0 + 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 new file mode 100644 index 0000000000..2c5d7cc3a0 --- /dev/null +++ b/spec/ruby/core/env/fetch_spec.rb @@ -0,0 +1,63 @@ +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" + 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") + end + + context "when the key is not found" do + it_behaves_like :key_error, -> obj, key { obj.fetch(key) }, ENV + + it "formats the object with #inspect in the KeyError message" do + -> { + ENV.fetch('foo') + }.should raise_error(KeyError, 'key not found: "foo"') + end + end + + it "provides the given default parameter" do + 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("foo") { |k| "wanted #{k}" }.should == "wanted foo" + end + + it "does not insist that the block return a String" do + ENV.fetch("foo") { |k| k.to_sym }.should == :foo + end + + it "warns on block and default parameter given" do + -> do + ENV.fetch("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["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 new file mode 100644 index 0000000000..52f8b79a0b --- /dev/null +++ b/spec/ruby/core/env/filter_spec.rb @@ -0,0 +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 +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/has_key_spec.rb b/spec/ruby/core/env/has_key_spec.rb new file mode 100644 index 0000000000..798668105d --- /dev/null +++ b/spec/ruby/core/env/has_key_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/include' + +describe "ENV.has_key?" do + it_behaves_like :env_include, :has_key? +end diff --git a/spec/ruby/core/env/has_value_spec.rb b/spec/ruby/core/env/has_value_spec.rb new file mode 100644 index 0000000000..a2bf3eb877 --- /dev/null +++ b/spec/ruby/core/env/has_value_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/value' + +describe "ENV.has_value?" do + it_behaves_like :env_value, :has_value? +end diff --git a/spec/ruby/core/env/include_spec.rb b/spec/ruby/core/env/include_spec.rb new file mode 100644 index 0000000000..3975f095ac --- /dev/null +++ b/spec/ruby/core/env/include_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/include' + +describe "ENV.include?" do + it_behaves_like :env_include, :include? +end diff --git a/spec/ruby/core/env/inspect_spec.rb b/spec/ruby/core/env/inspect_spec.rb new file mode 100644 index 0000000000..7dd92b120f --- /dev/null +++ b/spec/ruby/core/env/inspect_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../spec_helper' + +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.delete "foo" + end + +end diff --git a/spec/ruby/core/env/invert_spec.rb b/spec/ruby/core/env/invert_spec.rb new file mode 100644 index 0000000000..c095374d95 --- /dev/null +++ b/spec/ruby/core/env/invert_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../spec_helper' + +describe "ENV.invert" do + before :each do + ENV["foo"] = "bar" + end + + after :each do + ENV.delete "foo" + end + + it "returns a hash with ENV.keys as the values and vice versa" do + ENV.invert["bar"].should == "foo" + ENV["foo"].should == "bar" + end +end diff --git a/spec/ruby/core/env/keep_if_spec.rb b/spec/ruby/core/env/keep_if_spec.rb new file mode 100644 index 0000000000..64b6a207d0 --- /dev/null +++ b/spec/ruby/core/env/keep_if_spec.rb @@ -0,0 +1,54 @@ +require_relative '../../spec_helper' +require_relative '../enumerable/shared/enumeratorized' + +describe "ENV.keep_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 false" do + 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 equal(ENV) + end + + it "returns an Enumerator if no block given" do + ENV.keep_if.should be_an_instance_of(Enumerator) + end + + it "deletes pairs through enumerator" do + enum = ENV.keep_if + 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 +end diff --git a/spec/ruby/core/env/key_spec.rb b/spec/ruby/core/env/key_spec.rb new file mode 100644 index 0000000000..cf70286409 --- /dev/null +++ b/spec/ruby/core/env/key_spec.rb @@ -0,0 +1,39 @@ +require_relative '../../spec_helper' +require_relative 'shared/include' + +describe "ENV.key?" do + it_behaves_like :env_include, :key? +end + +describe "ENV.key" do + before :each do + @saved_foo = ENV["foo"] + end + + after :each do + ENV["foo"] = @saved_foo + end + + it "returns the index associated with the passed value" do + ENV["foo"] = "bar" + ENV.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 new file mode 100644 index 0000000000..b074a8f7c7 --- /dev/null +++ b/spec/ruby/core/env/keys_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' + +describe "ENV.keys" do + + 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 + ENV.keys.each do |key| + key.encoding.should == Encoding.find('locale') + end + end +end diff --git a/spec/ruby/core/env/length_spec.rb b/spec/ruby/core/env/length_spec.rb new file mode 100644 index 0000000000..c6f9062892 --- /dev/null +++ b/spec/ruby/core/env/length_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/length' + +describe "ENV.length" do + it_behaves_like :env_length, :length +end diff --git a/spec/ruby/core/env/member_spec.rb b/spec/ruby/core/env/member_spec.rb new file mode 100644 index 0000000000..9119022ae5 --- /dev/null +++ b/spec/ruby/core/env/member_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/include' + +describe "ENV.member?" do + it_behaves_like :env_include, :member? +end diff --git a/spec/ruby/core/env/merge_spec.rb b/spec/ruby/core/env/merge_spec.rb new file mode 100644 index 0000000000..f10662cf79 --- /dev/null +++ b/spec/ruby/core/env/merge_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/update' + +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 new file mode 100644 index 0000000000..ab9fe68088 --- /dev/null +++ b/spec/ruby/core/env/rassoc_spec.rb @@ -0,0 +1,42 @@ +require_relative '../../spec_helper' + +describe "ENV.rassoc" do + before :each do + @foo = ENV["foo"] + @baz = ENV["baz"] + end + + after :each do + 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 + ENV["foo"] = "bar" + 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 + + it "returns the value element coerced with #to_str" do + ENV["foo"] = "bar" + v = mock('value') + 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 new file mode 100644 index 0000000000..3782e4b727 --- /dev/null +++ b/spec/ruby/core/env/rehash_spec.rb @@ -0,0 +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 new file mode 100644 index 0000000000..6a9794925d --- /dev/null +++ b/spec/ruby/core/env/reject_spec.rb @@ -0,0 +1,101 @@ +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" } + ENV["foo"].should == nil + end + + it "rejects entries based on value" do + ENV["foo"] = "bar" + ENV.reject! { |k, v| v == "bar" } + ENV["foo"].should == nil + end + + it "returns itself or nil" do + ENV.reject! { false }.should == nil + ENV["foo"] = "bar" + 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) + ENV["foo"].should == nil + end + + it "doesn't raise if empty" do + orig = ENV.to_hash + begin + ENV.clear + -> { ENV.reject! }.should_not raise_error(LocalJumpError) + ensure + ENV.replace orig + end + end + + it_behaves_like :enumeratorized_with_origin_size, :reject!, ENV +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" } + e["foo"].should == nil + ENV["foo"].should == "bar" + ENV["foo"] = nil + end + + it "rejects entries based on value" do + ENV["foo"] = "bar" + e = ENV.reject { |k, v| v == "bar" } + e["foo"].should == nil + ENV["foo"].should == "bar" + ENV["foo"] = nil + end + + it "returns a Hash" do + ENV.reject { false }.should be_kind_of(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.each { |k, v| k == "foo"} + ENV["foo"] = nil + end + + it "doesn't raise if empty" do + orig = ENV.to_hash + begin + ENV.clear + -> { ENV.reject }.should_not raise_error(LocalJumpError) + ensure + ENV.replace orig + end + end + + it_behaves_like :enumeratorized_with_origin_size, :reject, ENV +end diff --git a/spec/ruby/core/env/replace_spec.rb b/spec/ruby/core/env/replace_spec.rb new file mode 100644 index 0000000000..9fc67643d1 --- /dev/null +++ b/spec/ruby/core/env/replace_spec.rb @@ -0,0 +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.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/select_spec.rb b/spec/ruby/core/env/select_spec.rb new file mode 100644 index 0000000000..c3a76f4434 --- /dev/null +++ b/spec/ruby/core/env/select_spec.rb @@ -0,0 +1,13 @@ +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 +end + +describe "ENV.select" do + it_behaves_like :env_select, :select + it_behaves_like :enumeratorized_with_origin_size, :select, ENV +end diff --git a/spec/ruby/core/env/shared/each.rb b/spec/ruby/core/env/shared/each.rb new file mode 100644 index 0000000000..d901b854c4 --- /dev/null +++ b/spec/ruby/core/env/shared/each.rb @@ -0,0 +1,65 @@ +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 be_an_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 new file mode 100644 index 0000000000..70aa555301 --- /dev/null +++ b/spec/ruby/core/env/shared/include.rb @@ -0,0 +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 + 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_error(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 new file mode 100644 index 0000000000..6d788a3f4a --- /dev/null +++ b/spec/ruby/core/env/shared/length.rb @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000000..75ba112a32 --- /dev/null +++ b/spec/ruby/core/env/shared/select.rb @@ -0,0 +1,61 @@ +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 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" } + 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 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 new file mode 100644 index 0000000000..d6265c66a5 --- /dev/null +++ b/spec/ruby/core/env/shared/store.rb @@ -0,0 +1,60 @@ +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 be_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_error(TypeError, "no implicit conversion of Object into String") + end + + it "raises TypeError when the value is not coercible to String" do + -> { ENV.send(@method, "foo", Object.new) }.should raise_error(TypeError, "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) + end + + it "raises Errno::EINVAL when the key is an empty string" do + -> { 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 + ENV.send(@method, "foo=", nil) + ENV.key?("foo=").should be_false + end +end diff --git a/spec/ruby/core/env/shared/to_hash.rb b/spec/ruby/core/env/shared/to_hash.rb new file mode 100644 index 0000000000..a0d4d7ce69 --- /dev/null +++ b/spec/ruby/core/env/shared/to_hash.rb @@ -0,0 +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" + end + + it "uses the locale encoding for keys" do + ENV.send(@method).keys.each {|k| k.should.be_locale_env } + end + + it "uses the locale encoding for values" do + 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 new file mode 100644 index 0000000000..e1b1c9c290 --- /dev/null +++ b/spec/ruby/core/env/shared/update.rb @@ -0,0 +1,104 @@ +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_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 + end +end diff --git a/spec/ruby/core/env/shared/value.rb b/spec/ruby/core/env/shared/value.rb new file mode 100644 index 0000000000..c2b5025465 --- /dev/null +++ b/spec/ruby/core/env/shared/value.rb @@ -0,0 +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 + 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/shift_spec.rb b/spec/ruby/core/env/shift_spec.rb new file mode 100644 index 0000000000..1b92e5d1e4 --- /dev/null +++ b/spec/ruby/core/env/shift_spec.rb @@ -0,0 +1,47 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/common' + +describe "ENV.shift" do + before :each do + @orig = ENV.to_hash + @external = Encoding.default_external + @internal = Encoding.default_internal + + Encoding.default_external = Encoding::BINARY + ENV.replace({"FOO"=>"BAR"}) + end + + after :each do + Encoding.default_external = @external + Encoding.default_internal = @internal + 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(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) + end +end diff --git a/spec/ruby/core/env/size_spec.rb b/spec/ruby/core/env/size_spec.rb new file mode 100644 index 0000000000..7c8072481e --- /dev/null +++ b/spec/ruby/core/env/size_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/length' + +describe "ENV.size" do + 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/store_spec.rb b/spec/ruby/core/env/store_spec.rb new file mode 100644 index 0000000000..b4700e0a96 --- /dev/null +++ b/spec/ruby/core/env/store_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/store' + +describe "ENV.store" do + it_behaves_like :env_store, :store +end diff --git a/spec/ruby/core/env/to_a_spec.rb b/spec/ruby/core/env/to_a_spec.rb new file mode 100644 index 0000000000..2b1649281f --- /dev/null +++ b/spec/ruby/core/env/to_a_spec.rb @@ -0,0 +1,21 @@ +require_relative 'spec_helper' + +describe "ENV.to_a" do + + it "returns the ENV as an array" do + a = ENV.to_a + a.is_a?(Array).should == true + 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.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 new file mode 100644 index 0000000000..58ea2d2030 --- /dev/null +++ b/spec/ruby/core/env/to_h_spec.rb @@ -0,0 +1,70 @@ +require_relative 'spec_helper' +require_relative 'shared/to_hash' + +describe "ENV.to_h" do + it_behaves_like :env_to_hash, :to_h + + 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 new file mode 100644 index 0000000000..306572c353 --- /dev/null +++ b/spec/ruby/core/env/to_hash_spec.rb @@ -0,0 +1,6 @@ +require_relative 'spec_helper' +require_relative 'shared/to_hash' + +describe "ENV.to_hash" do + it_behaves_like :env_to_hash, :to_hash +end diff --git a/spec/ruby/core/env/to_s_spec.rb b/spec/ruby/core/env/to_s_spec.rb new file mode 100644 index 0000000000..0bd92cf217 --- /dev/null +++ b/spec/ruby/core/env/to_s_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' + +describe "ENV.to_s" do + it "returns \"ENV\"" do + ENV.to_s.should == "ENV" + end +end diff --git a/spec/ruby/core/env/update_spec.rb b/spec/ruby/core/env/update_spec.rb new file mode 100644 index 0000000000..95a8a2eb49 --- /dev/null +++ b/spec/ruby/core/env/update_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/update' + +describe "ENV.update" do + it_behaves_like :env_update, :update +end diff --git a/spec/ruby/core/env/value_spec.rb b/spec/ruby/core/env/value_spec.rb new file mode 100644 index 0000000000..906e86ab39 --- /dev/null +++ b/spec/ruby/core/env/value_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/value' + +describe "ENV.value?" do + it_behaves_like :env_value, :value? +end diff --git a/spec/ruby/core/env/values_at_spec.rb b/spec/ruby/core/env/values_at_spec.rb new file mode 100644 index 0000000000..338680e820 --- /dev/null +++ b/spec/ruby/core/env/values_at_spec.rb @@ -0,0 +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 corresponding to the given keys" do + ENV["foo"] = "oof" + ENV["bar"] = "rab" + ENV.values_at("bar", "foo").should == ["rab", "oof"] + 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 == 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 new file mode 100644 index 0000000000..71bc877d31 --- /dev/null +++ b/spec/ruby/core/env/values_spec.rb @@ -0,0 +1,14 @@ +require_relative 'spec_helper' + +describe "ENV.values" do + + it "returns an array of the values" do + ENV.values.should == ENV.to_hash.values + end + + it "uses the locale encoding" do + ENV.values.each do |value| + value.should.be_locale_env + end + end +end |
