xref: /titanic_41/usr/src/lib/libshell/common/tests/case.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin########################################################################
2da2e3ebdSchin#                                                                      #
3da2e3ebdSchin#               This software is part of the ast package               #
4*3e14f97fSRoger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5da2e3ebdSchin#                      and is licensed under the                       #
6da2e3ebdSchin#                  Common Public License, Version 1.0                  #
77c2fbfb3SApril Chin#                    by AT&T Intellectual Property                     #
8da2e3ebdSchin#                                                                      #
9da2e3ebdSchin#                A copy of the License is available at                 #
10da2e3ebdSchin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11da2e3ebdSchin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12da2e3ebdSchin#                                                                      #
13da2e3ebdSchin#              Information and Software Systems Research               #
14da2e3ebdSchin#                            AT&T Research                             #
15da2e3ebdSchin#                           Florham Park NJ                            #
16da2e3ebdSchin#                                                                      #
17da2e3ebdSchin#                  David Korn <dgk@research.att.com>                   #
18da2e3ebdSchin#                                                                      #
19da2e3ebdSchin########################################################################
20da2e3ebdSchinfunction err_exit
21da2e3ebdSchin{
22da2e3ebdSchin	print -u2 -n "\t"
23da2e3ebdSchin	print -u2 -r ${Command}[$1]: "${@:2}"
24da2e3ebdSchin	let Errors+=1
25da2e3ebdSchin}
26da2e3ebdSchinalias err_exit='err_exit $LINENO'
27da2e3ebdSchin
28da2e3ebdSchinCommand=${0##*/}
29da2e3ebdSchininteger Errors=0
30da2e3ebdSchin
31da2e3ebdSchinbar=foo2
32da2e3ebdSchinbam=foo[3]
33da2e3ebdSchinfor i in foo1 foo2 foo3 foo4 foo5 foo6
34da2e3ebdSchindo	foo=0
35da2e3ebdSchin	case $i in
36da2e3ebdSchin	foo1)	foo=1;;
37da2e3ebdSchin	$bar)	foo=2;;
38da2e3ebdSchin	$bam)	foo=3;;
39da2e3ebdSchin	foo[4])	foo=4;;
40da2e3ebdSchin	${bar%?}5)
41da2e3ebdSchin		foo=5;;
42da2e3ebdSchin	"${bar%?}6")
43da2e3ebdSchin		foo=6;;
44da2e3ebdSchin	esac
45da2e3ebdSchin	if	[[ $i != foo$foo ]]
46da2e3ebdSchin	then	err_exit "$i not matching correct pattern"
47da2e3ebdSchin	fi
48da2e3ebdSchindone
49da2e3ebdSchinf="[ksh92]"
50da2e3ebdSchincase $f in
51da2e3ebdSchin\[*\])  ;;
52da2e3ebdSchin*)      err_exit "$f does not match \[*\]";;
53da2e3ebdSchinesac
54da2e3ebdSchin
55da2e3ebdSchinif	[[ $($SHELL -c '
56da2e3ebdSchin		x=$(case abc {
57da2e3ebdSchin			abc)	{ print yes;};;
58da2e3ebdSchin			*)	 print no;;
59da2e3ebdSchin			}
60da2e3ebdSchin		)
61da2e3ebdSchin		print -r -- "$x"' 2> /dev/null) != yes ]]
62da2e3ebdSchinthen err_exit 'case abc {...} not working'
63da2e3ebdSchinfi
64da2e3ebdSchin[[ $($SHELL -c 'case a in
65da2e3ebdSchina)      print -n a > /dev/null ;&
66da2e3ebdSchinb)      print b;;
67da2e3ebdSchinesac') != b ]] && err_exit 'bug in ;& at end of script'
68da2e3ebdSchin[[ $(VMDEBUG=1 $SHELL -c '
69da2e3ebdSchin	tmp=foo
70da2e3ebdSchin	for i in a b
71da2e3ebdSchin	do	case $i in
72da2e3ebdSchin		a)	:  tmp=$tmp tmp.h=$tmp.h;;
73da2e3ebdSchin		b)	( tmp=bar )
74da2e3ebdSchin			for j in a
75da2e3ebdSchin			do	print -r -- $tmp.h
76da2e3ebdSchin			done
77da2e3ebdSchin			;;
78da2e3ebdSchin		esac
79da2e3ebdSchin	done
80da2e3ebdSchin') == foo.h ]] || err_exit "optimizer bug"
81da2e3ebdSchinexit $((Errors))
82