svn commit: r292758 - head/bin/sh/tests/expansion

Jilles Tjoelker jilles at FreeBSD.org
Sat Dec 26 22:27:50 UTC 2015


Author: jilles
Date: Sat Dec 26 22:27:48 2015
New Revision: 292758
URL: https://svnweb.freebsd.org/changeset/base/292758

Log:
  sh: Add tests for #/##/%/%% on $* and $@.
  
  Although POSIX leaves things like ${*#X} unspecified, it occasionally occurs
  in practice. Add some tests that seem to work sensibly.

Added:
  head/bin/sh/tests/expansion/trim9.0   (contents, props changed)
Modified:
  head/bin/sh/tests/expansion/Makefile

Modified: head/bin/sh/tests/expansion/Makefile
==============================================================================
--- head/bin/sh/tests/expansion/Makefile	Sat Dec 26 19:48:36 2015	(r292757)
+++ head/bin/sh/tests/expansion/Makefile	Sat Dec 26 22:27:48 2015	(r292758)
@@ -90,5 +90,6 @@ FILES+=		trim5.0
 FILES+=		trim6.0
 FILES+=		trim7.0
 FILES+=		trim8.0
+FILES+=		trim9.0
 
 .include <bsd.test.mk>

Added: head/bin/sh/tests/expansion/trim9.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/bin/sh/tests/expansion/trim9.0	Sat Dec 26 22:27:48 2015	(r292758)
@@ -0,0 +1,61 @@
+# $FreeBSD$
+
+# POSIX does not specify these but they occasionally occur in the wild.
+# This just serves to keep working what currently works.
+
+failures=''
+ok=''
+
+testcase() {
+	code="$1"
+	expected="$2"
+	oIFS="$IFS"
+	eval "$code"
+	IFS='|'
+	result="$#|$*"
+	IFS="$oIFS"
+	if [ "x$result" = "x$expected" ]; then
+		ok=x$ok
+	else
+		failures=x$failures
+		echo "For $code, expected $expected actual $result"
+	fi
+}
+
+testcase 'shift $#; set -- "${*#Q}"'		'1|'
+testcase 'shift $#; set -- "${*##Q}"'		'1|'
+testcase 'shift $#; set -- "${*%Q}"'		'1|'
+testcase 'shift $#; set -- "${*%%Q}"'		'1|'
+testcase 'set -- Q R; set -- "${*#Q}"'		'1| R'
+testcase 'set -- Q R; set -- "${*##Q}"'		'1| R'
+testcase 'set -- Q R; set -- "${*%R}"'		'1|Q '
+testcase 'set -- Q R; set -- "${*%%R}"'		'1|Q '
+testcase 'set -- Q R; set -- "${*#S}"'		'1|Q R'
+testcase 'set -- Q R; set -- "${*##S}"'		'1|Q R'
+testcase 'set -- Q R; set -- "${*%S}"'		'1|Q R'
+testcase 'set -- Q R; set -- "${*%%S}"'		'1|Q R'
+testcase 'set -- Q R; set -- ${*#Q}'		'1|R'
+testcase 'set -- Q R; set -- ${*##Q}'		'1|R'
+testcase 'set -- Q R; set -- ${*%R}'		'1|Q'
+testcase 'set -- Q R; set -- ${*%%R}'		'1|Q'
+testcase 'set -- Q R; set -- ${*#S}'		'2|Q|R'
+testcase 'set -- Q R; set -- ${*##S}'		'2|Q|R'
+testcase 'set -- Q R; set -- ${*%S}'		'2|Q|R'
+testcase 'set -- Q R; set -- ${*%%S}'		'2|Q|R'
+testcase 'set -- Q R; set -- ${@#Q}'		'1|R'
+testcase 'set -- Q R; set -- ${@##Q}'		'1|R'
+testcase 'set -- Q R; set -- ${@%R}'		'1|Q'
+testcase 'set -- Q R; set -- ${@%%R}'		'1|Q'
+testcase 'set -- Q R; set -- ${@#S}'		'2|Q|R'
+testcase 'set -- Q R; set -- ${@##S}'		'2|Q|R'
+testcase 'set -- Q R; set -- ${@%S}'		'2|Q|R'
+testcase 'set -- Q R; set -- ${@%%S}'		'2|Q|R'
+testcase 'set -- Q R; set -- "${@#Q}"'		'2||R'
+testcase 'set -- Q R; set -- "${@%R}"'		'2|Q|'
+testcase 'set -- Q R; set -- "${@%%R}"'		'2|Q|'
+testcase 'set -- Q R; set -- "${@#S}"'		'2|Q|R'
+testcase 'set -- Q R; set -- "${@##S}"'		'2|Q|R'
+testcase 'set -- Q R; set -- "${@%S}"'		'2|Q|R'
+testcase 'set -- Q R; set -- "${@%%S}"'		'2|Q|R'
+
+test "x$failures" = x


More information about the svn-src-head mailing list