From fd8e3da7b8d116803ad2cebbd088d09356822329 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Thu, 25 Aug 2016 13:17:13 +1000 Subject: [PATCH] PreprocessorTest: test both implementations * Instead of only testing the configured preprocessor, test each in turn. * Fix a test error when testing Preprocessor_Hash by removing tags -- only Preprocessor_Hash emits them, but they have no effect on the expansion. Change-Id: I596f6b66fc636b767c447af3450556bfebe28241 --- .../includes/parser/PreprocessorTest.php | 74 +++++++++++++------ 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/tests/phpunit/includes/parser/PreprocessorTest.php b/tests/phpunit/includes/parser/PreprocessorTest.php index a62503a629..c557a6e1f1 100644 --- a/tests/phpunit/includes/parser/PreprocessorTest.php +++ b/tests/phpunit/includes/parser/PreprocessorTest.php @@ -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", "Foo" ], [ "", "<!-- Foo -->" ], [ "", "<!-- Foo --><!-- Bar -->" ], @@ -115,7 +131,7 @@ class PreprocessorTest extends MediaWikiTestCase { [ "{{Foo|} Bar=", "{{Foo|} Bar=" ], [ "{{Foo|} Bar=}}", "" ], /* [ 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>', str_replace( ' />', '/>', $xml ) ); + // Normalize self-closing tags + $xml = preg_replace( '!<([a-z]+)/>!', '<$1>', str_replace( ' />', '/>', $xml ) ); + // Remove tags, which only occur in Preprocessor_Hash and + // have no semantic value + $xml = preg_replace( '!!', '', $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 ==", "== h ==<!--c1-->" ], [ "== h == ", "== h == <!--c1-->" ], [ "== h == ", "== h ==<!--c1--> " ], @@ -233,7 +258,7 @@ class PreprocessorTest extends MediaWikiTestCase { [ "== h == x ", "== h == x <!--c1--><!--c2--><!--c3--> " ], [ "== h == x ", "== h ==<!--c1--> x <!--c2--><!--c3--> " ], [ "== h == x ", "== h ==<!--c1--><!--c2--><!--c3--> x " ], - ]; + ] ); // @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 ) ); } } -- 2.20.1