Skip to content
Tcl Source Code Artifact [d340ea90f6]
Login

Artifact [d340ea90f6]

Artifact d340ea90f62b3678328259aa67575574fc3e21f0c2b1764ef47a9b7242b09a9d:


if {"::tcltest" ni [namespace children]} {
    package require tcltest 2
    namespace import -force ::tcltest::*
}

testConstraint procbodytest [expr {![catch {package require procbodytest}]}]
testConstraint memory	    [llength [info commands memory]]

set setup {
    proc x {a args b} {
        return "a=$a, args=$args, b=$b"
    }
    proc y {a {b x} args c} {
        return "a=$a, b=$b, args=$args, c=$c"
    }
}
set cleanup {rename x {}; rename y {}}

test tip288-1.1 {Error cases, repeated args} -body {
    proc z {a args args} {
    }
} -returnCodes error -result {repeated "args" in argument list}

test tip288-1.2 {Examples for TIP#288} -setup $setup -cleanup $cleanup -body {
    x 1 2
} -result {a=1, args=, b=2}

test tip288-1.3 {Examples for TIP#288} -setup $setup -cleanup $cleanup -body {
    x 1 2 3
} -result {a=1, args=2, b=3}

test tip288-1.4 {Examples for TIP#288} -setup $setup -cleanup $cleanup -body {
    x 1
} -returnCodes error -result {wrong # args: should be "x a ?arg ...? b"}

test tip288-1.5 {Examples for TIP#288} -setup $setup -cleanup $cleanup -body {
    y 1 2 3
} -result {a=1, b=2, args=, c=3}

test tip288-1.6 {Examples for TIP#288} -setup $setup -cleanup $cleanup -body {
    y 1 2
} -result {a=1, b=x, args=, c=2}

# This is now an error
test tip288-1.7 {Examples for TIP#288} -body {
    proc z {a {b x} c args} {
        return "a=$a, b=$b, c=$c, args=$args"
    }
} -returnCodes error -result {required arg may not be in the middle}

# A default value for "args" is still allowed but pointless
test tip288-1.8 {Examples for TIP#288} -body {
    proc z {a b {args {b}}} {
        return "a=$a, b=$b, args=$args"
    }
    z 1 2
} -result {a=1, b=2, args=}


set setup {
    proc x {a b {c _c} {d _d} args {e _e} {f _f} g h} {
        list a $a b $b c $c d $d e $e f $f g $g h $h args $args
    }
}
set cleanup {rename x {}}

test tip288-2.1 {Pathological arglist} -setup $setup -cleanup $cleanup -body {
    x 1 2 3
} -returnCodes error -result {wrong # args: should be "x a b ?c? ?d? ?arg ...? ?e? ?f? g h"}

set i 1
foreach {args result} {
        {1 2 3 4}               {a 1 b 2 c _c d _d e _e f _f g 3 h 4  args {}}
        {1 2 3 4 5}             {a 1 b 2 c 3  d _d e _e f _f g 4 h 5  args {}}
        {1 2 3 4 5 6}           {a 1 b 2 c 3  d 4  e _e f _f g 5 h 6  args {}}
        {1 2 3 4 5 6 7}         {a 1 b 2 c 3  d 4  e _e f 5  g 6 h 7  args {}}
        {1 2 3 4 5 6 7 8}       {a 1 b 2 c 3  d 4  e 5  f 6  g 7 h 8  args {}}
        {1 2 3 4 5 6 7 8 9}     {a 1 b 2 c 3  d 4  e 6  f 7  g 8 h 9  args 5}
        {1 2 3 4 5 6 7 8 9 0}   {a 1 b 2 c 3  d 4  e 7  f 8  g 9 h 0  args {5 6}}
} {
    test tip288-2.[incr i] {Pathological arglist} -setup $setup -cleanup $cleanup -body [
        list x {*}$args
    ] -result [list {*}$result]
}

set setup {
    proc stup {{chan stdout} text} {
        list chan $chan text $text
    }
}
set cleanup {rename stup {}}
set i 0
foreach {args code result} {
        {}             error {wrong # args: should be "stup ?chan? text"}
        {foo}          ok    {chan stdout text foo}
        {foo bar}      ok    {chan foo text bar}
        {foo bar baz}  error {wrong # args: should be "stup ?chan? text"}
} {
    test tip288-3.[incr i] {Pathological arglist} -setup $setup -cleanup $cleanup -body [
        list stup {*}$args
    ] -returnCodes $code -result $result
}