Support >2 and JSON-formatted options in parser tests.
[lhc/web/wiklou.git] / tests / parser / parserTest.inc
index 3f8d7f9..55e93e2 100644 (file)
  */
 class ParserTest {
        /**
-        * boolean $color whereas output should be colorized
+        * @var bool $color whereas output should be colorized
         */
        private $color;
 
        /**
-        * boolean $showOutput Show test output
+        * @var bool $showOutput Show test output
         */
        private $showOutput;
 
        /**
-        * boolean $useTemporaryTables Use temporary tables for the temporary database
+        * @var bool $useTemporaryTables Use temporary tables for the temporary database
         */
        private $useTemporaryTables = true;
 
        /**
-        * boolean $databaseSetupDone True if the database has been set up
+        * @var bool $databaseSetupDone True if the database has been set up
         */
        private $databaseSetupDone = false;
 
@@ -65,7 +65,7 @@ class ParserTest {
        private $dbClone;
 
        /**
-        * string $oldTablePrefix Original table prefix
+        * @var string $oldTablePrefix Original table prefix
         */
        private $oldTablePrefix;
 
@@ -170,7 +170,7 @@ class ParserTest {
                        'transformVia404' => false,
                        'backend' => new FSFileBackend( array(
                                'name' => 'local-backend',
-                               'lockManager' => 'fsLockManager',
+                               'wikiId' => wfWikiId(),
                                'containerPaths' => array(
                                        'local-public' => wfTempDir() . '/test-repo/public',
                                        'local-thumb' => wfTempDir() . '/test-repo/thumb',
@@ -494,6 +494,9 @@ class ParserTest {
 
        /**
         * Get a Parser object
+        *
+        * @param string $preprocessor
+        * @return Parser
         */
        function getParser( $preprocessor = null ) {
                global $wgParserConf;
@@ -566,6 +569,7 @@ class ParserTest {
                        $out = $parser->getPreloadText( $input, $title, $options );
                } else {
                        $output = $parser->parse( $input, $title, $options, true, true, 1337 );
+                       $output->setTOCEnabled( !isset( $opts['notoc'] ) );
                        $out = $output->getText();
 
                        if ( isset( $opts['showtitle'] ) ) {
@@ -618,7 +622,7 @@ class ParserTest {
        /**
         * Use a regex to find out the value of an option
         * @param $key String: name of option val to retrieve
-        * @param $opts Options array to look in
+        * @param $opts array: Options array to look in
         * @param $default Mixed: default value returned if not found
         */
        private static function getOptionValue( $key, $opts, $default ) {
@@ -638,48 +642,65 @@ class ParserTest {
                // foo="bar baz"
                // foo=[[bar baz]]
                // foo=bar,"baz quux"
-               $regex = '/\b
-                       ([\w-]+)                                                # Key
-                       \b
-                       (?:\s*
-                               =                                               # First sub-value
-                               \s*
-                               (
-                                       "
-                                               [^"]*                   # Quoted val
-                                       "
+               // foo={...json...}
+               $defs = '(?(DEFINE)
+                       (?<qstr>                                        # Quoted string
+                               "
+                               (?:[^\\\\"] | \\\\.)*
+                               "
+                       )
+                       (?<json>
+                               \{              # Open bracket
+                               (?:
+                                       [^"{}] |                                # Not a quoted string or object, or
+                                       (?&qstr) |                              # A quoted string, or
+                                       (?&json)                                # A json object (recursively)
+                               )*
+                               \}              # Close bracket
+                       )
+            (?<value>
+                               (?:
+                                       (?&qstr)                        # Quoted val
                                |
                                        \[\[
                                                [^]]*                   # Link target
                                        \]\]
                                |
                                        [\w-]+                          # Plain word
+                               |
+                                       (?&json)                        # JSON object
+                               )
+                       )
+               )';
+               $regex = '/'.$defs.'\b
+                       (?<k>[\w-]+)                            # Key
+                       \b
+                       (?:\s*
+                               =                                               # First sub-value
+                               \s*
+                               (?<v>
+                                       (?&value)
+                                       (?:\s*
+                                               ,                               # Sub-vals 1..N
+                                               \s*
+                                               (?&value)
+                                       )*
                                )
-                               (?:\s*
-                                       ,                                       # Sub-vals 1..N
-                                       \s*
-                                       (
-                                               "[^"]*"                 # Quoted val
-                                       |
-                                               \[\[[^]]*\]\]   # Link target
-                                       |
-                                               [\w-]+                  # Plain word
-                                       )
-                               )*
                        )?
                        /x';
+               $valueregex = '/'.$defs.'(?&value)/x';
 
                if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
                        foreach ( $matches as $bits ) {
-                               array_shift( $bits );
-                               $key = strtolower( array_shift( $bits ) );
-                               if ( count( $bits ) == 0 ) {
+                               $key = strtolower( $bits[ 'k' ] );
+                               if ( !isset( $bits[ 'v' ] ) ) {
                                        $opts[$key] = true;
-                               } elseif ( count( $bits ) == 1 ) {
-                                       $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
                                } else {
-                                       // Array!
-                                       $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
+                                       preg_match_all( $valueregex, $bits[ 'v' ], $vmatches );
+                                       $opts[$key] = array_map( array( $this, 'cleanupOption' ), $vmatches[0] );
+                                       if ( count( $opts[$key] ) == 1 ) {
+                                               $opts[$key] = $opts[$key][0];
+                                       }
                                }
                        }
                }
@@ -688,12 +709,16 @@ class ParserTest {
 
        private function cleanupOption( $opt ) {
                if ( substr( $opt, 0, 1 ) == '"' ) {
-                       return substr( $opt, 1, -1 );
+                       return stripcslashes( substr( $opt, 1, -1 ) );
                }
 
                if ( substr( $opt, 0, 2 ) == '[[' ) {
                        return substr( $opt, 2, -2 );
                }
+
+               if ( substr( $opt, 0, 1 ) == '{' ) {
+                       return FormatJson::decode( $opt, true );
+               }
                return $opt;
        }
 
@@ -734,7 +759,7 @@ class ParserTest {
                                'transformVia404' => false,
                                'backend' => new FSFileBackend( array(
                                        'name' => 'local-backend',
-                                       'lockManager' => 'fsLockManager',
+                                       'wikiId' => wfWikiId(),
                                        'containerPaths' => array(
                                                'local-public' => $this->uploadDir,
                                                'local-thumb' => $this->uploadDir . '/thumb',