r104549 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104548‎ | r104549 | r104550 >
Date:11:27, 29 November 2011
Author:hashar
Status:ok
Tags:
Comment:
abstract out section sanity check

$section is now a private propery.
The new function avoid code duplication.
Modified paths:
  • /trunk/phase3/tests/testHelpers.inc (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/testHelpers.inc
@@ -355,6 +355,7 @@
356356 private $parserTest; /* An instance of ParserTest (parserTests.php) or MediaWikiParserTest (phpunit) */
357357 private $index = 0;
358358 private $test;
 359+ private $section = null; /** String|null: current test section being analyzed */
359360 private $lineNum;
360361 private $eof;
361362
@@ -410,36 +411,29 @@
411412
412413 function readNextTest() {
413414 $data = array();
414 - $section = null;
 415+ $this->section = null;
415416
416417 while ( false !== ( $line = fgets( $this->fh ) ) ) {
417418 $this->lineNum++;
418419 $matches = array();
419420
420421 if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
421 - $section = strtolower( $matches[1] );
 422+ $this->section = strtolower( $matches[1] );
422423
423 - if ( $section == 'endarticle' ) {
424 - if ( !isset( $data['text'] ) ) {
425 - throw new MWException( "'endarticle' without 'text' at line {$this->lineNum} of $this->file\n" );
426 - }
 424+ if ( $this->section == 'endarticle' ) {
 425+ $this->checkSection( $data, 'text' );
 426+ $this->checkSection( $data, 'article' );
427427
428 - if ( !isset( $data['article'] ) ) {
429 - throw new MWException( "'endarticle' without 'article' at line {$this->lineNum} of $this->file\n" );
430 - }
431 -
432428 $this->parserTest->addArticle( ParserTest::chomp( $data['article'] ), $data['text'], $this->lineNum );
433429
434430 $data = array();
435 - $section = null;
 431+ $this->section = null;
436432
437433 continue;
438434 }
439435
440 - if ( $section == 'endhooks' ) {
441 - if ( !isset( $data['hooks'] ) ) {
442 - throw new MWException( "'endhooks' without 'hooks' at line {$this->lineNum} of $this->file\n" );
443 - }
 436+ if ( $this->section == 'endhooks' ) {
 437+ $this->checkSection( $data, 'hooks' );
444438
445439 foreach ( explode( "\n", $data['hooks'] ) as $line ) {
446440 $line = trim( $line );
@@ -452,15 +446,13 @@
453447 }
454448
455449 $data = array();
456 - $section = null;
 450+ $this->section = null;
457451
458452 continue;
459453 }
460454
461 - if ( $section == 'endfunctionhooks' ) {
462 - if ( !isset( $data['functionhooks'] ) ) {
463 - throw new MWException( "'endfunctionhooks' without 'functionhooks' at line {$this->lineNum} of $this->file\n" );
464 - }
 455+ if ( $this->section == 'endfunctionhooks' ) {
 456+ $this->checkSection( $data, 'functionhooks' );
465457
466458 foreach ( explode( "\n", $data['functionhooks'] ) as $line ) {
467459 $line = trim( $line );
@@ -473,24 +465,16 @@
474466 }
475467
476468 $data = array();
477 - $section = null;
 469+ $this->section = null;
478470
479471 continue;
480472 }
481473
482 - if ( $section == 'end' ) {
483 - if ( !isset( $data['test'] ) ) {
484 - throw new MWException( "'end' without 'test' at line {$this->lineNum} of $this->file\n" );
485 - }
 474+ if ( $this->section == 'end' ) {
 475+ $this->checkSection( $data, 'test' );
 476+ $this->checkSection( $data, 'input' );
 477+ $this->checkSection( $data, 'result' );
486478
487 - if ( !isset( $data['input'] ) ) {
488 - throw new MWException( "'end' without 'input' at line {$this->lineNum} of $this->file\n" );
489 - }
490 -
491 - if ( !isset( $data['result'] ) ) {
492 - throw new MWException( "'end' without 'result' at line {$this->lineNum} of $this->file\n" );
493 - }
494 -
495479 if ( !isset( $data['options'] ) ) {
496480 $data['options'] = '';
497481 }
@@ -502,7 +486,7 @@
503487 || !preg_match( "/" . $this->parserTest->regex . "/i", $data['test'] ) ) ) {
504488 # disabled test
505489 $data = array();
506 - $section = null;
 490+ $this->section = null;
507491
508492 continue;
509493 }
@@ -517,20 +501,46 @@
518502 return true;
519503 }
520504
521 - if ( isset ( $data[$section] ) ) {
 505+ if ( isset ( $data[$this->section] ) ) {
522506 throw new MWException( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" );
523507 }
524508
525 - $data[$section] = '';
 509+ $data[$this->section] = '';
526510
527511 continue;
528512 }
529513
530 - if ( $section ) {
531 - $data[$section] .= $line;
 514+ if ( $this->section ) {
 515+ $data[$this->section] .= $line;
532516 }
533517 }
534518
535519 return false;
536520 }
 521+
 522+ /**
 523+ * Verify the first parameter array ($data) has a value for the second
 524+ * parameter key name ($token).
 525+ * Throw an exception if it is not set, referencing current section
 526+ * and adding the current file name and line number
 527+ *
 528+ * @param $data Array: an array of parser test data. See readNextTest()
 529+ * @param $token String: expected token that should have been mentionned before closing this section
 530+ */
 531+ private function checkSection( $data, $token ) {
 532+ if( is_null( $this->section ) ) {
 533+ throw new MWException( __METHOD__ . " could not verify a null section!\n" );
 534+ }
 535+
 536+ if( !isset($data[$token]) ) {
 537+ throw new MWException( sprintf(
 538+ "'%s' without '%s' at line %s of %s\n",
 539+ $this->section,
 540+ $token,
 541+ $this->lineNum,
 542+ $this->file
 543+ ));
 544+ }
 545+ return true;
 546+ }
537547 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r104551abstract out $data and name it 'sectionData'...hashar11:45, 29 November 2011

Status & tagging log