Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion spec/std/env_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe "ENV" do
(ENV["FOO"] = "1").should eq("1")
ENV["FOO"].should eq("1")
ENV["FOO"]?.should eq("1")
ensure
ENV.delete("FOO")
end

it "sets to nil (same as delete)" do
Expand All @@ -28,12 +30,16 @@ describe "ENV" do
it "sets to empty string" do
(ENV["FOO_EMPTY"] = "").should eq ""
ENV["FOO_EMPTY"]?.should eq ""
ensure
ENV.delete("FOO_EMPTY")
end

it "does has_key?" do
ENV["FOO"] = "1"
ENV.has_key?("BAR").should be_false
ENV.has_key?("NON_EXISTENT").should be_false
ENV.has_key?("FOO").should be_true
ensure
ENV.delete("FOO")
end

it "deletes a key" do
Expand All @@ -47,13 +53,19 @@ describe "ENV" do
%w(FOO BAR).each { |k| ENV.keys.should_not contain(k) }
ENV["FOO"] = ENV["BAR"] = "1"
%w(FOO BAR).each { |k| ENV.keys.should contain(k) }
ensure
ENV.delete("FOO")
ENV.delete("BAR")
end

it "does .values" do
[1, 2].each { |i| ENV.values.should_not contain("SOMEVALUE_#{i}") }
ENV["FOO"] = "SOMEVALUE_1"
ENV["BAR"] = "SOMEVALUE_2"
[1, 2].each { |i| ENV.values.should contain("SOMEVALUE_#{i}") }
ensure
ENV.delete("FOO")
ENV.delete("BAR")
end

describe "[]=" do
Expand All @@ -80,25 +92,33 @@ describe "ENV" do
it "fetches with one argument" do
ENV["1"] = "2"
ENV.fetch("1").should eq("2")
ensure
ENV.delete("1")
end

it "fetches with default value" do
ENV["1"] = "2"
ENV.fetch("1", "3").should eq("2")
ENV.fetch("2", "3").should eq("3")
ensure
ENV.delete("1")
end

it "fetches with block" do
ENV["1"] = "2"
ENV.fetch("1") { |k| k + "block" }.should eq("2")
ENV.fetch("2") { |k| k + "block" }.should eq("2block")
ensure
ENV.delete("1")
end

it "fetches and raises" do
ENV["1"] = "2"
expect_raises KeyError, "Missing ENV key: \"2\"" do
ENV.fetch("2")
end
ensure
ENV.delete("1")
end
end

Expand All @@ -118,5 +138,15 @@ describe "ENV" do
"TEST_UNICODE_1" => "bar\u{d7ff}\u{10000}",
"TEST_UNICODE_2" => "\u{1234}",
})
ensure
ENV.delete("TEST_UNICODE_1")
ENV.delete("TEST_UNICODE_2")
end

it "#to_h" do
ENV["FOO"] = "foo"
ENV.to_h["FOO"].should eq "foo"
ensure
ENV.delete("FOO")
end
end
2 changes: 1 addition & 1 deletion src/env.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module ENV
# ```
def self.each
Crystal::System::Env.each do |key, value|
yield key, value
yield({key, value})
end
end

Expand Down