summaryrefslogtreecommitdiffstats
path: root/tests/parse.test
blob: 7019b7af3212620fdea17dc3b1faed00c78fbc8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# Commands covered:  set (plus basic command syntax).  Also tests
# the procedures in the file tclParse.c.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1993 The Regents of the University of California.
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: parse.test,v 1.2 1998/09/14 18:40:12 stanton Exp $

if {[string compare test [info procs test]] == 1} then {source defs}

proc fourArgs {a b c d} {
    global arg1 arg2 arg3 arg4
    set arg1 $a
    set arg2 $b
    set arg3 $c
    set arg4 $d
}

proc getArgs args {
    global argv
    set argv $args
}

# Basic argument parsing.

test parse-1.1 {basic argument parsing} {
    set arg1 {}
    fourArgs a b	c 		 d
    list $arg1 $arg2 $arg3 $arg4
} {a b c d}
test parse-1.2 {basic argument parsing} {
    set arg1 {}
    eval "fourArgs 123\v4\f56\r7890"
    list $arg1 $arg2 $arg3 $arg4
} {123 4 56 7890}

# Quotes.

test parse-2.1 {quotes and variable-substitution} {
    getArgs "a b c" d
    set argv
} {{a b c} d}
test parse-2.2 {quotes and variable-substitution} {
    set a 101
    getArgs "a$a b c"
    set argv
} {{a101 b c}}
test parse-2.3 {quotes and variable-substitution} {
    set argv "xy[format xabc]"
    set argv
} {xyxabc}
test parse-2.4 {quotes and variable-substitution} {
    set argv "xy\t"
    set argv
} xy\t
test parse-2.5 {quotes and variable-substitution} {
    set argv "a b	c
d e f"
    set argv
} a\ b\tc\nd\ e\ f
test parse-2.6 {quotes and variable-substitution} {
    set argv a"bcd"e
    set argv
} {a"bcd"e}

# Braces.

test parse-3.1 {braces} {
    getArgs {a b c} d
    set argv
} "{a b c} d"
test parse-3.2 {braces} {
    set a 101
    set argv {a$a b c}
    set b [string index $argv 1]
    set b
} {$}
test parse-3.3 {braces} {
    set argv {a[format xyz] b}
    string length $argv
} 15
test parse-3.4 {braces} {
    set argv {a\nb\}}
    string length $argv
} 6
test parse-3.5 {braces} {
    set argv {{{{}}}}
    set argv
} "{{{}}}"
test parse-3.6 {braces} {
    set argv a{{}}b
    set argv
} "a{{}}b"
test parse-3.7 {braces} {
    set a [format "last]"]
    set a
} {last]}

# Command substitution.

test parse-4.1 {command substitution} {
    set a [format xyz]
    set a
} xyz
test parse-4.2 {command substitution} {
    set a a[format xyz]b[format q]
    set a
} axyzbq
test parse-4.3 {command substitution} {
    set a a[
set b 22;
format %s $b

]b
    set a
} a22b
test parse-4.4 {command substitution} {
    set a 7.7
    if [catch {expr int($a)}] {set a foo}
    set a
} 7.7

# Variable substitution.

