xref: /onnv-gate/usr/src/lib/libshell/common/tests/case.sh (revision 12068:08a39a083754)
14887Schin########################################################################
24887Schin#                                                                      #
34887Schin#               This software is part of the ast package               #
4*12068SRoger.Faulkner@Oracle.COM#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
54887Schin#                      and is licensed under the                       #
64887Schin#                  Common Public License, Version 1.0                  #
78462SApril.Chin@Sun.COM#                    by AT&T Intellectual Property                     #
84887Schin#                                                                      #
94887Schin#                A copy of the License is available at                 #
104887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
114887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
124887Schin#                                                                      #
134887Schin#              Information and Software Systems Research               #
144887Schin#                            AT&T Research                             #
154887Schin#                           Florham Park NJ                            #
164887Schin#                                                                      #
174887Schin#                  David Korn <dgk@research.att.com>                   #
184887Schin#                                                                      #
194887Schin########################################################################
204887Schinfunction err_exit
214887Schin{
224887Schin	print -u2 -n "\t"
234887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
244887Schin	let Errors+=1
254887Schin}
264887Schinalias err_exit='err_exit $LINENO'
274887Schin
284887SchinCommand=${0##*/}
294887Schininteger Errors=0
304887Schin
314887Schinbar=foo2
324887Schinbam=foo[3]
334887Schinfor i in foo1 foo2 foo3 foo4 foo5 foo6
344887Schindo	foo=0
354887Schin	case $i in
364887Schin	foo1)	foo=1;;
374887Schin	$bar)	foo=2;;
384887Schin	$bam)	foo=3;;
394887Schin	foo[4])	foo=4;;
404887Schin	${bar%?}5)
414887Schin		foo=5;;
424887Schin	"${bar%?}6")
434887Schin		foo=6;;
444887Schin	esac
454887Schin	if	[[ $i != foo$foo ]]
464887Schin	then	err_exit "$i not matching correct pattern"
474887Schin	fi
484887Schindone
494887Schinf="[ksh92]"
504887Schincase $f in
514887Schin\[*\])  ;;
524887Schin*)      err_exit "$f does not match \[*\]";;
534887Schinesac
544887Schin
554887Schinif	[[ $($SHELL -c '
564887Schin		x=$(case abc {
574887Schin			abc)	{ print yes;};;
584887Schin			*)	 print no;;
594887Schin			}
604887Schin		)
614887Schin		print -r -- "$x"' 2> /dev/null) != yes ]]
624887Schinthen err_exit 'case abc {...} not working'
634887Schinfi
644887Schin[[ $($SHELL -c 'case a in
654887Schina)      print -n a > /dev/null ;&
664887Schinb)      print b;;
674887Schinesac') != b ]] && err_exit 'bug in ;& at end of script'
684887Schin[[ $(VMDEBUG=1 $SHELL -c '
694887Schin	tmp=foo
704887Schin	for i in a b
714887Schin	do	case $i in
724887Schin		a)	:  tmp=$tmp tmp.h=$tmp.h;;
734887Schin		b)	( tmp=bar )
744887Schin			for j in a
754887Schin			do	print -r -- $tmp.h
764887Schin			done
774887Schin			;;
784887Schin		esac
794887Schin	done
804887Schin') == foo.h ]] || err_exit "optimizer bug"
814887Schinexit $((Errors))
82