summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLee Jarvis <ljjarvis@gmail.com>2013-08-29 14:56:36 +0100
committerLee Jarvis <ljjarvis@gmail.com>2013-08-29 14:56:36 +0100
commita35dbf55ba7b69aeb2dc5c21e72fa35be262ca63 (patch)
treec6ce67f5cc45c0b6af16bf5abd3e9c1bf1e0a58b /test
parent8e7dca4c0f6ecba4eb21f8ca44dff8293a8bbbd6 (diff)
downloadslop-a35dbf55ba7b69aeb2dc5c21e72fa35be262ca63.tar.gz
Beginning of Slop 4.0 rewrite
Diffstat (limited to 'test')
-rw-r--r--test/command_test.rb87
-rw-r--r--test/option_test.rb174
-rw-r--r--test/slop_test.rb487
3 files changed, 179 insertions, 569 deletions
diff --git a/test/command_test.rb b/test/command_test.rb
new file mode 100644
index 0000000..6924305
--- /dev/null
+++ b/test/command_test.rb
@@ -0,0 +1,87 @@
+require 'helper'
+
+class CommandTest < TestCase
+
+ def setup
+ @command = Slop::Command.new(:test)
+ end
+
+ def teardown
+ @command = nil
+ end
+
+ def option(*args, &block)
+ @command.option(*args, &block)
+ end
+
+ def options(*args)
+ args.map { |a| option(*Array(a)) }
+ end
+
+ def parse(items, &block)
+ @command.parse(items, &block)
+ end
+
+ test "option" do
+ @command.option :user
+ assert_kind_of Slop::Option, @command.options[:user]
+ end
+
+ test "command" do
+ @command.command :add
+ assert_kind_of Slop::Command, @command.commands[:add]
+ end
+
+ test "[]" do
+ option "user="
+ parse %w(--user Lee)
+ assert_equal "Lee", @command[:user]
+ end
+
+ test "present?" do
+ options "user", "verbose"
+ parse %w(--user)
+ assert @command.present?("user")
+ assert @command.user?
+ refute @command.verbose?
+ end
+
+ test "global?" do
+ assert Slop.new.global?
+ refute @command.global?
+ end
+
+ test "to_hash" do
+ options "user=", "verbose", "other"
+ parse %w(--user Lee --verbose)
+ assert_equal({user: "Lee", verbose: nil, other: nil}, @command.to_hash)
+
+ @command.command(:foo) { option :bar= }
+ parse %w(foo --bar baz etc)
+ assert_equal({user: "Lee", verbose: nil, other: nil, foo: {bar: "baz"}}, @command.to_hash)
+ end
+
+ test "short_flag_prefix" do
+ assert_equal '@', Slop.new(short_flag_prefix: '@').short_flag_prefix
+ assert_equal '-', @command.short_flag_prefix
+ end
+
+ test "long_flag_prefix" do
+ assert_equal '!!', Slop.new(long_flag_prefix: '!!').long_flag_prefix
+ assert_equal '--', @command.long_flag_prefix
+ end
+
+ test "flag_match" do
+ assert @command.flag_match?("--user")
+ refute @command.flag_match?("user")
+ assert Slop.new(short_flag_prefix: '@').flag_match?('@foo')
+ end
+
+ test "clean_flag" do
+ assert_equal "user", @command.clean_flag("--user")
+ assert_equal "u", @command.clean_flag("-u")
+ assert_equal "user", @command.clean_flag("user")
+ assert_equal "@user", @command.clean_flag("@user")
+ end
+
+end
diff --git a/test/option_test.rb b/test/option_test.rb
index c57126d..4d02140 100644
--- a/test/option_test.rb
+++ b/test/option_test.rb
@@ -1,134 +1,122 @@
require 'helper'
class OptionTest < TestCase
+
def option(*args, &block)
- Slop.new.on(*args, &block)
+ Slop::Option.build(Slop.new, args, &block)
end
- def option_with_argument(*args, &block)
- options = args.shift
- slop = Slop.new(strict: false, help: false)
- option = slop.opt(*args)
- slop.parse(options)
- slop.options.find {|opt| opt.key == option.key }
+ def option_to_a(*args, &block)
+ option = option(*args, &block)
+ [option.short, option.long, option.description]
end
- def option_value(*args, &block)
- option_with_argument(*args, &block).value
+ def option_as(as, value, config = {})
+ config.merge!(as: as)
+ option = Slop::Option.build(Slop.new, ["foo=", config])
+ option.value = value
+ option.value
end
- test "expects_argument?" do
- assert option(:f=).expects_argument?
- assert option(:foo=).expects_argument?
- assert option(:foo, :argument => true).expects_argument?
- end
+ test "::option" do
+ assert_equal ['u', nil, nil], option_to_a('u')
+ assert_equal ['u', nil, nil], option_to_a('u=')
- test "accepts_optional_argument?" do
- refute option(:f=).accepts_optional_argument?
- assert option(:f=, :argument => :optional).accepts_optional_argument?
- assert option(:f, :optional_argument => true).accepts_optional_argument?
- end
+ assert_equal [nil, 'user', nil], option_to_a('user')
+ assert_equal [nil, 'user', nil], option_to_a('user=')
- test "key" do
- assert_equal 'foo', option(:foo).key
- assert_equal 'foo', option(:f, :foo).key
- assert_equal 'f', option(:f).key
- end
+ assert_equal ['u', 'user', nil], option_to_a('u=', 'user')
+ assert_equal ['u', 'user', nil], option_to_a('u', 'user=')
- test "call" do
- foo = nil
- option(:f, :callback => proc { foo = "bar" }).call
- assert_equal "bar", foo
- option(:f) { foo = "baz" }.call
- assert_equal "baz", foo
- option(:f) { |o| assert_equal 1, o }.call(1)
- end
+ assert_equal ['u', 'user', 'Foo'], option_to_a('u', 'user=', 'Foo')
+ assert_equal [nil, 'user', 'Foo'], option_to_a('user', 'Foo')
+ assert_equal ['u', nil, 'Foo Bar'], option_to_a('u', 'Foo Bar')
- # type casting
+ assert_equal ['u', nil, 'Foo Bar'], option_to_a('u', 'Foo Bar', lorem: 'ipsum')
+ end
- test "proc/custom type cast" do
- assert_equal 1, option_value(%w'-f 1', :f=, :as => proc {|x| x.to_i })
- assert_equal "oof", option_value(%w'-f foo', :f=, :as => proc {|x| x.reverse })
+ test "argument?" do
+ assert option("user=").argument?
+ refute option("user").argument?
end
- test "integer type cast" do
- opts = Slop.new
- opts.on :f=, :as => Integer
- opts.parse %w'-f 1'
- assert_equal 1, opts[:f]
+ test "optional_argument?" do
+ assert option("user", optional_argument: true).optional_argument?
+ refute option("user").optional_argument?
+ end
- opts = Slop.new { on :r=, :as => Integer }
- assert_raises(Slop::InvalidArgumentError) { opts.parse %w/-r abc/ }
+ test "runner" do
+ assert_equal "foo", option("user", runner: "foo").runner
+ assert_kind_of Proc, option("user") { }.runner
+ refute option("user").runner
end
- test "float type cast" do
- opts = Slop.new { on :r=, :as => Float }
- assert_raises(Slop::InvalidArgumentError) { opts.parse %w/-r abc/ }
+ test "call" do
+ called = true
+ option = option("user") { called = true }
+ option.value = 'foo'
+ assert_equal 'foo', option.call
+ assert called
end
- test "symbol type cast" do
- assert_equal :foo, option_value(%w'-f foo', :f=, :as => Symbol)
+ test "execute" do
+ option = option("user")
+ assert_equal 0, option.count
+ option.execute
+ assert_equal 1, option.count
end
- test "range type cast" do
- assert_equal((1..10), option_value(%w/-r 1..10/, :r=, :as => Range))
- assert_equal((1..10), option_value(%w/-r 1-10/, :r=, :as => Range))
- assert_equal((1..10), option_value(%w/-r 1,10/, :r=, :as => Range))
- assert_equal((1...10), option_value(%w/-r 1...10/, :r=, :as => Range))
- assert_equal((-1..10), option_value(%w/-r -1..10/, :r=, :as => Range))
- assert_equal((1..-10), option_value(%w/-r 1..-10/, :r=, :as => Range))
- assert_equal((1..1), option_value(%w/-r 1/, :r=, :as => Range))
- assert_equal((-1..10), option_value(%w/-r -1..10/, :r, :as => Range, :optional_argument => true))
+ test "key" do
+ assert_equal "foo", option("f", "foo").key
+ assert_equal "foo", option("foo").key
+ assert_equal "f", option("f").key
+ end
- opts = Slop.new { on :r=, :as => Range }
- assert_raises(Slop::InvalidArgumentError) { opts.parse %w/-r abc/ }
+ test "help" do
+ skip "not implemented"
end
- test "array type cast" do
- assert_equal %w/lee john bill/, option_value(%w/-p lee,john,bill/, :p=, :as => Array)
- assert_equal %w/lee john bill jeff jill/, option_value(%w/-p lee,john,bill -p jeff,jill/, :p=, :as => Array)
- assert_equal %w/lee john bill/, option_value(%w/-p lee:john:bill/, :p=, :as => Array, :delimiter => ':')
- assert_equal %w/lee john,bill/, option_value(%w/-p lee,john,bill/, :p=, :as => Array, :limit => 2)
- assert_equal %w/lee john:bill/, option_value(%w/-p lee:john:bill/, :p=, :as => Array, :limit => 2, :delimiter => ':')
+ test "default" do
+ assert_equal nil, option("f").call
+ assert_equal "foo", option("f", default: "foo").call
end
- test "adding custom types" do
- opts = Slop.new
- opt = opts.on :f=, :as => :reverse
- opt.types[:reverse] = proc { |v| v.reverse }
- opts.parse %w'-f bar'
- assert_equal 'rab', opt.value
+ test "as(String)" do
+ assert_equal "foo", option_as(String, "foo")
end
- test "count type" do
- assert_equal 3, option_value(%w/-c -c -c/, :c, :as => :count)
- assert_equal 0, option_value(%w/-a -b -z/, :c, :as => :count)
- assert_equal 3, option_value(%w/-vvv/, :v, :as => :count)
+ test "as(Symbol)" do
+ assert_equal :foo, option_as(Symbol, "foo")
end
- # end type casting tests
+ test "as(Array)" do
+ assert_equal ["foo", "bar"], option_as(Array, "foo,bar")
+ assert_equal ["foo", "bar"], option_as(Array, "foo:bar", delimiter: ":")
+ assert_equal ["foo", "bar:baz"], option_as(Array, "foo:bar:baz", delimiter: ":", limit: 2)
+ end
- test "using a default value as fallback" do
- opts = Slop.new
- opts.on :f, :argument => :optional, :default => 'foo'
- opts.parse %w'-f'
- assert_equal 'foo', opts[:f]
+ test "as(Integer)" do
+ assert_equal 1, option_as(Integer, "1")
end
- test "printing options" do
- slop = Slop.new
- slop.opt :n, :name=, 'Your name'
- slop.opt :age=, 'Your age'
- slop.opt :V, 'Display the version'
+ test "as(Float)" do
+ assert_equal 1.4, option_as(Float, "1.4")
+ end
- assert_equal " -n, --name Your name", slop.fetch_option(:name).to_s
- assert_equal " --age Your age", slop.fetch_option(:age).to_s
- assert_equal " -V, Display the version", slop.fetch_option(:V).help
+ test "as(Range)" do
+ assert_equal((1..10), option_as(Range, "1..10"))
+ assert_equal((1..10), option_as(Range, "1-10"))
+ assert_equal((1..10), option_as(Range, "1,10"))
+ assert_equal((1...10), option_as(Range, "1...10"))
+ assert_equal((-1..10), option_as(Range, "-1..10"))
+ assert_equal((1..-10), option_as(Range, "1..-10"))
+ assert_equal((1..1), option_as(Range, "1"))
+ assert_equal((-1..10), option_as(Range, "-1..10", optional_argument: true))
end
- test "overwriting the help text" do
- slop = Slop.new
- slop.on :foo, :help => ' -f, --foo SOMETHING FOOEY'
- assert_equal ' -f, --foo SOMETHING FOOEY', slop.fetch_option(:foo).help
+ test "as(Custom)" do
+ reverse = proc { |v| v.reverse }
+ assert_equal "oof", option_as(reverse, "foo")
end
+
end
diff --git a/test/slop_test.rb b/test/slop_test.rb
index d215440..d7b44d0 100644
--- a/test/slop_test.rb
+++ b/test/slop_test.rb
@@ -2,490 +2,25 @@ require 'helper'
class SlopTest < TestCase
- def build_option(*args)
- opt = Slop.new.send(:build_option, args)
- config = opt.config.reject { |k, v| v == Slop::Option::DEFAULT_OPTIONS[k] }
- [opt.short, opt.long, opt.description, config]
+ test "::parse!" do
+ assert_kind_of Slop::Command, Slop.parse!(%w(foo))
end
- def temp_argv(items)
- old_argv = ARGV.clone
- ARGV.replace items
- yield
- ensure
- ARGV.replace old_argv
+ test "::parse" do
+ assert_kind_of Slop::Command, Slop.parse(%w(foo))
end
- def temp_stdout
- $stdout = StringIO.new
- yield $stdout.string
- ensure
- $stdout = STDOUT
+ test "::new" do
+ assert Slop.new.global?
end
- test "build_option" do
- assert_equal ['f', nil, nil, {}], build_option(:f)
- assert_equal [nil, 'foo', nil, {}], build_option(:foo)
- assert_equal ['f', nil, 'Some description', {}], build_option(:f, 'Some description')
- assert_equal ['f', 'foo', nil, {}], build_option(:f, :foo)
- assert_equal [nil, '1.8', 'Use v. 1.8', {}], build_option('--1.8', 'Use v. 1.8')
-
- # with arguments
- assert_equal ['f', nil, nil, {:argument=>true}], build_option('f=')
- assert_equal [nil, 'foo', nil, {:argument=>true}], build_option('foo=')
- assert_equal [nil, 'foo', nil, {:optional_argument=>true}], build_option('foo=?')
- end
-
- test "parsing option=value" do
- slop = Slop.new { on :foo= }
- slop.parse %w' --foo=bar '
- assert_equal 'bar', slop[:foo]
-
- slop = Slop.new(:multiple_switches => false) { on :f=; on :b= }
- slop.parse %w' -fabc -bdef '
- assert_equal 'abc', slop[:f]
- assert_equal 'def', slop[:b]
- end
-
- test "fetch_option" do
- slop = Slop.new
- opt1 = slop.on :f, :foo
- opt2 = slop.on :bar
-
- assert_equal opt1, slop.fetch_option(:foo)
- assert_equal opt1, slop.fetch_option(:f)
- assert_equal opt2, slop.fetch_option(:bar)
- assert_equal opt2, slop.fetch_option('--bar')
- assert_nil slop.fetch_option(:baz)
- end
-
- test "default all options to take arguments" do
- slop = Slop.new(:arguments => true)
- opt1 = slop.on :foo
- opt2 = slop.on :bar, :argument => false
-
- assert opt1.expects_argument?
- refute opt2.expects_argument?
- end
-
- test "extract_option" do
- slop = Slop.new
- extract = proc { |flag| slop.send(:extract_option, flag) }
- slop.on :opt=
-
- assert_kind_of Array, extract['--foo']
- assert_equal 'bar', extract['--foo=bar'][1]
- assert_equal 'bar', extract['-f=bar'][1]
- assert_nil extract['--foo'][0]
- assert_kind_of Slop::Option, extract['--opt'][0]
- assert_equal false, extract['--no-opt'][1]
- end
-
- test "non-options yielded to parse()" do
- foo = nil
- slop = Slop.new
- slop.parse ['foo'] do |x| foo = x end
- assert_equal 'foo', foo
- end
-
- test "::parse returns a Slop object" do
- assert_kind_of Slop, Slop.parse([])
- end
-
- test "parse" do
- slop = Slop.new
- assert_equal ['foo'], slop.parse(%w'foo')
- assert_equal ['foo'], slop.parse!(%w'foo')
- end
-
- test "parse!" do
- slop = Slop.new { on :foo= }
- assert_equal [], slop.parse!(%w'--foo bar')
- slop = Slop.new { on :baz }
- assert_equal ['etc'], slop.parse!(%w'--baz etc')
- end
-
- test "new() accepts a hash of configuration options" do
- slop = Slop.new(:foo => :bar)
- assert_equal :bar, slop.config[:foo]
- end
-
- test "defaulting to ARGV" do
- temp_argv(%w/--name lee/) do
- opts = Slop.parse { on :name= }
- assert_equal 'lee', opts[:name]
- end
- end
-
- test "automatically adding the help option" do
- slop = Slop.new :help => true
- refute_empty slop.options
- assert_equal 'Display this help message.', slop.options.first.description
- end
-
- test "default help exits" do
- temp_stdout do
- slop = Slop.new :help => true
- assert_raises SystemExit do
- slop.parse %w/--help/
- end
- end
- end
-
- test ":arguments and :optional_arguments config options" do
- slop = Slop.new(:arguments => true) { on :foo }
- assert slop.fetch_option(:foo).expects_argument?
-
- slop = Slop.new(:optional_arguments => true) { on :foo }
- assert slop.fetch_option(:foo).accepts_optional_argument?
- end
-
- test "yielding non-options when a block is passed to parse()" do
- items = []
- opts = Slop.new { on :name= }
- opts.parse(%w/--name lee a b c/) { |v| items << v }
- assert_equal ['a', 'b', 'c'], items
- end
-
- test "on empty callback" do
- opts = Slop.new
- foo = nil
- opts.add_callback(:empty) { foo = "bar" }
- opts.parse []
- assert_equal "bar", foo
- end
-
- test "on no_options callback" do
- opts = Slop.new(strict: false)
- foo = nil
- opts.add_callback(:no_options) { foo = "bar" }
- opts.parse %w( --foo --bar etc hello )
- assert_equal "bar", foo
- end
-
- test "to_hash()" do
- opts = Slop.new { on :foo=; on :bar; on :baz; on :zip }
- opts.parse(%w'--foo hello --no-bar --baz')
- assert_equal({ :foo => 'hello', :bar => false, :baz => true, :zip => nil }, opts.to_hash)
- end
-
- test "missing() returning all missing option keys" do
- opts = Slop.new { on :foo; on :bar }
- opts.parse %w'--foo'
- assert_equal ['bar'], opts.missing
- end
-
- test "autocreating options" do
- opts = Slop.new :autocreate => true
- opts.parse %w[ --foo bar --baz ]
- assert opts.fetch_option(:foo).expects_argument?
- assert opts.fetch_option(:foo).autocreated?
- assert_equal 'bar', opts.fetch_option(:foo).value
- refute opts.fetch_option(:baz).expects_argument?
- assert_equal nil, opts.fetch_option(:bar)
-
- opts = Slop.new :autocreate => true do
- on :f, :foo=
- end
- opts.parse %w[ --foo bar --baz stuff ]
- assert_equal 'bar', opts[:foo]
- assert_equal 'stuff', opts[:baz]
- end
-
- test "option terminator" do
- opts = Slop.new { on :foo= }
- items = %w' foo -- --foo bar '
- opts.parse! items
- assert_equal %w' foo --foo bar ', items
- end
-
- test "raising an InvalidArgumentError when the argument doesn't match" do
- opts = Slop.new { on :foo=, :match => /^[a-z]+$/ }
- assert_raises(Slop::InvalidArgumentError) { opts.parse %w' --foo b4r '}
- end
-
- test "raising a MissingArgumentError when the option expects an argument" do
- opts = Slop.new { on :foo= }
- assert_raises(Slop::MissingArgumentError) { opts.parse %w' --foo '}
- end
-
- test "raising a MissingOptionError when a required option is missing" do
- opts = Slop.new { on :foo, :required => true }
- assert_raises(Slop::MissingOptionError) { opts.parse %w'' }
- end
-
- test "raising InvalidOptionError when strict mode is enabled and an unknown option appears" do
- opts = Slop.new :strict => true
- assert_raises(Slop::InvalidOptionError) { opts.parse %w'--foo' }
- assert_raises(Slop::InvalidOptionError) { opts.parse %w'-fabc' }
- end
-
- test "raising InvalidOptionError for multiple short options" do
- opts = Slop.new :strict => true
- opts.on :L
- assert_raises(Slop::InvalidOptionError) { opts.parse %w'-Ly' }
-
- # but not with no strict mode!
- opts = Slop.new
- opts.on :L
- assert opts.parse %w'-Ly'
- end
-
- test "multiple_switches is enabled by default" do
- opts = Slop.new { on :f; on :b }
- opts.parse %w[ -fb ]
- assert opts.present?(:f)
- assert opts.present?(:b)
- end
-
- test "multiple_switches disabled" do
- opts = Slop.new(:multiple_switches => false) { on :f= }
- opts.parse %w[ -fabc123 ]
- assert_equal 'abc123', opts[:f]
- end
-
- test "muiltiple_switches should not trash arguments" do
- opts = Slop.new{ on :f; on :b }
- args = opts.parse!(%w'-fb foo')
- assert_equal %w'foo', args
- end
-
- test "multiple options should still accept trailing arguments" do
- opts = Slop.new { on :a; on :b= }
- opts.parse %w'-ab foo'
- assert_equal 'foo', opts[:b]
- end
-
- test "setting/getting the banner" do
- opts = Slop.new :banner => 'foo'
- assert_equal 'foo', opts.banner
-
- opts = Slop.new
- opts.banner 'foo'
- assert_equal 'foo', opts.banner
-
- opts = Slop.new
- opts.banner = 'foo'
- assert_equal 'foo', opts.banner
- end
-
- test "get/[] fetching an options argument value" do
- opts = Slop.new { on :foo=; on :bar; on :baz }
- opts.parse %w' --foo hello --bar '
- assert_equal 'hello', opts[:foo]
- assert_equal true, opts[:bar]
- assert_nil opts[:baz]
- end
-
- test "checking for an options presence" do
- opts = Slop.new { on :foo; on :bar }
- opts.parse %w' --foo '
- assert opts.present?(:foo)
- refute opts.present?(:bar)
- end
-
- test "ignoring case" do
- opts = Slop.new(strict: false) { on :foo }
- opts.parse %w' --FOO bar '
- assert_nil opts[:foo]
-
- opts = Slop.new(:ignore_case => true) { on :foo= }
- opts.parse %w' --FOO bar '
- assert_equal 'bar', opts[:foo]
- end
-
- test "supporting dash" do
- opts = Slop.new { on :foo_bar= }
- opts.parse %w' --foo-bar baz '
- assert_equal 'baz', opts[:foo_bar]
- assert opts.foo_bar?
- end
-
- test "supporting underscore" do
- opts = Slop.new { on :foo_bar= }
- opts.parse %w' --foo_bar baz '
- assert_equal 'baz', opts[:foo_bar]
- assert opts.foo_bar?
- end
-
- test "ensure negative integers are not processed as options" do
- items = %w(-1)
- Slop.parse!(items, strict: false)
- assert_equal %w(-1), items
- end
-
- test "separators" do
- opts = Slop.new(:banner => false) do
- on :foo
- separator "hello"
- separator "world"
- on :bar
- end
- assert_equal " --foo \nhello\nworld\n --bar ", opts.help
-
- opts = Slop.new do
- banner "foo"
- separator "bar"
- end
- assert_equal "foo\nbar\n", opts.help
- end
-
- test "printing help with :help => true" do
- temp_stdout do |string|
- opts = Slop.new(:help => true, :banner => false)
- assert_raises SystemExit do
- opts.parse %w( --help )
- end
- assert_equal " -h, --help Display this help message.\n", string
- end
-
- temp_stdout do |string|
- opts = Slop.new(:help => true)
- assert_raises SystemExit do
- opts.parse %w( --help )
- end
- assert_equal "Usage: rake_test_loader [options]\n -h, --help Display this help message.\n", string
- end
- end
-
- test "fallback to substituting - for _ when using <option>?" do
- opts = Slop.new do
- on 'foo-bar'
- end
- opts.parse %w( --foo-bar )
- assert opts.foo_bar?
- end
-
- test "option=value syntax does NOT consume following argument" do
- opts = Slop.new { on :foo=; on 'bar=?' }
- args = %w' --foo=bar baz --bar=zing hello '
- opts.parse!(args)
- assert_equal %w' baz hello ', args
- end
-
- test "context and return value of constructor block" do
- peep = nil
- ret = Slop.new { peep = self }
- assert_same ret, peep
- assert !equal?(peep)
-
- peep = nil
- ret = Slop.new { |a| peep = self }
- assert !peep.equal?(ret)
- assert_same peep, self
-
- peep = nil
- ret = Slop.new { |a, b| peep = self }
- assert_same ret, peep
- assert !equal?(peep)
-
- peep = nil
- ret = Slop.new { |a, *rest| peep = self }
- assert_same ret, peep
- assert !equal?(peep)
-
- peep = nil
- ret = Slop.parse([]) { peep = self }
- assert_same ret, peep
- assert !equal?(peep)
-
- peep = nil
- ret = Slop.parse([]) { |a| peep = self }
- assert !peep.equal?(ret)
- assert_same peep, self
-
- peep = nil
- ret = Slop.parse([]) { |a, b| peep = self }
- assert_same ret, peep
- assert !equal?(peep)
-
- peep = nil
- ret = Slop.parse([]) { |a, *rest| peep = self }
- assert_same ret, peep
- assert !equal?(peep)
- end
-
- test "to_s do not break self" do
- slop = Slop.new do
- banner "foo"
- end
-
- assert_equal "foo", slop.banner
- slop.to_s
- assert_equal "foo", slop.banner
- end
-
- test "options with prefixed --no should not default to inverted behaviour unless intended" do
- opts = Slop.new { on :bar }
- opts.parse %w'--no-bar'
- assert_equal false, opts[:bar]
-
- opts = Slop.new { on 'no-bar' }
- opts.parse %w'--no-bar'
- assert_equal true, opts['no-bar']
- end
-
- test "method missing() is a private method" do
- assert Slop.new.private_methods.map(&:to_sym).include?(:method_missing)
- end
-
- test "respond_to?() arity checker is similar of other objects" do
- slop = Slop.new
- obj = Object.new
-
- assert_same obj.respond_to?(:__id__), slop.respond_to?(:__id__)
- assert_same obj.respond_to?(:__id__, false), slop.respond_to?(:__id__, false)
- assert_same obj.respond_to?(:__id__, true), slop.respond_to?(:__id__, true)
-
- assert_raises ArgumentError do
- slop.respond_to? :__id__, false, :INVALID_ARGUMENT
- end
- end
-
- test "adding a runner" do
- orun = proc { |r| assert_instance_of Slop, r }
- arun = proc { |r| assert_equal ['foo', 'bar'], r }
-
- Slop.parse(%w'foo --foo bar -v bar') do
- on :v
- on :foo=
- run { |o, a| orun[o]; arun[a] }
- end
- end
-
- test "ensure a runner does not execute when a help option is present" do
- items = []
- Slop.parse(%w'--help foo bar', strict: false) do
- run { |o, a| items.concat a }
- end
- assert_equal %w'--help foo bar', items
- items.clear
- temp_stdout do
- assert_raises SystemExit do
- Slop.parse(%w'--help foo bar', :help => true) do
- run { |o, a| items.concat a }
- end
- end
- assert_empty items
- end
- end
-
- test "duplicate options should not exist, new options should replace old ones" do
- i = nil
- Slop.parse(%w'-v') do
- on(:v) { i = 'first' }
- on(:v) { i = 'second' }
- end
- assert_equal 'second', i
- end
-
- test 'Slop::Error sets @slop and has a help method' do
- s = Slop.new
+ test "errors" do
begin
- s.parse %w'--abc'
+ Slop.parse(%w(--foo))
rescue Slop::Error => e
- assert_equal s, e.slop
- assert_equal s.help, e.help
+ assert_kind_of Slop::Command, e.command
+ assert_kind_of Slop::Command, e.opts
+ assert e.message.include?("foo")
end
end