r3729 - in trunk/src/host/devirginator: . tests

werner at sita.openmoko.org werner at sita.openmoko.org
Mon Dec 24 15:21:10 CET 2007


Author: werner
Date: 2007-12-24 15:21:01 +0100 (Mon, 24 Dec 2007)
New Revision: 3729

Added:
   trunk/src/host/devirginator/tests/
   trunk/src/host/devirginator/tests/Common
   trunk/src/host/devirginator/tests/Makefile
   trunk/src/host/devirginator/tests/concat
   trunk/src/host/devirginator/tests/else
   trunk/src/host/devirginator/tests/endif
   trunk/src/host/devirginator/tests/ifdef
   trunk/src/host/devirginator/tests/ifndef
   trunk/src/host/devirginator/tests/macro
   trunk/src/host/devirginator/tests/nested
Modified:
   trunk/src/host/devirginator/envedit.pl
Log:
- envedit.pl: added CPP-style conditionals (#ifdef, #ifndef, #else, #endif)
- envedit.pl: added option -D var[=value] to add variables for use during
  preprocessing
- envedit.pl: added CPP-style macro expansion (MACRO and MACRO##TOKEN)
- tests/: regression tests for new envedit.pl features



Modified: trunk/src/host/devirginator/envedit.pl
===================================================================
--- trunk/src/host/devirginator/envedit.pl	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/envedit.pl	2007-12-24 14:21:01 UTC (rev 3729)
@@ -21,6 +21,26 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 
+#
+# Preprocessing operations:
+#
+# CPP-like conditionals:
+#
+# #ifdef VAR
+# #ifndef VAR
+# #else
+# #endif
+#
+# Macro expansion:
+#
+# MACRO
+# MACRO##TOKEN
+#
+# Note that #ifdef/#ifndef use both existing environment variables (i.e., those
+# present in the environment at the time of processing) and  preprocessing
+# variables (i.e., those set with -D), while macro expansion only uses
+# preprocessing variables.
+#
 
 $ENV_SIZE = 0x4000;
 
@@ -29,17 +49,20 @@
 {
     print STDERR
 "usage: $0 [-I dir] [-s size] [-c] [-i file] [-o file|-p] [-f env_file]\n".
-"                  [var=[value] ...]\n".
-"  -c           ignore CRC errors in input environment\n".
-"  -i file      read environment from file (default: use empty environment)\n".
-"  -o file      write environment to file (default: write to stdout)\n".
-"  -p           print environment in human-readable form to stdout\n".
-"  -s bytes     environment size in bytes (default: 16384)\n".
-"  -f env_file  read changes from env_file\n".
-"  -I dir       add directory to INC path (to find crc32.pl)\n".
-"  var=         remove the specified variable\n".
-"  var=value    set the specified variable\n".
+"                  [-D var[=value]] [var=[value] ...]\n".
+"  -c             ignore CRC errors in input environment\n".
+"  -f env_file    read changes from env_file\n".
+"  -i file        read environment from file (default: use empty ".
+  "environment)\n".
+"  -o file        write environment to file (default: write to stdout)\n".
+"  -p             print environment in human-readable form to stdout\n".
+"  -s bytes       environment size in bytes (default: 16384)\n".
+"  -D var[=value] define a variable for env_file preprocessing only\n".
+"  -I dir         add directory to INC path (to find crc32.pl)\n".
+"  var=           remove the specified variable\n".
+"  var=value      set the specified variable\n".
 "The options -I, -c, and -s, if present, must precede all other options.\n";
+"The options -D, if present, must precede -f.\n";
     exit(1);
 }
 
@@ -166,6 +189,17 @@
 	shift(@ARGV);
 	push(@INC, shift @ARGV);
     }
+    elsif ($ARGV[0] eq "-D") {
+	&usage unless defined $ARGV[1];
+	shift(@ARGV);
+	if ($ARGV[0] =~ /=/) {
+	    $def{$`} = $';
+	}
+	else {
+	    $def{$ARGV[0]} = 1;
+	}
+	shift @ARGV;
+    }
     elsif ($ARGV[0] eq "-f") {
 	&usage unless defined $ARGV[1];
 	shift(@ARGV);
@@ -174,6 +208,51 @@
 	open(FILE, $file) || die "$file: $!";
 	while (<FILE>) {
 	    chop;
+	    if (/^\s*#if(n?)def\s+(\S+)/) {
+		if (!$false &&
+		  (defined $env{$2} || defined $def{$2}) == ($1 ne "n")) {
+		    $true++;
+		}
+		else {
+		    $false++;
+		}
+	    }
+	    elsif (/^\s*#else\b/) {
+		if (!$false && !$true) {
+		    print STDERR "$file:$.: #else without #if...\n";
+		    exit(1);
+		}
+		if (!$false) {
+		    $true--;
+		    $false++;
+		}
+		elsif ($false == 1) {
+		    $false--;
+		    $true++;
+		}
+	    }
+	    elsif (/^\s*#endif\b/) {
+		if (!$false && !$true) {
+		    print STDERR "$file:$.: #endif without #if...\n";
+		    exit(1);
+		}
+		if ($false) {
+		    $false--;
+		}
+		else {
+		    $true--;
+		}
+	    }
+	    next if $false;
+
+	    $tmp = "";
+	    for $def (keys %def) {
+		while (/(##)?\b$def\b(##)?/) {
+		    $tmp = $`.$def{$def};
+		    $_ = $';
+		}
+	    }
+	    $_ = $tmp.$_;
 	    s/#.*//;
 	    s/\s*$//;
 	    next if /^\s*$/;

Added: trunk/src/host/devirginator/tests/Common
===================================================================
--- trunk/src/host/devirginator/tests/Common	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/Common	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+
+tests=0
+
+
+fail()
+{
+    echo FAILED "($SCRIPT)" 1>&2
+    cat _out 1>&2
+    exit 1
+}
+
+
+setup()
+{
+    echo -n "$1: " 1>&2
+    shift
+    ../envedit.pl -o _env "$@" -f - || fail
+}
+
+
+edit()
+{
+    ../envedit.pl -i _env "$@" -f - >_out 2>&1 || fail
+}
+
+
+edit_fail()
+{
+    ../envedit.pl -i _env "$@" -f - >_out 2>&1 && fail
+}
+
+
+expect()
+{
+    if ! diff -u - _out >_tmp; then
+	echo FAILED "($SCRIPT)" 1>&2
+	cat _tmp
+	exit
+    fi
+    rm -rf _env _out _tmp
+    echo PASSED 1>&2
+    passed=`expr ${passed:-0} + 1`
+}

Added: trunk/src/host/devirginator/tests/Makefile
===================================================================
--- trunk/src/host/devirginator/tests/Makefile	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/Makefile	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,8 @@
+.PHONY:	tests
+
+# Explicitly enumerate the lower case letters to escape localization weirdness.
+
+tests:
+	for n in [abcdefghijklmnopqrstuvwxyz]*; do \
+	  SCRIPT=$$n . ./$$n; done; \
+	  echo "Passed all $$passed tests" 2>&1

Added: trunk/src/host/devirginator/tests/concat
===================================================================
--- trunk/src/host/devirginator/tests/concat	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/concat	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,42 @@
+#!/bin/sh
+. Common
+
+# -----------------------------------------------------------------------------
+
+setup "concatenate prefix" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=foo##bar
+EOF
+
+expect <<EOF
+a=barbar
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "concatenate suffix" <<EOF
+EOF
+
+edit -D bar=foo -p <<EOF
+a=foo##bar
+EOF
+
+expect <<EOF
+a=foofoo
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "concatenate embedded" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=x##foo##y
+EOF
+
+expect <<EOF
+a=xbary
+EOF
+


Property changes on: trunk/src/host/devirginator/tests/concat
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/host/devirginator/tests/else
===================================================================
--- trunk/src/host/devirginator/tests/else	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/else	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,61 @@
+#!/bin/sh
+. Common
+
+# -----------------------------------------------------------------------------
+
+setup "else if true" <<EOF
+EOF
+
+edit -D foo -p <<EOF
+a=1
+#ifdef foo
+b=2
+#else
+c=3
+#endif
+d=4
+EOF
+
+expect <<EOF
+a=1
+b=2
+d=4
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "else if false" <<EOF
+EOF
+
+edit -p <<EOF
+a=1
+#ifdef foo
+b=2
+#else
+c=3
+#endif
+d=4
+EOF
+
+expect <<EOF
+a=1
+c=3
+d=4
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "else without if" <<EOF
+EOF
+
+edit_fail -p <<EOF
+a=1
+#else
+b=2
+#endif
+c=3
+EOF
+
+expect <<EOF
+-:2: #else without #if...
+EOF


Property changes on: trunk/src/host/devirginator/tests/else
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/host/devirginator/tests/endif
===================================================================
--- trunk/src/host/devirginator/tests/endif	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/endif	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,17 @@
+#!/bin/sh
+. Common
+
+# -----------------------------------------------------------------------------
+
+setup "endif without if" <<EOF
+EOF
+
+edit_fail -p <<EOF
+a=1
+#endif
+b=2
+EOF
+
+expect <<EOF
+-:2: #endif without #if...
+EOF


Property changes on: trunk/src/host/devirginator/tests/endif
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/host/devirginator/tests/ifdef
===================================================================
--- trunk/src/host/devirginator/tests/ifdef	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/ifdef	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,60 @@
+#!/bin/sh
+. Common
+
+# -----------------------------------------------------------------------------
+
+setup "ifdef preprocessing variable" <<EOF
+EOF
+
+edit -D foo -p <<EOF
+one=1
+#ifdef foo
+two=2
+#endif
+three=3
+EOF
+
+expect <<EOF
+one=1
+three=3
+two=2
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "ifdef environment variable" <<EOF
+bar=foo
+EOF
+
+edit -p <<EOF
+alpha=a
+#ifdef bar
+beta=b
+#endif
+gamma=c
+EOF
+
+expect <<EOF
+alpha=a
+bar=foo
+beta=b
+gamma=c
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "ifdef undefined variable" <<EOF
+EOF
+
+edit -p <<EOF
+a=A
+#ifdef foo
+b=B
+#endif
+c=C
+EOF
+
+expect <<EOF
+a=A
+c=C
+EOF


Property changes on: trunk/src/host/devirginator/tests/ifdef
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/host/devirginator/tests/ifndef
===================================================================
--- trunk/src/host/devirginator/tests/ifndef	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/ifndef	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,39 @@
+#!/bin/sh
+. Common
+
+# -----------------------------------------------------------------------------
+
+setup "ifndef preprocessing variable" <<EOF
+EOF
+
+edit -D foo -p <<EOF
+a=1
+#ifndef foo
+b=2
+#endif
+c=3
+EOF
+
+expect <<EOF
+a=1
+c=3
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "ifndef undefined variable" <<EOF
+EOF
+
+edit -p <<EOF
+a=1
+#ifndef foo
+b=2
+#endif
+c=3
+EOF
+
+expect <<EOF
+a=1
+b=2
+c=3
+EOF


Property changes on: trunk/src/host/devirginator/tests/ifndef
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/host/devirginator/tests/macro
===================================================================
--- trunk/src/host/devirginator/tests/macro	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/macro	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,114 @@
+#!/bin/sh
+. Common
+
+# -----------------------------------------------------------------------------
+
+setup "expand macro once" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=foo
+EOF
+
+expect <<EOF
+a=bar
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "expand macro multiple times" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=foo
+b=foo
+EOF
+
+expect <<EOF
+a=bar
+b=bar
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "no recursive expansion (same word)" <<EOF
+EOF
+
+edit -D foo=foo -p <<EOF
+a=foo
+EOF
+
+expect <<EOF
+a=foo
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "no recursive expansion (multiple words)" <<EOF
+EOF
+
+edit -D foo='a foo b' -p <<EOF
+a=foo
+EOF
+
+expect <<EOF
+a=a foo b
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "do not expand macro if prefix" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=foobar
+EOF
+
+expect <<EOF
+a=foobar
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "do not expand macro if suffix" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=barfoo
+EOF
+
+expect <<EOF
+a=barfoo
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "do not expand macro if embedded" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=somefoobar
+EOF
+
+expect <<EOF
+a=somefoobar
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "do not expand macro in #if" <<EOF
+EOF
+
+edit -D foo=bar -p <<EOF
+a=1
+#ifdef foo
+b=2
+#endif
+c=3
+EOF
+
+expect <<EOF
+a=1
+b=2
+c=3
+EOF


Property changes on: trunk/src/host/devirginator/tests/macro
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/host/devirginator/tests/nested
===================================================================
--- trunk/src/host/devirginator/tests/nested	2007-12-23 19:05:36 UTC (rev 3728)
+++ trunk/src/host/devirginator/tests/nested	2007-12-24 14:21:01 UTC (rev 3729)
@@ -0,0 +1,87 @@
+#!/bin/sh
+. Common
+
+# -----------------------------------------------------------------------------
+
+setup "ifdef true ... ifdef true nested" <<EOF
+EOF
+
+edit -D foo -D bar -p <<EOF
+a=1
+#ifdef foo
+b=2
+#ifdef bar
+c=3
+#else
+d=4
+#endif # bar
+e=5
+#else
+f=6
+#endif # foo
+g=7
+EOF
+
+expect <<EOF
+a=1
+b=2
+c=3
+e=5
+g=7
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "ifdef true ... ifdef false nested" <<EOF
+EOF
+
+edit -D foo -p <<EOF
+a=1
+#ifdef foo
+b=2
+#ifdef bar
+c=3
+#else
+d=4
+#endif # bar
+e=5
+#else
+f=6
+#endif # foo
+g=7
+EOF
+
+expect <<EOF
+a=1
+b=2
+d=4
+e=5
+g=7
+EOF
+
+# -----------------------------------------------------------------------------
+
+setup "ifdef false ... ifdef true nested" <<EOF
+EOF
+
+edit -D bar -p <<EOF
+a=1
+#ifdef foo
+b=2
+#ifdef bar
+c=3
+#else
+d=4
+#endif # bar
+e=5
+#else
+f=6
+#endif # foo
+g=7
+EOF
+
+expect <<EOF
+a=1
+f=6
+g=7
+EOF


Property changes on: trunk/src/host/devirginator/tests/nested
___________________________________________________________________
Name: svn:executable
   + *





More information about the commitlog mailing list