Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobaldassi committed Aug 10, 2014
1 parent 47db6cb commit 923a0e1
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 56 deletions.
3 changes: 3 additions & 0 deletions test/argparse_test1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ let s = ap_settings1()
@test ap_test1(["--opt1", "X", "-o=5", "--", "-arg"]) == (String=>Any)["opt1"=>"X", "opt2"=>"5", "arg1"=>"-arg"]
@ap_test_throws ap_test1(["--opt1", "X", "-o=5", "-arg"])
@test ap_test1(["--opt1=", "--opt2=5"]) == (String=>Any)["opt1"=>"", "opt2"=>"5", "arg1"=>nothing]

@test_throws_02 ErrorException @add_arg_table(s, "--opt1") # long option already added
@test_throws_02 ErrorException @add_arg_table(s, "-o") # short option already added
end
48 changes: 46 additions & 2 deletions test/argparse_test2.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# test 2: version information, default values, flags,
# options with types, optional arguments, variable
# number of arguments
# number of arguments;
# function version of add_arg_table

using OptionsMod

function ap_settings2()

Expand Down Expand Up @@ -40,7 +43,45 @@ function ap_settings2()
return s
end

let s = ap_settings2()
function ap_settings2b()

s = ArgParseSettings(description = "Test 2 for ArgParse.jl",
version = "Version 1.0", # version info
add_version = true) # audo-add version option

add_arg_table(s,
"--opt1", @options(
nargs = '?', # '?' means optional argument
arg_type = Int, # only Int arguments allowed
default = 0, # this is used when the option is not passed
constant = 1, # this is used if --opt1 is paseed with no argument
help = "an option"),
["--flag", "-f"], @options(
action = :store_true, # this makes it a flag
help = "a flag"),
["--karma", "-k"], @options(
action = :count_invocations, # increase a counter each time the option is given
help = "increase karma"),
"arg1", @options(
nargs = 2, # eats up two arguments; puts the result in a Vector
help = "first argument, two " *
"entries at once",
required = true),
"arg2", @options(
nargs = '*', # eats up as many arguments as possible before an option
default = {"no_arg_given"}, # since the result will be a Vector{Any}, the default must
# also be (or it can be [] or nothing)
help = "second argument, eats up " *
"as many items as possible " *
"before an option")
)

s.exc_handler = (settings, err)->throw(err)

return s
end

for s = [ap_settings2(), ap_settings2b()]
ap_test2(args) = parse_args(args, s)

@test stringhelp(s) == """
Expand All @@ -67,4 +108,7 @@ let s = ap_settings2()
@test ap_test2(["X", "Y", "-k", "-f", "Z", "--karma", "--opt"]) == (String=>Any)["opt1"=>1, "flag"=>true, "karma"=>2, "arg1"=>{"X", "Y"}, "arg2"=>{"Z"}]
@test ap_test2(["--opt", "-3", "X", "Y", "-k", "-f", "Z", "--karma"]) == (String=>Any)["opt1"=>-3, "flag"=>true, "karma"=>2, "arg1"=>{"X", "Y"}, "arg2"=>{"Z"}]
@ap_test_throws ap_test2(["--opt", "1e-2", "X", "Y"])

@test_throws_02 ErrorException @add_arg_table(s, "required_arg_after_optional_args", required=true)
@test_throws_02 ErrorException @add_arg_table(s, "--opt", arg_type = Int, default = 1.5) # wrong default
end
14 changes: 14 additions & 0 deletions test/argparse_test3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ let s = ap_settings3()
(String=>Any)["O_stack"=>String["O1", "O2", "O2", "O1"], "k"=>42, "awk"=>{{"X", "X"}, {"Y", "X"}}]
@ap_test_throws ap_test3(["X"])
@ap_test_throws ap_test3(["--awk", "Z"])

# wrong constants
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :store_const, arg_type = Int, default = 1, constant = 1.5)
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :append_const, arg_type = Int, constant = 1.5)
# wrong defaults
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :append_arg, arg_type = Int, default = Float64[])
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :append_arg, nargs = '+', arg_type = Int, default = Vector{Float64}[])
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :store_arg, nargs = '+', arg_type = Int, default = [1.5])
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :append_arg, arg_type = Int, range_tester=x->x<=1, default = Int[0, 1, 2])
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :append_arg, nargs = '+', arg_type = Int, range_tester=x->x<=1, default = Vector{Int}[[1,1],[0,2]])
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :store_arg, nargs = '+', range_tester = x->x<=1, default = [1.5])
# no constants
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :store_const, arg_type = Int, default = 1)
@test_throws_02 ErrorException @add_arg_table(s, "--opt", action = :append_const, arg_type = Int)
end
4 changes: 2 additions & 2 deletions test/argparse_test4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function ap_settings4()
help = "this will help you"
"-v", "--VERSION"
action = :show_version # will show version information
help = "show version information" *
help = "show version information " *
"and exit"
end

Expand All @@ -63,7 +63,7 @@ let s = ap_settings4()
-o child flag
--flag another child flag
-?, --HELP, --¡ḧëļṕ this will help you
-v, --VERSION show version informationand exit
-v, --VERSION show version information and exit
"""

