xref: /freebsd/bin/sh/tests/expansion/trim1.0 (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1# $FreeBSD$
2
3e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}'
4h='##'
5failures=''
6ok=''
7
8testcase() {
9	code="$1"
10	expected="$2"
11	oIFS="$IFS"
12	eval "$code"
13	IFS='|'
14	result="$#|$*"
15	IFS="$oIFS"
16	if [ "x$result" = "x$expected" ]; then
17		ok=x$ok
18	else
19		failures=x$failures
20		echo "For $code, expected $expected actual $result"
21	fi
22}
23
24testcase 'set -- ${t%t}'			'1|texttex'
25testcase 'set -- "${t%t}"'			'1|texttex'
26testcase 'set -- ${t%e*}'			'1|textt'
27testcase 'set -- "${t%e*}"'			'1|textt'
28testcase 'set -- ${t%%e*}'			'1|t'
29testcase 'set -- "${t%%e*}"'			'1|t'
30testcase 'set -- ${t%%*}'			'0|'
31testcase 'set -- "${t%%*}"'			'1|'
32testcase 'set -- ${t#t}'			'1|exttext'
33testcase 'set -- "${t#t}"'			'1|exttext'
34testcase 'set -- ${t#*x}'			'1|ttext'
35testcase 'set -- "${t#*x}"'			'1|ttext'
36testcase 'set -- ${t##*x}'			'1|t'
37testcase 'set -- "${t##*x}"'			'1|t'
38testcase 'set -- ${t##*}'			'0|'
39testcase 'set -- "${t##*}"'			'1|'
40testcase 'set -- ${t%e$a}'			'1|textt'
41
42set -f
43testcase 'set -- ${s%[?]*}'			'1|ast*que'
44testcase 'set -- "${s%[?]*}"'			'1|ast*que'
45testcase 'set -- ${s%[*]*}'			'1|ast'
46testcase 'set -- "${s%[*]*}"'			'1|ast'
47set +f
48
49testcase 'set -- $b'				'1|{{(#)}}'
50testcase 'set -- ${b%\}}'			'1|{{(#)}'
51testcase 'set -- ${b#{}'			'1|{(#)}}'
52testcase 'set -- "${b#{}"'			'1|{(#)}}'
53# Parentheses are special in ksh, check that they can be escaped
54testcase 'set -- ${b%\)*}'			'1|{{(#'
55testcase 'set -- ${b#{}'			'1|{(#)}}'
56testcase 'set -- $h'				'1|##'
57testcase 'set -- ${h#\#}'			'1|#'
58testcase 'set -- ${h###}'			'1|#'
59testcase 'set -- "${h###}"'			'1|#'
60testcase 'set -- ${h%#}'			'1|#'
61testcase 'set -- "${h%#}"'			'1|#'
62
63set -f
64testcase 'set -- ${s%"${s#?}"}'			'1|a'
65testcase 'set -- ${s%"${s#????}"}'		'1|ast*'
66testcase 'set -- ${s%"${s#????????}"}'		'1|ast*que?'
67testcase 'set -- ${s#"${s%?}"}'			'1|n'
68testcase 'set -- ${s#"${s%????}"}'		'1|?non'
69testcase 'set -- ${s#"${s%????????}"}'		'1|*que?non'
70set +f
71testcase 'set -- "${s%"${s#?}"}"'		'1|a'
72testcase 'set -- "${s%"${s#????}"}"'		'1|ast*'
73testcase 'set -- "${s%"${s#????????}"}"'	'1|ast*que?'
74testcase 'set -- "${s#"${s%?}"}"'		'1|n'
75testcase 'set -- "${s#"${s%????}"}"'		'1|?non'
76testcase 'set -- "${s#"${s%????????}"}"'	'1|*que?non'
77testcase 'set -- ${p#${p}}'			'1|/etc/'
78testcase 'set -- "${p#${p}}"'			'1|/et[c]/'
79testcase 'set -- ${p#*[[]}'			'1|c]/'
80testcase 'set -- "${p#*[[]}"'			'1|c]/'
81testcase 'set -- ${p#*\[}'			'1|c]/'
82testcase 'set -- ${p#*"["}'			'1|c]/'
83testcase 'set -- "${p#*"["}"'			'1|c]/'
84
85test "x$failures" = x
86