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