Index: trunk/phase3/tests/testHelpers.inc |
— | — | @@ -355,6 +355,7 @@ |
356 | 356 | private $parserTest; /* An instance of ParserTest (parserTests.php) or MediaWikiParserTest (phpunit) */ |
357 | 357 | private $index = 0; |
358 | 358 | private $test; |
| 359 | + private $section = null; /** String|null: current test section being analyzed */ |
359 | 360 | private $lineNum; |
360 | 361 | private $eof; |
361 | 362 | |
— | — | @@ -410,36 +411,29 @@ |
411 | 412 | |
412 | 413 | function readNextTest() { |
413 | 414 | $data = array(); |
414 | | - $section = null; |
| 415 | + $this->section = null; |
415 | 416 | |
416 | 417 | while ( false !== ( $line = fgets( $this->fh ) ) ) { |
417 | 418 | $this->lineNum++; |
418 | 419 | $matches = array(); |
419 | 420 | |
420 | 421 | if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) { |
421 | | - $section = strtolower( $matches[1] ); |
| 422 | + $this->section = strtolower( $matches[1] ); |
422 | 423 | |
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' ); |
427 | 427 | |
428 | | - if ( !isset( $data['article'] ) ) { |
429 | | - throw new MWException( "'endarticle' without 'article' at line {$this->lineNum} of $this->file\n" ); |
430 | | - } |
431 | | - |
432 | 428 | $this->parserTest->addArticle( ParserTest::chomp( $data['article'] ), $data['text'], $this->lineNum ); |
433 | 429 | |
434 | 430 | $data = array(); |
435 | | - $section = null; |
| 431 | + $this->section = null; |
436 | 432 | |
437 | 433 | continue; |
438 | 434 | } |
439 | 435 | |
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' ); |
444 | 438 | |
445 | 439 | foreach ( explode( "\n", $data['hooks'] ) as $line ) { |
446 | 440 | $line = trim( $line ); |
— | — | @@ -452,15 +446,13 @@ |
453 | 447 | } |
454 | 448 | |
455 | 449 | $data = array(); |
456 | | - $section = null; |
| 450 | + $this->section = null; |
457 | 451 | |
458 | 452 | continue; |
459 | 453 | } |
460 | 454 | |
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' ); |
465 | 457 | |
466 | 458 | foreach ( explode( "\n", $data['functionhooks'] ) as $line ) { |
467 | 459 | $line = trim( $line ); |
— | — | @@ -473,24 +465,16 @@ |
474 | 466 | } |
475 | 467 | |
476 | 468 | $data = array(); |
477 | | - $section = null; |
| 469 | + $this->section = null; |
478 | 470 | |
479 | 471 | continue; |
480 | 472 | } |
481 | 473 | |
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' ); |
486 | 478 | |
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 | | - |
495 | 479 | if ( !isset( $data['options'] ) ) { |
496 | 480 | $data['options'] = ''; |
497 | 481 | } |
— | — | @@ -502,7 +486,7 @@ |
503 | 487 | || !preg_match( "/" . $this->parserTest->regex . "/i", $data['test'] ) ) ) { |
504 | 488 | # disabled test |
505 | 489 | $data = array(); |
506 | | - $section = null; |
| 490 | + $this->section = null; |
507 | 491 | |
508 | 492 | continue; |
509 | 493 | } |
— | — | @@ -517,20 +501,46 @@ |
518 | 502 | return true; |
519 | 503 | } |
520 | 504 | |
521 | | - if ( isset ( $data[$section] ) ) { |
| 505 | + if ( isset ( $data[$this->section] ) ) { |
522 | 506 | throw new MWException( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" ); |
523 | 507 | } |
524 | 508 | |
525 | | - $data[$section] = ''; |
| 509 | + $data[$this->section] = ''; |
526 | 510 | |
527 | 511 | continue; |
528 | 512 | } |
529 | 513 | |
530 | | - if ( $section ) { |
531 | | - $data[$section] .= $line; |
| 514 | + if ( $this->section ) { |
| 515 | + $data[$this->section] .= $line; |
532 | 516 | } |
533 | 517 | } |
534 | 518 | |
535 | 519 | return false; |
536 | 520 | } |
| 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 | + } |
537 | 547 | } |