vendor/gems/trollop/test/test_trollop.rb in auser-poolparty-1.2.12 vs vendor/gems/trollop/test/test_trollop.rb in auser-poolparty-1.3.0
- old
+ new
@@ -97,19 +97,10 @@
assert_equal 2.41, opts["argsf"]
assert_nothing_raised { opts = @p.parse(%w(--argsf 2)) }
assert_equal 2, opts["argsf"]
assert_raise(CommandlineError) { @p.parse(%w(--argsf hello)) }
- # single arg: date
- date = Date.today
- assert_nothing_raised { @p.opt "argsd", "desc", :default => date }
- assert_nothing_raised { opts = @p.parse("--") }
- assert_equal Date.today, opts["argsd"]
- assert_nothing_raised { opts = @p.parse(['--argsd', 'Jan 4, 2007']) }
- assert_equal Date.civil(2007, 1, 4), opts["argsd"]
- assert_raise(CommandlineError) { @p.parse(%w(--argsd hello)) }
-
# single arg: string
assert_nothing_raised { @p.opt "argss", "desc", :default => "foobar" }
assert_nothing_raised { opts = @p.parse("--") }
assert_equal "foobar", opts["argss"]
assert_nothing_raised { opts = @p.parse(%w(--argss 2.41)) }
@@ -134,27 +125,18 @@
assert_equal [2], opts["argmf"]
assert_nothing_raised { opts = @p.parse(%w(--argmf 4.0)) }
assert_equal [4.0], opts["argmf"]
assert_raise(CommandlineError) { @p.parse(%w(--argmf hello)) }
- # multi args: dates
- dates = [Date.today, Date.civil(2007, 1, 4)]
- assert_nothing_raised { @p.opt "argmd", "desc", :default => dates }
- assert_nothing_raised { opts = @p.parse("--") }
- assert_equal dates, opts["argmd"]
- assert_nothing_raised { opts = @p.parse(['--argmd', 'Jan 4, 2007']) }
- assert_equal [Date.civil(2007, 1, 4)], opts["argmd"]
- assert_raise(CommandlineError) { @p.parse(%w(--argmd hello)) }
-
# multi args: strings
- assert_nothing_raised { @p.opt "argmst", "desc", :default => %w(hello world) }
+ assert_nothing_raised { @p.opt "argms", "desc", :default => %w(hello world) }
assert_nothing_raised { opts = @p.parse("--") }
- assert_equal %w(hello world), opts["argmst"]
- assert_nothing_raised { opts = @p.parse(%w(--argmst 3.4)) }
- assert_equal ["3.4"], opts["argmst"]
- assert_nothing_raised { opts = @p.parse(%w(--argmst goodbye)) }
- assert_equal ["goodbye"], opts["argmst"]
+ assert_equal %w(hello world), opts["argms"]
+ assert_nothing_raised { opts = @p.parse(%w(--argms 3.4)) }
+ assert_equal ["3.4"], opts["argms"]
+ assert_nothing_raised { opts = @p.parse(%w(--argms goodbye)) }
+ assert_equal ["goodbye"], opts["argms"]
end
## :type and :default must match if both are specified
def test_type_and_default_must_match
assert_raise(ArgumentError) { @p.opt "badarg", "desc", :type => :int, :default => "hello" }
@@ -162,16 +144,14 @@
assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :String, :default => ["hi"] }
assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :ints, :default => [3.14] }
assert_nothing_raised { @p.opt "argsi", "desc", :type => :int, :default => 4 }
assert_nothing_raised { @p.opt "argsf", "desc", :type => :float, :default => 3.14 }
- assert_nothing_raised { @p.opt "argsd", "desc", :type => :date, :default => Date.today }
assert_nothing_raised { @p.opt "argss", "desc", :type => :string, :default => "yo" }
assert_nothing_raised { @p.opt "argmi", "desc", :type => :ints, :default => [4] }
assert_nothing_raised { @p.opt "argmf", "desc", :type => :floats, :default => [3.14] }
- assert_nothing_raised { @p.opt "argmd", "desc", :type => :dates, :default => [Date.today] }
- assert_nothing_raised { @p.opt "argmst", "desc", :type => :strings, :default => ["yo"] }
+ assert_nothing_raised { @p.opt "argms", "desc", :type => :strings, :default => ["yo"] }
end
def test_long_detects_bad_names
assert_nothing_raised { @p.opt "goodarg", "desc", :long => "none" }
assert_nothing_raised { @p.opt "goodarg2", "desc", :long => "--two" }
@@ -191,10 +171,11 @@
def test_short_names_created_automatically
@p.opt "arg"
@p.opt "arg2"
@p.opt "arg3"
+ assert_raise(ArgumentError) { @p.opt "gra" }
opts = @p.parse %w(-a -g)
assert_equal true, opts["arg"]
assert_equal false, opts["arg2"]
assert_equal true, opts["arg3"]
end
@@ -216,12 +197,12 @@
def test_short_autocreation_detects_running_out
@p.opt :arg1 # auto: a
@p.opt :arg2 # auto: r
@p.opt :arg3 # auto: g
- @p.opt :arg4 # auto: uh oh!
- assert_raises(ArgumentError) { @p.parse [] }
+ assert_raises(ArgumentError) { @p.opt :arg4 }
+ assert_nothing_raised { @p.opt :argf }
end
def test_short_can_be_nothing
assert_nothing_raised do
@p.opt "arg", "desc", :short => :none
@@ -370,24 +351,10 @@
assert_raises(CommandlineError) { @p.parse %w(-f 1.0.0) }
assert_raises(CommandlineError) { @p.parse %w(-f .) }
assert_raises(CommandlineError) { @p.parse %w(-f -.) }
end
- def test_date_formatting
- @p.opt :arg, "desc", :type => :date, :short => 'd'
- opts = nil
- assert_nothing_raised { opts = @p.parse(['-d', 'Jan 4, 2007']) }
- assert_equal Date.civil(2007, 1, 4), opts[:arg]
- begin
- require 'chronic'
- assert_nothing_raised { opts = @p.parse(['-d', 'today']) }
- assert_equal Date.today, opts[:arg]
- rescue LoadError
- # chronic is not available
- end
- end
-
def test_short_options_cant_be_numeric
assert_raises(ArgumentError) { @p.opt :arg, "desc", :short => "-1" }
@p.opt :a1b, "desc"
@p.opt :a2b, "desc"
assert_not_equal "2", @p.specs[:a2b][:short]
@@ -580,13 +547,11 @@
assert_equal "hello", opts[:arg]
assert_nothing_raised { opts = @p.parse %w(--arg goat) }
assert_equal "goat", opts[:arg]
assert_nothing_raised { opts = @p.parse %w(--arg=goat) }
assert_equal "goat", opts[:arg]
- ## actually, this next one is valid. empty string for --arg, and goat as a
- ## leftover.
- ## assert_raises(CommandlineError) { opts = @p.parse %w(--arg= goat) }
+ assert_raises(CommandlineError) { opts = @p.parse %w(--arg= goat) }
end
def test_auto_generated_long_names_convert_underscores_to_hyphens
@p.opt :hello_there
assert_equal "hello-there", @p.specs[:hello_there][:long]
@@ -872,19 +837,19 @@
def test_unknown_subcommand
@p.opt :global_flag, "Global flag", :short => "-g", :type => :flag
@p.opt :global_param, "Global parameter", :short => "-p", :default => 5
@p.stop_on_unknown
- expected_opts = { :global_flag => true, :help => false, :global_param => 5, :global_flag_given => true }
+ expected_opts = { :global_flag =>true, :help =>false, :global_param =>5 }
expected_leftovers = [ "my_subcommand", "-c" ]
assert_parses_correctly @p, %w(--global-flag my_subcommand -c), \
expected_opts, expected_leftovers
assert_parses_correctly @p, %w(-g my_subcommand -c), \
expected_opts, expected_leftovers
- expected_opts = { :global_flag => false, :help => false, :global_param => 5, :global_param_given => true }
+ expected_opts = { :global_flag =>false, :help =>false, :global_param =>5 }
expected_leftovers = [ "my_subcommand", "-c" ]
assert_parses_correctly @p, %w(-p 5 my_subcommand -c), \
expected_opts, expected_leftovers
assert_parses_correctly @p, %w(--global-param 5 my_subcommand -c), \
@@ -983,59 +948,9 @@
assert_equal [], @p.leftovers
## arg4 should be multi-valued but not multi-occurring
opts = @p.parse %w()
assert_equal ["potato", "rhubarb"], opts[:arg4]
- end
-
- def test_given_keys
- @p.opt :arg1
- @p.opt :arg2
-
- opts = @p.parse %w(--arg1)
- assert opts[:arg1_given]
- assert !opts[:arg2_given]
-
- opts = @p.parse %w(--arg2)
- assert !opts[:arg1_given]
- assert opts[:arg2_given]
-
- opts = @p.parse []
- assert !opts[:arg1_given]
- assert !opts[:arg2_given]
-
- opts = @p.parse %w(--arg1 --arg2)
- assert opts[:arg1_given]
- assert opts[:arg2_given]
- end
-
- def test_default_shorts_assigned_only_after_user_shorts
- @p.opt :aab, "aaa" # should be assigned to -b
- @p.opt :ccd, "bbb" # should be assigned to -d
- @p.opt :user1, "user1", :short => 'a'
- @p.opt :user2, "user2", :short => 'c'
-
- opts = @p.parse %w(-a -b)
- assert opts[:user1]
- assert !opts[:user2]
- assert opts[:aab]
- assert !opts[:ccd]
-
- opts = @p.parse %w(-c -d)
- assert !opts[:user1]
- assert opts[:user2]
- assert !opts[:aab]
- assert opts[:ccd]
- end
-
- def test_accepts_arguments_with_spaces
- @p.opt :arg1, "arg", :type => String
- @p.opt :arg2, "arg2", :type => String
-
- opts = @p.parse ["--arg1", "hello there", "--arg2=hello there"]
- assert_equal "hello there", opts[:arg1]
- assert_equal "hello there", opts[:arg2]
- assert_equal 0, @p.leftovers.size
end
end
end
end