Move IDatabase/IMaintainableDatabase to Rdbms namespace
[mediawiki.git] / tests / parser / ParserTestPrinter.php
blobac9f1509455106a56a617baa41bf7844e352c10a
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Testing
22 /**
23 * This is a TestRecorder responsible for printing information about progress,
24 * success and failure to the console. It is specific to the parserTests.php
25 * frontend.
27 class ParserTestPrinter extends TestRecorder {
28 private $total;
29 private $success;
30 private $skipped;
31 private $term;
32 private $showDiffs;
33 private $showProgress;
34 private $showFailure;
35 private $showOutput;
36 private $useDwdiff;
37 private $markWhitespace;
38 private $xmlError;
40 function __construct( $term, $options ) {
41 $this->term = $term;
42 $options += [
43 'showDiffs' => true,
44 'showProgress' => true,
45 'showFailure' => true,
46 'showOutput' => false,
47 'useDwdiff' => false,
48 'markWhitespace' => false,
50 $this->showDiffs = $options['showDiffs'];
51 $this->showProgress = $options['showProgress'];
52 $this->showFailure = $options['showFailure'];
53 $this->showOutput = $options['showOutput'];
54 $this->useDwdiff = $options['useDwdiff'];
55 $this->markWhitespace = $options['markWhitespace'];
58 public function start() {
59 $this->total = 0;
60 $this->success = 0;
61 $this->skipped = 0;
64 public function startTest( $test ) {
65 if ( $this->showProgress ) {
66 $this->showTesting( $test['desc'] );
70 private function showTesting( $desc ) {
71 print "Running test $desc... ";
74 /**
75 * Show "Reading tests from ..."
77 * @param string $path
79 public function startSuite( $path ) {
80 print $this->term->color( 1 ) .
81 "Running parser tests from \"$path\"..." .
82 $this->term->reset() .
83 "\n";
86 public function endSuite( $path ) {
87 print "\n";
90 public function record( $test, ParserTestResult $result ) {
91 $this->total++;
92 $this->success += ( $result->isSuccess() ? 1 : 0 );
94 if ( $result->isSuccess() ) {
95 $this->showSuccess( $result );
96 } else {
97 $this->showFailure( $result );
102 * Print a happy success message.
104 * @param ParserTestResult $testResult
105 * @return bool
107 private function showSuccess( ParserTestResult $testResult ) {
108 if ( $this->showProgress ) {
109 print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
114 * Print a failure message and provide some explanatory output
115 * about what went wrong if so configured.
117 * @param ParserTestResult $testResult
118 * @return bool
120 private function showFailure( ParserTestResult $testResult ) {
121 if ( $this->showFailure ) {
122 if ( !$this->showProgress ) {
123 # In quiet mode we didn't show the 'Testing' message before the
124 # test, in case it succeeded. Show it now:
125 $this->showTesting( $testResult->getDescription() );
128 print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
130 if ( $this->showOutput ) {
131 print "--- Expected ---\n{$testResult->expected}\n";
132 print "--- Actual ---\n{$testResult->actual}\n";
135 if ( $this->showDiffs ) {
136 print $this->quickDiff( $testResult->expected, $testResult->actual );
137 if ( !$this->wellFormed( $testResult->actual ) ) {
138 print "XML error: $this->xmlError\n";
143 return false;
147 * Run given strings through a diff and return the (colorized) output.
148 * Requires writable /tmp directory and a 'diff' command in the PATH.
150 * @param string $input
151 * @param string $output
152 * @param string $inFileTail Tailing for the input file name
153 * @param string $outFileTail Tailing for the output file name
154 * @return string
156 private function quickDiff( $input, $output,
157 $inFileTail = 'expected', $outFileTail = 'actual'
159 if ( $this->markWhitespace ) {
160 $pairs = [
161 "\n" => '¶',
162 ' ' => '·',
163 "\t" => '→'
165 $input = strtr( $input, $pairs );
166 $output = strtr( $output, $pairs );
169 # Windows, or at least the fc utility, is retarded
170 $slash = wfIsWindows() ? '\\' : '/';
171 $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
173 $infile = "$prefix-$inFileTail";
174 $this->dumpToFile( $input, $infile );
176 $outfile = "$prefix-$outFileTail";
177 $this->dumpToFile( $output, $outfile );
179 $shellInfile = wfEscapeShellArg( $infile );
180 $shellOutfile = wfEscapeShellArg( $outfile );
182 global $wgDiff3;
183 // we assume that people with diff3 also have usual diff
184 if ( $this->useDwdiff ) {
185 $shellCommand = 'dwdiff -Pc';
186 } else {
187 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au';
190 $diff = wfShellExec( "$shellCommand $shellInfile $shellOutfile" );
192 unlink( $infile );
193 unlink( $outfile );
195 if ( $this->useDwdiff ) {
196 return $diff;
197 } else {
198 return $this->colorDiff( $diff );
203 * Write the given string to a file, adding a final newline.
205 * @param string $data
206 * @param string $filename
208 private function dumpToFile( $data, $filename ) {
209 $file = fopen( $filename, "wt" );
210 fwrite( $file, $data . "\n" );
211 fclose( $file );
215 * Colorize unified diff output if set for ANSI color output.
216 * Subtractions are colored blue, additions red.
218 * @param string $text
219 * @return string
221 private function colorDiff( $text ) {
222 return preg_replace(
223 [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
224 [ $this->term->color( 34 ) . '$1' . $this->term->reset(),
225 $this->term->color( 31 ) . '$1' . $this->term->reset() ],
226 $text );
229 private function wellFormed( $text ) {
230 $html =
231 Sanitizer::hackDocType() .
232 '<html>' .
233 $text .
234 '</html>';
236 $parser = xml_parser_create( "UTF-8" );
238 # case folding violates XML standard, turn it off
239 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
241 if ( !xml_parse( $parser, $html, true ) ) {
242 $err = xml_error_string( xml_get_error_code( $parser ) );
243 $position = xml_get_current_byte_index( $parser );
244 $fragment = $this->extractFragment( $html, $position );
245 $this->xmlError = "$err at byte $position:\n$fragment";
246 xml_parser_free( $parser );
248 return false;
251 xml_parser_free( $parser );
253 return true;
256 private function extractFragment( $text, $position ) {
257 $start = max( 0, $position - 10 );
258 $before = $position - $start;
259 $fragment = '...' .
260 $this->term->color( 34 ) .
261 substr( $text, $start, $before ) .
262 $this->term->color( 0 ) .
263 $this->term->color( 31 ) .
264 $this->term->color( 1 ) .
265 substr( $text, $position, 1 ) .
266 $this->term->color( 0 ) .
267 $this->term->color( 34 ) .
268 substr( $text, $position + 1, 9 ) .
269 $this->term->color( 0 ) .
270 '...';
271 $display = str_replace( "\n", ' ', $fragment );
272 $caret = ' ' .
273 str_repeat( ' ', $before ) .
274 $this->term->color( 31 ) .
275 '^' .
276 $this->term->color( 0 );
278 return "$display\n$caret";
282 * Show a warning to the user
284 public function warning( $message ) {
285 echo "$message\n";
289 * Mark a test skipped
291 public function skipped( $test, $subtest ) {
292 if ( $this->showProgress ) {
293 print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n";
295 $this->skipped++;
298 public function report() {
299 if ( $this->total > 0 ) {
300 $this->reportPercentage( $this->success, $this->total );
301 } else {
302 print $this->term->color( 31 ) . "No tests found." . $this->term->reset() . "\n";
306 private function reportPercentage( $success, $total ) {
307 $ratio = wfPercent( 100 * $success / $total );
308 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)";
309 if ( $this->skipped ) {
310 print ", skipped {$this->skipped}";
312 print "... ";
314 if ( $success == $total ) {
315 print $this->term->color( 32 ) . "ALL TESTS PASSED!";
316 } else {
317 $failed = $total - $success;
318 print $this->term->color( 31 ) . "$failed tests failed!";
321 print $this->term->reset() . "\n";
323 return ( $success == $total );