summaryrefslogtreecommitdiff
path: root/spec/ruby/core/env/shared
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/env/shared')
-rw-r--r--spec/ruby/core/env/shared/each.rb65
-rw-r--r--spec/ruby/core/env/shared/include.rb11
-rw-r--r--spec/ruby/core/env/shared/key.rb15
-rw-r--r--spec/ruby/core/env/shared/length.rb13
-rw-r--r--spec/ruby/core/env/shared/store.rb56
-rw-r--r--spec/ruby/core/env/shared/to_hash.rb22
-rw-r--r--spec/ruby/core/env/shared/value.rb11
7 files changed, 193 insertions, 0 deletions
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