Project

General

Profile

Misc #16235 ยป t.diff

View differences:

spec/ruby/core/env/assoc_spec.rb
1 1
require_relative '../../spec_helper'
2 2

  
3 3
describe "ENV.assoc" do
4
  before :each do
5
    reserve_names("foo")
6
  end
7

  
4 8
  after :each do
5
    ENV.delete("foo")
9
    release_names
6 10
  end
7 11

  
8
  it "returns an array of the key and value of the environment variable with the given key" do
12
  it "returns an array of the name and value of the environment variable with the given name" do
9 13
    ENV["foo"] = "bar"
10 14
    ENV.assoc("foo").should == ["foo", "bar"]
11 15
  end
12 16

  
13
  it "returns nil if no environment variable with the given key exists" do
17
  it "returns nil if no environment variable with the given name exists" do
14 18
    ENV.assoc("foo").should == nil
15 19
  end
16 20

  
17
  it "returns the key element coerced with #to_str" do
21
  it "coerces a non-String name by calling :to_str" do
18 22
    ENV["foo"] = "bar"
19
    k = mock('key')
20
    k.should_receive(:to_str).and_return("foo")
21
    ENV.assoc(k).should == ["foo", "bar"]
23
    mock_object = mock_to_str(:foo)
24
    ENV.assoc(mock_object).should == ["foo", "bar"]
25
  end
26

  
27
  it "raises TypeError if the argument is not a String and does not respond to :to_str" do
28
    -> { ENV.assoc(Object.new) }.should raise_error(TypeError)
22 29
  end
23 30
end
spec/ruby/spec_helper.rb
3 3
dir = "fixtures/code"
4 4
CODE_LOADING_DIR = use_realpath ? File.realpath(dir, root) : File.expand_path(dir, root)
5 5

  
6
# Reserve names.
7
def reserve_names(*names)
8
  @reserved = names
9
  @reserved.each  do |name|
10
    fail "Name #{name} is already in use" if ENV.include?(name)
11
  end
12
end
13

  
14
# Release reserved names.
15
def release_names
16
  @reserved.each do |name|
17
    ENV.delete(name)
18
  end
19
end
20

  
21
# Mock object for calling to_str.
22
def mock_to_str(s)
23
  mock_object = mock('name')
24
  mock_object.should_receive(:to_str).and_return(s.to_s)
25
  mock_object
26
end
27

  
6 28
# Enable Thread.report_on_exception by default to catch thread errors earlier
7 29
if Thread.respond_to? :report_on_exception=
8 30
  Thread.report_on_exception = true