test parse-5.1 {variable substitution} {
    set a 123
    set b $a
    set b
} 123
test parse-5.2 {variable substitution} {
    set a 345
    set b x$a.b
    set b
} x345.b
test parse-5.3 {variable substitution} {
    set _123z xx
    set b $_123z^
    set b
} xx^
test parse-5.4 {variable substitution} {
    set a 78
    set b a${a}b
    set b
} a78b
test parse-5.5 {variable substitution} {catch {$_non_existent_} msg} 1
test parse-5.6 {variable substitution} {
    catch {$_non_existent_} msg
    set msg
} {can't read "_non_existent_": no such variable}
test parse-5.7 {array variable substitution} {
    catch {unset a}
    set a(xyz) 123
    set b $a(xyz)foo
    set b
} 123foo
test parse-5.8 {array variable substitution} {
    catch {unset a}
    set "a(x y z)" 123
    set b $a(x y z)foo
    set b
} 123foo
test parse-5.9 {array variable substitution} {
    catch {unset a}; catch {unset qqq}
    set "a(x y z)" qqq
    set $a([format x]\ y [format z]) foo
    set qqq
} foo
test parse-5.10 {array variable substitution} {
    catch {unset a}
    list [catch {set b $a(22)} msg] $msg
} {1 {can't read "a(22)": no such variable}}
test parse-5.11 {array variable substitution} {
    set b a$!
    set b
} {a$!}
test parse-5.12 {array variable substitution} {
    set b a$()
    set b
} {a$()}
catch {unset a}
test parse-5.13 {array variable substitution} {
    catch {unset a}
    set long {This is a very long variable, long enough to cause storage \
	allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
	freed up correctly, then a core leak will occur when this test is \
	run.  This text is probably beginning to sound like drivel, but I've \
	run out of things to say and I need more characters still.}
    set a($long) 777
    set b $a($long)
    list $b [array names a]
} {777 {{This is a very long variable, long enough to cause storage \
	allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
	freed up correctly, then a core leak will occur when this test is \
	run.  This text is probably beginning to sound like drivel, but I've \
	run out of things to say and I need more characters still.}}}
test parse-5.14 {array variable substitution} {
    catch {unset a}; catch {unset b}; catch {unset a1}
    set a1(22) foo
    set a(foo) bar
    set b $a($a1(22))
    set b
} bar
catch {unset a}; catch {unset a1}

# Backslash substitution.

set errNum 1
proc bsCheck {char num} {
    global errNum
;   test parse-6.$errNum {backslash substitution} {
	scan $char %c value
	set value
    } $num
    set errNum [expr $errNum+1]
}

bsCheck \b	8
bsCheck \e	101
bsCheck \f	12
bsCheck \n	10
bsCheck \r	13
bsCheck \t	9
bsCheck \v	11
bsCheck \{	123
bsCheck \}	125
bsCheck \[	91
bsCheck \]	93
bsCheck \$	36
bsCheck \ 	32
bsCheck \;	59
bsCheck \\	92
bsCheck \Ca	67
bsCheck \Ma	77
bsCheck \CMa	67
bsCheck \8a	8
bsCheck \14	12
bsCheck \141	97
bsCheck \340	224
bsCheck b\0	98
bsCheck \x	120
bsCheck \xa	10
bsCheck \x41	65
bsCheck \x541	65

test parse-6.1 {backslash substitution} {
    set a "\a\c\n\]\}"
    string length $a
} 5
test parse-6.2 {backslash substitution} {
    set a {\a\c\n\]\}}
    string length $a
} 10
test parse-6.3 {backslash substitution} {
    set a "abc\
def"
    set a
} {abc def}
test parse-6.4 {backslash substitution} {
    set a {abc\
def}
    set a
} {abc def}
test parse-6.5 {backslash substitution} {
    set msg {}
    set a xxx
    set error [catch {if {24 < \
	35} {set a 22} {set \
	    a 33}} msg]
    list $error $msg $a
} {0 22 22}
test parse-6.6 {backslash substitution} {
    eval "concat abc\\"
} "abc\\"
test parse-6.7 {backslash substitution} {
    eval "concat \\\na"
} "a"
test parse-6.8 {backslash substitution} {
    eval "concat x\\\n   	a"
} "x a"
test parse-6.9 {backslash substitution} {
    eval "concat \\x"
} "x"
test parse-6.10 {backslash substitution} {
    eval "list a b\\\nc d"
} {a b c d}
test parse-6.11 {backslash substitution} {
    eval "list a \"b c\"\\\nd e"
} {a {b c} d e}

# Semi-colon.

test parse-7.1 {semi-colons} {
    set b 0
    getArgs a;set b 2
    set argv
} a
test parse-7.2 {semi-colons} {
    set b 0
    getArgs a;set b 2
    set b
} 2
test parse-7.3 {semi-colons} {
    getArgs a b ; set b 1
    set argv
} {a b}
test parse-7.4 {semi-colons} {
    getArgs a b ; set b 1
    set b
} 1

# The following checks are to ensure that the interpreter's result
# gets re-initialized by Tcl_Eval in all the right places.

test parse-8.1 {result initialization} {concat abc} abc
test parse-8.2 {result initialization} {concat abc; proc foo {} {}} {}
test parse-8.3 {result initialization} {concat abc; proc foo {} $a} {}
test parse-8.4 {result initialization} {proc foo {} [concat abc]} {}
test parse-8.5 {result initialization} {concat abc; } abc
test parse-8.6 {result initialization} {
    eval {
    concat abc
}} abc
test parse-8.7 {result initialization} {} {}
test parse-8.8 {result initialization} {concat abc; ; ;} abc

# Syntax errors.

test parse-9.1 {syntax errors} {catch "set a \{bcd" msg} 1
test parse-9.2 {syntax errors} {
	catch "set a \{bcd" msg
	set msg
} {missing close-brace}
test parse-9.3 {syntax errors} {catch {set a "bcd} msg} 1
test parse-9.4 {syntax errors} {
	catch {set a "bcd} msg
	set msg
} {quoted string doesn't terminate properly}
test parse-9.5 {syntax errors} {catch {set a "bcd"xy} msg} 1
test parse-9.6 {syntax errors} {
	catch {set a "bcd"xy} msg
	set msg
} {quoted string doesn't terminate properly}
test parse-9.7 {syntax errors} {catch "set a {bcd}xy" msg} 1
test parse-9.8 {syntax errors} {
	catch "set a {bcd}xy" msg
	set msg
} {argument word in braces doesn't terminate properly}
test parse-9.9 {syntax errors} {catch {set a [format abc} msg} 1
test parse-9.10 {syntax errors} {
	catch {set a [format abc} msg
	set msg
} {missing close-bracket or close-brace}
test parse-9.11 {syntax errors} {catch gorp-a-lot msg} 1
test parse-9.12 {syntax errors} {
	catch gorp-a-lot msg
	set msg
} {invalid command name "gorp-a-lot"}
test parse-9.13 {syntax errors} {
    set a [concat {a}\
 {b}]
    set a
} {a b}
test parse-9.14 {syntax errors} {
    list [catch {eval \$x[format "%01000d" 0](} msg] $msg $errorInfo
} {1 {missing )} {missing )
    (parsing index for array "x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
    while compiling
"$x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ..."
    ("eval" body line 1)
    invoked from within
"eval \$x[format "%01000d" 0]("}}
test parse-9.15 {syntax errors, missplaced braces} {
    catch {
        proc misplaced_end_brace {} {
            set what foo
            set when [expr ${what}size - [set off$what]}]
    } msg
    set msg
} {wrong # args: should be "proc name args body"}
test parse-9.16 {syntax errors, missplaced braces} {
    catch {
        set a {
            set what foo
            set when [expr ${what}size - [set off$what]}]
    } msg
    set msg
} {argument word in braces doesn't terminate properly}

# Long values (stressing storage management)

set 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}

test parse-10.1 {long values} {
    string length $a
} 214
test parse-10.2 {long values} {
    llength $a
} 43
test parse-10.3 {long values} {
    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"
    set b
} $a
test parse-10.4 {long values} {
    set b "$a"
    set b
} $a
test parse-10.5 {long values} {
    set b [set a]
    set b
} $a
test parse-10.6 {long values} {
    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]
    string length $b
} 214
test parse-10.7 {long values} {
    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]
    llength $b
} 43
test parse-10.8 {long values} {
    set b
} $a
test parse-10.9 {long values} {
    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]
    llength $a
} 62
set i 0
foreach 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] {
    set test [string index 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ $i]
    set test $test$test$test$test
    set i [expr $i+1]
    test parse-10.10 {long values} {
	set j
    } $test
}
test parse-10.11 {test buffer overflow in backslashes in braces} {
    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}}
} 0

test parse-11.1 {comments} {
    set a old
    eval {  # set a new}
    set a
} {old}
test parse-11.2 {comments} {
    set a old
    eval "  # set a new\nset a new"
    set a
} {new}
test parse-11.3 {comments} {
    set a old
    eval "  # set a new\\\nset a new"
    set a
} {old}
test parse-11.4 {comments} {
    set a old
    eval "  # set a new\\\\\nset a new"
    set a
} {new}

test parse-12.1 {comments at the end of a bracketed script} {
    set x "[
expr 1+1
# skip this!
]"
} {2}

if {[info command testwordend] == "testwordend"} {
    test parse-13.1 {TclWordEnd procedure} {
	testwordend " 	\n abc"
    } {c}
    test parse-13.2 {TclWordEnd procedure} {
	testwordend "   \\\n"
    } {}
    test parse-13.3 {TclWordEnd procedure} {
	testwordend "   \\\n "
    } { }
    test parse-13.4 {TclWordEnd procedure} {
	testwordend {"abc"}
    } {"}
    test parse-13.5 {TclWordEnd procedure} {
	testwordend {{xyz}}
    } \}
    test parse-13.6 {TclWordEnd procedure} {
	testwordend {{a{}b{}\}} xyz}
    } "\} xyz"
    test parse-13.7 {TclWordEnd procedure} {
	testwordend {abc[this is a]def ghi}
    } {f ghi}
    test parse-13.8 {TclWordEnd procedure} {
	testwordend "puts\\\n\n  "
    } "s\\\n\n  "
    test parse-13.9 {TclWordEnd procedure} {
	testwordend "puts\\\n   	"
    } "s\\\n   	"
    test parse-13.10 {TclWordEnd procedure} {
	testwordend "puts\\\n   	xyz"
    } "s\\\n   	xyz"
    test parse-13.11 {TclWordEnd procedure} {
	testwordend {a$x.$y(a long index) foo}
    } ") foo"
    test parse-13.12 {TclWordEnd procedure} {
	testwordend {abc; def}
    } {; def}
    test parse-13.13 {TclWordEnd procedure} {
	testwordend {abc def}
    } {c def}
    test parse-13.14 {TclWordEnd procedure} {
	testwordend {abc	def}
    } {c	def}
    test parse-13.15 {TclWordEnd procedure} {
	testwordend "abc\ndef"
    } "c\ndef"
    test parse-13.16 {TclWordEnd procedure} {
	testwordend "abc"
    } {c}
    test parse-13.17 {TclWordEnd procedure} {
	testwordend "a\000bc"
    } {c}
    test parse-13.18 {TclWordEnd procedure} {
	testwordend \[a\000\]
    } {]}
    test parse-13.19 {TclWordEnd procedure} {
	testwordend \"a\000\"
    } {"}
    test parse-13.20 {TclWordEnd procedure} {
	testwordend a{\000}b
    } {b}
    test parse-13.21 {TclWordEnd procedure} {
	testwordend "   \000b"
    } {b}
}

test parse-14.1 {TclScriptEnd procedure} {
    info complete {puts [
	expr 1+1
	#this is a comment ]}
} {0}
test parse-14.2 {TclScriptEnd procedure} {
    info complete "abc\\\n"
} {0}
test parse-14.3 {TclScriptEnd procedure} {
    info complete "abc\\\\\n"
} {1}
test parse-14.4 {TclScriptEnd procedure} {
    info complete "xyz \[abc \{abc\]"
} {0}
test parse-14.5 {TclScriptEnd procedure} {
    info complete "xyz \[abc"
} {0}