source: tol/tolbase/lib/trycatch/trycatch.test @ 35458ba

1941-docker-image-to-testdevel
Last change on this file since 35458ba was a680983, checked in by Jorge Pérez <josp.jorge@…>, 15 years ago

refs #1347, hipertext widget extracted from Notebook App

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@3878 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 12.3 KB
Line 
1# -*-Tcl-*-
2#---------------------------------------------------------------------
3# TITLE:
4#       trycatch.test
5#
6# AUTHOR:
7#       Will Duquette
8#
9# DESCRIPTION:
10#       Test cases for trycatch.tcl.  Uses the ::tcltest:: harness.
11#
12#       Copyright (C) 2001 by William H. Duquette.  See license.txt
13#       for license information.
14
15#---------------------------------------------------------------------
16# Load the tcltest package
17
18if {[lsearch [namespace children] ::tcltest] == -1} {
19    package require tcltest
20    namespace import ::tcltest::*
21}
22
23#---------------------------------------------------------------------
24# Load the trycatch package
25source trycatch.tcl
26namespace import ::trycatch::*
27
28
29#---------------------------------------------------------------------
30# Test cases
31
32#---------------------------------------------------------------------
33# Form: try {...}
34
35test trycatch-1.1 {try with no errors in tryScript} {} {
36    set a "Before tryScript"
37    try {
38        set a "In tryScript"
39    }
40    set a
41} {In tryScript}
42
43test trycatch-1.2 {try with an error in tryScript} {} {
44    set a "Before tryScript"
45    try {
46        set a "In tryScript"
47        error "Should be ignored"
48    }
49    set a
50} {In tryScript}
51
52test trycatch-1.3 {try with continue in tryScript} {} {
53    set before 0
54    set after 0
55    for {set i 0} {$i < 5} {incr i} {
56        incr before
57        try {
58            continue
59        }
60        incr after
61    }
62    list $before $after
63} {5 0}
64
65test trycatch-1.4 {try with break in tryScript} {} {
66    set before 0
67    set after 0
68    for {set i 0} {$i < 5} {incr i} {
69        incr before
70        try {
71            break
72        }
73        incr after
74    }
75    list $before $after
76} {1 0}
77
78test trycatch-1.5 {try with return in tryScript} {} {
79    proc myproc {} {
80        try {
81            return "OK"
82        }
83        return "NOGO"
84    }
85    myproc
86} {OK}
87
88test trycatch-1.6 {return value from try when no error} {} {
89    try {
90        set a "In tryScript"
91    }
92} {In tryScript}
93
94test trycatch-1.7 {return value from try when error} {} {
95    try {
96        set a "In tryScript"
97        error "Simulated Error"
98    }
99} {}
100
101test trycatch-2.1 {try/catch with no errors in tryScript} {} {
102    try {
103        set a "In tryScript"
104    } catch {
105        set a "In catchScript"
106    }
107    set a
108} {In tryScript}
109
110test trycatch-2.2 {try/catch with continue in tryScript} {} {
111    set before 0
112    set after 0
113    for {set i 0} {$i < 5} {incr i} {
114        incr before
115        try {
116            continue
117        } catch {
118            error "Should not catch!"
119        }
120        incr after
121    }
122    list $before $after
123} {5 0}
124
125test trycatch-2.3 {try/catch with break in tryScript} {} {
126    set before 0
127    set after 0
128    for {set i 0} {$i < 5} {incr i} {
129        incr before
130        try {
131            break
132        } catch {
133            error "Should not catch!"
134        }
135        incr after
136    }
137    list $before $after
138} {1 0}
139
140test trycatch-2.4 {try/catch with return in tryScript} {} {
141    proc myproc {} {
142        try {
143            return "OK"
144        } catch {
145            error "Should not have caught"
146        }
147        return "NOGO"
148    }
149    myproc
150} {OK}
151
152test trycatch-2.5 {try/catch with simulated error in tryScript} {} {
153    try {
154        set a 0
155        set msg OK
156        error "Simulated"
157    } catch -msg msg {
158        set a 1
159    }
160
161    list $a $msg
162} {1 Simulated}
163
164test trycatch-2.6 {return value of try/catch with no errors in tryScript} {} {
165    try {
166        set a "In tryScript"
167    } catch {
168        set a "In catchScript"
169    }
170} {In tryScript}
171
172test trycatch-2.7 {return value of try/catch with error in tryScript} {} {
173    try {
174        set a "In tryScript"
175        error "Simulated Error"
176    } catch {
177        set a "In catchScript"
178    }
179} {In catchScript}
180
181test trycatch-3.1 {try/catch with continue in catchScript} {} {
182    set before 0
183    set after 0
184    for {set i 0} {$i < 5} {incr i} {
185        incr before
186        try {
187            error "try"
188        } catch {
189            continue
190        }
191        incr after
192    }
193    list $before $after
194} {5 0}
195
196test trycatch-3.2 {try/catch with break in catchScript} {} {
197    set before 0
198    set after 0
199    for {set i 0} {$i < 5} {incr i} {
200        incr before
201        try {
202            error "try"
203        } catch {
204            break
205        }
206        incr after
207    }
208    list $before $after
209} {1 0}
210
211test trycatch-3.3 {try/catch with return in catchScript} {} {
212    proc myproc {} {
213        try {
214            error "try"
215        } catch {
216            return "OK"
217        }
218        return "NOGO"
219    }
220    myproc
221} {OK}
222
223test trycatch-3.4 {try/catch with simulated error in catchScript} {} {
224    global errorInfo
225    global errorCode
226
227    proc myproc {} {
228        try {
229            error "try"
230        } catch {
231            error "Simulated" myErrorInfo myErrorCode
232        }
233        return "NOGO"
234    }
235
236    set returnCode [catch {myproc} result]
237   
238    set matchCode [string match "myErrorInfo*" $errorInfo]
239
240    list $returnCode $result $matchCode $errorCode
241} {1 Simulated 1 myErrorCode}
242
243test trycatch-3.5 {try/catch with options} {} {
244    proc myproc {} {
245        try {
246            error "try"
247        } catch {
248            error "Simulated" myErrorInfo myErrorCode
249        }
250        return "NOGO"
251    }
252
253    try {
254        myproc
255    } catch -msg msg -info info -code code {
256        set info [string match "myErrorInfo*" $info]
257        list $msg $info $code
258    }
259} {Simulated 1 myErrorCode}
260
261test trycatch-3.6 {Nested try/catch with return in tryScript} {} {
262    proc myproc {} {
263        try {
264            try {
265                return "OK"
266            } catch {
267                error "try"
268            }
269        } catch {
270            error "try"
271        }
272        return "NOGO"
273    }
274    myproc
275} {OK}
276
277
278test trycatch-4.1 {try/finally with empty try script} {} {
279    set msg "try"
280
281    try {
282        # do nothing
283    } finally {
284        set msg "finally"
285    }
286
287    set msg
288} {finally}
289
290test trycatch-4.2 {return value of try/finally} {} {
291    set msg ""
292
293    try {
294        set msg "try"
295    } finally {
296        set msg "finally"
297    }
298} {try}
299
300test trycatch-4.3 {try/finally with break in try script} {} {
301    set msg "try"
302
303    foreach x {1 2 3} {
304        try {
305            break
306        } finally {
307            set msg "finally"
308        }
309    }
310
311    list $msg $x
312} {finally 1}
313
314test trycatch-4.4 {try/finally with continue in try script} {} {
315    set msg "try"
316
317    foreach x {1 2 3} {
318        set val "before"
319        try {
320            continue
321        } finally {
322            set msg "finally"
323        }
324        set val "after"
325    }
326
327    list $msg $val $x
328} {finally before 3}
329
330test trycatch-4.5 {try/finally with return in try script} {} {
331    set msg "try"
332
333    proc myproc {var} {
334        upvar $var msg
335        try {
336            return "ok"
337        } finally {
338            set msg "finally"
339        }
340    }
341
342    set result [myproc msg]
343
344    list $result $msg
345} {ok finally}
346
347test trycatch-4.6 {try/finally with error in try script} {} {
348    set msg1 ""
349    set msg2 ""
350    set msg3 ""
351    set msg4 ""
352
353    try {
354        try {
355            set msg1 "try"
356            error "Simulated Error"
357            set msg2 "try"
358        } finally {
359            set msg3 "finally"
360        }
361    } catch {
362        set msg4 "catch"
363    }
364    list $msg1 $msg2 $msg3 $msg4
365} {try {} finally catch}
366
367test trycatch-5.1 {try/finally with break in finally script} {} {
368    proc myproc {} {
369        try {
370            # Do nothing
371        } finally {
372            break
373        }
374    }
375
376    catch {myproc} result
377    set result
378} {break, return, or continue in finally script}
379
380test trycatch-5.2 {try/finally with continue in finally script} {} {
381    proc myproc {} {
382        try {
383            # Do nothing
384        } finally {
385            continue
386        }
387    }
388
389    catch {myproc} result
390    set result
391} {break, return, or continue in finally script}
392
393test trycatch-5.3 {try/finally with return in finally script} {} {
394    proc myproc {} {
395        try {
396            # Do nothing
397        } finally {
398            return
399        }
400    }
401
402    catch {myproc} result
403    set result
404} {break, return, or continue in finally script}
405
406test trycatch-5.4 {try/finally with error in finally script} {} {
407    proc myproc {} {
408        try {
409        } finally {
410            error "finally"
411        }
412        return "after"
413    }
414
415    catch {myproc} result
416    set result
417} {finally}
418
419test trycatch-6.1 {try/catch with no catch script} {} {
420    proc myproc {} {
421        try {
422            # Do nothing
423        } catch
424    }
425
426    catch {myproc} result
427    set result
428} {syntax: try {...} catch ?opts? {...}, try {...} finally {...}}
429
430test trycatch-6.2 {try/catch with extra args} {} {
431    proc myproc {} {
432        try {
433            # Do nothing
434        } catch {
435            # Do nothing
436        } foobar
437    }
438
439    catch {myproc} result
440    set result
441} {syntax: try {...} catch ?opts? {...}, try {...} finally {...}}
442
443test trycatch-6.3 {try/finally with no finally script} {} {
444    proc myproc {} {
445        try {
446            # Do nothing
447        } finally
448    }
449
450    catch {myproc} result
451    set result
452} {syntax: try {...} catch ?opts? {...}, try {...} finally {...}}
453
454test trycatch-6.4 {try/finally with extra args} {} {
455    proc myproc {} {
456        try {
457            # Do nothing
458        } finally {
459            # Do nothing
460        } foobar
461    }
462
463    catch {myproc} result
464    set result
465} {syntax: try {...} catch ?opts? {...}, try {...} finally {...}}
466
467test trycatch-6.5 {try/foo} {} {
468    proc myproc {} {
469        try {
470            # Do nothing
471        } foo {
472            # Do nothing
473        }
474    }
475
476    catch {myproc} result
477    set result
478} {syntax: try {...} catch ?opts? {...}, try {...} finally {...}}
479
480test trycatch-6.6 {try/catch with unknown option} {} {
481    proc myproc {} {
482        try {
483            # Do nothing
484        } catch -foo fooVar {
485            # Do nothing
486        }
487    }
488
489    catch {myproc} result
490    set result
491} {valid options: -msg msgVar -code codeVar -info infoVar}
492
493test trycatch-7.1 {throw} {} {
494    try {
495        throw MyErrorCode "My Error Message"
496    } catch -code code -msg msg {
497        list $code $msg
498    }
499} {MyErrorCode {My Error Message}}
500
501test trycatch-8.1 {Match} {} {
502    list \
503            [::trycatch::Match {*} {}] \
504            [::trycatch::Match {*} {abc}] \
505            [::trycatch::Match {abc} {abc}] \
506            [::trycatch::Match {abc*} {abcd}] \
507            [::trycatch::Match {abc* * def*} {abcd x defg}] \
508            [::trycatch::Match {abc} {def}] \
509            [::trycatch::Match {abc def} {abc}]
510} {0 1 1 1 1 0 0}
511
512test trycatch-9.1 {onerr with bad option} {} {
513    proc myproc {} {
514        onerr -foo None {*} {
515            # Do Nothing
516        }
517    }
518    catch {myproc} result
519    set result
520} {syntax: onerr ?-code errorCode? pattern script ...}
521
522test trycatch-9.2 {onerr with uneven args} {} {
523    proc myproc {} {
524        onerr None {*} {
525            # Do Nothing
526        }
527    }
528    catch {myproc} result
529    set result
530} {syntax: onerr ?-code errorCode? pattern script ...}
531
532test trycatch-9.3 {onerr with implicit error code} {} {
533    set msg1 ""
534    set errorCode {POSIX ENOENT}
535    onerr CHILDKILLED {
536        set msg1 CHILDKILLED
537    } POSIX {
538        set msg1 POSIX
539    } * {
540        set msg1 DEFAULT
541    }
542} {POSIX}
543
544test trycatch-9.4 {onerr with explicit error code} {} {
545    set code {CHILDKILLED 1234 SIGTERM}
546    set msg1 ""
547    onerr -code $code {POSIX ENOENT} {
548        set msg1 ENOENT
549    } POSIX {
550        set msg1 POSIX
551    } {* * SIGTERM} {
552        set msg1 SIGTERM
553    } * {
554        set msg1 DEFAULT
555    }
556} {SIGTERM}
557
558test trycatch-9.5 {onerr with default error code} {} {
559    set code {CHILDKILLED 1234 SIGTERM}
560    set msg1 ""
561    onerr -code $code {POSIX ENOENT} {
562        set msg1 ENOENT
563    } * {
564        set msg1 DEFAULT
565    }
566} {DEFAULT}
567
568test trycatch-9.6 {onerr with no matching error code} {} {
569    set code {CHILDKILLED 1234 SIGTERM}
570    set msg1 ""
571    onerr -code $code {POSIX ENOENT} {
572        set msg1 ENOENT
573    }
574} {}
575
576test trycatch-10.1 {try/catch/throw/onerr} {} {
577    set tag ""
578
579    try {
580        throw {MyPackage MyCode} "Simulated MyCode Error"
581    } catch -msg msg -code code {
582        onerr MyPackage {
583            set tag "onerr"
584        }
585    }
586
587    list $msg $code $tag
588} {{Simulated MyCode Error} {MyPackage MyCode} onerr}
589
590
591#---------------------------------------------------------------------
592# Clean up
593
594::tcltest::cleanupTests
595return
596
597
598
Note: See TracBrowser for help on using the repository browser.