test: abstract parser test result
authorAntoine Musso <hashar@free.fr>
Tue, 19 Mar 2013 14:00:44 +0000 (15:00 +0100)
committerAntoine Musso <hashar@free.fr>
Mon, 8 Apr 2013 09:15:28 +0000 (11:15 +0200)
This patch introduce the new ParserTestResult class which is meant to
represent the result of a parser test.  I have refactored some methods
to take advantage of this new class.

It just hold the test description and the actual/expected parser output.
A short isSuccess() method is provided for convenience, we can later
improve the class to carry more methods.

Change-Id: Ifb86e09451875dc119633b52d3f7e4f47c67cc60

tests/TestsAutoLoader.php
tests/parser/ParserTestResult.php [new file with mode: 0644]
tests/parser/parserTest.inc

index 264ba69..363d0a2 100644 (file)
@@ -30,6 +30,7 @@ $wgAutoloadClasses += array(
        'DbTestPreviewer' => "$testDir/testHelpers.inc",
        'DbTestRecorder' => "$testDir/testHelpers.inc",
        'DelayedParserTest' => "$testDir/testHelpers.inc",
+       'ParserTestResult' => "$testDir/parser/ParserTestResult.php",
        'TestFileIterator' => "$testDir/testHelpers.inc",
        'TestRecorder' => "$testDir/testHelpers.inc",
 
diff --git a/tests/parser/ParserTestResult.php b/tests/parser/ParserTestResult.php
new file mode 100644 (file)
index 0000000..e846da5
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+/**
+ * @copyright Copyright © 2013, Antoine Musso
+ * @copyright Copyright © 2013, Wikimedia Foundation Inc.
+ * @license GNU GPL v2
+ *
+ * @file
+ */
+
+/**
+ * Represent the result of a parser test.
+ *
+ * @since 1.22
+ */
+class ParserTestResult {
+       /**
+        * Description of the parser test.
+        *
+        * This is usually the text used to describe a parser test in the .txt
+        * files.  It is initialized on a construction and you most probably
+        * never want to change it.
+        */
+       public $description;
+       /** Text that was expected */
+       public $expected;
+       /** Actual text rendered */
+       public $actual;
+
+       /**
+        * @param $description string A short text describing the parser test
+        *        usually the text in the parser test .txt file.  The description
+        *        is later available using the property $description.
+        */
+       public function __construct( $description ) {
+               $this->description = $description;
+       }
+
+       /** Whether the test passed */
+       public function isSuccess() {
+               return ($this->expected === $this->actual);
+       }
+}
index ce621f4..526a6d6 100644 (file)
@@ -518,18 +518,23 @@ class ParserTest {
                }
 
                $this->teardownGlobals();
-               return $this->showTestResult( $desc, $result, $out );
+
+               $testResult = new ParserTestResult( $desc );
+               $testResult->expected = $result;
+               $testResult->actual = $out;
+
+               return $this->showTestResult( $testResult );
        }
 
        /**
-        *
+        * Refactored in 1.22 to use ParserTestResult
         */
-       function showTestResult( $desc, $result, $out ) {
-               if ( $result === $out ) {
-                       $this->showSuccess( $desc );
+       function showTestResult( ParserTestResult $testResult ) {
+               if ( $testResult->isSuccess() ) {
+                       $this->showSuccess( $testResult );
                        return true;
                } else {
-                       $this->showFailure( $desc, $result, $out );
+                       $this->showFailure( $testResult );
                        return false;
                }
        }
@@ -1070,10 +1075,12 @@ class ParserTest {
        /**
         * Print a happy success message.
         *
-        * @param $desc String: the test name
+        * Refactored in 1.22 to use ParserTestResult
+        *
+        * @param $testResult ParserTestResult
         * @return Boolean
         */
-       protected function showSuccess( $desc ) {
+       protected function showSuccess( ParserTestResult $testResult ) {
                if ( $this->showProgress ) {
                        print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
                }
@@ -1085,28 +1092,29 @@ class ParserTest {
         * Print a failure message and provide some explanatory output
         * about what went wrong if so configured.
         *
-        * @param $desc String: the test name
-        * @param $result String: expected HTML output
-        * @param $html String: actual HTML output
+        * Refactored in 1.22 to use ParserTestResult
+        *
+        * @param $testResult ParserTestResult
         * @return Boolean
         */
-       protected function showFailure( $desc, $result, $html ) {
+       protected function showFailure( ParserTestResult $testResult ) {
                if ( $this->showFailure ) {
                        if ( !$this->showProgress ) {
                                # In quiet mode we didn't show the 'Testing' message before the
                                # test, in case it succeeded. Show it now:
-                               $this->showTesting( $desc );
+                               $this->showTesting( $testResult->description );
                        }
 
                        print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
 
                        if ( $this->showOutput ) {
-                               print "--- Expected ---\n$result\n--- Actual ---\n$html\n";
+                               print "--- Expected ---\n{$testResult->expected}\n";
+                               print "--- Actual ---\n{$testResult->actual}\n";
                        }
 
                        if ( $this->showDiffs ) {
-                               print $this->quickDiff( $result, $html );
-                               if ( !$this->wellFormed( $html ) ) {
+                               print $this->quickDiff( $testResult->expected, $testResult->actual );
+                               if ( !$this->wellFormed( $testResult->actual ) ) {
                                        print "XML error: $this->mXmlError\n";
                                }
                        }