3 * Fake parser that output the difference of two different parsers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
27 class ParserDiffTest
{
30 public $shortOutput = false;
32 public function __construct( $conf ) {
33 if ( !isset( $conf['parsers'] ) ) {
34 throw new MWException( __METHOD__
. ': no parsers specified' );
39 public function init() {
40 if ( !is_null( $this->parsers
) ) {
44 if ( isset( $this->conf
['shortOutput'] ) ) {
45 $this->shortOutput
= $this->conf
['shortOutput'];
48 foreach ( $this->conf
['parsers'] as $i => $parserConf ) {
49 if ( !is_array( $parserConf ) ) {
51 $parserConf = [ 'class' => $parserConf ];
53 $class = $parserConf['class'];
55 $this->parsers
[$i] = new $class( $parserConf );
59 public function __call( $name, $args ) {
65 foreach ( $this->parsers
as $i => $parser ) {
66 $currentResult = call_user_func_array( [ &$this->parsers
[$i], $name ], $args );
70 if ( is_object( $lastResult ) ) {
71 if ( $lastResult != $currentResult ) {
75 if ( $lastResult !== $currentResult ) {
80 $results[$i] = $currentResult;
81 $lastResult = $currentResult;
84 if ( count( $results ) == 2 ) {
86 foreach ( $this->parsers
as $i => $parser ) {
87 $resultsList[] = var_export( $results[$i], true );
89 $diff = wfDiff( $resultsList[0], $resultsList[1] );
91 $diff = '[too many parsers]';
93 $msg = "ParserDiffTest: results mismatch on call to $name\n";
94 if ( !$this->shortOutput
) {
95 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
97 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
99 throw new MWException( $msg );
104 public function formatArray( $array ) {
105 if ( $this->shortOutput
) {
106 foreach ( $array as $key => $value ) {
107 if ( $value instanceof ParserOutput
) {
108 $array[$key] = "ParserOutput: {$value->getText()}";
112 return var_export( $array, true );
115 public function setFunctionHook( $id, $callback, $flags = 0 ) {
117 foreach ( $this->parsers
as $parser ) {
118 $parser->setFunctionHook( $id, $callback, $flags );