| 1 |
<?php |
| 2 |
// phpcs:disable Generic.Files.LineLength.TooLong |
| 3 |
require_once __DIR__ . '/../tools/Maintenance.php'; |
| 4 |
|
| 5 |
use MediaWiki\Settings\SettingsBuilder; |
| 6 |
use SebastianBergmann\Diff\Differ; |
| 7 |
use Wikimedia\Parsoid\ParserTests\Stats; |
| 8 |
use Wikimedia\Parsoid\ParserTests\Test; |
| 9 |
use Wikimedia\Parsoid\ParserTests\TestRunner; |
| 10 |
use Wikimedia\Parsoid\ParserTests\TestUtils; |
| 11 |
use Wikimedia\Parsoid\Utils\ScriptUtils; |
| 12 |
|
| 13 |
// phpcs:ignore MediaWiki.Files.ClassMatchesFilename.WrongCase |
| 14 |
class ParserTests extends \Wikimedia\Parsoid\Tools\Maintenance { |
| 15 |
use \Wikimedia\Parsoid\Tools\ExtendedOptsProcessor; |
| 16 |
|
| 17 |
/** @var array */ |
| 18 |
public $processedOptions; |
| 19 |
|
| 20 |
// PORT-FIXME: Used to be colors::mode in all the use sites |
| 21 |
public static $colors_mode; |
| 22 |
|
| 23 |
/** @var Differ */ |
| 24 |
private static $differ; |
| 25 |
|
| 26 |
public function __construct() { |
| 27 |
parent::__construct(); |
| 28 |
self::setupOpts( $this ); |
| 29 |
$this->setAllowUnregisteredOptions( false ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritDoc |
| 34 |
*/ |
| 35 |
public function finalSetup( ?SettingsBuilder $settingsBuilder = null ) { |
| 36 |
parent::finalSetup( $settingsBuilder ); |
| 37 |
self::requireTestsAutoloader(); |
| 38 |
} |
| 39 |
|
| 40 |
public function execute(): bool { |
| 41 |
$this->processedOptions = self::processOptions( $this ); |
| 42 |
|
| 43 |
$testFile = $this->getArg( 0 ); |
| 44 |
if ( $testFile ) { |
| 45 |
$testFilePaths = [ realpath( $testFile ) ]; |
| 46 |
} else { |
| 47 |
$testFilePaths = []; |
| 48 |
$repos = json_decode( file_get_contents( __DIR__ . '/../tests/parserTests.json' ), true ); |
| 49 |
foreach ( $repos as $repo => $repoInfo ) { |
| 50 |
foreach ( $repoInfo["targets"] as $f => $info ) { |
| 51 |
$testFilePaths[] = realpath( __DIR__ . '/../tests/parser/' . $f ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
$globalStats = new Stats(); |
| 57 |
$knownFailuresChanged = false; |
| 58 |
$exitCode = 0; |
| 59 |
if ( $this->processedOptions['integrated'] ?? false ) { |
| 60 |
// Some methods which are discouraged for normal code throw |
| 61 |
// exceptions unless we declare this is just a test. |
| 62 |
define( 'MW_PARSER_TEST', true ); |
| 63 |
|
| 64 |
// See Ifaf53862b96e9127d8f375ad8dd0cc362cba9f5b in gerrit; |
| 65 |
// this should use the \ParserTestRunner::runParsoidTest() |
| 66 |
// method from core. |
| 67 |
throw new \RuntimeException( "Not yet implemented" ); |
| 68 |
} else { |
| 69 |
foreach ( $testFilePaths as $testFile ) { |
| 70 |
$testRunner = new TestRunner( $testFile, 'standalone', $this->processedOptions['modes'] ); |
| 71 |
$result = $testRunner->run( $this->processedOptions ); |
| 72 |
$globalStats->accum( $result['stats'] ); // Sum all stats |
| 73 |
$knownFailuresChanged = $knownFailuresChanged || $result['knownFailuresChanged']; |
| 74 |
$exitCode = $exitCode ?: $result['exitCode']; |
| 75 |
if ( $exitCode !== 0 && $this->processedOptions['exit-unexpected'] ) { |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$this->processedOptions['reportSummary']( |
| 82 |
[], $globalStats, null, null, $knownFailuresChanged, $this->processedOptions |
| 83 |
); |
| 84 |
|
| 85 |
return $exitCode === 0; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Process CLI opts and return |
| 90 |
* |
| 91 |
* @param \Wikimedia\Parsoid\Tools\Maintenance $script |
| 92 |
*/ |
| 93 |
public static function setupOpts( \Wikimedia\Parsoid\Tools\Maintenance $script ): void { |
| 94 |
$opts = ScriptUtils::addStandardOptions( [ |
| 95 |
'wt2html' => [ |
| 96 |
'description' => 'Wikitext -> HTML(DOM)', |
| 97 |
'default' => false, |
| 98 |
'boolean' => true |
| 99 |
], |
| 100 |
'html2wt' => [ |
| 101 |
'description' => 'HTML(DOM) -> Wikitext', |
| 102 |
'default' => false, |
| 103 |
'boolean' => true |
| 104 |
], |
| 105 |
'wt2wt' => [ |
| 106 |
'description' => 'Roundtrip testing: Wikitext -> DOM(HTML) -> Wikitext', |
| 107 |
'default' => false, |
| 108 |
'boolean' => true |
| 109 |
], |
| 110 |
'html2html' => [ |
| 111 |
'description' => 'Roundtrip testing: HTML(DOM) -> Wikitext -> HTML(DOM)', |
| 112 |
'default' => false, |
| 113 |
'boolean' => true |
| 114 |
], |
| 115 |
'selser' => [ |
| 116 |
'description' => 'Roundtrip testing: Wikitext -> DOM(HTML) -> Wikitext (with selective serialization). ' . |
| 117 |
'Set to "noauto" to just run the tests with manual selser changes.', |
| 118 |
'boolean' => false |
| 119 |
], |
| 120 |
'changetree' => [ |
| 121 |
'description' => 'Changes to apply to parsed HTML to generate new HTML to be serialized (useful with selser)', |
| 122 |
'default' => null, |
| 123 |
'boolean' => false |
| 124 |
], |
| 125 |
'numchanges' => [ |
| 126 |
'description' => 'Make multiple different changes to the DOM, run a selser test for each one.', |
| 127 |
'default' => 20, |
| 128 |
'boolean' => false |
| 129 |
], |
| 130 |
'cache' => [ |
| 131 |
'description' => 'Get tests cases from cache file', |
| 132 |
'boolean' => true, |
| 133 |
'default' => false |
| 134 |
], |
| 135 |
'filter' => [ |
| 136 |
'description' => 'Only run tests whose descriptions match given string' |
| 137 |
], |
| 138 |
'regex' => [ |
| 139 |
'description' => 'Only run tests whose descriptions match given regex', |
| 140 |
'alias' => [ 'regexp', 're' ] |
| 141 |
], |
| 142 |
'run-disabled' => [ |
| 143 |
'description' => 'Run disabled tests', |
| 144 |
'default' => false, |
| 145 |
'boolean' => true |
| 146 |
], |
| 147 |
'run-php' => [ |
| 148 |
'description' => 'Run php-only tests', |
| 149 |
'default' => false, |
| 150 |
'boolean' => true |
| 151 |
], |
| 152 |
'maxtests' => [ |
| 153 |
'description' => 'Maximum number of tests to run', |
| 154 |
'boolean' => false |
| 155 |
], |
| 156 |
'quick' => [ |
| 157 |
'description' => 'Suppress diff output of failed tests', |
| 158 |
'boolean' => true, |
| 159 |
'default' => false |
| 160 |
], |
| 161 |
'quiet' => [ |
| 162 |
'description' => 'Suppress notification of passed tests (shows only failed tests)', |
| 163 |
'boolean' => true, |
| 164 |
'default' => false |
| 165 |
], |
| 166 |
'quieter' => [ |
| 167 |
'description' => 'Suppress per-file summary and failed test diffs. ' . |
| 168 |
'Implies --quick and --quiet.', |
| 169 |
'boolean' => true, |
| 170 |
'default' => false |
| 171 |
], |
| 172 |
'offsetType' => [ |
| 173 |
'description' => 'Test DSR offset conversion code while running tests.', |
| 174 |
'boolean' => false, |
| 175 |
'default' => 'byte', |
| 176 |
], |
| 177 |
'knownFailures' => [ |
| 178 |
'description' => 'Compare against known failures', |
| 179 |
'default' => true, |
| 180 |
'boolean' => false |
| 181 |
], |
| 182 |
'updateKnownFailures' => [ |
| 183 |
'description' => 'Update parserTests-knownFailures.json with failing tests.', |
| 184 |
'default' => false, |
| 185 |
'boolean' => true |
| 186 |
], |
| 187 |
'exit-zero' => [ |
| 188 |
'description' => "Don't exit with nonzero status if failures are found.", |
| 189 |
'default' => false, |
| 190 |
'boolean' => true |
| 191 |
], |
| 192 |
'xml' => [ |
| 193 |
'description' => 'Print output in JUnit XML format.', |
| 194 |
'default' => false, |
| 195 |
'boolean' => true |
| 196 |
], |
| 197 |
'exit-unexpected' => [ |
| 198 |
'description' => 'Exit after the first unexpected result.', |
| 199 |
'default' => false, |
| 200 |
'boolean' => true |
| 201 |
], |
| 202 |
'update-tests' => [ |
| 203 |
'description' => 'Update parserTests.txt with results from wt2html fails.', |
| 204 |
'default' => false, |
| 205 |
'boolean' => true |
| 206 |
], |
| 207 |
'update-unexpected' => [ |
| 208 |
'description' => 'Update parserTests.txt with results from wt2html unexpected fails.', |
| 209 |
'default' => false, |
| 210 |
'boolean' => true |
| 211 |
], |
| 212 |
'update-format' => [ |
| 213 |
'description' => 'format with which to update tests; only useful in conjunction ' . |
| 214 |
'with update-tests or update-unexpected. Values: raw, noDsr, actualNormalized.', |
| 215 |
'default' => 'noDsr', |
| 216 |
] |
| 217 |
], [ |
| 218 |
// override defaults for standard options |
| 219 |
'fetchTemplates' => false, |
| 220 |
'usePHPPreProcessor' => false, |
| 221 |
'fetchConfig' => false |
| 222 |
] ); |
| 223 |
|
| 224 |
foreach ( $opts as $opt => $optInfo ) { |
| 225 |
$script->addOption( $opt, |
| 226 |
$optInfo['description'], false, empty( $optInfo['boolean'] ), false ); |
| 227 |
if ( isset( $optInfo['default'] ) ) { |
| 228 |
$script->setOptionDefault( $opt, $optInfo['default'] ); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
public static function processOptions( \Wikimedia\Parsoid\Tools\Maintenance $script ): array { |
| 234 |
$options = $script->optionsToArray(); |
| 235 |
|
| 236 |
if ( $options['help'] ) { |
| 237 |
$script->maybeHelp(); |
| 238 |
print "Additional dump options specific to parserTests script:\n" |
| 239 |
. "* dom:post-changes : Dumps DOM after applying selser changetree\n" |
| 240 |
. "Examples\n" |
| 241 |
. "\$ php parserTests.php --selser --filter '...' --dump dom:post-changes\n" |
| 242 |
. "\$ php parserTests.php --selser --filter '...' --changetree '...' --dump dom:post-changes\n"; |
| 243 |
die( 0 ); |
| 244 |
} |
| 245 |
|
| 246 |
ScriptUtils::setColorFlags( $options ); |
| 247 |
|
| 248 |
if ( !( $options['wt2wt'] || $options['wt2html'] || $options['html2wt'] || $options['html2html'] |
| 249 |
|| isset( $options['selser'] ) ) |
| 250 |
) { |
| 251 |
$options['wt2wt'] = true; |
| 252 |
$options['wt2html'] = true; |
| 253 |
$options['html2html'] = true; |
| 254 |
$options['html2wt'] = true; |
| 255 |
if ( ScriptUtils::booleanOption( $options['updateKnownFailures'] ?? null ) ) { |
| 256 |
// turn on all modes by default for --updateKnownFailures |
| 257 |
$options['selser'] = true; |
| 258 |
// double checking options are valid (T53448 asks to be able to use --filter here) |
| 259 |
if ( isset( $options['filter'] ) || isset( $options['regex'] ) || |
| 260 |
isset( $options['maxtests'] ) || $options['exit-unexpected'] |
| 261 |
) { |
| 262 |
print "\nERROR: can't combine --updateKnownFailures with --filter, --maxtests or --exit-unexpected"; |
| 263 |
die( 1 ); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if ( $options['xml'] ) { |
| 269 |
$options['reportResult'] = [ self::class, 'reportResultXML' ]; |
| 270 |
$options['reportStart'] = [ self::class, 'reportStartXML' ]; |
| 271 |
$options['reportSummary'] = [ self::class, 'reportSummaryXML' ]; |
| 272 |
$options['reportFailure'] = [ self::class, 'reportFailureXML' ]; |
| 273 |
self::$colors_mode = 'none'; |
| 274 |
} |
| 275 |
|
| 276 |
if ( !is_callable( $options['reportFailure'] ?? null ) ) { |
| 277 |
// default failure reporting is standard out, |
| 278 |
// see printFailure for documentation of the default. |
| 279 |
$options['reportFailure'] = [ self::class, 'printFailure' ]; |
| 280 |
} |
| 281 |
|
| 282 |
if ( !is_callable( $options['reportSuccess'] ?? null ) ) { |
| 283 |
// default success reporting is standard out, |
| 284 |
// see printSuccess for documentation of the default. |
| 285 |
$options['reportSuccess'] = [ self::class, 'printSuccess' ]; |
| 286 |
} |
| 287 |
|
| 288 |
if ( !is_callable( $options['reportStart'] ?? null ) ) { |
| 289 |
// default summary reporting is standard out, |
| 290 |
// see reportStart for documentation of the default. |
| 291 |
$options['reportStart'] = [ self::class, 'reportStartOfTests' ]; |
| 292 |
} |
| 293 |
|
| 294 |
if ( !is_callable( $options['reportSummary'] ?? null ) ) { |
| 295 |
// default summary reporting is standard out, |
| 296 |
// see reportSummary for documentation of the default. |
| 297 |
$options['reportSummary'] = [ self::class, 'reportSummary' ]; |
| 298 |
} |
| 299 |
|
| 300 |
if ( !is_callable( $options['reportResult'] ?? null ) ) { |
| 301 |
// default result reporting is standard out, |
| 302 |
// see printResult for documentation of the default. |
| 303 |
$options['reportResult'] = function ( ...$args ) use ( &$options ) { |
| 304 |
return self::printResult( $options['reportFailure'], $options['reportSuccess'], ...$args ); |
| 305 |
}; |
| 306 |
} |
| 307 |
|
| 308 |
if ( !is_callable( $options['getDiff'] ?? null ) ) { |
| 309 |
// this is the default for diff-getting, but it can be overridden |
| 310 |
// see doDiff for documentation of the default. |
| 311 |
$options['getDiff'] = [ self::class, 'doDiff' ]; |
| 312 |
} |
| 313 |
|
| 314 |
if ( !is_callable( $options['getActualExpected'] ?? null ) ) { |
| 315 |
// this is the default for getting the actual and expected |
| 316 |
// outputs, but it can be overridden |
| 317 |
// see getActualExpected for documentation of the default. |
| 318 |
$options['getActualExpected'] = [ self::class, 'getActualExpected' ]; |
| 319 |
} |
| 320 |
|
| 321 |
$options['modes'] = []; |
| 322 |
foreach ( Test::ALL_TEST_MODES as $m ) { |
| 323 |
if ( ( $m !== 'selser' && $options[$m] ) || |
| 324 |
( $m === 'selser' && isset( $options[$m] ) ) |
| 325 |
) { |
| 326 |
$options['modes'][] = $m; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return $options; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Colorize given number if <> 0. |
| 335 |
* |
| 336 |
* @param int $count |
| 337 |
* @param string $color |
| 338 |
* @return string Colorized count |
| 339 |
*/ |
| 340 |
private static function colorizeCount( int $count, string $color ): string { |
| 341 |
$s = (string)$count; |
| 342 |
return TestUtils::colorString( $s, $color ); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* @param array $modesRan |
| 347 |
* @param Stats $stats |
| 348 |
* - failedTests int Number of failed tests due to differences in output. |
| 349 |
* - passedTests int Number of tests passed without any special consideration. |
| 350 |
* - modes array All of the stats (failedTests, passedTests) per-mode. |
| 351 |
* @param ?string $file |
| 352 |
* @param ?array $testFilter |
| 353 |
* @param bool $knownFailuresChanged |
| 354 |
* @param array $options |
| 355 |
*/ |
| 356 |
public static function reportSummary( |
| 357 |
array $modesRan, Stats $stats, ?string $file, ?array $testFilter, |
| 358 |
bool $knownFailuresChanged, array $options |
| 359 |
): void { |
| 360 |
$curStr = null; |
| 361 |
$mode = null; |
| 362 |
$thisMode = null; |
| 363 |
$failTotalTests = $stats->failedTests; |
| 364 |
$happiness = $stats->passedTestsUnexpected === 0 && $stats->failedTestsUnexpected === 0; |
| 365 |
$filename = $file === null ? 'ALL TESTS' : $file; |
| 366 |
|
| 367 |
$quieter = ScriptUtils::booleanOption( $options['quieter'] ?? '' ); |
| 368 |
if ( $quieter && $file !== null ) { |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
if ( !$quieter ) { |
| 373 |
print "==========================================================\n"; |
| 374 |
print 'SUMMARY:' . TestUtils::colorString( $filename, $happiness ? 'green' : 'red' ) . |
| 375 |
"\n"; |
| 376 |
} |
| 377 |
if ( $file !== null ) { |
| 378 |
print 'Execution time: ' . round( 1000 * ( microtime( true ) - $stats->startTime ), 3 ) . "ms\n"; |
| 379 |
} |
| 380 |
|
| 381 |
if ( $failTotalTests !== 0 ) { |
| 382 |
foreach ( $modesRan as $mode ) { |
| 383 |
$curStr = $mode . ': '; |
| 384 |
$thisMode = $stats->modes[$mode]; |
| 385 |
$curStr .= self::colorizeCount( $thisMode->passedTests, 'green' ) . ' passed ('; |
| 386 |
$curStr .= self::colorizeCount( $thisMode->passedTestsUnexpected, 'red' ) . ' unexpected) / '; |
| 387 |
$curStr .= self::colorizeCount( $thisMode->failedTests, 'red' ) . ' failed ('; |
| 388 |
$curStr .= self::colorizeCount( $thisMode->failedTestsUnexpected, 'red' ) . ' unexpected)'; |
| 389 |
print $curStr . "\n"; |
| 390 |
} |
| 391 |
|
| 392 |
$curStr = 'TOTAL' . ': '; |
| 393 |
$curStr .= self::colorizeCount( $stats->passedTests, 'green' ) . ' passed ('; |
| 394 |
$curStr .= self::colorizeCount( $stats->passedTestsUnexpected, 'red' ) . ' unexpected) / '; |
| 395 |
$curStr .= self::colorizeCount( $stats->failedTests, 'red' ) . ' failed ('; |
| 396 |
$curStr .= self::colorizeCount( $stats->failedTestsUnexpected, 'red' ) . ' unexpected)'; |
| 397 |
print $curStr . "\n"; |
| 398 |
|
| 399 |
if ( $file === null ) { |
| 400 |
$buf = self::colorizeCount( $stats->passedTests, 'green' ); |
| 401 |
$buf .= ' total passed tests (expected '; |
| 402 |
$buf .= (string)( $stats->passedTests - $stats->passedTestsUnexpected + $stats->failedTestsUnexpected ); |
| 403 |
$buf .= '), '; |
| 404 |
$buf .= self::colorizeCount( $failTotalTests, 'red' ) . ' total failures (expected '; |
| 405 |
$buf .= (string)( $stats->failedTests - $stats->failedTestsUnexpected + $stats->passedTestsUnexpected ); |
| 406 |
$buf .= ")\n"; |
| 407 |
print $buf; |
| 408 |
} |
| 409 |
} else { |
| 410 |
if ( $testFilter !== null ) { |
| 411 |
$buf = 'Passed ' . $stats->passedTests . ' of ' |
| 412 |
. $stats->passedTests . ' tests matching ' . $testFilter['raw']; |
| 413 |
} else { |
| 414 |
// Should not happen if it does: Champagne! |
| 415 |
$buf = 'Passed ' . $stats->passedTests . ' of ' . $stats->passedTests . ' tests'; |
| 416 |
} |
| 417 |
print $buf . '... ' . TestUtils::colorString( 'ALL TESTS PASSED!', 'green' ) . "\n"; |
| 418 |
} |
| 419 |
|
| 420 |
// If we logged error messages, complain about it. |
| 421 |
$logMsg = TestUtils::colorString( 'No errors logged.', 'green' ); |
| 422 |
if ( $stats->loggedErrorCount > 0 ) { |
| 423 |
$logMsg = TestUtils::colorString( $stats->loggedErrorCount . ' errors logged.', 'red' ); |
| 424 |
} |
| 425 |
if ( $file === null ) { |
| 426 |
if ( $stats->loggedErrorCount > 0 ) { |
| 427 |
$logMsg = TestUtils::colorString( '' . $stats->loggedErrorCount, 'red' ); |
| 428 |
} else { |
| 429 |
$logMsg = TestUtils::colorString( '' . $stats->loggedErrorCount, 'green' ); |
| 430 |
} |
| 431 |
$logMsg .= ' errors logged.'; |
| 432 |
} |
| 433 |
print $logMsg . "\n"; |
| 434 |
|
| 435 |
$failures = $stats->allFailures(); |
| 436 |
|
| 437 |
// If the knownFailures changed, complain about it. |
| 438 |
if ( $knownFailuresChanged ) { |
| 439 |
print TestUtils::colorString( 'Known failures changed!', 'red' ) . "\n"; |
| 440 |
} |
| 441 |
|
| 442 |
if ( $file === null ) { |
| 443 |
if ( $failures === 0 ) { |
| 444 |
print '--> ' . TestUtils::colorString( 'NO UNEXPECTED RESULTS', 'green' ) . " <--\n"; |
| 445 |
if ( $knownFailuresChanged ) { |
| 446 |
print "Perhaps some tests were deleted or renamed.\n"; |
| 447 |
print "Use `php bin/parserTests.php --updateKnownFailures` to update knownFailures list.\n"; |
| 448 |
} |
| 449 |
} else { |
| 450 |
print TestUtils::colorString( '--> ' . $failures . ' UNEXPECTED RESULTS. <--', 'red' ) . "\n"; |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
private static function prettyPrintIOptions( |
| 456 |
?array $iopts = null |
| 457 |
): string { |
| 458 |
if ( !$iopts ) { |
| 459 |
return ''; |
| 460 |
} |
| 461 |
|
| 462 |
$ppValue = null; // Forward declaration |
| 463 |
$ppValue = static function ( $v ) use ( &$ppValue ) { |
| 464 |
if ( is_array( $v ) ) { |
| 465 |
return implode( ',', array_map( $ppValue, $v ) ); |
| 466 |
} |
| 467 |
|
| 468 |
if ( is_string( $v ) && |
| 469 |
( preg_match( '/^\[\[[^\]]*\]\]$/D', $v ) || preg_match( '/^[-\w]+$/D', $v ) ) |
| 470 |
) { |
| 471 |
return $v; |
| 472 |
} |
| 473 |
|
| 474 |
return json_encode( $v ); |
| 475 |
}; |
| 476 |
|
| 477 |
$strPieces = array_map( |
| 478 |
static function ( $k ) use ( $iopts, $ppValue ) { |
| 479 |
if ( $iopts[$k] === '' ) { |
| 480 |
return $k; |
| 481 |
} |
| 482 |
return $k . '=' . $ppValue( $iopts[$k] ); |
| 483 |
}, |
| 484 |
array_keys( $iopts ) |
| 485 |
); |
| 486 |
return implode( ' ', $strPieces ); |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* @param Stats $stats |
| 491 |
* @param Test $item |
| 492 |
* @param array $options |
| 493 |
* @param string $mode |
| 494 |
* @param string $title |
| 495 |
* @param array $actual |
| 496 |
* @param array $expected |
| 497 |
* @param ?string $expectFail If this test was expected to fail (on knownFailures list), then the expected failure output; otherwise null. |
| 498 |
* @return bool true if the failure was expected. |
| 499 |
*/ |
| 500 |
public static function printFailure( |
| 501 |
Stats $stats, Test $item, array $options, string $mode, string $title, |
| 502 |
array $actual, array $expected, ?string $expectFail |
| 503 |
): bool { |
| 504 |
$quiet = ScriptUtils::booleanOption( $options['quiet'] ?? null ); |
| 505 |
$quieter = ScriptUtils::booleanOption( $options['quieter'] ?? null ); |
| 506 |
$quick = ScriptUtils::booleanOption( $options['quick'] ?? null ); |
| 507 |
$failureOnly = $quieter || $quick; |
| 508 |
$extTitle = str_replace( "\n", ' ', "$title ($mode)" ); |
| 509 |
|
| 510 |
$knownFailures = false; |
| 511 |
if ( ScriptUtils::booleanOption( $options['knownFailures'] ?? null ) && $expectFail !== null ) { |
| 512 |
// compare with remembered output |
| 513 |
$offsetType = $options['offsetType'] ?? 'byte'; |
| 514 |
|
| 515 |
if ( |
| 516 |
$offsetType === 'byte' && |
| 517 |
$item->normalizeKnownFailure( $expectFail ) !== |
| 518 |
$item->normalizeKnownFailure( $actual['raw'] ) |
| 519 |
) { |
| 520 |
$knownFailures = true; |
| 521 |
} else { |
| 522 |
if ( !$quiet && !$quieter ) { |
| 523 |
print TestUtils::colorString( 'EXPECTED FAIL', 'red' ) . ': ' . TestUtils::colorString( $extTitle, 'yellow' ) . "\n"; |
| 524 |
} |
| 525 |
return true; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
if ( !$failureOnly ) { |
| 530 |
print "=====================================================\n"; |
| 531 |
} |
| 532 |
|
| 533 |
if ( $knownFailures ) { |
| 534 |
print TestUtils::colorString( 'UNEXPECTED CHANGE TO KNOWN FAILURE OUTPUT', 'red', true ) . ': ' |
| 535 |
. TestUtils::colorString( $extTitle, 'yellow' ) . "\n"; |
| 536 |
print TestUtils::colorString( 'Known failure, but the output changed!', 'red' ) . "\n"; |
| 537 |
} else { |
| 538 |
print TestUtils::colorString( 'UNEXPECTED FAIL', 'red', true ) . ': ' |
| 539 |
. TestUtils::colorString( $extTitle, 'yellow' ) . "\n"; |
| 540 |
} |
| 541 |
|
| 542 |
if ( $mode === 'selser' ) { |
| 543 |
if ( $item->wt2wtPassed ) { |
| 544 |
print TestUtils::colorString( 'Even worse, the non-selser wt2wt test passed!', 'red' ) . "\n"; |
| 545 |
} elseif ( $actual && $item->wt2wtResult !== $actual['raw'] ) { |
| 546 |
print TestUtils::colorString( 'Even worse, the non-selser wt2wt test had a different result!', 'red' ) . "\n"; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
if ( !$failureOnly ) { |
| 551 |
// PORT-FIXME: Removed comments .. maybe need to put it back |
| 552 |
// print implode( "\n", $item->comments ) . "\n"; |
| 553 |
if ( $options ) { |
| 554 |
print TestUtils::colorString( 'OPTIONS', 'cyan' ) . ':' . "\n"; |
| 555 |
print self::prettyPrintIOptions( $item->options ) . "\n"; |
| 556 |
} |
| 557 |
print TestUtils::colorString( 'INPUT', 'cyan' ) . ':' . "\n"; |
| 558 |
print $actual['input'] . "\n"; |
| 559 |
if ( $knownFailures ) { |
| 560 |
print "\n" . TestUtils::colorString( 'KNOWN FAILURE OUTPUT', 'cyan' ) . ":\n"; |
| 561 |
print $expectFail . "\n"; |
| 562 |
} |
| 563 |
print $options['getActualExpected']( $actual, $expected, $options['getDiff'] ) . "\n"; |
| 564 |
} |
| 565 |
|
| 566 |
return false; |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* @param Stats $stats |
| 571 |
* @param Test $item |
| 572 |
* @param array $options |
| 573 |
* @param string $mode |
| 574 |
* @param string $title |
| 575 |
* @param string $raw |
| 576 |
* @param bool $expectSuccess Whether this success was expected (or was it a known failure). |
| 577 |
* @return bool true if the success was expected. |
| 578 |
*/ |
| 579 |
public static function printSuccess( |
| 580 |
Stats $stats, Test $item, array $options, string $mode, string $title, string $raw, bool $expectSuccess |
| 581 |
): bool { |
| 582 |
$quiet = ScriptUtils::booleanOption( $options['quiet'] ?? null ); |
| 583 |
$quieter = ScriptUtils::booleanOption( $options['quieter'] ?? null ); |
| 584 |
$quick = ScriptUtils::booleanOption( $options['quick'] ?? null ); |
| 585 |
$failureOnly = $quieter || $quick; |
| 586 |
|
| 587 |
$extTitle = str_replace( "\n", ' ', "$title ($mode)" ); |
| 588 |
|
| 589 |
if ( ScriptUtils::booleanOption( $options['knownFailures'] ?? null ) && !$expectSuccess ) { |
| 590 |
if ( !$failureOnly ) { |
| 591 |
print "=====================================================\n"; |
| 592 |
} |
| 593 |
print TestUtils::colorString( 'UNEXPECTED PASS', 'green', true ) . ': ' . |
| 594 |
TestUtils::colorString( $extTitle, 'yellow' ) . "\n"; |
| 595 |
if ( !$failureOnly ) { |
| 596 |
print TestUtils::colorString( 'RAW RENDERED', 'cyan' ) . ":\n"; |
| 597 |
print $raw . "\n"; |
| 598 |
} |
| 599 |
return false; |
| 600 |
} |
| 601 |
if ( !$quiet && !$quieter ) { |
| 602 |
$outStr = 'EXPECTED PASS'; |
| 603 |
|
| 604 |
$outStr = TestUtils::colorString( $outStr, 'green' ) . ': ' |
| 605 |
. TestUtils::colorString( $extTitle, 'yellow' ); |
| 606 |
|
| 607 |
print $outStr . "\n"; |
| 608 |
|
| 609 |
if ( $mode === 'selser' && isset( $item->wt2wtPassed ) && !$item->wt2wtPassed ) { |
| 610 |
print TestUtils::colorString( 'Even better, the non-selser wt2wt test failed!', 'red' ) . "\n"; |
| 611 |
} |
| 612 |
} |
| 613 |
return true; |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Print the actual and expected outputs. |
| 618 |
* |
| 619 |
* @param array $actual |
| 620 |
* - string raw |
| 621 |
* - string normal |
| 622 |
* @param array $expected |
| 623 |
* - string raw |
| 624 |
* - string normal |
| 625 |
* @param callable $getDiff Returns a string showing the diff(s) for the test. |
| 626 |
* - array actual |
| 627 |
* - array expected |
| 628 |
* @return string |
| 629 |
*/ |
| 630 |
public static function getActualExpected( array $actual, array $expected, callable $getDiff ): string { |
| 631 |
if ( self::$colors_mode === 'none' ) { |
| 632 |
$mkVisible = static function ( $s ) { |
| 633 |
return $s; |
| 634 |
}; |
| 635 |
} else { |
| 636 |
$mkVisible = static function ( $s ) { |
| 637 |
return preg_replace( '/\xA0/', TestUtils::colorString( "␣", "white" ), |
| 638 |
preg_replace( '/\n/', TestUtils::colorString( "↵\n", "white" ), $s ) ); |
| 639 |
}; |
| 640 |
} |
| 641 |
|
| 642 |
$returnStr = ''; |
| 643 |
$returnStr .= TestUtils::colorString( 'RAW EXPECTED', 'cyan' ) . ":\n"; |
| 644 |
$returnStr .= $expected['raw'] . "\n"; |
| 645 |
|
| 646 |
$returnStr .= TestUtils::colorString( 'RAW RENDERED', 'cyan' ) . ":\n"; |
| 647 |
$returnStr .= $actual['raw'] . "\n"; |
| 648 |
|
| 649 |
$returnStr .= TestUtils::colorString( 'NORMALIZED EXPECTED', 'magenta' ) . ":\n"; |
| 650 |
$returnStr .= $mkVisible( $expected['normal'] ) . "\n"; |
| 651 |
|
| 652 |
$returnStr .= TestUtils::colorString( 'NORMALIZED RENDERED', 'magenta' ) . ":\n"; |
| 653 |
$returnStr .= $mkVisible( $actual['normal'] ) . "\n"; |
| 654 |
|
| 655 |
$returnStr .= TestUtils::colorString( 'DIFF', 'cyan' ) . ":\n"; |
| 656 |
$returnStr .= $getDiff( $actual, $expected ); |
| 657 |
|
| 658 |
return $returnStr; |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* @param array $actual |
| 663 |
* - string normal |
| 664 |
* @param array $expected |
| 665 |
* - string normal |
| 666 |
* @return string Colorized diff |
| 667 |
*/ |
| 668 |
public static function doDiff( array $actual, array $expected ): string { |
| 669 |
// safe to always request color diff, because we set color mode='none' |
| 670 |
// if colors are turned off. |
| 671 |
$e = preg_replace( '/\xA0/', "␣", $expected['normal'] ); |
| 672 |
$a = preg_replace( '/\xA0/', "␣", $actual['normal'] ); |
| 673 |
// PORT_FIXME: |
| 674 |
if ( !self::$differ ) { |
| 675 |
self::$differ = new Differ(); |
| 676 |
} |
| 677 |
|
| 678 |
$diffs = self::$differ->diff( $e, $a ); |
| 679 |
$diffs = preg_replace_callback( '/^(-.*)/m', static function ( $m ) { |
| 680 |
return TestUtils::colorString( $m[0], 'green' ); |
| 681 |
}, $diffs ); |
| 682 |
$diffs = preg_replace_callback( '/^(\+.*)/m', static function ( $m ) { |
| 683 |
return TestUtils::colorString( $m[0], 'red' ); |
| 684 |
}, $diffs ); |
| 685 |
|
| 686 |
return $diffs; |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* @param callable $reportFailure |
| 691 |
* @param callable $reportSuccess |
| 692 |
* @param Stats $stats |
| 693 |
* @param Test $item |
| 694 |
* @param array $options |
| 695 |
* @param string $mode |
| 696 |
* @param array $expected |
| 697 |
* @param array $actual |
| 698 |
* @param ?callable $pre |
| 699 |
* @param ?callable $post |
| 700 |
* @return bool True if the result was as expected. |
| 701 |
*/ |
| 702 |
public static function printResult( |
| 703 |
callable $reportFailure, callable $reportSuccess, |
| 704 |
Stats $stats, Test $item, array $options, string $mode, |
| 705 |
array $expected, array $actual, ?callable $pre = null, ?callable $post = null |
| 706 |
): bool { |
| 707 |
$title = $item->testName; // Title may be modified here, so pass it on. |
| 708 |
$changeTree = $item->changetree; |
| 709 |
|
| 710 |
$suffix = ''; |
| 711 |
if ( $mode === 'selser' ) { |
| 712 |
$suffix = ' ' . |
| 713 |
( $changeTree === [ 'manual' ] ? '[manual]' : json_encode( $changeTree ) ); |
| 714 |
} elseif ( $mode === 'wt2html' && isset( $item->options['langconv'] ) ) { |
| 715 |
$title .= ' [langconv]'; |
| 716 |
} |
| 717 |
$title .= $suffix; |
| 718 |
|
| 719 |
$expectFail = $item->knownFailures[$mode . $suffix] ?? null; |
| 720 |
$fail = $expected['normal'] !== $actual['normal']; |
| 721 |
// Return whether the test was as expected, independent of pass/fail |
| 722 |
$asExpected = null; |
| 723 |
|
| 724 |
if ( $mode === 'wt2wt' ) { |
| 725 |
$item->wt2wtPassed = !$fail; |
| 726 |
$item->wt2wtResult = $actual['raw']; |
| 727 |
} |
| 728 |
|
| 729 |
// don't report selser fails when nothing was changed or it's a dup |
| 730 |
if ( |
| 731 |
$mode === 'selser' && $changeTree !== [ 'manual' ] && |
| 732 |
( $changeTree === [] || $item->duplicateChange ) |
| 733 |
) { |
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
if ( is_callable( $pre ) ) { |
| 738 |
$pre( $stats, $mode, $title, $item->time ); |
| 739 |
} |
| 740 |
|
| 741 |
if ( $fail ) { |
| 742 |
$stats->failedTests++; |
| 743 |
$stats->modes[$mode]->failedTests++; |
| 744 |
$asExpected = $reportFailure( $stats, $item, $options, $mode, $title, $actual, $expected, $expectFail ); |
| 745 |
if ( !$asExpected ) { |
| 746 |
$stats->failedTestsUnexpected++; |
| 747 |
$stats->modes[$mode]->failedTestsUnexpected++; |
| 748 |
} |
| 749 |
$stats->modes[$mode]->failList[] = [ |
| 750 |
'testName' => $item->testName, |
| 751 |
'suffix' => $suffix, |
| 752 |
'raw' => $actual['raw'] ?? null, |
| 753 |
'expected' => $expected['raw'] ?? null, |
| 754 |
'actualNormalized' => $actual['normal'] ?? null, |
| 755 |
'unexpected' => !$asExpected, |
| 756 |
]; |
| 757 |
} else { |
| 758 |
$stats->passedTests++; |
| 759 |
$stats->modes[$mode]->passedTests++; |
| 760 |
$asExpected = $reportSuccess( $stats, $item, $options, $mode, $title, $actual['raw'], $expectFail === null ); |
| 761 |
if ( !$asExpected ) { |
| 762 |
$stats->passedTestsUnexpected++; |
| 763 |
$stats->modes[$mode]->passedTestsUnexpected++; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
if ( is_callable( $post ) ) { |
| 768 |
$post( $stats, $mode ); |
| 769 |
} |
| 770 |
|
| 771 |
return $asExpected; |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* Simple function for reporting the start of the tests. |
| 776 |
* |
| 777 |
* This method can be reimplemented in the options of the ParserTests object. |
| 778 |
*/ |
| 779 |
public static function reportStartOfTests() { |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
* Get the actual and expected outputs encoded for XML output. |
| 784 |
* |
| 785 |
* @inheritDoc getActualExpected |
| 786 |
* |
| 787 |
* @return string $The XML representation of the actual and expected outputs. |
| 788 |
*/ |
| 789 |
public static function getActualExpectedXML( array $actual, array $expected, callable $getDiff ) { |
| 790 |
$returnStr = ''; |
| 791 |
|
| 792 |
$returnStr .= "RAW EXPECTED:\n"; |
| 793 |
$returnStr .= self::encodeXml( $expected['raw'] ) . "\n\n"; |
| 794 |
|
| 795 |
$returnStr .= "RAW RENDERED:\n"; |
| 796 |
$returnStr .= self::encodeXml( $actual['raw'] ) . "\n\n"; |
| 797 |
|
| 798 |
$returnStr .= "NORMALIZED EXPECTED:\n"; |
| 799 |
$returnStr .= self::encodeXml( $expected['normal'] ) . "\n\n"; |
| 800 |
|
| 801 |
$returnStr .= "NORMALIZED RENDERED:\n"; |
| 802 |
$returnStr .= self::encodeXml( $actual['normal'] ) . "\n\n"; |
| 803 |
|
| 804 |
$returnStr .= "DIFF:\n"; |
| 805 |
$returnStr .= self::encodeXml( $getDiff( $actual, $expected, false ) ); |
| 806 |
|
| 807 |
return $returnStr; |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Report the start of the tests output. |
| 812 |
* |
| 813 |
* @inheritDoc reportStart |
| 814 |
*/ |
| 815 |
public static function reportStartXML(): void { |
| 816 |
} |
| 817 |
|
| 818 |
/** |
| 819 |
* Report the end of the tests output. |
| 820 |
* |
| 821 |
* @inheritDoc reportSummary |
| 822 |
*/ |
| 823 |
public static function reportSummaryXML( |
| 824 |
array $modesRan, Stats $stats, ?string $file, ?array $testFilter, |
| 825 |
bool $knownFailuresChanged, array $options |
| 826 |
): void { |
| 827 |
if ( $file === null ) { |
| 828 |
/* Summary for all tests; not included in XML format output. */ |
| 829 |
return; |
| 830 |
} |
| 831 |
print '<testsuites file="' . $file . '">'; |
| 832 |
foreach ( $modesRan as $mode ) { |
| 833 |
print '<testsuite name="parserTests-' . $mode . '">'; |
| 834 |
print $stats->modes[$mode]->result; |
| 835 |
print '</testsuite>'; |
| 836 |
} |
| 837 |
print '</testsuites>'; |
| 838 |
} |
| 839 |
|
| 840 |
/** |
| 841 |
* Print a failure message for a test in XML. |
| 842 |
* |
| 843 |
* @inheritDoc printFailure |
| 844 |
*/ |
| 845 |
public static function reportFailureXML( |
| 846 |
Stats $stats, Test $item, array $options, string $mode, string $title, |
| 847 |
array $actual, array $expected, ?string $expectFail, bool $failureOnly |
| 848 |
): bool { |
| 849 |
$failEle = ''; |
| 850 |
$knownFailures = false; |
| 851 |
if ( ScriptUtils::booleanOption( $options['knownFailures'] ) && $expectFail !== null ) { |
| 852 |
// compare with remembered output |
| 853 |
$knownFailures = $expectFail === $actual['raw']; |
| 854 |
} |
| 855 |
if ( !$knownFailures ) { |
| 856 |
$failEle .= "<failure type=\"parserTestsDifferenceInOutputFailure\">\n"; |
| 857 |
$failEle .= self::getActualExpectedXML( $actual, $expected, $options['getDiff'] ); |
| 858 |
$failEle .= "\n</failure>"; |
| 859 |
$stats->modes[$mode]->result .= $failEle; |
| 860 |
return false; |
| 861 |
} |
| 862 |
return true; |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
* Print a success method for a test in XML. |
| 867 |
* |
| 868 |
* @inheritDoc printSuccess |
| 869 |
*/ |
| 870 |
public static function reportSuccessXML( |
| 871 |
Stats $stats, Test $item, array $options, string $mode, string $title, string $raw, |
| 872 |
bool $expectSuccess |
| 873 |
): bool { |
| 874 |
return ScriptUtils::booleanOption( $options['knownFailures'] ?? null ) && !$expectSuccess; |
| 875 |
} |
| 876 |
|
| 877 |
private static function pre( |
| 878 |
Stats $stats, string $mode, string $title, array $time |
| 879 |
): void { |
| 880 |
$testcaseEle = '<testcase name="' . self::encodeXml( $title ) . '" '; |
| 881 |
$testcaseEle .= 'assertions="1" '; |
| 882 |
|
| 883 |
$timeTotal = null; |
| 884 |
if ( $time && $time['end'] && $time['start'] ) { |
| 885 |
$timeTotal = $time['end'] - $time['start']; |
| 886 |
if ( !is_nan( $timeTotal ) ) { |
| 887 |
$testcaseEle .= 'time="' . ( ( $time['end'] - $time['start'] ) / 1000.0 ) . '"'; |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
$testcaseEle .= '>'; |
| 892 |
$stats->modes[$mode]->result .= $testcaseEle; |
| 893 |
} |
| 894 |
|
| 895 |
private static function post( Stats $stats, string $mode ): void { |
| 896 |
$stats->modes[$mode]->result .= '</testcase>'; |
| 897 |
} |
| 898 |
|
| 899 |
/** |
| 900 |
* Print the result of a test in XML. |
| 901 |
* |
| 902 |
* @inheritDoc printResult |
| 903 |
*/ |
| 904 |
public static function reportResultXML( ...$args ) { |
| 905 |
$args[] = [ self::class, 'pre' ]; |
| 906 |
$args[] = [ self::class, 'post' ]; |
| 907 |
self::printResult( |
| 908 |
[ self::class, 'reportFailureXML' ], |
| 909 |
[ self::class, 'reportSuccessXML' ], |
| 910 |
...$args |
| 911 |
); |
| 912 |
|
| 913 |
// In xml, test all cases always |
| 914 |
return true; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
$maintClass = ParserTests::class; |
| 919 |
require_once PARSOID_RUN_MAINTENANCE_IF_MAIN; |