diff options
Diffstat (limited to 'spec/ruby/core/env')
48 files changed, 949 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..fb10a52b3c --- /dev/null +++ b/spec/ruby/core/env/assoc_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.assoc" do + after :each do + ENV.delete("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 +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..c184926cc2 --- /dev/null +++ b/spec/ruby/core/env/clear_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.clear" do + it "deletes all environment variables" do + orig = ENV.to_hash + begin + ENV.clear + + # 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/delete_if_spec.rb b/spec/ruby/core/env/delete_if_spec.rb new file mode 100644 index 0000000000..9a8220ae7d --- /dev/null +++ b/spec/ruby/core/env/delete_if_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +describe "ENV.delete_if" do + it "deletes pairs if the block returns true" do + ENV["foo"] = "bar" + ENV.delete_if { |k, v| k == "foo" } + ENV["foo"].should == nil + end + + it "returns ENV even if nothing deleted" do + ENV.delete_if { false }.should_not == nil + 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 + ENV["foo"] = "bar" + enum = ENV.delete_if + enum.each { |k, v| k == "foo" } + ENV["foo"].should == nil + 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..e02adf963f --- /dev/null +++ b/spec/ruby/core/env/delete_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.delete" do + after :each do + ENV.delete("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 "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 +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..82721cdb96 --- /dev/null +++ b/spec/ruby/core/env/each_key_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +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 } + e.should include("1") + e.should include("2") + ensure + ENV.replace orig + end + end + + it "returns an Enumerator if called without a block" do + ENV.each_key.should be_an_instance_of(Enumerator) + 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..255ccd86c5 --- /dev/null +++ b/spec/ruby/core/env/each_pair_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/each.rb', __FILE__) + +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..2424c5e4e0 --- /dev/null +++ b/spec/ruby/core/env/each_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/each.rb', __FILE__) + +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..070a1d2cb9 --- /dev/null +++ b/spec/ruby/core/env/each_value_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +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 } + e.should include("3") + e.should include("4") + ensure + ENV.replace orig + end + end + + it "returns an Enumerator if called without a block" do + ENV.each_value.should be_an_instance_of(Enumerator) + end + + it "uses the locale encoding" do + ENV.each_value do |value| + value.encoding.should == Encoding.find('locale') + 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..2e6402dd28 --- /dev/null +++ b/spec/ruby/core/env/element_reference_spec.rb @@ -0,0 +1,66 @@ +# -*- encoding: ascii-8bit -*- +require File.expand_path('../../../spec_helper', __FILE__) + +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].frozen?.should == true + end + + platform_is :windows do + it "looks up values case-insensitively" do + ENV[@variable] = "bar" + ENV[@variable.upcase].should == "bar" + end + end +end + +with_feature :encoding do + describe "ENV.[]" do + before :each do + @variable = "env_element_reference_encoding_specs" + + @external = Encoding.default_external + @internal = Encoding.default_internal + + Encoding.default_external = Encoding::ASCII_8BIT + 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 = Encoding.find('locale') + locale = Encoding::ASCII_8BIT 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 +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..a80cd0c51e --- /dev/null +++ b/spec/ruby/core/env/element_set_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/store.rb', __FILE__) + +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..fa02985a6e --- /dev/null +++ b/spec/ruby/core/env/empty_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.empty?" do + + it "returns true if the Environment is empty" do + if ENV.keys.size > 0 + ENV.empty?.should == false + end + orig = ENV.to_hash + begin + ENV.clear + ENV.empty?.should == true + ensure + ENV.replace orig + end + end + + it "returns false if not empty" do + if ENV.keys.size > 0 + ENV.empty?.should == false + end + 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..708ee91c39 --- /dev/null +++ b/spec/ruby/core/env/fetch_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.fetch" do + it "returns a value" do + ENV["foo"] = "bar" + ENV.fetch("foo").should == "bar" + ENV.delete "foo" + end + + it "raises a TypeError if the key is not a String" do + lambda { ENV.fetch :should_never_be_set }.should raise_error(TypeError) + end + + it "raises a KeyError if the key is not found" do + lambda { ENV.fetch "should_never_be_set" }.should raise_error(KeyError) + end + + it "provides the given default parameter" do + ENV.fetch("should_never_be_set", "default").should == "default" + end + + it "provides a default value from a block" do + ENV.fetch("should_never_be_set") { |k| "wanted #{k}" }.should == "wanted should_never_be_set" + end + + it "warns on block and default parameter given" do + lambda do + ENV.fetch("should_never_be_set", "default") { 1 }.should == 1 + end.should complain(/block supersedes default value argument/) + end + + it "uses the locale encoding" do + ENV.fetch(ENV.keys.first).encoding.should == Encoding.find('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..8da2d94265 --- /dev/null +++ b/spec/ruby/core/env/has_key_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/include.rb', __FILE__) + +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..76980a8df4 --- /dev/null +++ b/spec/ruby/core/env/has_value_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/value.rb', __FILE__) + +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..4a716fee85 --- /dev/null +++ b/spec/ruby/core/env/include_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/include.rb', __FILE__) + +describe "ENV.include?" do + it_behaves_like(:env_include, :include?) +end diff --git a/spec/ruby/core/env/index_spec.rb b/spec/ruby/core/env/index_spec.rb new file mode 100644 index 0000000000..95009b3558 --- /dev/null +++ b/spec/ruby/core/env/index_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/key.rb', __FILE__) + +describe "ENV.index" do + it_behaves_like(:env_key, :index) +end diff --git a/spec/ruby/core/env/indexes_spec.rb b/spec/ruby/core/env/indexes_spec.rb new file mode 100644 index 0000000000..14fb93ef07 --- /dev/null +++ b/spec/ruby/core/env/indexes_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../spec_helper', __FILE__) diff --git a/spec/ruby/core/env/indices_spec.rb b/spec/ruby/core/env/indices_spec.rb new file mode 100644 index 0000000000..14fb93ef07 --- /dev/null +++ b/spec/ruby/core/env/indices_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../spec_helper', __FILE__) diff --git a/spec/ruby/core/env/inspect_spec.rb b/spec/ruby/core/env/inspect_spec.rb new file mode 100644 index 0000000000..9c4273e188 --- /dev/null +++ b/spec/ruby/core/env/inspect_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +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..42170230db --- /dev/null +++ b/spec/ruby/core/env/invert_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +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..c5bbc3dc05 --- /dev/null +++ b/spec/ruby/core/env/keep_if_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +describe "ENV.keep_if" do + before :each do + ENV["foo"] = "bar" + end + + after :each do + ENV.delete "foo" + end + + it "deletes pairs if the block returns false" do + ENV.keep_if { |k, v| k != "foo" } + ENV["foo"].should == nil + end + + it "returns ENV even if nothing deleted" do + ENV.keep_if { true }.should_not == nil + 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| k != "foo" } + ENV["foo"].should == nil + 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..b653b1b1a5 --- /dev/null +++ b/spec/ruby/core/env/key_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/include.rb', __FILE__) +require File.expand_path('../shared/key.rb', __FILE__) + +describe "ENV.key?" do + it_behaves_like(:env_include, :key?) +end + +describe "ENV.key" do + it_behaves_like(:env_key, :key) +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..d79919b79d --- /dev/null +++ b/spec/ruby/core/env/keys_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.keys" do + + it "returns all the keys" do + ENV.keys.sort.should == ENV.to_hash.keys.sort + 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..83d1b58c74 --- /dev/null +++ b/spec/ruby/core/env/length_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/length.rb', __FILE__) + +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..25aa71e973 --- /dev/null +++ b/spec/ruby/core/env/member_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/include.rb', __FILE__) + +describe "ENV.member?" do + it_behaves_like(:env_include, :member?) +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..3a86e7e158 --- /dev/null +++ b/spec/ruby/core/env/rassoc_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.rassoc" do + after :each do + ENV.delete("foo") + 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 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 +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..14fb93ef07 --- /dev/null +++ b/spec/ruby/core/env/rehash_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../spec_helper', __FILE__) diff --git a/spec/ruby/core/env/reject_spec.rb b/spec/ruby/core/env/reject_spec.rb new file mode 100644 index 0000000000..0da84425b6 --- /dev/null +++ b/spec/ruby/core/env/reject_spec.rb @@ -0,0 +1,77 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +describe "ENV.reject!" do + 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 == ENV + ENV["foo"].should == nil + end + + it "returns an Enumerator if called without a block" do + ENV.reject!.should be_an_instance_of(Enumerator) + end + + it "doesn't raise if empty" do + orig = ENV.to_hash + begin + ENV.clear + lambda { 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 + 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.reject.should be_an_instance_of(Enumerator) + end + + it "doesn't raise if empty" do + orig = ENV.to_hash + begin + ENV.clear + lambda { 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..0c11e173ac --- /dev/null +++ b/spec/ruby/core/env/replace_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.replace" do + + it "replaces ENV with a Hash" do + ENV["foo"] = "bar" + e = ENV.reject { |k, v| k == "foo" } + e["baz"] = "bam" + ENV.replace e + ENV["foo"].should == nil + ENV["baz"].should == "bam" + ENV.delete "baz" + 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..8261ff593a --- /dev/null +++ b/spec/ruby/core/env/select_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +describe "ENV.select!" do + it "removes environment variables for which the block returns true" 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 be_an_instance_of(Enumerator) + end + + it_behaves_like :enumeratorized_with_origin_size, :select!, ENV +end + +describe "ENV.select" do + it "returns a Hash of names and values for which block return true" do + ENV["foo"] = "bar" + ENV.select { |k, v| k == "foo" }.should == {"foo" => "bar"} + ENV.delete "foo" + end + + it "returns an Enumerator when no block is given" do + ENV.select.should be_an_instance_of(Enumerator) + end + + 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..494fd5cee1 --- /dev/null +++ b/spec/ruby/core/env/shared/each.rb @@ -0,0 +1,65 @@ +require File.expand_path('../../../enumerable/shared/enumeratorized', __FILE__) + +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] } + 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 + ENV.send(@method).should be_an_instance_of(Enumerator) + end + + before :all do + @object = ENV + end + it_should_behave_like :enumeratorized_with_origin_size + + with_feature :encoding do + describe "with encoding" do + before :each do + @external = Encoding.default_external + @internal = Encoding.default_internal + + Encoding.default_external = Encoding::ASCII_8BIT + + @locale_encoding = Encoding.find "locale" + 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.encoding.should equal(@locale_encoding) + value.encoding.should equal(@locale_encoding) + 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 +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..8d8311dcf2 --- /dev/null +++ b/spec/ruby/core/env/shared/include.rb @@ -0,0 +1,11 @@ +describe :env_include, shared: true do + it "returns true if ENV has the key" do + ENV["foo"] = "bar" + ENV.send(@method, "foo").should == true + ENV.delete "foo" + end + + it "returns false if ENV doesn't include the key" do + ENV.send(@method, "should_never_be_set").should == false + end +end diff --git a/spec/ruby/core/env/shared/key.rb b/spec/ruby/core/env/shared/key.rb new file mode 100644 index 0000000000..5e6c21840f --- /dev/null +++ b/spec/ruby/core/env/shared/key.rb @@ -0,0 +1,15 @@ +describe :env_key, shared: true do + it "needs to be reviewed for completeness" + + it "returns the index associated with the passed value" do + ENV["foo"] = "bar" + ENV.send(@method, "bar").should == "foo" + ENV.delete "foo" + end + + it "returns nil if the passed value is not found" do + ENV.send(@method, "should_never_be_set").should be_nil + end +end + + diff --git a/spec/ruby/core/env/shared/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/store.rb b/spec/ruby/core/env/shared/store.rb new file mode 100644 index 0000000000..4949ca8c73 --- /dev/null +++ b/spec/ruby/core/env/shared/store.rb @@ -0,0 +1,56 @@ +describe :env_store, shared: true do + after :each do + ENV.delete("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 + lambda { ENV.send(@method, Object.new, "bar") }.should raise_error(TypeError) + end + + it "raises TypeError when the value is not coercible to String" do + lambda { ENV.send(@method, "foo", Object.new) }.should raise_error(TypeError) + end + + it "raises Errno::EINVAL when the key contains the '=' character" do + lambda { ENV.send(@method, "foo=", "bar") }.should raise_error(Errno::EINVAL) + end + + it "raises Errno::EINVAL when the key is an empty string" do + lambda { ENV.send(@method, "", "bar") }.should raise_error(Errno::EINVAL) + 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..3bfbc415f7 --- /dev/null +++ b/spec/ruby/core/env/shared/to_hash.rb @@ -0,0 +1,22 @@ +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["foo"].should == "bar" + ENV.delete "foo" + end + + it "uses the locale encoding for keys" do + ENV.send(@method).keys.all? {|k| k.encoding == Encoding.find('locale') }.should be_true + end + + it "uses the locale encoding for values" do + ENV.send(@method).values.all? {|v| v.encoding == Encoding.find('locale') }.should be_true + end + + it "duplicates the ENV when converting to a Hash" do + h = ENV.send(@method) + h.object_id.should_not == ENV.object_id + 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..d9ee90f12d --- /dev/null +++ b/spec/ruby/core/env/shared/value.rb @@ -0,0 +1,11 @@ +describe :env_value, shared: true do + it "returns true if ENV has the value" do + ENV["foo"] = "bar" + ENV.send(@method, "bar").should == true + ENV["foo"] = nil + end + + it "returns false if ENV doesn't have the value" do + ENV.send(@method, "this_value_should_never_exist").should == false + 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..bae6a17ba0 --- /dev/null +++ b/spec/ruby/core/env/shift_spec.rb @@ -0,0 +1,59 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.shift" do + it "returns a pair and deletes it" do + ENV.empty?.should == false + orig = ENV.to_hash + begin + pair = ENV.shift + ENV.has_key?(pair.first).should == false + ensure + ENV.replace orig + end + ENV.has_key?(pair.first).should == true + end + + it "returns nil if ENV.empty?" do + orig = ENV.to_hash + begin + ENV.clear + ENV.shift.should == nil + ensure + ENV.replace orig + end + end +end + +with_feature :encoding do + describe "ENV.shift" do + before :each do + @orig = ENV.to_hash + @external = Encoding.default_external + @internal = Encoding.default_internal + + Encoding.default_external = Encoding::ASCII_8BIT + end + + after :each do + Encoding.default_external = @external + Encoding.default_internal = @internal + ENV.replace @orig + end + + it "uses the locale encoding if Encoding.default_internal is nil" do + Encoding.default_internal = nil + + pair = ENV.shift + pair.first.encoding.should equal(Encoding.find("locale")) + pair.last.encoding.should equal(Encoding.find("locale")) + 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 +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..882ceac485 --- /dev/null +++ b/spec/ruby/core/env/size_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/length.rb', __FILE__) + +describe "ENV.size" do + it_behaves_like(:env_length, :size) +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..1ee5ce020e --- /dev/null +++ b/spec/ruby/core/env/store_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/store.rb', __FILE__) + +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..ffb66b8767 --- /dev/null +++ b/spec/ruby/core/env/to_a_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.to_a" do + + it "returns the ENV as an array" do + ENV["foo"] = "bar" + a = ENV.to_a + a.is_a?(Array).should == true + a.find { |e| e.first == "foo" }.should == ["foo", "bar"] + ENV.delete "foo" + end + + it "returns the entries in the locale encoding" do + ENV.to_a.each do |key, value| + key.encoding.should == Encoding.find('locale') + value.encoding.should == Encoding.find('locale') + 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..d0fef5382b --- /dev/null +++ b/spec/ruby/core/env/to_h_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/to_hash.rb', __FILE__) + +describe "ENV.to_hash" do + it_behaves_like(:env_to_hash, :to_h) +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..3362fa9307 --- /dev/null +++ b/spec/ruby/core/env/to_hash_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/to_hash.rb', __FILE__) + +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..10aca09723 --- /dev/null +++ b/spec/ruby/core/env/to_s_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +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..7ebfbb313d --- /dev/null +++ b/spec/ruby/core/env/update_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.update" do + + it "adds the parameter hash to ENV" do + ENV["foo"].should == nil + ENV.update "foo" => "bar" + ENV["foo"].should == "bar" + ENV.delete "foo" + end + + it "yields key, the old value and the new value when replacing entries" do + ENV.update "foo" => "bar" + ENV["foo"].should == "bar" + ENV.update("foo" => "boo") do |key, old, new| + key.should == "foo" + old.should == "bar" + new.should == "boo" + "rab" + end + ENV["foo"].should == "rab" + ENV.delete "foo" + end + +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..c7eb7c5376 --- /dev/null +++ b/spec/ruby/core/env/value_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/value.rb', __FILE__) + +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..efc7de2a05 --- /dev/null +++ b/spec/ruby/core/env/values_at_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.values_at" do + + it "returns an array of the values referenced by the parameters as keys" do + ENV["foo"] = "oof" + ENV["bar"] = "rab" + ENV.values_at.should == [] + ENV.values_at("bar", "foo").should == ["rab", "oof"] + ENV.delete "foo" + ENV.delete "bar" + end + + it "uses the locale encoding" do + ENV.values_at(ENV.keys.first).first.encoding.should == Encoding.find('locale') + 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..3a620bdb8b --- /dev/null +++ b/spec/ruby/core/env/values_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ENV.values" do + + it "returns an array of the values" do + orig = ENV.to_hash + begin + ENV.replace "a" => "b", "c" => "d" + a = ENV.values + a.sort.should == ["b", "d"] + ensure + ENV.replace orig + end + end + + it "uses the locale encoding" do + ENV.values.each do |value| + value.encoding.should == Encoding.find('locale') + end + end +end |