Expand Down
110 changes: 58 additions & 52 deletions test/argparse_test5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function ap_settings5()
action = :command
dest_name = "som"
help = "somersault jumping mode"
"--clap-feet"
"--clap-feet", "-c"
action = :command
help = "clap feet jumping mode"
end
Expand All @@ -43,6 +43,63 @@ function ap_settings5()
return s
end

let s = ap_settings5()
ap_test5(args) = parse_args(args, s)

@test stringhelp(s) == """
usage: $(basename(Base.source_path())) {run|jump}
Test 5 for ArgParse.jl
commands:
run start running mode
jump start jumping mode
"""

@test stringhelp(s["run"]) == """
usage: $(basename(Base.source_path())) run [--speed SPEED]
optional arguments:
--speed SPEED running speed, in Å/month (type: Float64, default:
10.0)
"""

@test stringhelp(s["jump"]) == """
usage: $(basename(Base.source_path())) jump [--higher] [--somersault|-c]
Jump mode
commands:
--somersault somersault jumping mode
-c, --clap-feet clap feet jumping mode
optional arguments:
--higher enhance jumping
"""

@test stringhelp(s["jump"]["som"]) == """
usage: $(basename(Base.source_path())) jump --somersault
Somersault jump mode
"""

@ap_test_throws ap_test5([])
@test ap_test5(["run", "--speed", "3"]) == (String=>Any)["%COMMAND%"=>"run", "run"=>(String=>Any)["speed"=>3.0]]
@test ap_test5(["jump"]) == (String=>Any)["%COMMAND%"=>"jump", "jump"=>(String=>Any)["higher"=>false, "%COMMAND%"=>nothing]]
@test ap_test5(["jump", "--higher", "--clap"]) == (String=>Any)["%COMMAND%"=>"jump", "jump"=>(String=>Any)["higher"=>true, "%COMMAND%"=>"clap_feet", "clap_feet"=>(String=>Any)[]]]
@ap_test_throws ap_test5(["jump", "--clap", "--higher"])

@test_throws_02 ErrorException @add_arg_table(s, "arg_after_command")
@test_throws_02 ErrorException @add_arg_table(s, "run") # arg with same name as command
@test_throws_02 ErrorException @add_arg_table(s["jump"], "-c") # short option with same name as command
@test_throws_02 ErrorException @add_arg_table(s["jump"], "--somersault") # long option with same name as command
@test_throws_02 ErrorException @add_arg_table(s["jump"], "--som") # long option with same dest_name as command
end

function ap_settings5b()

s0 = ArgParseSettings()
Expand Down Expand Up @@ -122,57 +179,6 @@ function ap_settings5b()
return s
end

let s = ap_settings5()
ap_test5(args) = parse_args(args, s)

@test stringhelp(s) == """
usage: $(basename(Base.source_path())) {run|jump}
Test 5 for ArgParse.jl
commands:
run start running mode
jump start jumping mode
"""

@test stringhelp(s["run"]) == """
usage: $(basename(Base.source_path())) run [--speed SPEED]
optional arguments:
--speed SPEED running speed, in Å/month (type: Float64, default:
10.0)
"""

@test stringhelp(s["jump"]) == """
usage: $(basename(Base.source_path())) jump [--higher] [--somersault|--clap-feet]
Jump mode
commands:
--somersault somersault jumping mode
--clap-feet clap feet jumping mode
optional arguments:
--higher enhance jumping
"""

@test stringhelp(s["jump"]["som"]) == """
usage: $(basename(Base.source_path())) jump --somersault
Somersault jump mode
"""

@ap_test_throws ap_test5([])
@test ap_test5(["run", "--speed", "3"]) == (String=>Any)["%COMMAND%"=>"run", "run"=>(String=>Any)["speed"=>3.0]]
@test ap_test5(["jump"]) == (String=>Any)["%COMMAND%"=>"jump", "jump"=>(String=>Any)["higher"=>false, "%COMMAND%"=>nothing]]
@test ap_test5(["jump", "--higher", "--clap"]) == (String=>Any)["%COMMAND%"=>"jump", "jump"=>(String=>Any)["higher"=>true, "%COMMAND%"=>"clap_feet", "clap_feet"=>(String=>Any)[]]]
@ap_test_throws ap_test5(["jump", "--clap", "--higher"])
end

let s = ap_settings5b()
ap_test5b(args) = parse_args(args, s)

Expand Down

0 comments on commit 923a0e1

Please sign in to comment.