1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
This commit is contained in:
Benoit Daloze 2019-10-26 20:53:01 +02:00
parent 3eb0d50c0b
commit 664e96b1de
42 changed files with 484 additions and 117 deletions

View file

@ -17,7 +17,11 @@ describe :env_each, shared: true do
end
it "returns an Enumerator if called without a block" do
ENV.send(@method).should be_an_instance_of(Enumerator)
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

View file

@ -1,11 +1,23 @@
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
ENV.delete "foo"
end
it "returns false if ENV doesn't include the key" do
ENV.send(@method, "should_never_be_set").should == false
ENV.delete("foo")
ENV.send(@method, "foo").should == false
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

View file

@ -1,11 +1,23 @@
describe :env_key, shared: true 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.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
ENV.delete("foo")
ENV.send(@method, "foo").should be_nil
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

View file

@ -1,16 +1,38 @@
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" }
ENV.delete "foo"
end
it "returns an Enumerator when no block is given" do
ENV.send(@method).should be_an_instance_of(Enumerator)
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" }
@ -29,4 +51,11 @@ describe :env_select!, shared: true do
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

View file

@ -1,6 +1,10 @@
describe :env_store, shared: true do
before :each do
@saved_foo = ENV["foo"]
end
after :each do
ENV.delete("foo")
ENV["foo"] = @saved_foo
end
it "sets the environment variable to the given value" do
@ -34,11 +38,11 @@ describe :env_store, shared: true do
end
it "raises TypeError when the key is not coercible to String" do
-> { ENV.send(@method, Object.new, "bar") }.should raise_error(TypeError)
-> { 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)
-> { 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

View file

@ -1,21 +1,66 @@
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" do
ENV["foo"].should == nil
ENV.send @method, "foo" => "bar"
ENV["foo"].should == "bar"
ENV.delete "foo"
ENV.send @method, {"foo" => "0", "bar" => "1"}
ENV["foo"].should == "0"
ENV["bar"].should == "1"
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 entries" do
ENV.send @method, "foo" => "bar"
ENV["foo"].should == "bar"
ENV.send(@method, "foo" => "boo") do |key, old, new|
key.should == "foo"
old.should == "bar"
new.should == "boo"
"rab"
ENV.send @method, {"foo" => "0", "bar" => "3"}
a = []
ENV.send @method, {"foo" => "1", "bar" => "4"} do |key, old, new|
a << [key, old, new]
(new.to_i + 1).to_s
end
ENV["foo"].should == "2"
ENV["bar"].should == "5"
a[0].should == ["foo", "0", "1"]
a[1].should == ["bar", "3", "4"]
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 "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
ENV["foo"].should == "rab"
ENV.delete "foo"
end
end

View file

@ -1,11 +1,22 @@
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
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
ENV.send(@method, "foo").should == false
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