Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Add specs to cover #23 - :required options!

- Small improvements to the env specs
  • Loading branch information...
commit 2862082fa2509e7f7cd079c2b8f23fdb2c76e0c4 1 parent d878c74
Jordan Sissel authored
Showing with 63 additions and 5 deletions.
  1. +47 −4 spec/clamp/command_spec.rb
  2. +16 −1 spec/clamp/option_spec.rb
51 spec/clamp/command_spec.rb
View
@@ -119,6 +119,7 @@ def default_port
end
it "should use the default if neither flag nor env var are present" do
+ ENV.delete("PORT")
@command.parse([])
@command.port.should == 4321
end
@@ -171,10 +172,12 @@ def default_port
Clamp::Option::Parsing::TRUTHY_ENVIRONMENT_VALUES.should == %w(1 yes enable on true)
end
- it "should use an env value other than '1' to mean false" do
- ENV["ENABLE"] = "0"
- @command.parse([])
- @command.enable?.should == false
+ it "should use an env value other than truthy ones to mean false" do
+ [nil, "0", "no", "whatever"].each do |val|
+ ENV["ENABLE"] = val
+ @command.parse([])
+ @command.enable?.should == false
+ end
end
it "should use the the flag value if present (instead of env)" do
@@ -193,6 +196,46 @@ def default_port
end
+ describe "with :required value" do
+
+ before do
+ @command.class.option "--port", "PORT", "port to listen on", :required => true
+ end
+
+ it "should fail if a required option is not provided" do
+ expect { @command.parse([]) }.to raise_error(Clamp::UsageError)
+ end
+
+ it "should succeed if a required option is provided" do
+ @command.parse(["--port", "12345"])
+ end
+
+ end
+
+ describe "with :required value with :env" do
+
+ before do
+ @command.class.option "--port", "PORT", "port to listen on", :required => true, :env => "PORT"
+ end
+
+ it "should fail if a required option is not provided" do
+ ENV.delete("PORT")
+ expect { @command.parse([]) }.to raise_error(Clamp::UsageError)
+ end
+
+ it "should succeed if a required option is provided via arguments" do
+ ENV.delete("PORT")
+ @command.parse(["--port", "12345"])
+ end
+
+ it "should succeed if a required option is provided via env" do
+ ENV["PORT"] = "12345"
+ @command.parse([])
+ end
+
+ end
+
+
describe "with a block" do
before do
17 spec/clamp/option_spec.rb
View
@@ -145,5 +145,20 @@
end
end
-
+
+ describe "a required option" do
+ it "rejects :default" do
+ expect do
+ Clamp::Option.new("--key-file", "FILE", "SSH identity",
+ :required => true, :default => "hello")
+ end.to raise_error(ArgumentError)
+ end
+
+ it "rejects :flag options" do
+ expect do
+ Clamp::Option.new("--awesome", :flag, "Be awesome?", :required => true)
+ end.to raise_error(ArgumentError)
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.