Allow more-descriptive section names for parserTests
authorArlo Breault <abreault@wikimedia.org>
Thu, 10 Oct 2013 23:10:15 +0000 (16:10 -0700)
committerC. Scott Ananian <cscott@cscott.net>
Wed, 12 Mar 2014 16:07:56 +0000 (12:07 -0400)
This patch allows `!!wikitext` (for `!!input`) and `!!html` (for
`!!result`). This is more in line with what those sections actually
contain and closer to the semantics of how they are used.

The old names are accepted as aliases to accomodate parser tests to
provide a migration path for extensions and other users of the
parser tests framework.

In addition to `!!html`, this patch also accepts `!!html/*` and the
more-specific `!!html/php`.  This allows tests to include a number
of different "outputs" for a given wikitext input, for example
`!!html/parsoid` and `!!html/php`.

Co-authored-by: C. Scott Ananian <cscott@cscott.net>
Co-authored-by: Arlo Breault <abreault@wikimedia.org>
Change-Id: Ie4e68960ca7c352af495ebb59ba83488935a44c4

RELEASE-NOTES-1.23
tests/parser/parserTest.inc
tests/parser/parserTests.txt
tests/testHelpers.inc

index 38302f8..145f29b 100644 (file)
@@ -292,6 +292,12 @@ changes to languages because of Bugzilla reports.
 * RecentChange::mExtra['lang'] is no longer set and should no longer be used.
   Extensions should read from other configuration variables, including
   $wgLocalInterwikis, to identify the current wiki.
+* Sections in the parser test framework have been renamed and the old
+  section names are deprecated.  Please use "!!wikitext" and "!!html"
+  (or "!!html/php") instead of "!!input" and "!!result".  This allows
+  us to extend parser tests to accommodate additional input/output
+  pairs, such as "!!html/parsoid" (for the output of the Parsoid
+  parser, where it differs from the PHP parser).
 
 ==== Removed classes ====
 * FakeMemCachedClient (deprecated in 1.18)
index 55e93e2..95b9d05 100644 (file)
@@ -387,7 +387,7 @@ class ParserTest {
 
                foreach ( $filenames as $filename ) {
                        $contents = file_get_contents( $filename );
-                       preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
+                       preg_match_all( '/!!\s*(input|wikitext)\n(.*?)\n!!\s*(result|html|html\/\*|html\/php)/s', $contents, $matches );
 
                        foreach ( $matches[1] as $match ) {
                                $dict .= $match . "\n";
index bbfea6a..4f85b9f 100644 (file)
@@ -9388,7 +9388,7 @@ bug 22297: safesubst: works during normal parse
 </p>
 !! end
 
-!! test:
+!! test
 subst: does not work during normal parse
 !! input
 {{SubstTest}}
index f4433f4..847ae32 100644 (file)
@@ -419,7 +419,7 @@ class TestFileIterator implements Iterator {
                        $this->lineNum++;
                        $matches = array();
 
-                       if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
+                       if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
                                $this->section = strtolower( $matches[1] );
 
                                if ( $this->section == 'endarticle' ) {
@@ -467,8 +467,10 @@ class TestFileIterator implements Iterator {
 
                                if ( $this->section == 'end' ) {
                                        $this->checkSection( 'test' );
-                                       $this->checkSection( 'input' );
-                                       $this->checkSection( 'result' );
+                                       // "input" and "result" are old section names allowed
+                                       // for backwards-compatibility.
+                                       $input = $this->checkSection( array( 'wikitext', 'input' ), false );
+                                       $result = $this->checkSection( array( 'html/php', 'html/*', 'html', 'result' ), false );
 
                                        if ( !isset( $this->sectionData['options'] ) ) {
                                                $this->sectionData['options'] = '';
@@ -478,8 +480,9 @@ class TestFileIterator implements Iterator {
                                                $this->sectionData['config'] = '';
                                        }
 
-                                       if ( ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled )
-                                               || ( preg_match( '/\\bparsoid\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runParsoid )
+                                       if ( $input == false || $result == false ||
+                                                ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled )
+                                               || ( preg_match( '/\\bparsoid\\b/i', $this->sectionData['options'] ) && $result != 'html/php' && !$this->parserTest->runParsoid )
                                                || !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] ) )
                                        ) {
                                                # disabled test
@@ -501,8 +504,8 @@ class TestFileIterator implements Iterator {
 
                                        $this->test = array(
                                                'test' => ParserTest::chomp( $this->sectionData['test'] ),
-                                               'input' => ParserTest::chomp( $this->sectionData['input'] ),
-                                               'result' => ParserTest::chomp( $this->sectionData['result'] ),
+                                               'input' => ParserTest::chomp( $this->sectionData[ $input ] ),
+                                               'result' => ParserTest::chomp( $this->sectionData[ $result ] ),
                                                'options' => ParserTest::chomp( $this->sectionData['options'] ),
                                                'config' => ParserTest::chomp( $this->sectionData['config'] ),
                                        );
@@ -538,18 +541,35 @@ class TestFileIterator implements Iterator {
 
        /**
         * Verify the current section data has some value for the given token
-        * name (first parameter).
+        * name(s) (first parameter).
         * Throw an exception if it is not set, referencing current section
         * and adding the current file name and line number
         *
-        * @param $token String: expected token that should have been mentionned before closing this section
+        * @param $token String|Array: expected token(s) that should have been
+        * mentioned before closing this section
+        * @param $fatal Boolean: true iff an exception should be thrown if
+        * the section is not found.
         */
-       private function checkSection( $token ) {
+       private function checkSection( $tokens, $fatal = true ) {
                if ( is_null( $this->section ) ) {
                        throw new MWException( __METHOD__ . " can not verify a null section!\n" );
                }
+               if ( !is_array( $tokens ) ) {
+                       $tokens = array( $tokens );
+               }
+               if ( count( $tokens ) == 0 ) {
+                       throw new MWException( __METHOD__ . " can not verify zero sections!\n" );
+               }
 
-               if ( !isset( $this->sectionData[$token] ) ) {
+               $data = $this->sectionData;
+               $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
+                       return isset( $data[ $token ] );
+               } );
+
+               if ( count( $tokens ) == 0 ) {
+                       if ( !$fatal ) {
+                               return false;
+                       }
                        throw new MWException( sprintf(
                                "'%s' without '%s' at line %s of %s\n",
                                $this->section,
@@ -558,7 +578,18 @@ class TestFileIterator implements Iterator {
                                $this->file
                        ) );
                }
-               return true;
+               if ( count( $tokens ) > 1 ) {
+                       throw new MWException( sprintf(
+                               "'%s' with unexpected tokens '%s' at line %s of %s\n",
+                               $this->section,
+                               implode( ',', $tokens ),
+                               $this->lineNum,
+                               $this->file
+                       ) );
+               }
+
+               $tokens = array_values( $tokens );
+               return $tokens[ 0 ];
        }
 }