diff --git a/spec/ruby/core/env/assoc_spec.rb b/spec/ruby/core/env/assoc_spec.rb index 853eca79a5..e544116967 100644 --- a/spec/ruby/core/env/assoc_spec.rb +++ b/spec/ruby/core/env/assoc_spec.rb @@ -1,23 +1,30 @@ require_relative '../../spec_helper' describe "ENV.assoc" do + before :each do + reserve_names("foo") + end + after :each do - ENV.delete("foo") + release_names end - it "returns an array of the key and value of the environment variable with the given key" do + it "returns an array of the name and value of the environment variable with the given name" do ENV["foo"] = "bar" ENV.assoc("foo").should == ["foo", "bar"] end - it "returns nil if no environment variable with the given key exists" do + it "returns nil if no environment variable with the given name exists" do ENV.assoc("foo").should == nil end - it "returns the key element coerced with #to_str" do + it "coerces a non-String name by calling :to_str" do ENV["foo"] = "bar" - k = mock('key') - k.should_receive(:to_str).and_return("foo") - ENV.assoc(k).should == ["foo", "bar"] + mock_object = mock_to_str(:foo) + ENV.assoc(mock_object).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) end end diff --git a/spec/ruby/spec_helper.rb b/spec/ruby/spec_helper.rb index c38965d3c5..408efb2e23 100644 --- a/spec/ruby/spec_helper.rb +++ b/spec/ruby/spec_helper.rb @@ -3,6 +3,28 @@ dir = "fixtures/code" CODE_LOADING_DIR = use_realpath ? File.realpath(dir, root) : File.expand_path(dir, root) +# Reserve names. +def reserve_names(*names) + @reserved = names + @reserved.each do |name| + fail "Name #{name} is already in use" if ENV.include?(name) + end +end + +# Release reserved names. +def release_names + @reserved.each do |name| + ENV.delete(name) + end +end + +# Mock object for calling to_str. +def mock_to_str(s) + mock_object = mock('name') + mock_object.should_receive(:to_str).and_return(s.to_s) + mock_object +end + # Enable Thread.report_on_exception by default to catch thread errors earlier if Thread.respond_to? :report_on_exception= Thread.report_on_exception = true