PreprocessorTest: test both implementations
authorTim Starling <tstarling@wikimedia.org>
Thu, 25 Aug 2016 03:17:13 +0000 (13:17 +1000)
committerTim Starling <tstarling@wikimedia.org>
Thu, 25 Aug 2016 03:40:37 +0000 (13:40 +1000)
* Instead of only testing the configured preprocessor, test each in turn.
* Fix a test error when testing Preprocessor_Hash by removing <equals>
  tags -- only Preprocessor_Hash emits them, but they have no effect on
  the expansion.

Change-Id: I596f6b66fc636b767c447af3450556bfebe28241

tests/phpunit/includes/parser/PreprocessorTest.php

index a62503a..c557a6e 100644 (file)
@@ -8,28 +8,44 @@ class PreprocessorTest extends MediaWikiTestCase {
         */
        protected $mOptions;
        /**
-        * @var Preprocessor
+        * @var array
         */
-       protected $mPreprocessor;
+       protected $mPreprocessors;
+
+       protected static $classNames = [
+               'Preprocessor_DOM',
+               'Preprocessor_Hash'
+       ];
 
        protected function setUp() {
-               global $wgParserConf, $wgContLang;
+               global $wgContLang;
                parent::setUp();
                $this->mOptions = ParserOptions::newFromUserAndLang( new User, $wgContLang );
-               $name = isset( $wgParserConf['preprocessorClass'] )
-                       ? $wgParserConf['preprocessorClass']
-                       : 'Preprocessor_DOM';
 
-               $this->mPreprocessor = new $name( $this );
+               $this->mPreprocessors = [];
+               foreach ( self::$classNames as $className ) {
+                       $this->mPreprocessors[$className] = new $className( $this );
+               }
        }
 
        function getStripList() {
                return [ 'gallery', 'display map' /* Used by Maps, see r80025 CR */, '/foo' ];
        }
 
+       protected static function addClassArg( $testCases ) {
+               $newTestCases = [];
+               foreach ( self::$classNames as $className ) {
+                       foreach ( $testCases as $testCase ) {
+                               array_unshift( $testCase, $className );
+                               $newTestCases[] = $testCase;
+                       }
+               }
+               return $newTestCases;
+       }
+
        public static function provideCases() {
                // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
-               return [
+               return self::addClassArg( [
                        [ "Foo", "<root>Foo</root>" ],
                        [ "<!-- Foo -->", "<root><comment>&lt;!-- Foo --&gt;</comment></root>" ],
                        [ "<!-- Foo --><!-- Bar -->", "<root><comment>&lt;!-- Foo --&gt;</comment><comment>&lt;!-- Bar --&gt;</comment></root>" ],
@@ -115,7 +131,7 @@ class PreprocessorTest extends MediaWikiTestCase {
                        [ "{{Foo|} Bar=", "<root>{{Foo|} Bar=</root>" ],
                        [ "{{Foo|} Bar=}}", "<root><template><title>Foo</title><part><name>} Bar</name>=<value></value></part></template></root>" ],
                        /* [ file_get_contents( __DIR__ . '/QuoteQuran.txt' ], file_get_contents( __DIR__ . '/QuoteQuranExpanded.txt' ) ], */
-               ];
+               ] );
                // @codingStandardsIgnoreEnd
        }
 
@@ -123,15 +139,17 @@ class PreprocessorTest extends MediaWikiTestCase {
         * Get XML preprocessor tree from the preprocessor (which may not be the
         * native XML-based one).
         *
+        * @param string $className
         * @param string $wikiText
         * @return string
         */
-       protected function preprocessToXml( $wikiText ) {
-               if ( method_exists( $this->mPreprocessor, 'preprocessToXml' ) ) {
-                       return $this->normalizeXml( $this->mPreprocessor->preprocessToXml( $wikiText ) );
+       protected function preprocessToXml( $className, $wikiText ) {
+               $preprocessor = $this->mPreprocessors[$className];
+               if ( method_exists( $preprocessor, 'preprocessToXml' ) ) {
+                       return $this->normalizeXml( $preprocessor->preprocessToXml( $wikiText ) );
                }
 
-               $dom = $this->mPreprocessor->preprocessToObj( $wikiText );
+               $dom = $preprocessor->preprocessToObj( $wikiText );
                if ( is_callable( [ $dom, 'saveXML' ] ) ) {
                        return $dom->saveXML();
                } else {
@@ -146,15 +164,21 @@ class PreprocessorTest extends MediaWikiTestCase {
         * @return string
         */
        protected function normalizeXml( $xml ) {
-               return preg_replace( '!<([a-z]+)/>!', '<$1></$1>', str_replace( ' />', '/>', $xml ) );
+               // Normalize self-closing tags
+               $xml = preg_replace( '!<([a-z]+)/>!', '<$1></$1>', str_replace( ' />', '/>', $xml ) );
+               // Remove <equals> tags, which only occur in Preprocessor_Hash and
+               // have no semantic value
+               $xml = preg_replace( '!</?equals>!', '', $xml );
+               return $xml;
        }
 
        /**
         * @dataProvider provideCases
         * @covers Preprocessor_DOM::preprocessToXml
         */
-       public function testPreprocessorOutput( $wikiText, $expectedXml ) {
-               $this->assertEquals( $this->normalizeXml( $expectedXml ), $this->preprocessToXml( $wikiText ) );
+       public function testPreprocessorOutput( $className, $wikiText, $expectedXml ) {
+               $this->assertEquals( $this->normalizeXml( $expectedXml ),
+                       $this->preprocessToXml( $className, $wikiText ) );
        }
 
        /**
@@ -162,13 +186,13 @@ class PreprocessorTest extends MediaWikiTestCase {
         */
        public static function provideFiles() {
                // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
-               return [
+               return self::addClassArg( [
                        [ "QuoteQuran" ], # http://en.wikipedia.org/w/index.php?title=Template:QuoteQuran/sandbox&oldid=237348988 GFDL + CC BY-SA by Striver
                        [ "Factorial" ], # http://en.wikipedia.org/w/index.php?title=Template:Factorial&oldid=98548758 GFDL + CC BY-SA by Polonium
                        [ "All_system_messages" ], # http://tl.wiktionary.org/w/index.php?title=Suleras:All_system_messages&oldid=2765 GPL text generated by MediaWiki
                        [ "Fundraising" ], # http://tl.wiktionary.org/w/index.php?title=MediaWiki:Sitenotice&oldid=5716 GFDL + CC BY-SA, copied there by Sky Harbor.
                        [ "NestedTemplates" ], # bug 27936
-               ];
+               ] );
                // @codingStandardsIgnoreEnd
        }
 
@@ -176,10 +200,10 @@ class PreprocessorTest extends MediaWikiTestCase {
         * @dataProvider provideFiles
         * @covers Preprocessor_DOM::preprocessToXml
         */
-       public function testPreprocessorOutputFiles( $filename ) {
+       public function testPreprocessorOutputFiles( $className, $filename ) {
                $folder = __DIR__ . "/../../../parser/preprocess";
                $wikiText = file_get_contents( "$folder/$filename.txt" );
-               $output = $this->preprocessToXml( $wikiText );
+               $output = $this->preprocessToXml( $className, $wikiText );
 
                $expectedFilename = "$folder/$filename.expected";
                if ( file_exists( $expectedFilename ) ) {
@@ -197,7 +221,8 @@ class PreprocessorTest extends MediaWikiTestCase {
         */
        public static function provideHeadings() {
                // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
-               return [ /* These should become headings: */
+               return self::addClassArg( [
+                       /* These should become headings: */
                        [ "== h ==<!--c1-->", "<root><h level=\"2\" i=\"1\">== h ==<comment>&lt;!--c1--&gt;</comment></h></root>" ],
                        [ "== h ==      <!--c1-->", "<root><h level=\"2\" i=\"1\">== h ==       <comment>&lt;!--c1--&gt;</comment></h></root>" ],
                        [ "== h ==<!--c1-->     ", "<root><h level=\"2\" i=\"1\">== h ==<comment>&lt;!--c1--&gt;</comment>      </h></root>" ],
@@ -233,7 +258,7 @@ class PreprocessorTest extends MediaWikiTestCase {
                        [ "== h == x <!--c1--><!--c2--><!--c3-->  ", "<root>== h == x <comment>&lt;!--c1--&gt;</comment><comment>&lt;!--c2--&gt;</comment><comment>&lt;!--c3--&gt;</comment>  </root>" ],
                        [ "== h ==<!--c1--> x <!--c2--><!--c3-->  ", "<root>== h ==<comment>&lt;!--c1--&gt;</comment> x <comment>&lt;!--c2--&gt;</comment><comment>&lt;!--c3--&gt;</comment>  </root>" ],
                        [ "== h ==<!--c1--><!--c2--><!--c3--> x ", "<root>== h ==<comment>&lt;!--c1--&gt;</comment><comment>&lt;!--c2--&gt;</comment><comment>&lt;!--c3--&gt;</comment> x </root>" ],
-               ];
+               ] );
                // @codingStandardsIgnoreEnd
        }
 
@@ -241,7 +266,8 @@ class PreprocessorTest extends MediaWikiTestCase {
         * @dataProvider provideHeadings
         * @covers Preprocessor_DOM::preprocessToXml
         */
-       public function testHeadings( $wikiText, $expectedXml ) {
-               $this->assertEquals( $this->normalizeXml( $expectedXml ), $this->preprocessToXml( $wikiText ) );
+       public function testHeadings( $className, $wikiText, $expectedXml ) {
+               $this->assertEquals( $this->normalizeXml( $expectedXml ),
+                       $this->preprocessToXml( $className, $wikiText ) );
        }
 }