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