@ -7,17 +7,6 @@ namespace eval ::testspace {
set result ""
}
test parse_withdef_leaders_min_max {Test anonymous leaders with @leaders -min and -max}\
-setup $common -body {
set argd [punk::args::parse {a b c d} withdef {@leaders -min 1 -max 3} ]
lappend result [dict get $argd leaders]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{0 a 1 b 2 c} {3 d}
]
test parse_withdef_leaders_ordering_defaults {Test ordering of leaders when some have defaults}\
-setup $common -body {
@ -69,10 +58,45 @@ namespace eval ::testspace {
x a y b
]
test parse_withdef_values_no_phantom_default {Test no phantom default with intermediate optional argument}\
-setup $common -body {
#y was not received, and has no default, so should not appear in 'values' element
#we don't want to see {x a y {} z b}
set argd [punk::args::parse {a b} withdef @values x {y -optional 1} z]
set vals [dict get $argd values]
set result $vals
}\
-cleanup {
}\
-result [list\
x a z b
]
test parse_withdef_value_multiple1 {Test named value with -multiple true and required trailing value}\
-setup $common -body {
set argd [punk::args::parse {a b c} withdef @values {arg -type string -multiple 1} endval]
lappend result [dict get $argd leaders]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{} {arg {a b} endval c}
]
test parse_withdef_leader_multiple {Test named leader with -multiple true}\
test parse_withdef_value_multiple2 {Test named value followed by named value with -multiple true and a default}\
-setup $common -body {
set argd [punk::args::parse {a b c} withdef @values A {arg -type string -multiple 1 -default X}]
lappend result [dict get $argd leaders]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{} {A a arg {b c}}
]
test parse_withdef_leader_multiple1 {Test named leader with -multiple true and 1 value required}\
-setup $common -body {
#should not error
set argd [punk::args::parse {a b c} withdef {@leaders -min 0} {L -multiple 1} {@values -min 1 -max 1} V]
lappend result [dict get $argd leaders]
lappend result [dict get $argd values]
@ -83,10 +107,21 @@ namespace eval ::testspace {
{L {a b}} {V c}
]
test parse_withdef_leader_min_max {Test unnamed leaders with -min and -max}\
test parse_withdef_leader_min_max1 {Test unnamed leaders with @leaders -min and -max}\
-setup $common -body {
set argd [punk::args::parse {a b c d} withdef {@leaders -min 1 -max 3 -unnamed true} {@values -unnamed true} ]
lappend result [dict get $argd leaders]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{0 a 1 b 2 c} {3 d}
]
test parse_withdef_leader_min_max_with_required_value {Test unnamed leaders with -min and -max followed by required unnamed value}\
-setup $common -body {
#should not error - should allocate d to values
set argd [punk::args::parse {a b c d} withdef {@leaders -min 1 -max 4} {@values -min 1 -max 1}]
set argd [punk::args::parse {a b c d} withdef {@leaders -min 1 -max 4 -unnamed true } {@values -min 1 -max 1 -unnamed true }]
lappend result [dict get $argd leaders]
lappend result [dict get $argd values]
}\
@ -96,10 +131,101 @@ namespace eval ::testspace {
{0 a 1 b 2 c} {3 d}
]
test parse_withdef_leader_stride {Test stride leaders}\
test parse_withdef_value_clause_typedefaults {test clause with optional element and -typedefaults specified}\
-setup $common -body {
set argd [punk::args::parse {1} withdef @values {v -type {int ?int?} -typedefaults {"" 12}}]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{v {1 12}}
]
test parse_withdef_value_clause_typedefaults2 {test clause with optional element and -typedefaults specified - entire arg optional -default}\
-setup $common -body {
#-default has deliberate type violations - should still produce result as default is not meant to be subject to validation.
set argd [punk::args::parse {} withdef @values {v -type {int ?int?} -typedefaults {"" 12} -default {x y} -optional 1}]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{v {x y}}
]
test parse_withdef_value_clause_defaulted_optional {test clause with optional element and -typedefaults not matching all types}\
-setup $common -body {
#-typedefaults has deliberate type violations - should still produce result as defaulted value is not meant to be subject to validation.
#(uses the ?defaulted-<type>? typelist mechanism)
set argd [punk::args::parse {1} withdef @values {v -type {int ?int?} -typedefaults {"" xxx}}]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{v {1 xxx}}
]
test parse_withdef_value_clause_missing_optional {test clause with optional element and no -typedefaults}\
-setup $common -body {
#an optional clause member will be replaced with empty string when missing if there is no -typedefaults
#This empty string needs to be in the result, but not be subject to validation
#(uses the ?ommitted-<type>? typelist mechanism)
set argd [punk::args::parse {1} withdef @values {v -type {int ?int?}}]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{v {1 {}}}
]
test parse_withdef_value_clause_arity1 {Test value clause result with optional member}\
-setup $common -body {
#default for missing optional member ?literal(then)? should be empty string
set argd [punk::args::parse {elseif 1 x} withdef {@values} {"elseifclause" -type {literal(elseif) expr ?literal(then)? any}}]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{elseifclause {elseif 1 {} x}}
]
test parse_withdef_value_clause_arity2 {Test value clause result with missing optional member in optional clauses at tail}\
-setup $common -body {
set argd [punk::args::parse {1 2 x 1 y} withdef {@values -unnamed true} {arg -multiple 1} {X -type {literal(x) any} -optional 1} {Y -type {literal(y) ?int?} -optional 1}]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{arg {1 2} X {x 1} Y {y {}}}
]
test parse_withdef_value_clause_arity3 {Test value clause result with filled optional member in optional clauses at tail}\
-setup $common -body {
set argd [punk::args::parse {1 2 x 1 y 2} withdef {@values -unnamed true} {arg -multiple 1} {X -type {literal(x) any} -optional 1} {Y -type {literal(y) ?int?} -optional 1}]
lappend result [dict get $argd values]
}\
-cleanup {
}\
-result [list\
{arg {1 2} X {x 1} Y {y 2}}
]
#todo - test L1 parsed to Lit1 not arg
#punk::args::parse {x y L1} withdef @values (arg -multiple 1) {lit1 -type literal(L1) -optional 1} {lit2 -type literal(L2) -optional 1}
#todo
#see i -form 1 file copy -- x
#fix end-of-opts handling
#see also file copy -force x
#(not handled by punk::args as the command does..)
test parse_withdef_leader_clause {Test leader clause with multiple}\
-setup $common -body {
#see for example ::tcl::dict::create which has a stride of 2
set argd [punk::args::parse {k v e k1 v1 k2 v2} withdef {@leaders} {"key val etc" -multiple 0} {"key val" -multiple 1} {@values -min 0 -max 0}]
#see for example ::tcl::dict::create which has a clause length of 2
set argd [punk::args::parse {k v e k1 v1 k2 v2} withdef {@leaders} {"key val etc" -type {any any any} - multiple 0} {"key val" -type {any any} -multiple 1} {@values -min 0 -max 0}]
lappend result [dict get $argd leaders]
}\
-cleanup {
@ -108,10 +234,10 @@ namespace eval ::testspace {
{{key val etc} {k v e} {key val} {{k1 v1} {k2 v2}}}
]
test parse_withdef_value_stride {Test stride values }\
test parse_withdef_value_clause_multiple {Test value clause with multiple }\
-setup $common -body {
#see for example ::tcl::dict::create which has a stride of 2
set argd [punk::args::parse {k v e k1 v1 k2 v2} withdef {@values} {"key val etc" -multiple 0} {"key val" -multiple 1}]
#see for example ::tcl::dict::create which has a clause length of 2
set argd [punk::args::parse {k v e k1 v1 k2 v2} withdef {@values} {"key val etc" -type {any any any} - multiple 0} {"key val" -type {any any} -multiple 1}]
lappend result [dict get $argd values]
}\
-cleanup {
@ -120,15 +246,15 @@ namespace eval ::testspace {
{{key val etc} {k v e} {key val} {{k1 v1} {k2 v2}}}
]
test parse_withdef_value_strid e_error {Test stride values with error due to not enough args for strid e}\
test parse_withdef_value_clau se_error {Test value clau se with error due to not enough args for clau se}\
-setup $common -body {
#see for example ::tcl::dict::create which has a stride of 2
if {[catch {punk::args::parse {k v} withdef {@values} {"key val etc" -multiple 0}} emsg eopts]} {
#see for example ::tcl::dict::create which has a clause length of 2
if {[catch {punk::args::parse {k v} withdef {@values} {"key val etc" -type {any any any} - multiple 0}} emsg eopts]} {
set expected [dict get $eopts -errorcode]
if {[lindex $expected 0] eq "PUNKARGS" && [lindex $expected 1] eq "VALIDATION" && [lindex $expected 2 0] eq "stridevaluecount "} {
if {[lindex $expected 0] eq "PUNKARGS" && [lindex $expected 1] eq "VALIDATION" && [lindex $expected 2 0] eq "clausevaluelength "} {
lappend result "RECEIVED_EXPECTED_ERROR"
} else {
lappend result "WRONG_ERROR_RECEIVED - $expected (expected PUNKARGS VALIDATION {stridevaluecount ...} ..."
lappend result "WRONG_ERROR_RECEIVED - $expected (expected PUNKARGS VALIDATION {clausevaluelength ...} ..."
}
} else {
lappend result "MISSING_REQUIRED_ERROR"