Debian Codegraph @

tests/case.test

raw
1 # Commands covered: case
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 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 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 test case-1.1 {simple pattern} {
20 case a in a {format 1} b {format 2} c {format 3} default {format 4}
21 } 1
22 test case-1.2 {simple pattern} {
23 case b a {format 1} b {format 2} c {format 3} default {format 4}
24 } 2
25 test case-1.3 {simple pattern} {
26 case x in a {format 1} b {format 2} c {format 3} default {format 4}
27 } 4
28 test case-1.4 {simple pattern} {
29 case x a {format 1} b {format 2} c {format 3}
30 } {}
31 test case-1.5 {simple pattern matches many times} {
32 case b a {format 1} b {format 2} b {format 3} b {format 4}
33 } 2
34 test case-1.6 {fancier pattern} {
35 case cx a {format 1} *c {format 2} *x {format 3} default {format 4}
36 } 3
37 test case-1.7 {list of patterns} {
38 case abc in {a b c} {format 1} {def abc ghi} {format 2}
39 } 2
40
41 test case-2.1 {error in executed command} {
42 list [catch {case a in a {error "Just a test"} default {format 1}} msg] \
43 $msg $::errorInfo
44 } {1 {Just a test} {Just a test
45 while executing
46 "error "Just a test""
47 ("a" arm line 1)
48 invoked from within
49 "case a in a {error "Just a test"} default {format 1}"}}
50 test case-2.2 {error: not enough args} {
51 list [catch {case} msg] $msg
52 } {1 {wrong # args: should be "case string ?in? ?pattern body ...? ?default body?"}}
53 test case-2.3 {error: pattern with no body} {
54 list [catch {case a b} msg] $msg
55 } {1 {extra case pattern with no body}}
56 test case-2.4 {error: pattern with no body} {
57 list [catch {case a in b {format 1} c} msg] $msg
58 } {1 {extra case pattern with no body}}
59 test case-2.5 {error in default command} {
60 list [catch {case foo in a {error case1} default {error case2} \
61 b {error case 3}} msg] $msg $::errorInfo
62 } {1 case2 {case2
63 while executing
64 "error case2"
65 ("default" arm line 1)
66 invoked from within
67 "case foo in a {error case1} default {error case2} b {error case 3}"}}
68
69 test case-3.1 {single-argument form for pattern/command pairs} {
70 case b in {
71 a {format 1}
72 b {format 2}
73 default {format 6}
74 }
75 } {2}
76 test case-3.2 {single-argument form for pattern/command pairs} {
77 case b {
78 a {format 1}
79 b {format 2}
80 default {format 6}
81 }
82 } {2}
83 test case-3.3 {single-argument form for pattern/command pairs} {
84 list [catch {case z in {a 2 b}} msg] $msg
85 } {1 {extra case pattern with no body}}
86
87 # cleanup
88 ::tcltest::cleanupTests
89 return