summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLee Jarvis <ljjarvis@gmail.com>2014-12-17 08:26:50 +0000
committerLee Jarvis <ljjarvis@gmail.com>2014-12-17 08:26:50 +0000
commitf77101e3fef89fd70ae0db3708ce623b7d8d32c1 (patch)
tree54635808da506585eefe8de2fdda53a3f17498cd /test
parent3bd5ec4eabf14dc89a14b285bd5ccf1c0a45d6a5 (diff)
downloadslop-f77101e3fef89fd70ae0db3708ce623b7d8d32c1.tar.gz
Add NullOption and default to using it
A NullOption is one whos return value we don't care about. For example, you might just want a `--version` option which simply prints the version and exits. In this case, having a `true` value in `to_hash` is really just noise. We probably don't care about it, using NullOption discards it. I think using this for Options#on makes sense because this is logical: opts.on '--version' do puts VERSION exit end Rather than: opts.add '--version' do puts VERSION exit end And defaulting to a StringOption. This also means you have to be explicit about adding such an option, which is a good thing.
Diffstat (limited to 'test')
-rw-r--r--test/options_test.rb26
-rw-r--r--test/types_test.rb15
2 files changed, 28 insertions, 13 deletions
diff --git a/test/options_test.rb b/test/options_test.rb
index 57083ac..5b12f7b 100644
--- a/test/options_test.rb
+++ b/test/options_test.rb
@@ -5,34 +5,34 @@ describe Slop::Options do
@options = Slop::Options.new
end
- describe "#add" do
- it "defaults to string type" do
- assert_kind_of Slop::StringOption, @options.add("--foo")
+ describe "#on" do
+ it "defaults to null type" do
+ assert_kind_of Slop::NullOption, @options.on("--foo")
end
it "accepts custom types" do
module Slop; class FooOption < Option; end; end
- assert_kind_of Slop::FooOption, @options.add("--foo", type: :foo)
+ assert_kind_of Slop::FooOption, @options.on("--foo", type: :foo)
end
it "adds multiple flags" do
- option = @options.add("-f", "-F", "--foo")
+ option = @options.on("-f", "-F", "--foo")
assert_equal %w(-f -F --foo), option.flags
end
it "accepts a trailing description" do
- option = @options.add("--foo", "fooey")
+ option = @options.on("--foo", "fooey")
assert_equal "fooey", option.desc
end
it "adds the option" do
- option = @options.add("--foo")
+ option = @options.on("--foo")
assert_equal [option], @options.to_a
end
it "raises an error when a duplicate flag is used" do
- @options.add("--foo")
- assert_raises(ArgumentError) { @options.add("--foo") }
+ @options.on("--foo")
+ assert_raises(ArgumentError) { @options.on("--foo") }
end
end
@@ -60,19 +60,19 @@ describe Slop::Options do
end
it "aligns option strings" do
- @options.add "-f", "--foo", "fooey"
- @options.add "-s", "short"
+ @options.on "-f", "--foo", "fooey"
+ @options.on "-s", "short"
assert_match(/^ -f, --foo fooey/, @options.to_s)
assert_match(/^ -s short/, @options.to_s)
end
it "can use a custom prefix" do
- @options.add "-f", "--foo"
+ @options.on "-f", "--foo"
assert_match(/^ -f, --foo/, @options.to_s(prefix: " "))
end
it "ignores options with help: false" do
- @options.add "-x", "something", help: false
+ @options.on "-x", "something", help: false
refute_match(/something/, @options.to_s)
end
end
diff --git a/test/types_test.rb b/test/types_test.rb
index 44846b1..3696126 100644
--- a/test/types_test.rb
+++ b/test/types_test.rb
@@ -61,3 +61,18 @@ describe Slop::ArrayOption do
end
end
+describe Slop::NullOption do
+ before do
+ @options = Slop::Options.new
+ @version = @options.null('--version')
+ @result = @options.parse %w(--version)
+ end
+
+ it 'has a return value of true' do
+ assert_equal true, @result[:version]
+ end
+
+ it 'is not included in to_hash' do
+ assert_equal({}, @result.to_hash)
+ end
+end