1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-26 00:32:41 +01:00

Make assertTrue() / assertFalse() messages more descriptive

Summary: See discussion in D8460. Primarily, this prints out the failing value when a true/false assertion fails, so if it was something useful (like a function result) it's visible.

Test Plan:
Added `assertTrue("quack")`:

     FAIL  ArcanistDiffParserTestCase::testParser
  Assertion failed, expected 'true' (at ArcanistDiffParserTestCase.php:16).

  ACTUAL VALUE
  quack

Added `assertFalse("quack")`:

     FAIL  ArcanistDiffParserTestCase::testParser
  Assertion failed, expected 'false' (at ArcanistDiffParserTestCase.php:16).

  ACTUAL VALUE
  quack

Added `assertEqual("quack", "moo")`:

     FAIL  ArcanistDiffParserTestCase::testParser
  Assertion failed, expected values to be equal (at ArcanistDiffParserTestCase.php:16).
  Expected: quack
    Actual: moo

Reviewers: joshuaspence

Reviewed By: joshuaspence

CC: aran

Differential Revision: https://secure.phabricator.com/D8465
This commit is contained in:
epriestley 2014-03-08 19:23:23 -08:00
parent 06cfe0746e
commit a90a72c648

View file

@ -7,8 +7,6 @@
* @task exceptions Exception Handling
* @task hook Hooks for Setup and Teardown
* @task internal Internals
*
* @group unitrun
*/
abstract class ArcanistPhutilTestCase {
@ -25,8 +23,9 @@ abstract class ArcanistPhutilTestCase {
/* -( Making Test Assertions )--------------------------------------------- */
/**
* Assert that a value is false. The test fails if it is not.
* Assert that a value is `false`, strictly. The test fails if it is not.
*
* @param wild The empirically derived value, generated by executing the
* test.
@ -42,21 +41,12 @@ abstract class ArcanistPhutilTestCase {
return;
}
$result = PhutilReadableSerializer::printableValue($result);
$caller = self::getCallerInfo();
$output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
if ($message) {
$output .= ": {$message}";
}
$this->failTest($output);
throw new ArcanistPhutilTestTerminatedException($output);
$this->failAssertionWithExpectedValue('false', $result, $message);
}
/**
* Assert that a value is true. The test fails if it is not.
* Assert that a value is `true`, strictly. The test fails if it is not.
*
* @param wild The empirically derived value, generated by executing the
* test.
@ -72,23 +62,14 @@ abstract class ArcanistPhutilTestCase {
return;
}
$result = PhutilReadableSerializer::printableValue($result);
$caller = self::getCallerInfo();
$output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
if ($message) {
$output .= ": {$message}";
}
$this->failTest($output);
throw new ArcanistPhutilTestTerminatedException($output);
$this->failAssertionWithExpectedValue('true', $result, $message);
}
/**
* Assert that two values are equal. The test fails if they are not.
* Assert that two values are equal, strictly. The test fails if they are not.
*
* NOTE: This method uses PHP's strict equality test operator ("===") to
* NOTE: This method uses PHP's strict equality test operator (`===`) to
* compare values. This means values and types must be equal, key order must
* be identical in arrays, and objects must be referentially identical.
*
@ -111,11 +92,20 @@ abstract class ArcanistPhutilTestCase {
$expect = PhutilReadableSerializer::printableValue($expect);
$result = PhutilReadableSerializer::printableValue($result);
$caller = self::getCallerInfo();
$file = $caller['file'];
$line = $caller['line'];
$output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
if ($message) {
$output .= ": {$message}";
if ($message !== null) {
$output = pht(
"Assertion failed, expected values to be equal (at %s:%d): %s",
$file,
$line,
$message);
} else {
$output = pht(
"Assertion failed, expected values to be equal (at %s:%d).",
$file,
$line);
}
$output .= "\n";
@ -665,4 +655,50 @@ abstract class ArcanistPhutilTestCase {
);
}
/**
* Fail an assertion which checks that some result is equal to a specific
* value, like 'true' or 'false'. This prints a readable error message and
* fails the current test.
*
* This method throws and does not return.
*
* @param string Human readable description of the expected value.
* @param string The actual value.
* @param string|null Optional assertion message.
* @return void
* @task internal
*/
private function failAssertionWithExpectedValue(
$expect_description,
$actual_result,
$message) {
$caller = self::getCallerInfo();
$file = $caller['file'];
$line = $caller['line'];
if ($message !== null) {
$description = pht(
"Assertion failed, expected '%s' (at %s:%d): %s",
$expect_description,
$file,
$line,
$message);
} else {
$description = pht(
"Assertion failed, expected '%s' (at %s:%d).",
$expect_description,
$file,
$line);
}
$actual_result = PhutilReadableSerializer::printableValue($actual_result);
$header = pht('ACTUAL VALUE');
$output = $description."\n\n".$header."\n".$actual_result;
$this->failTest($output);
throw new ArcanistPhutilTestTerminatedException($output);
}
}