| 1 | # Package covered:  opt1.0/optparse.tcl | 
|---|
| 2 | # | 
|---|
| 3 | # This file contains a collection of tests for one or more of the Tcl | 
|---|
| 4 | # built-in commands.  Sourcing this file into Tcl runs the tests and | 
|---|
| 5 | # generates output for errors.  No output means no errors were found. | 
|---|
| 6 | # | 
|---|
| 7 | # Copyright (c) 1991-1993 The Regents of the University of California. | 
|---|
| 8 | # Copyright (c) 1994-1997 Sun Microsystems, Inc. | 
|---|
| 9 | # Copyright (c) 1998-1999 by Scriptics Corporation. | 
|---|
| 10 | # | 
|---|
| 11 | # See the file "license.terms" for information on usage and redistribution | 
|---|
| 12 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
| 13 | # | 
|---|
| 14 | # RCS: @(#) $Id: opt.test,v 1.9 2004/05/19 12:48:32 dkf Exp $ | 
|---|
| 15 |  | 
|---|
| 16 | if {[lsearch [namespace children] ::tcltest] == -1} { | 
|---|
| 17 |     package require tcltest | 
|---|
| 18 |     namespace import -force ::tcltest::* | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | # the package we are going to test | 
|---|
| 22 | package require opt 0.4.1 | 
|---|
| 23 |  | 
|---|
| 24 | # we are using implementation specifics to test the package | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | #### functions tests ##### | 
|---|
| 28 |  | 
|---|
| 29 | set n $::tcl::OptDescN | 
|---|
| 30 |  | 
|---|
| 31 | test opt-1.1 {OptKeyRegister / check that auto allocation is skipping existing keys} { | 
|---|
| 32 |     list [::tcl::OptKeyRegister {} $n] [::tcl::OptKeyRegister {} [expr $n+1]] [::tcl::OptKeyRegister {}] | 
|---|
| 33 | } "$n [expr $n+1] [expr $n+2]" | 
|---|
| 34 |  | 
|---|
| 35 | test opt-2.1 {OptKeyDelete} { | 
|---|
| 36 |     list [::tcl::OptKeyRegister {} testkey] \ | 
|---|
| 37 |             [info exists ::tcl::OptDesc(testkey)] \ | 
|---|
| 38 |             [::tcl::OptKeyDelete testkey] \ | 
|---|
| 39 |             [info exists ::tcl::OptDesc(testkey)] | 
|---|
| 40 | } {testkey 1 {} 0} | 
|---|
| 41 |  | 
|---|
| 42 | test opt-3.1 {OptParse / temp key is removed} { | 
|---|
| 43 |     set n $::tcl::OptDescN | 
|---|
| 44 |     set prev [array names ::tcl::OptDesc] | 
|---|
| 45 |     ::tcl::OptKeyRegister {} $n | 
|---|
| 46 |     list [info exists ::tcl::OptDesc($n)]\ | 
|---|
| 47 |             [::tcl::OptKeyDelete $n]\ | 
|---|
| 48 |             [::tcl::OptParse {{-foo}} {}]\ | 
|---|
| 49 |             [info exists ::tcl::OptDesc($n)]\ | 
|---|
| 50 |             [expr {"[lsort $prev]"=="[lsort [array names ::tcl::OptDesc]]"}] | 
|---|
| 51 | } {1 {} {} 0 1} | 
|---|
| 52 | test opt-3.2 {OptParse / temp key is removed even on errors} { | 
|---|
| 53 |     set n $::tcl::OptDescN | 
|---|
| 54 |     catch {::tcl::OptKeyDelete $n} | 
|---|
| 55 |     list [catch {::tcl::OptParse {{-foo}} {-blah}}] \ | 
|---|
| 56 |             [info exists ::tcl::OptDesc($n)] | 
|---|
| 57 | } {1 0} | 
|---|
| 58 |  | 
|---|
| 59 | test opt-4.1 {OptProc} { | 
|---|
| 60 |     ::tcl::OptProc optTest {} {} | 
|---|
| 61 |     optTest ; | 
|---|
| 62 |     ::tcl::OptKeyDelete optTest | 
|---|
| 63 | } {} | 
|---|
| 64 |  | 
|---|
| 65 | test opt-5.1 {OptProcArgGiven} { | 
|---|
| 66 |     ::tcl::OptProc optTest {{-foo}} { | 
|---|
| 67 |         if {[::tcl::OptProcArgGiven "-foo"]} { | 
|---|
| 68 |             return 1 | 
|---|
| 69 |         } else { | 
|---|
| 70 |             return 0 | 
|---|
| 71 |         } | 
|---|
| 72 |     } | 
|---|
| 73 |     list [optTest] [optTest -f] [optTest -F] [optTest -fOO] | 
|---|
| 74 | } {0 1 1 1} | 
|---|
| 75 |  | 
|---|
| 76 | test opt-6.1 {OptKeyParse} { | 
|---|
| 77 |     ::tcl::OptKeyRegister {} test; | 
|---|
| 78 |     list [catch {::tcl::OptKeyParse test {-help}} msg] $msg | 
|---|
| 79 | } {1 {Usage information: | 
|---|
| 80 |     Var/FlagName Type Value Help | 
|---|
| 81 |     ------------ ---- ----- ---- | 
|---|
| 82 |     ( -help                 gives this help )}} | 
|---|
| 83 |  | 
|---|
| 84 | test opt-7.1 {OptCheckType} { | 
|---|
| 85 |     list \ | 
|---|
| 86 |             [::tcl::OptCheckType 23 int] \ | 
|---|
| 87 |             [::tcl::OptCheckType 23 float] \ | 
|---|
| 88 |             [::tcl::OptCheckType true boolean] \ | 
|---|
| 89 |             [::tcl::OptCheckType "-blah" any] \ | 
|---|
| 90 |             [::tcl::OptCheckType {a b c} list] \ | 
|---|
| 91 |             [::tcl::OptCheckType maYbe choice {yes maYbe no}] \ | 
|---|
| 92 |             [catch {::tcl::OptCheckType "-blah" string}] \ | 
|---|
| 93 |             [catch {::tcl::OptCheckType 6 boolean}] \ | 
|---|
| 94 |             [catch {::tcl::OptCheckType x float}] \ | 
|---|
| 95 |             [catch {::tcl::OptCheckType "a \{ c" list}] \ | 
|---|
| 96 |             [catch {::tcl::OptCheckType 2.3 int}] \ | 
|---|
| 97 |             [catch {::tcl::OptCheckType foo choice {x y Foo z}}] | 
|---|
| 98 | } {23 23.0 1 -blah {a b c} maYbe 1 1 1 1 1 1} | 
|---|
| 99 |  | 
|---|
| 100 | test opt-8.1 {List utilities} { | 
|---|
| 101 |     ::tcl::Lempty {} | 
|---|
| 102 | } 1 | 
|---|
| 103 | test opt-8.2 {List utilities} { | 
|---|
| 104 |     ::tcl::Lempty {a b c} | 
|---|
| 105 | } 0 | 
|---|
| 106 | test opt-8.3 {List utilities} { | 
|---|
| 107 |     ::tcl::Lget {a {b c d} e} {1 2} | 
|---|
| 108 | } d | 
|---|
| 109 | test opt-8.4 {List utilities} { | 
|---|
| 110 |     set l {a {b c d e} f} | 
|---|
| 111 |     ::tcl::Lvarset l {1 2} D | 
|---|
| 112 |     set l | 
|---|
| 113 | } {a {b c D e} f} | 
|---|
| 114 | test opt-8.5 {List utilities} { | 
|---|
| 115 |     set l {a b c} | 
|---|
| 116 |     ::tcl::Lvarset1 l 6 X | 
|---|
| 117 |     set l | 
|---|
| 118 | } {a b c {} {} {} X} | 
|---|
| 119 | test opt-8.6 {List utilities} { | 
|---|
| 120 |     set l {a {b c 7 e} f} | 
|---|
| 121 |     ::tcl::Lvarincr l {1 2} | 
|---|
| 122 |     set l | 
|---|
| 123 | } {a {b c 8 e} f} | 
|---|
| 124 | test opt-8.7 {List utilities} { | 
|---|
| 125 |     set l {a {b c 7 e} f} | 
|---|
| 126 |     ::tcl::Lvarincr l {1 2} -9 | 
|---|
| 127 |     set l | 
|---|
| 128 | } {a {b c -2 e} f} | 
|---|
| 129 | # 8.8 and 8.9 missing? | 
|---|
| 130 | test opt-8.10 {List utilities} { | 
|---|
| 131 |     set l {a {b c 7 e} f} | 
|---|
| 132 |     ::tcl::Lvarpop l | 
|---|
| 133 |     set l | 
|---|
| 134 | } {{b c 7 e} f} | 
|---|
| 135 | test opt-8.11 {List utilities} { | 
|---|
| 136 |     catch {unset x} | 
|---|
| 137 |     set l {a {b c 7 e} f} | 
|---|
| 138 |     list [::tcl::Lassign $l u v w x] \ | 
|---|
| 139 |             $u $v $w [info exists x] | 
|---|
| 140 | } {3 a {b c 7 e} f 0} | 
|---|
| 141 |  | 
|---|
| 142 | test opt-9.1 {Misc utilities} { | 
|---|
| 143 |     catch {unset v} | 
|---|
| 144 |     ::tcl::SetMax v 3 | 
|---|
| 145 |     ::tcl::SetMax v 7 | 
|---|
| 146 |     ::tcl::SetMax v 6 | 
|---|
| 147 |     set v | 
|---|
| 148 | } 7 | 
|---|
| 149 | test opt-9.2 {Misc utilities} { | 
|---|
| 150 |     catch {unset v} | 
|---|
| 151 |     ::tcl::SetMin v 3 | 
|---|
| 152 |     ::tcl::SetMin v -7 | 
|---|
| 153 |     ::tcl::SetMin v 1 | 
|---|
| 154 |     set v | 
|---|
| 155 | } -7 | 
|---|
| 156 |  | 
|---|
| 157 | #### behaviour tests ##### | 
|---|
| 158 |  | 
|---|
| 159 | test opt-10.1 {ambigous flags} { | 
|---|
| 160 |     ::tcl::OptProc optTest {{-fla} {-other} {-flag2xyz} {-flag3xyz}} {} | 
|---|
| 161 |     catch {optTest -fL} msg | 
|---|
| 162 |     set msg | 
|---|
| 163 | } {ambigous option "-fL", choose from: | 
|---|
| 164 |     -fla      boolflag (false)  | 
|---|
| 165 |     -flag2xyz boolflag (false)  | 
|---|
| 166 |     -flag3xyz boolflag (false) } | 
|---|
| 167 | test opt-10.2 {non ambigous flags} { | 
|---|
| 168 |     ::tcl::OptProc optTest {{-flag1xyz} {-other} {-flag2xyz} {-flag3xyz}} { | 
|---|
| 169 |         return $flag2xyz | 
|---|
| 170 |     } | 
|---|
| 171 |     optTest -fLaG2 | 
|---|
| 172 | } 1 | 
|---|
| 173 | test opt-10.3 {non ambigous flags because of exact match} { | 
|---|
| 174 |     ::tcl::OptProc optTest {{-flag1x} {-other} {-flag1} {-flag1xy}} { | 
|---|
| 175 |         return $flag1 | 
|---|
| 176 |     } | 
|---|
| 177 |     optTest -flAg1 | 
|---|
| 178 | } 1 | 
|---|
| 179 | test opt-10.4 {ambigous flags, not exact match} { | 
|---|
| 180 |     ::tcl::OptProc optTest {{-flag1xy} {-other} {-flag1} {-flag1xyz}} { | 
|---|
| 181 |         return $flag1 | 
|---|
| 182 |     } | 
|---|
| 183 |     catch {optTest -fLag1X} msg | 
|---|
| 184 |     set msg | 
|---|
| 185 | } {ambigous option "-fLag1X", choose from: | 
|---|
| 186 |     -flag1xy  boolflag (false)  | 
|---|
| 187 |     -flag1xyz boolflag (false) } | 
|---|
| 188 |  | 
|---|
| 189 | # medium size overall test example: (defined once) | 
|---|
| 190 | ::tcl::OptProc optTest { | 
|---|
| 191 |     {cmd -choice {print save delete} "sub command to choose"} | 
|---|
| 192 |     {-allowBoing -boolean true} | 
|---|
| 193 |     {arg2 -string "this is help"} | 
|---|
| 194 |     {?arg3? 7 "optional number"} | 
|---|
| 195 |     {-moreflags} | 
|---|
| 196 | } { | 
|---|
| 197 |     list $cmd $allowBoing $arg2 $arg3 $moreflags | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | test opt-10.5 {medium size overall test} { | 
|---|
| 201 |     list [catch {optTest} msg] $msg | 
|---|
| 202 | } {1 {no value given for parameter "cmd" (use -help for full usage) : | 
|---|
| 203 |     cmd choice (print save delete) sub command to choose}} | 
|---|
| 204 | test opt-10.6 {medium size overall test} { | 
|---|
| 205 |     list [catch {optTest -help} msg] $msg | 
|---|
| 206 | } {1 {Usage information: | 
|---|
| 207 |     Var/FlagName Type     Value   Help | 
|---|
| 208 |     ------------ ----     -----   ---- | 
|---|
| 209 |     ( -help                       gives this help ) | 
|---|
| 210 |     cmd          choice   (print save delete) sub command to choose | 
|---|
| 211 |     -allowBoing  boolean  (true)   | 
|---|
| 212 |     arg2         string   ()      this is help | 
|---|
| 213 |     ?arg3?       int      (7)     optional number | 
|---|
| 214 |     -moreflags   boolflag (false) }} | 
|---|
| 215 | test opt-10.7 {medium size overall test} { | 
|---|
| 216 |     optTest save tst | 
|---|
| 217 | } {save 1 tst 7 0} | 
|---|
| 218 | test opt-10.8 {medium size overall test} { | 
|---|
| 219 |     optTest save -allowBoing false -- 8 | 
|---|
| 220 | } {save 0 8 7 0} | 
|---|
| 221 | test opt-10.9 {medium size overall test} { | 
|---|
| 222 |     optTest save tst -m -- | 
|---|
| 223 | } {save 1 tst 7 1} | 
|---|
| 224 | test opt-10.10 {medium size overall test} { | 
|---|
| 225 |     list [catch {optTest save tst foo} msg] [lindex [split $msg "\n"] 0] | 
|---|
| 226 | } {1 {too many arguments (unexpected argument(s): foo), usage:}} | 
|---|
| 227 |  | 
|---|
| 228 | test opt-11.1 {too many args test 2} { | 
|---|
| 229 |     set key [::tcl::OptKeyRegister {-foo}] | 
|---|
| 230 |     list [catch {::tcl::OptKeyParse $key {-foo blah}} msg] $msg\ | 
|---|
| 231 |             [::tcl::OptKeyDelete $key] | 
|---|
| 232 | } {1 {too many arguments (unexpected argument(s): blah), usage: | 
|---|
| 233 |     Var/FlagName Type     Value   Help | 
|---|
| 234 |     ------------ ----     -----   ---- | 
|---|
| 235 |     ( -help                       gives this help ) | 
|---|
| 236 |     -foo         boolflag (false) } {}} | 
|---|
| 237 | test opt-11.2 {default value for args} { | 
|---|
| 238 |     set args {} | 
|---|
| 239 |     set key [::tcl::OptKeyRegister {{args -list {a b c} "args..."}}] | 
|---|
| 240 |     ::tcl::OptKeyParse $key {} | 
|---|
| 241 |     ::tcl::OptKeyDelete $key | 
|---|
| 242 |     set args | 
|---|
| 243 | } {a b c} | 
|---|
| 244 |  | 
|---|
| 245 | # cleanup | 
|---|
| 246 | ::tcltest::cleanupTests | 
|---|
| 247 | return | 
|---|