Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: test output pattern matching



On Thu, 1 Dec 2011 12:52:47 +0000
Peter Stephenson <Peter.Stephenson@xxxxxxx> wrote:
> We really need a way of telling the test system that it should match
> output by pattern, but I've never got round to it.

This could probably be used in other places.

Index: Test/A04redirect.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A04redirect.ztst,v
retrieving revision 1.21
diff -p -u -r1.21 A04redirect.ztst
--- Test/A04redirect.ztst	1 Dec 2011 14:22:56 -0000	1.21
+++ Test/A04redirect.ztst	1 Dec 2011 21:24:02 -0000
@@ -153,31 +153,17 @@
 >goodbye
 
   (exec 3<&-
-  read foo <&-) 2>errmsg1.txt
-  mystat=$?
-  (( $mystat == 1 )) || print "Unexpected error status $mystat" >&2
-  input=("${(f)$(<errmsg1.txt)}")
-  if [[ ${#input} != 1 || \
-        $input[1] !=   "(eval):1: failed to close file descriptor 3:"* ]];
-  then
-    print "Unexpected error output:\n$input" >&2
-  fi
-0:'<&-' redirection
+  read foo <&-)
+1:'<&-' redirection
+*?\(eval\):1: failed to close file descriptor 3:*
 
   print foo >&-
 0:'>&-' redirection
 
   (exec >&-
-  print foo) 2>errmsg2.txt
-  mystat=$?
-  (( $mystat == 0 )) || print "Unexpected error status $mystat" >&2
-  input=("${(f)$(<errmsg2.txt)}")
-  if [[ ${#input} != 1 || \
-        $input[1] !=   "(eval):2: write error:"* ]];
-  then
-    print "Unexpected error output:\n$input" >&2
-  fi
+  print foo)
 0:'>&-' with attempt to use closed fd
+*?\(eval\):2: write error:*
 
   fn() { local foo; read foo; print $foo; }
   coproc fn
Index: Test/B01cd.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/B01cd.ztst,v
retrieving revision 1.4
diff -p -u -r1.4 B01cd.ztst
--- Test/B01cd.ztst	20 Mar 2009 22:52:56 -0000	1.4
+++ Test/B01cd.ztst	1 Dec 2011 21:24:02 -0000
@@ -57,6 +57,14 @@
 # lines are not subject to any substitution unless the `q' flag (see
 # below) is set.
 #
+# '>' and '?' may be preceded by a '*', in which case all lines
+# in the chunk must be so delimited (i.e. all lines must start either
+# '*>' or '>' but not a mixture).  If the '*' is present, the lines
+# in the actual output are pattern matched against the lines in the
+# test output.  The entire line following '*>' or '*?' must be a
+# valid pattern, so characters special to patterns such as parentheses
+# must be quoted.  The EXTENDED_GLOB option is used for all such patterns.
+#
 # Each chunk of indented code is to be evaluated in one go and is to
 # be followed by a line starting (in the first column) with
 # the expected status returned by the code when run, or - if it is
Index: Test/ztst.zsh
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/ztst.zsh,v
retrieving revision 1.29
diff -p -u -r1.29 ztst.zsh
--- Test/ztst.zsh	25 Dec 2007 01:40:18 -0000	1.29
+++ Test/ztst.zsh	1 Dec 2011 21:24:02 -0000
@@ -285,12 +285,52 @@ $ZTST_code" && return 0
 
 # diff wrapper
 ZTST_diff() {
-  local diff_out diff_ret
+  emulate -L zsh
+  setopt extendedglob
 
-  diff_out=$(diff "$@")
-  diff_ret="$?"
-  if [[ "$diff_ret" != "0" ]]; then
-    print -r "$diff_out"
+  local diff_out
+  integer diff_pat diff_ret
+
+  case $1 in
+    (p)
+    diff_pat=1
+    ;;
+
+    (d)
+    ;;
+
+    (*)
+    print "Bad ZTST_diff code: d for diff, p for pattern match"
+    ;;
+  esac
+  shift
+      
+  if (( diff_pat )); then
+    local -a diff_lines1 diff_lines2
+    integer failed i
+
+    diff_lines1=("${(f)$(<$argv[-2])}")
+    diff_lines2=("${(f)$(<$argv[-1])}")
+    if (( ${#diff_lines1} != ${#diff_lines2} )); then
+      failed=1
+    else
+      for (( i = 1; i <= ${#diff_lines1}; i++ )); do
+	if [[ ${diff_lines2[i]} != ${~diff_lines1[i]} ]]; then
+	  failed=1
+	  break
+	fi
+      done
+    fi
+    if (( failed )); then
+      print -rl "Pattern match failed:" \<${^diff_lines1} \>${^diff_lines2}
+      diff_ret=1
+    fi
+  else
+    diff_out=$(diff "$@")
+    diff_ret="$?"
+    if [[ "$diff_ret" != "0" ]]; then
+      print -r "$diff_out"
+    fi
   fi
 
   return "$diff_ret"
@@ -298,6 +338,7 @@ ZTST_diff() {
     
 ZTST_test() {
   local last match mbegin mend found substlines
+  local diff_out diff_err
 
   while true; do
     rm -f $ZTST_in $ZTST_out $ZTST_err
@@ -305,6 +346,8 @@ ZTST_test() {
     ZTST_message=''
     ZTST_failmsg=''
     found=0
+    diff_out=d
+    diff_err=d
 
     ZTST_verbose 2 "ZTST_test: looking for new test"
 
@@ -343,10 +386,20 @@ $ZTST_curline"
 	('<'*) ZTST_getredir || return 1
 	  found=1
 	  ;;
-	('>'*) ZTST_getredir || return 1
+	('*>'*)
+	  ZTST_curline=${ZTST_curline[2,-1]}
+	  diff_out=p
+	  ;&
+	('>'*)
+	  ZTST_getredir || return 1
 	  found=1
 	  ;;
-	('?'*) ZTST_getredir || return 1
+	('*?'*)
+	  ZTST_curline=${ZTST_curline[2,-1]}
+	  diff_err=p
+	  ;&
+	('?'*)
+	  ZTST_getredir || return 1
 	  found=1
 	  ;;
 	('F:'*) ZTST_failmsg="${ZTST_failmsg:+${ZTST_failmsg}
@@ -390,7 +443,7 @@ $(<$ZTST_terr)"
 	rm -rf $ZTST_out
 	print -r -- "${(e)substlines}" >$ZTST_out
       fi
-      if [[ $ZTST_flags != *d* ]] && ! ZTST_diff -c $ZTST_out $ZTST_tout; then
+      if [[ $ZTST_flags != *d* ]] && ! ZTST_diff $diff_out -c $ZTST_out $ZTST_tout; then
 	ZTST_testfailed "output differs from expected as shown above for:
 $ZTST_code${$(<$ZTST_terr):+
 Error output:
@@ -402,7 +455,7 @@ $(<$ZTST_terr)}"
 	rm -rf $ZTST_err
 	print -r -- "${(e)substlines}" >$ZTST_err
       fi
-      if [[ $ZTST_flags != *D* ]] && ! ZTST_diff -c $ZTST_err $ZTST_terr; then
+      if [[ $ZTST_flags != *D* ]] && ! ZTST_diff $diff_err -c $ZTST_err $ZTST_terr; then
 	ZTST_testfailed "error output differs from expected as shown above for:
 $ZTST_code"
 	return 1

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author