if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}

try {
	package require parse_args
} on error {errmsg options} {
	puts stderr "Error loading parse_args:\n$options"
	return
}

test parse_args-1.1 {no args, dict mode} -body { #<<<
	parse_args::parse_args {} {} d
	set d
} -cleanup {
	unset -nocomplain d
} -result {}
#>>>
test parse_args-1.2 {no args, vars mode} -body { #<<<
	parse_args::parse_args {} {}
} -result {}
#>>>
test parse_args-2.1 {basic option, dict mode} -body { #<<<
	parse_args::parse_args {-foo bar} {-foo {}} d
	set d
} -cleanup {
	unset -nocomplain d
} -result {foo bar}
#>>>
test parse_args-2.2 {basic option, vars mode} -body { #<<<
	parse_args::parse_args {-foo bar} {-foo {}}
	set foo
} -cleanup {
	unset -nocomplain foo
} -result bar
#>>>
test parse_args-3.2 {option escape} -body { #<<<
	parse_args::parse_args {-foo bar -- -baz quux} {
		-foo	{}
		-bar	{}
		-baz	{-default def}
		a		{}
		b		{}
	}
	list $foo [info exists bar] $baz $a $b
} -cleanup {
	unset -nocomplain foo bar baz a b
} -result {bar 0 def -baz quux}
#>>>
test parse_args-3.3 {default positional arg} -body { #<<<
	parse_args::parse_args {a b} {
		-foo	{}
		first	{}
		second	{-default default_second}
		third	{-default default_third}
	}
	list $first $second $third
} -cleanup {
	unset -nocomplain foo first second third
} -result {a b default_third}
#>>>
test parse_args-10.1 {reject negative -args} -body { #<<<
	parse_args::parse_args {-foo bar} {
		-foo	{-args -1}
	}
	set foo
} -cleanup {
	unset -nocomplain foo
} -returnCodes error -result {-args cannot be negative}
#>>>
test parse_args-10.2 {-args 0, supplied} -body { #<<<
	parse_args::parse_args {-foo} {
		-foo	{-args 0}
	}
	set foo
} -cleanup {
	unset -nocomplain foo
} -result 1
#>>>
test parse_args-10.3 {-args 0, omitted} -body { #<<<
	parse_args::parse_args {-bar ""} {
		-foo	{-args 0}
		-bar	{}
	}
	set foo
} -cleanup {
	unset -nocomplain foo bar
} -result 0
#>>>
test parse_args-10.4 {-args 3, enough} -body { #<<<
	parse_args::parse_args {-foo a "b c" d e f g h i j} {
		-foo	{-args 3}
		-bar	{}
		p1		{}
		p2		{}
		args	{}
	}
	list $foo $p1 $p2 $args
} -cleanup {
	unset -nocomplain foo bar args
} -result [list [list a "b c" d] e f {g h i j}]
#>>>
test parse_args-10.5 {-args 3, too few} -body { #<<<
	parse_args::parse_args {-foo a "b c"} {
		-foo	{-args 3}
		-bar	{}
	}
} -cleanup {
	unset -nocomplain foo bar
} -returnCodes error -match glob -result *
#>>>
test parse_args-11.1 {invalid setting} -body { #<<<
	parse_args::parse_args {} {
		-foo	{-foo 3}
		-bar	{}
	}
} -cleanup {
	unset -nocomplain foo bar
} -returnCodes error -result {bad setting "-foo": must be -default, -required, -validate, -name, -boolean, -args, -enum, -#, -multi, or -alias}
#>>>
test parse_args-20.1 {no args} -body { #<<<
	parse_args::parse_args
} -returnCodes error -result {wrong # args: should be "parse_args::parse_args args args_spec ?dict?"}
#>>>
test parse_args-30.1 {Doesn't generate string rep} -body { # Known to fail currently - it's an still open problem to find a way to avoid this <<<
	set i	[expr {2-1}]
	parse_args::parse_args [list $i] {
		foo {}
	}
	tcl::unsupported::representation $i
} -cleanup {
	unset -nocomplain i foo
} -match glob -result {value is a int * no string representation}
#>>>
test parse_args-40.1 {namespace import} -body { #<<<
	set interp	[interp create]
	$interp eval {
		load {} Parse_args
		namespace import ::parse_args::*
		parse_args {-foo foo-parse_args-40.1} {
			-foo	{-default x}
		}
		set foo
	}
} -cleanup {
	interp delete $interp
	unset -nocomplain interp
} -result foo-parse_args-40.1
#>>>

# cleanup
::tcltest::cleanupTests
return

# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
