OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [tests/] [opt.test] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# Package covered:  opt0.1/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
#
10
# See the file "license.terms" for information on usage and redistribution
11
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
#
13
# RCS: @(#) $Id: opt.test,v 1.1.1.1 2002-01-16 10:25:36 markom Exp $
14
 
15
if {[string compare test [info procs test]] == 1} then {source defs}
16
 
17
# the package we are going to test
18
package require opt 0.1
19
 
20
# we are using implementation specifics to test the package
21
 
22
 
23
#### functions tests #####
24
 
25
set n $::tcl::OptDescN
26
 
27
test opt-1.1 {OptKeyRegister / check that auto allocation is skipping existing keys} {
28
    list [::tcl::OptKeyRegister {} $n] [::tcl::OptKeyRegister {} [expr $n+1]] [::tcl::OptKeyRegister {}]
29
} "$n [expr $n+1] [expr $n+2]"
30
 
31
test opt-2.1 {OptKeyDelete} {
32
    list [::tcl::OptKeyRegister {} testkey] [::tcl::OptKeyDelete testkey] \
33
            [catch {::tcl::OptKeyDelete testkey} msg] $msg;
34
} {testkey {} 1 {can't unset "OptDesc(testkey)": no such element in array}}
35
 
36
 
37
test opt-3.1 {OptParse / temp key is removed} {
38
    set n $::tcl::OptDescN
39
    set prev [array names ::tcl::OptDesc]
40
    ::tcl::OptKeyRegister {} $n
41
    list [info exists ::tcl::OptDesc($n)]\
42
            [::tcl::OptKeyDelete $n]\
43
            [::tcl::OptParse {{-foo}} {}]\
44
            [info exists ::tcl::OptDesc($n)]\
45
            [expr {"[lsort $prev]"=="[lsort [array names ::tcl::OptDesc]]"}]
46
} {1 {} {} 0 1}
47
 
48
 
49
test opt-3.2 {OptParse / temp key is removed even on errors} {
50
    set n $::tcl::OptDescN
51
    catch {::tcl::OptKeyDelete $n}
52
    list [catch {::tcl::OptParse {{-foo}} {-blah}}] \
53
            [info exists ::tcl::OptDesc($n)]
54
} {1 0}
55
 
56
test opt-4.1 {OptProc} {
57
    ::tcl::OptProc optTest {} {}
58
    optTest ;
59
    ::tcl::OptKeyDelete optTest
60
} {}
61
 
62
 
63
test opt-5.1 {OptProcArgGiven} {
64
    ::tcl::OptProc optTest {{-foo}} {
65
        if {[::tcl::OptProcArgGiven "-foo"]} {
66
            return 1
67
        } else {
68
            return 0
69
        }
70
    }
71
    list [optTest] [optTest -f] [optTest -F] [optTest -fOO]
72
} {0 1 1 1}
73
 
74
test opt-6.1 {OptKeyParse} {
75
    ::tcl::OptKeyRegister {} test;
76
    list [catch {::tcl::OptKeyParse test {-help}} msg] $msg
77
} {1 {Usage information:
78
    Var/FlagName Type Value Help
79
    ------------ ---- ----- ----
80
    ( -help                 gives this help )}}
81
 
82
 
83
test opt-7.1 {OptCheckType} {
84
    list \
85
            [::tcl::OptCheckType 23 int] \
86
            [::tcl::OptCheckType 23 float] \
87
            [::tcl::OptCheckType true boolean] \
88
            [::tcl::OptCheckType "-blah" any] \
89
            [::tcl::OptCheckType {a b c} list] \
90
            [::tcl::OptCheckType maYbe choice {yes maYbe no}] \
91
            [catch {::tcl::OptCheckType "-blah" string}] \
92
            [catch {::tcl::OptCheckType 6 boolean}] \
93
            [catch {::tcl::OptCheckType x float}] \
94
            [catch {::tcl::OptCheckType "a \{ c" list}] \
95
            [catch {::tcl::OptCheckType 2.3 int}] \
96
            [catch {::tcl::OptCheckType foo choice {x y Foo z}}]
97
} {23 23.0 1 -blah {a b c} maYbe 1 1 1 1 1 1}
98
 
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
 
110
test opt-8.4 {List utilities} {
111
    set l {a {b c d e} f}
112
    ::tcl::Lvarset l {1 2} D
113
    set l
114
} {a {b c D e} f}
115
 
116
test opt-8.5 {List utilities} {
117
    set l {a b c}
118
    ::tcl::Lvarset1 l 6 X
119
    set l
120
} {a b c {} {} {} X}
121
 
122
test opt-8.6 {List utilities} {
123
    set l {a {b c 7 e} f}
124
    ::tcl::Lvarincr l {1 2}
125
    set l
126
} {a {b c 8 e} f}
127
 
128
test opt-8.7 {List utilities} {
129
    set l {a {b c 7 e} f}
130
    ::tcl::Lvarincr l {1 2} -9
131
    set l
132
} {a {b c -2 e} f}
133
 
134
test opt-8.8 {List utilities} {
135
    set l {{b c 7 e} f}
136
    ::tcl::Lfirst $l
137
} {b c 7 e}
138
 
139
 
140
test opt-8.9 {List utilities} {
141
    set l {a {b c 7 e} f}
142
    ::tcl::Lrest $l
143
} {{b c 7 e} f}
144
 
145
test opt-8.10 {List utilities} {
146
    set l {a {b c 7 e} f}
147
    ::tcl::Lvarpop l
148
    set l
149
} {{b c 7 e} f}
150
 
151
test opt-8.11 {List utilities} {
152
    catch {unset x}
153
    set l {a {b c 7 e} f}
154
    list [::tcl::Lassign $l u v w x] \
155
            $u $v $w [info exists x]
156
} {3 a {b c 7 e} f 0}
157
 
158
test opt-9.1 {Misc utilities} {
159
    catch {unset v}
160
    ::tcl::SetMax v 3
161
    ::tcl::SetMax v 7
162
    ::tcl::SetMax v 6
163
    set v
164
} 7
165
 
166
test opt-9.2 {Misc utilities} {
167
    catch {unset v}
168
    ::tcl::SetMin v 3
169
    ::tcl::SetMin v -7
170
    ::tcl::SetMin v 1
171
    set v
172
} -7
173
 
174
#### behaviour tests #####
175
 
176
test opt-10.1 {ambigous flags} {
177
    ::tcl::OptProc optTest {{-fla} {-other} {-flag2xyz} {-flag3xyz}} {}
178
    catch {optTest -fL} msg
179
    set msg
180
} {ambigous option "-fL", choose from:
181
    -fla      boolflag (false)
182
    -flag2xyz boolflag (false)
183
    -flag3xyz boolflag (false) }
184
 
185
test opt-10.2 {non ambigous flags} {
186
    ::tcl::OptProc optTest {{-flag1xyz} {-other} {-flag2xyz} {-flag3xyz}} {
187
        return $flag2xyz
188
    }
189
    optTest -fLaG2
190
} 1
191
 
192
test opt-10.3 {non ambigous flags because of exact match} {
193
    ::tcl::OptProc optTest {{-flag1x} {-other} {-flag1} {-flag1xy}} {
194
        return $flag1
195
    }
196
    optTest -flAg1
197
} 1
198
 
199
test opt-10.4 {ambigous flags, not exact match} {
200
    ::tcl::OptProc optTest {{-flag1xy} {-other} {-flag1} {-flag1xyz}} {
201
        return $flag1
202
    }
203
    catch {optTest -fLag1X} msg
204
    set msg
205
} {ambigous option "-fLag1X", choose from:
206
    -flag1xy  boolflag (false)
207
    -flag1xyz boolflag (false) }
208
 
209
 
210
 
211
# medium size overall test example: (defined once)
212
::tcl::OptProc optTest {
213
    {cmd -choice {print save delete} "sub command to choose"}
214
    {-allowBoing -boolean true}
215
    {arg2 -string "this is help"}
216
    {?arg3? 7 "optional number"}
217
    {-moreflags}
218
} {
219
    list $cmd $allowBoing $arg2 $arg3 $moreflags
220
}
221
 
222
test opt-10.5 {medium size overall test} {
223
    list [catch {optTest} msg] $msg
224
} {1 {no value given for parameter "cmd" (use -help for full usage) :
225
    cmd choice (print save delete) sub command to choose}}
226
 
227
 
228
test opt-10.6 {medium size overall test} {
229
    list [catch {optTest -help} msg] $msg
230
} {1 {Usage information:
231
    Var/FlagName Type     Value   Help
232
    ------------ ----     -----   ----
233
    ( -help                       gives this help )
234
    cmd          choice   (print save delete) sub command to choose
235
    -allowBoing  boolean  (true)
236
    arg2         string   ()      this is help
237
    ?arg3?       int      (7)     optional number
238
    -moreflags   boolflag (false) }}
239
 
240
test opt-10.7 {medium size overall test} {
241
    optTest save tst
242
} {save 1 tst 7 0}
243
 
244
test opt-10.8 {medium size overall test} {
245
    optTest save -allowBoing false -- 8
246
} {save 0 8 7 0}
247
 
248
test opt-10.9 {medium size overall test} {
249
    optTest save tst -m --
250
} {save 1 tst 7 1}
251
 
252
test opt-10.10 {medium size overall test} {
253
    list [catch {optTest save tst foo} msg] [lindex [split $msg "\n"] 0]
254
} {1 {too many arguments (unexpected argument(s): foo), usage:}}
255
 
256
 
257
test opt-11.1 {too many args test 2} {
258
    set key [::tcl::OptKeyRegister {-foo}]
259
    list [catch {::tcl::OptKeyParse $key {-foo blah}} msg] $msg\
260
            [::tcl::OptKeyDelete $key]
261
} {1 {too many arguments (unexpected argument(s): blah), usage:
262
    Var/FlagName Type     Value   Help
263
    ------------ ----     -----   ----
264
    ( -help                       gives this help )
265
    -foo         boolflag (false) } {}}
266
 
267
 
268
 
269
test opt-11.2 {default value for args} {
270
    set args {}
271
    set key [::tcl::OptKeyRegister {{args -list {a b c} "args..."}}]
272
    ::tcl::OptKeyParse $key {}
273
    ::tcl::OptKeyDelete $key
274
    set args
275
} {a b c}
276
 
277
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.