com phpruntests: Added support for %r: src/testcase/sections/outputsections/rtExpectFSection.php
tests/testcase/sections/outputsections/rtExpectFSectionTest.php

From: Date: Wed, 13 May 2009 16:58:27 +0000
Subject: com phpruntests: Added support for %r: src/testcase/sections/outputsections/rtExpectFSection.php
tests/testcase/sections/outputsections/rtExpectFSectionTest.php
Groups: php.qa 
Request: Send a blank email to php-qa+get-66463@lists.php.net to get a copy of this message
Commit: d238e3db2d982e5fdb3a7407e014ea4e7ea24d4b Author: Zoe Slattery <zoe@php.net> Wed, 13 May 2009 16:58:27 +0000 Parents: 093b1ba2b18f0a4498152deb355d43a0c8414841 Branches: master Link: http://git.php.net/?p=phpruntests.git;a=commitdiff;h=d238e3db2d982e5fdb3a7407e014ea4e7ea24d4b Log: Added support for %r Changed paths: M src/testcase/sections/outputsections/rtExpectFSection.php M tests/testcase/sections/outputsections/rtExpectFSectionTest.php Diff: diff --git a/src/testcase/sections/outputsections/rtExpectFSection.php b/src/testcase/sections/outputsections/rtExpectFSection.php index 40de4df...80c698b 100644 --- a/src/testcase/sections/outputsections/rtExpectFSection.php +++ b/src/testcase/sections/outputsections/rtExpectFSection.php @@ -9,18 +9,19 @@ class rtExpectFSection extends rtOutputSection parent::createPattern(); $this->createPattern(); } - + /** * Create the pattern used to match against actual output * */ protected function createPattern() - { - $this->expectedPattern = preg_quote($this->expectedPattern, '/'); - $this->expectedPattern = $this->expectfUnicodeSubstitutions ($this->expectedPattern); - $this->expectedPattern = $this->expectfRegexSubstitutions($this->expectedPattern); + { + + $this->expectedPattern = $this->expectfEmbeddedRegex($this->expectedPattern); + $this->expectedPattern = $this->expectfUnicodeSubstitutions ($this->expectedPattern); + $this->expectedPattern = $this->expectfRegexSubstitutions($this->expectedPattern); } - + /* * Replaces string with unicode and vice versa. Allows same tests for PHP5 and PHP6 * @param string @@ -29,25 +30,25 @@ class rtExpectFSection extends rtOutputSection private function expectfUnicodeSubstitutions($string) { $string = str_replace( - array('%unicode_string_optional%'), - version_compare(PHP_VERSION, '6.0.0-dev') == -1 ? 'string' : 'Unicode string', $string + array('%unicode_string_optional%'), + version_compare(PHP_VERSION, '6.0.0-dev') == -1 ? 'string' : 'Unicode string', $string ); - + $string = str_replace( - array('%unicode\|string%', '%string\|unicode%'), - version_compare(PHP_VERSION, '6.0.0-dev') == -1 ? 'string' : 'unicode', - $string + array('%unicode\|string%', '%string\|unicode%'), + version_compare(PHP_VERSION, '6.0.0-dev') == -1 ? 'string' : 'unicode', + $string ); - + $string = str_replace( - array('%u\|b%', '%b\|u%'), - version_compare(PHP_VERSION, '6.0.0-dev') == -1 ? '' : 'u', - $string + array('%u\|b%', '%b\|u%'), + version_compare(PHP_VERSION, '6.0.0-dev') == -1 ? '' : 'u', + $string ); return $string; } - + /* * Substitute the %strings used in EXPECTF sections with a regular expression * @param string @@ -68,7 +69,40 @@ class rtExpectFSection extends rtOutputSection $string = str_replace('%c', '.', $string); return $string; - } + } + + /* + * Deal with embedded regular expressions between %r tags. + * @param string + * @return string + */ + private function expectfEmbeddedRegex($string) { + $temp = ""; + $r = "%r"; + $startOffset = 0; + + $length = strlen($string); + while($startOffset < $length) { + $start = strpos($string, $r, $startOffset); + if ($start !== false) { + // we have found a start tag + $end = strpos($string, $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($string, $startOffset, ($start - $startOffset)), '/'); + // add the re unquoted. + $temp = $temp . '(' .substr($string, $start+2, ($end - $start-2)) . ')'; + $startOffset = $end + 2; + } + return substr($temp, 0, -2); + } /** * Compare the test output with the expected pattern @@ -79,14 +113,14 @@ class rtExpectFSection extends rtOutputSection public function compare($testOutput) { $testOutput = trim(preg_replace("/$this->carriageReturnLineFeed/", $this->lineFeed, $testOutput)); - + /*For debugging: - + file_put_contents(sys_get_temp_dir().'/zrtexp',$this->expectedPattern ); file_put_contents(sys_get_temp_dir().'/zrtout', $testOutput ); */ - + if (preg_match((binary) "/^$this->expectedPattern\$/s", $testOutput)) { return true; } else { diff --git a/tests/testcase/sections/outputsections/rtExpectFSectionTest.php b/tests/testcase/sections/outputsections/rtExpectFSectionTest.php index a72c7ef..a776b1b 100644 --- a/tests/testcase/sections/outputsections/rtExpectFSectionTest.php +++ b/tests/testcase/sections/outputsections/rtExpectFSectionTest.php @@ -76,6 +76,14 @@ class rtExpectFSectionTest extends PHPUnit_Framework_TestCase $this->assertEquals('Hello [+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $pattern); } + + public function testPercentR() + { + $expectFSection = new rtExpectFSection('EXPECTF', array('%unicode|string%(18) "%r\0%rMyClass%r\0%rpri_value%r\0%r"')); + $pattern = $expectFSection->getPattern(); + + $this->assertEquals('string\(18\) "(\0)MyClass(\0)pri_value(\0)"', $pattern); + } public function testPercentC() {

« previous php.qa (#66463) next »