Re: Re: regular expression in test output (new feature for run-tests.php)

From: Date: Tue, 03 Mar 2009 10:55:40 +0000
Subject: Re: Re: regular expression in test output (new feature for run-tests.php)
References: 1 2  Groups: php.qa 
Request: Send a blank email to php-qa+get-64700@lists.php.net to get a copy of this message
Iain Lewis wrote:
Not that I got any responses to this but I still think this would be a really useful idea. I've attached a patch below and a trivial phpt example that passes on both 32bit and 64bit php which demos what you could do with it. If people think this is useful, I'd love to see it committed. Iain - it looks good to me. I've committed in 6, 53 and 52. Will update the QA documentation as well. Thanks.
--- run-tests.php       2009-02-27 15:48:02.000000000 +0000
+++ run-tests.php.mod 2009-02-27 14:48:28.000000000 +0000 @@ -1738,7 +1738,33 @@
                $wanted_re = preg_replace('/\r\n/', "\n", $wanted);
                if (isset($section_text['EXPECTF'])) {
-                       $wanted_re = preg_quote($wanted_re, '/');
+
+            // do preg_quote, but miss out any %r delimited sections
+            $temp = "";
+            $r = "%r";
+            $startOffset = 0;
+            $length = strlen($wanted_re);
+            while($startOffset < $length) {
+              $start = strpos($wanted_re, $r, $startOffset);
+              if ($start !== false) {
+                // we have found a start tag
+                $end = strpos($wanted_re, $r, $start+2);
+                if ($end === false) {
+                  // unbalanced tag, ignore it.
+                  $end = $start = $length;
+                }
+              } else {
+                // no more %r sections
+                $start = $end = $length;
+              }
+              // quote a non re portion of the string
+              $temp = $temp . preg_quote(substr($wanted_re, $startOffset, ($start - $startOffset)),  '/');
+              // add the re unquoted.
+              $temp = $temp . substr($wanted_re, $start+2, ($end - $start-2));
+              $startOffset = $end + 2;
+            }
+            $wanted_re = $temp;
+
                        $wanted_re = str_replace(
                                array('%binary_string_optional%'),
                                version_compare(PHP_VERSION, '6.0.0-dev') == -1 ? 'string' : 'binary string',
and a short example phpt which makes use of this: --TEST-- A test --FILE-- <?php $number = 9223372036854775807; var_dump($number); ?> --EXPECTF-- %rint|float%r(%f) Iain Lewis wrote:
Hi all, I have been writing some tests recently, and occasionally thought it would be useful to use a regex for output matching. However, using the --EXPECTREGEX-- can be a bit painful as you sometimes have to escape a large number of characters. I think it would be really useful to add a new specifier to the EXPECTF section that would allow you to embed a small regex in the output. If you used say %r as an opening and closing tag you could then do something like (for a very trivial example): --TEST-- Testing host --FILE-- <?php echo "host name is " . gethostbyname("example.com"); ?> --EXPECTF-- host name is %r\d\d\d\.\d\d\d.\d\d\d.\d\d\d%r This allows you to write a test which is a bit more specific about what it is expecting, and hopefully less prone to false positives. What does anyone think? I've spoken to Zoe about this and she is happy to try and implement it if people are interested.


« previous php.qa (#64700) next »