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
22 use MediaWiki\Shell\Shell
;
25 * This is a TestRecorder responsible for printing information about progress,
26 * success and failure to the console. It is specific to the parserTests.php
29 class ParserTestPrinter
extends TestRecorder
{
35 private $showProgress;
39 private $markWhitespace;
42 function __construct( $term, $options ) {
46 'showProgress' => true,
47 'showFailure' => true,
48 'showOutput' => false,
50 'markWhitespace' => false,
52 $this->showDiffs
= $options['showDiffs'];
53 $this->showProgress
= $options['showProgress'];
54 $this->showFailure
= $options['showFailure'];
55 $this->showOutput
= $options['showOutput'];
56 $this->useDwdiff
= $options['useDwdiff'];
57 $this->markWhitespace
= $options['markWhitespace'];
60 public function start() {
66 public function startTest( $test ) {
67 if ( $this->showProgress
) {
68 $this->showTesting( $test['desc'] );
72 private function showTesting( $desc ) {
73 print "Running test $desc... ";
77 * Show "Reading tests from ..."
81 public function startSuite( $path ) {
82 print $this->term
->color( 1 ) .
83 "Running parser tests from \"$path\"..." .
84 $this->term
->reset() .
88 public function endSuite( $path ) {
92 public function record( $test, ParserTestResult
$result ) {
94 $this->success +
= ( $result->isSuccess() ?
1 : 0 );
96 if ( $result->isSuccess() ) {
97 $this->showSuccess( $result );
99 $this->showFailure( $result );
104 * Print a happy success message.
106 * @param ParserTestResult $testResult
109 private function showSuccess( ParserTestResult
$testResult ) {
110 if ( $this->showProgress
) {
111 print $this->term
->color( '1;32' ) . 'PASSED' . $this->term
->reset() . "\n";
116 * Print a failure message and provide some explanatory output
117 * about what went wrong if so configured.
119 * @param ParserTestResult $testResult
122 private function showFailure( ParserTestResult
$testResult ) {
123 if ( $this->showFailure
) {
124 if ( !$this->showProgress
) {
125 # In quiet mode we didn't show the 'Testing' message before the
126 # test, in case it succeeded. Show it now:
127 $this->showTesting( $testResult->getDescription() );
130 print $this->term
->color( '31' ) . 'FAILED!' . $this->term
->reset() . "\n";
132 if ( $this->showOutput
) {
133 print "--- Expected ---\n{$testResult->expected}\n";
134 print "--- Actual ---\n{$testResult->actual}\n";
137 if ( $this->showDiffs
) {
138 print $this->quickDiff( $testResult->expected
, $testResult->actual
);
139 if ( !$this->wellFormed( $testResult->actual
) ) {
140 print "XML error: $this->xmlError\n";
149 * Run given strings through a diff and return the (colorized) output.
150 * Requires writable /tmp directory and a 'diff' command in the PATH.
152 * @param string $input
153 * @param string $output
154 * @param string $inFileTail Tailing for the input file name
155 * @param string $outFileTail Tailing for the output file name
158 private function quickDiff( $input, $output,
159 $inFileTail = 'expected', $outFileTail = 'actual'
161 if ( $this->markWhitespace
) {
167 $input = strtr( $input, $pairs );
168 $output = strtr( $output, $pairs );
171 $infile = tempnam( wfTempDir(), "mwParser-$inFileTail" );
172 $this->dumpToFile( $input, $infile );
174 $outfile = tempnam( wfTempDir(), "mwParser-$outFileTail" );
175 $this->dumpToFile( $output, $outfile );
178 // we assume that people with diff3 also have usual diff
179 if ( $this->useDwdiff
) {
180 $shellCommand = 'dwdiff -Pc';
182 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ?
'fc' : 'diff -au';
185 $result = Shell
::command()
186 ->unsafeParams( $shellCommand )
187 ->params( $infile, $outfile )
189 $diff = $result->getStdout();
194 if ( $this->useDwdiff
) {
197 return $this->colorDiff( $diff );
202 * Write the given string to a file, adding a final newline.
204 * @param string $data
205 * @param string $filename
207 private function dumpToFile( $data, $filename ) {
208 $file = fopen( $filename, "wt" );
209 fwrite( $file, $data . "\n" );
214 * Colorize unified diff output if set for ANSI color output.
215 * Subtractions are colored blue, additions red.
217 * @param string $text
220 private function colorDiff( $text ) {
222 [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
223 [ $this->term
->color( 34 ) . '$1' . $this->term
->reset(),
224 $this->term
->color( 31 ) . '$1' . $this->term
->reset() ],
228 private function wellFormed( $text ) {
230 Sanitizer
::hackDocType() .
235 $parser = xml_parser_create( "UTF-8" );
237 # case folding violates XML standard, turn it off
238 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
240 if ( !xml_parse( $parser, $html, true ) ) {
241 $err = xml_error_string( xml_get_error_code( $parser ) );
242 $position = xml_get_current_byte_index( $parser );
243 $fragment = $this->extractFragment( $html, $position );
244 $this->xmlError
= "$err at byte $position:\n$fragment";
245 xml_parser_free( $parser );
250 xml_parser_free( $parser );
255 private function extractFragment( $text, $position ) {
256 $start = max( 0, $position - 10 );
257 $before = $position - $start;
259 $this->term
->color( 34 ) .
260 substr( $text, $start, $before ) .
261 $this->term
->color( 0 ) .
262 $this->term
->color( 31 ) .
263 $this->term
->color( 1 ) .
264 substr( $text, $position, 1 ) .
265 $this->term
->color( 0 ) .
266 $this->term
->color( 34 ) .
267 substr( $text, $position +
1, 9 ) .
268 $this->term
->color( 0 ) .
270 $display = str_replace( "\n", ' ', $fragment );
272 str_repeat( ' ', $before ) .
273 $this->term
->color( 31 ) .
275 $this->term
->color( 0 );
277 return "$display\n$caret";
281 * Show a warning to the user
282 * @param string $message
284 public function warning( $message ) {
289 * Mark a test skipped
290 * @param string $test
291 * @param string $subtest
293 public function skipped( $test, $subtest ) {
294 if ( $this->showProgress
) {
295 print $this->term
->color( '1;33' ) . 'SKIPPED' . $this->term
->reset() . "\n";
300 public function report() {
301 if ( $this->total
> 0 ) {
302 $this->reportPercentage( $this->success
, $this->total
);
304 print $this->term
->color( 31 ) . "No tests found." . $this->term
->reset() . "\n";
308 private function reportPercentage( $success, $total ) {
309 $ratio = wfPercent( 100 * $success / $total );
310 print $this->term
->color( 1 ) . "Passed $success of $total tests ($ratio)";
311 if ( $this->skipped
) {
312 print ", skipped {$this->skipped}";
316 if ( $success == $total ) {
317 print $this->term
->color( 32 ) . "ALL TESTS PASSED!";
319 $failed = $total - $success;
320 print $this->term
->color( 31 ) . "$failed tests failed!";
323 print $this->term
->reset() . "\n";
325 return ( $success == $total );