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