Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/parseOld.test @ 42

Last change on this file since 42 was 25, checked in by landauf, 18 years ago

added tcl to libs

File size: 15.8 KB
Line 
1# Commands covered:  set (plus basic command syntax).  Also tests the
2# procedures in the file tclOldParse.c.  This set of tests is an old
3# one that predates the new parser in Tcl 8.1.
4#
5# This file contains a collection of tests for one or more of the Tcl
6# built-in commands.  Sourcing this file into Tcl runs the tests and
7# generates output for errors.  No output means no errors were found.
8#
9# Copyright (c) 1991-1993 The Regents of the University of California.
10# Copyright (c) 1994-1996 Sun Microsystems, Inc.
11# Copyright (c) 1998-1999 by Scriptics Corporation.
12#
13# See the file "license.terms" for information on usage and redistribution
14# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15#
16# RCS: @(#) $Id: parseOld.test,v 1.14 2006/10/09 19:15:45 msofer Exp $
17
18if {[lsearch [namespace children] ::tcltest] == -1} {
19    package require tcltest
20    namespace import -force ::tcltest::*
21}
22
23testConstraint testwordend [llength [info commands testwordend]]
24
25# Save the argv value for restoration later
26set savedArgv $argv
27
28proc fourArgs {a b c d} {
29    global arg1 arg2 arg3 arg4
30    set arg1 $a
31    set arg2 $b
32    set arg3 $c
33    set arg4 $d
34}
35
36proc getArgs args {
37    global argv
38    set argv $args
39}
40
41# Basic argument parsing.
42
43test parseOld-1.1 {basic argument parsing} {
44    set arg1 {}
45    fourArgs a b        c                d
46    list $arg1 $arg2 $arg3 $arg4
47} {a b c d}
48test parseOld-1.2 {basic argument parsing} {
49    set arg1 {}
50    eval "fourArgs 123\v4\f56\r7890"
51    list $arg1 $arg2 $arg3 $arg4
52} {123 4 56 7890}
53
54# Quotes.
55
56test parseOld-2.1 {quotes and variable-substitution} {
57    getArgs "a b c" d
58    set argv
59} {{a b c} d}
60test parseOld-2.2 {quotes and variable-substitution} {
61    set a 101
62    getArgs "a$a b c"
63    set argv
64} {{a101 b c}}
65test parseOld-2.3 {quotes and variable-substitution} {
66    set argv "xy[format xabc]"
67    set argv
68} {xyxabc}
69test parseOld-2.4 {quotes and variable-substitution} {
70    set argv "xy\t"
71    set argv
72} xy\t
73test parseOld-2.5 {quotes and variable-substitution} {
74    set argv "a b       c
75d e f"
76    set argv
77} a\ b\tc\nd\ e\ f
78test parseOld-2.6 {quotes and variable-substitution} {
79    set argv a"bcd"e
80    set argv
81} {a"bcd"e}
82
83# Braces.
84
85test parseOld-3.1 {braces} {
86    getArgs {a b c} d
87    set argv
88} "{a b c} d"
89test parseOld-3.2 {braces} {
90    set a 101
91    set argv {a$a b c}
92    set b [string index $argv 1]
93    set b
94} {$}
95test parseOld-3.3 {braces} {
96    set argv {a[format xyz] b}
97    string length $argv
98} 15
99test parseOld-3.4 {braces} {
100    set argv {a\nb\}}
101    string length $argv
102} 6
103test parseOld-3.5 {braces} {
104    set argv {{{{}}}}
105    set argv
106} "{{{}}}"
107test parseOld-3.6 {braces} {
108    set argv a{{}}b
109    set argv
110} "a{{}}b"
111test parseOld-3.7 {braces} {
112    set a [format "last]"]
113    set a
114} {last]}
115
116# Command substitution.
117
118test parseOld-4.1 {command substitution} {
119    set a [format xyz]
120    set a
121} xyz
122test parseOld-4.2 {command substitution} {
123    set a a[format xyz]b[format q]
124    set a
125} axyzbq
126test parseOld-4.3 {command substitution} {
127    set a a[
128set b 22;
129format %s $b
130
131]b
132    set a
133} a22b
134test parseOld-4.4 {command substitution} {
135    set a 7.7
136    if [catch {expr int($a)}] {set a foo}
137    set a
138} 7.7
139
140# Variable substitution.
141
142test parseOld-5.1 {variable substitution} {
143    set a 123
144    set b $a
145    set b
146} 123
147test parseOld-5.2 {variable substitution} {
148    set a 345
149    set b x$a.b
150    set b
151} x345.b
152test parseOld-5.3 {variable substitution} {
153    set _123z xx
154    set b $_123z^
155    set b
156} xx^
157test parseOld-5.4 {variable substitution} {
158    set a 78
159    set b a${a}b
160    set b
161} a78b
162test parseOld-5.5 {variable substitution} {catch {$_non_existent_} msg} 1
163test parseOld-5.6 {variable substitution} {
164    catch {$_non_existent_} msg
165    set msg
166} {can't read "_non_existent_": no such variable}
167test parseOld-5.7 {array variable substitution} {
168    catch {unset a}
169    set a(xyz) 123
170    set b $a(xyz)foo
171    set b
172} 123foo
173test parseOld-5.8 {array variable substitution} {
174    catch {unset a}
175    set "a(x y z)" 123
176    set b $a(x y z)foo
177    set b
178} 123foo
179test parseOld-5.9 {array variable substitution} {
180    catch {unset a}; catch {unset qqq}
181    set "a(x y z)" qqq
182    set $a([format x]\ y [format z]) foo
183    set qqq
184} foo
185test parseOld-5.10 {array variable substitution} {
186    catch {unset a}
187    list [catch {set b $a(22)} msg] $msg
188} {1 {can't read "a(22)": no such variable}}
189test parseOld-5.11 {array variable substitution} {
190    set b a$!
191    set b
192} {a$!}
193test parseOld-5.12 {empty array name support} {
194    list [catch {set b a$()} msg] $msg
195} {1 {can't read "()": no such variable}}
196catch {unset a}
197test parseOld-5.13 {array variable substitution} {
198    catch {unset a}
199    set long {This is a very long variable, long enough to cause storage \
200        allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
201        freed up correctly, then a core leak will occur when this test is \
202        run.  This text is probably beginning to sound like drivel, but I've \
203        run out of things to say and I need more characters still.}
204    set a($long) 777
205    set b $a($long)
206    list $b [array names a]
207} {777 {{This is a very long variable, long enough to cause storage \
208        allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
209        freed up correctly, then a core leak will occur when this test is \
210        run.  This text is probably beginning to sound like drivel, but I've \
211        run out of things to say and I need more characters still.}}}
212test parseOld-5.14 {array variable substitution} {
213    catch {unset a}; catch {unset b}; catch {unset a1}
214    set a1(22) foo
215    set a(foo) bar
216    set b $a($a1(22))
217    set b
218} bar
219catch {unset a}; catch {unset a1}
220
221test parseOld-7.1 {backslash substitution} {
222    set a "\a\c\n\]\}"
223    string length $a
224} 5
225test parseOld-7.2 {backslash substitution} {
226    set a {\a\c\n\]\}}
227    string length $a
228} 10
229test parseOld-7.3 {backslash substitution} {
230    set a "abc\
231def"
232    set a
233} {abc def}
234test parseOld-7.4 {backslash substitution} {
235    set a {abc\
236def}
237    set a
238} {abc def}
239test parseOld-7.5 {backslash substitution} {
240    set msg {}
241    set a xxx
242    set error [catch {if {24 < \
243        35} {set a 22} {set \
244            a 33}} msg]
245    list $error $msg $a
246} {0 22 22}
247test parseOld-7.6 {backslash substitution} {
248    eval "concat abc\\"
249} "abc\\"
250test parseOld-7.7 {backslash substitution} {
251    eval "concat \\\na"
252} "a"
253test parseOld-7.8 {backslash substitution} {
254    eval "concat x\\\n          a"
255} "x a"
256test parseOld-7.9 {backslash substitution} {
257    eval "concat \\x"
258} "x"
259test parseOld-7.10 {backslash substitution} {
260    eval "list a b\\\nc d"
261} {a b c d}
262test parseOld-7.11 {backslash substitution} {
263    eval "list a \"b c\"\\\nd e"
264} {a {b c} d e}
265test parseOld-7.12 {backslash substitution} {
266    list \ua2
267} [bytestring "\xc2\xa2"]
268test parseOld-7.13 {backslash substitution} {
269    list \u4e21
270} [bytestring "\xe4\xb8\xa1"]
271test parseOld-7.14 {backslash substitution} {
272    list \u4e2k
273} [bytestring "\xd3\xa2k"]
274
275# Semi-colon.
276
277test parseOld-8.1 {semi-colons} {
278    set b 0
279    getArgs a;set b 2
280    set argv
281} a
282test parseOld-8.2 {semi-colons} {
283    set b 0
284    getArgs a;set b 2
285    set b
286} 2
287test parseOld-8.3 {semi-colons} {
288    getArgs a b ; set b 1
289    set argv
290} {a b}
291test parseOld-8.4 {semi-colons} {
292    getArgs a b ; set b 1
293    set b
294} 1
295
296# The following checks are to ensure that the interpreter's result
297# gets re-initialized by Tcl_Eval in all the right places.
298
299test parseOld-9.1 {result initialization} {concat abc} abc
300test parseOld-9.2 {result initialization} {concat abc; proc foo {} {}} {}
301test parseOld-9.3 {result initialization} {concat abc; proc foo {} $a} {}
302test parseOld-9.4 {result initialization} {proc foo {} [concat abc]} {}
303test parseOld-9.5 {result initialization} {concat abc; } abc
304test parseOld-9.6 {result initialization} {
305    eval {
306    concat abc
307}} abc
308test parseOld-9.7 {result initialization} {} {}
309test parseOld-9.8 {result initialization} {concat abc; ; ;} abc
310
311# Syntax errors.
312
313test parseOld-10.1 {syntax errors} {catch "set a \{bcd" msg} 1
314test parseOld-10.2 {syntax errors} {
315        catch "set a \{bcd" msg
316        set msg
317} {missing close-brace}
318test parseOld-10.3 {syntax errors} {catch {set a "bcd} msg} 1
319test parseOld-10.4 {syntax errors} {
320        catch {set a "bcd} msg
321        set msg
322} {missing "}
323#" Emacs formatting >:^(
324test parseOld-10.5 {syntax errors} {catch {set a "bcd"xy} msg} 1
325test parseOld-10.6 {syntax errors} {
326        catch {set a "bcd"xy} msg
327        set msg
328} {extra characters after close-quote}
329test parseOld-10.7 {syntax errors} {catch "set a {bcd}xy" msg} 1
330test parseOld-10.8 {syntax errors} {
331        catch "set a {bcd}xy" msg
332        set msg
333} {extra characters after close-brace}
334test parseOld-10.9 {syntax errors} {catch {set a [format abc} msg} 1
335test parseOld-10.10 {syntax errors} {
336        catch {set a [format abc} msg
337        set msg
338} {missing close-bracket}
339test parseOld-10.11 {syntax errors} {catch gorp-a-lot msg} 1
340test parseOld-10.12 {syntax errors} {
341        catch gorp-a-lot msg
342        set msg
343} {invalid command name "gorp-a-lot"}
344test parseOld-10.13 {syntax errors} {
345    set a [concat {a}\
346 {b}]
347    set a
348} {a b}
349
350# The next test will fail on the Mac, 'cause the MSL uses a fixed sized
351# buffer for %d conversions (LAME!).  I won't leave the test out, however,
352# since MetroWerks may some day fix this.
353
354test parseOld-10.14 {syntax errors} {
355    list [catch {eval \$x[format "%01000d" 0](} msg] $msg $::errorInfo
356} {1 {missing )} {missing )
357    while executing
358"$x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
359    ("eval" body line 1)
360    invoked from within
361"eval \$x[format "%01000d" 0]("}}
362test parseOld-10.15 {syntax errors, missplaced braces} {
363    catch {
364        proc misplaced_end_brace {} {
365            set what foo
366            set when [expr ${what}size - [set off$what]}]
367    } msg
368    set msg
369} {extra characters after close-brace}
370test parseOld-10.16 {syntax errors, missplaced braces} {
371    catch {
372        set a {
373            set what foo
374            set when [expr ${what}size - [set off$what]}]
375    } msg
376    set msg
377} {extra characters after close-brace}
378test parseOld-10.17 {syntax errors, unusual spacing} {
379    list [catch {return [ [1]]} msg] $msg
380} {1 {invalid command name "1"}}
381# Long values (stressing storage management)
382
383set a {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH}
384
385test parseOld-11.1 {long values} {
386    string length $a
387} 214
388test parseOld-11.2 {long values} {
389    llength $a
390} 43
391test parseOld-11.3 {long values} {
392    set b "1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH"
393    set b
394} $a
395test parseOld-11.4 {long values} {
396    set b "$a"
397    set b
398} $a
399test parseOld-11.5 {long values} {
400    set b [set a]
401    set b
402} $a
403test parseOld-11.6 {long values} {
404    set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
405    string length $b
406} 214
407test parseOld-11.7 {long values} {
408    set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
409    llength $b
410} 43
411test parseOld-11.8 {long values} {
412    set b
413} $a
414test parseOld-11.9 {long values} {
415    set a [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ]
416    llength $a
417} 62
418set i 0
419foreach j [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ] {
420    set test [string index 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ $i]
421    set test $test$test$test$test
422    test parseOld-11.10-[incr i] {long values} {
423        set j
424    } $test
425}
426test parseOld-11.11 {test buffer overflow in backslashes in braces} {
427    expr {"a" == {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101}}
428} 0
429
430test parseOld-12.1 {comments} {
431    set a old
432    eval {  # set a new}
433    set a
434} {old}
435test parseOld-12.2 {comments} {
436    set a old
437    eval "  # set a new\nset a new"
438    set a
439} {new}
440test parseOld-12.3 {comments} {
441    set a old
442    eval "  # set a new\\\nset a new"
443    set a
444} {old}
445test parseOld-12.4 {comments} {
446    set a old
447    eval "  # set a new\\\\\nset a new"
448    set a
449} {new}
450
451test parseOld-13.1 {comments at the end of a bracketed script} {
452    set x "[
453expr 1+1
454# skip this!
455]"
456} {2}
457
458test parseOld-14.1 {TclWordEnd procedure} {testwordend} {
459    testwordend "       \n abc"
460} {c}
461test parseOld-14.2 {TclWordEnd procedure} {testwordend} {
462    testwordend "   \\\n"
463} {}
464test parseOld-14.3 {TclWordEnd procedure} {testwordend} {
465    testwordend "   \\\n "
466} { }
467test parseOld-14.4 {TclWordEnd procedure} {testwordend} {
468    testwordend {"abc"}
469} {"}
470#" Emacs formatting :^(
471test parseOld-14.5 {TclWordEnd procedure} {testwordend} {
472    testwordend {{xyz}}
473} \}
474test parseOld-14.6 {TclWordEnd procedure} {testwordend} {
475    testwordend {{a{}b{}\}} xyz}
476} "\} xyz"
477test parseOld-14.7 {TclWordEnd procedure} {testwordend} {
478    testwordend {abc[this is a]def ghi}
479} {f ghi}
480test parseOld-14.8 {TclWordEnd procedure} {testwordend} {
481    testwordend "puts\\\n\n  "
482} "s\\\n\n  "
483test parseOld-14.9 {TclWordEnd procedure} {testwordend} {
484    testwordend "puts\\\n       "
485} "s\\\n        "
486test parseOld-14.10 {TclWordEnd procedure} {testwordend} {
487    testwordend "puts\\\n       xyz"
488} "s\\\n        xyz"
489test parseOld-14.11 {TclWordEnd procedure} {testwordend} {
490    testwordend {a$x.$y(a long index) foo}
491} ") foo"
492test parseOld-14.12 {TclWordEnd procedure} {testwordend} {
493    testwordend {abc; def}
494} {; def}
495test parseOld-14.13 {TclWordEnd procedure} {testwordend} {
496    testwordend {abc def}
497} {c def}
498test parseOld-14.14 {TclWordEnd procedure} {testwordend} {
499    testwordend {abc    def}
500} {c    def}
501test parseOld-14.15 {TclWordEnd procedure} {testwordend} {
502    testwordend "abc\ndef"
503} "c\ndef"
504test parseOld-14.16 {TclWordEnd procedure} {testwordend} {
505    testwordend "abc"
506} {c}
507test parseOld-14.17 {TclWordEnd procedure} {testwordend} {
508    testwordend "a\000bc"
509} {c}
510test parseOld-14.18 {TclWordEnd procedure} {testwordend} {
511    testwordend \[a\000\]
512} {]}
513test parseOld-14.19 {TclWordEnd procedure} {testwordend} {
514    testwordend \"a\000\"
515} {"}
516#" Emacs formatting :^(
517test parseOld-14.20 {TclWordEnd procedure} {testwordend} {
518    testwordend a{\000}b
519} {b}
520test parseOld-14.21 {TclWordEnd procedure} {testwordend} {
521    testwordend "   \000b"
522} {b}
523
524test parseOld-15.1 {TclScriptEnd procedure} {
525    info complete {puts [
526        expr 1+1
527        #this is a comment ]}
528} {0}
529test parseOld-15.2 {TclScriptEnd procedure} {
530    info complete "abc\\\n"
531} {0}
532test parseOld-15.3 {TclScriptEnd procedure} {
533    info complete "abc\\\\\n"
534} {1}
535test parseOld-15.4 {TclScriptEnd procedure} {
536    info complete "xyz \[abc \{abc\]"
537} {0}
538test parseOld-15.5 {TclScriptEnd procedure} {
539    info complete "xyz \[abc"
540} {0}
541
542# cleanup
543set argv $savedArgv
544::tcltest::cleanupTests
545return
Note: See TracBrowser for help on using the repository browser.