mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-22 23:02: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:
parent
06cfe0746e
commit
a90a72c648
1 changed files with 68 additions and 32 deletions
|
@ -7,8 +7,6 @@
|
||||||
* @task exceptions Exception Handling
|
* @task exceptions Exception Handling
|
||||||
* @task hook Hooks for Setup and Teardown
|
* @task hook Hooks for Setup and Teardown
|
||||||
* @task internal Internals
|
* @task internal Internals
|
||||||
*
|
|
||||||
* @group unitrun
|
|
||||||
*/
|
*/
|
||||||
abstract class ArcanistPhutilTestCase {
|
abstract class ArcanistPhutilTestCase {
|
||||||
|
|
||||||
|
@ -25,8 +23,9 @@ abstract class ArcanistPhutilTestCase {
|
||||||
|
|
||||||
/* -( Making Test Assertions )--------------------------------------------- */
|
/* -( 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
|
* @param wild The empirically derived value, generated by executing the
|
||||||
* test.
|
* test.
|
||||||
|
@ -42,21 +41,12 @@ abstract class ArcanistPhutilTestCase {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = PhutilReadableSerializer::printableValue($result);
|
$this->failAssertionWithExpectedValue('false', $result, $message);
|
||||||
$caller = self::getCallerInfo();
|
|
||||||
|
|
||||||
$output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
|
|
||||||
|
|
||||||
if ($message) {
|
|
||||||
$output .= ": {$message}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->failTest($output);
|
|
||||||
throw new ArcanistPhutilTestTerminatedException($output);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* @param wild The empirically derived value, generated by executing the
|
||||||
* test.
|
* test.
|
||||||
|
@ -72,23 +62,14 @@ abstract class ArcanistPhutilTestCase {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = PhutilReadableSerializer::printableValue($result);
|
$this->failAssertionWithExpectedValue('true', $result, $message);
|
||||||
$caller = self::getCallerInfo();
|
|
||||||
|
|
||||||
$output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
|
|
||||||
|
|
||||||
if ($message) {
|
|
||||||
$output .= ": {$message}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->failTest($output);
|
|
||||||
throw new ArcanistPhutilTestTerminatedException($output);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* compare values. This means values and types must be equal, key order must
|
||||||
* be identical in arrays, and objects must be referentially identical.
|
* be identical in arrays, and objects must be referentially identical.
|
||||||
*
|
*
|
||||||
|
@ -111,11 +92,20 @@ abstract class ArcanistPhutilTestCase {
|
||||||
$expect = PhutilReadableSerializer::printableValue($expect);
|
$expect = PhutilReadableSerializer::printableValue($expect);
|
||||||
$result = PhutilReadableSerializer::printableValue($result);
|
$result = PhutilReadableSerializer::printableValue($result);
|
||||||
$caller = self::getCallerInfo();
|
$caller = self::getCallerInfo();
|
||||||
|
$file = $caller['file'];
|
||||||
|
$line = $caller['line'];
|
||||||
|
|
||||||
$output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
|
if ($message !== null) {
|
||||||
|
$output = pht(
|
||||||
if ($message) {
|
"Assertion failed, expected values to be equal (at %s:%d): %s",
|
||||||
$output .= ": {$message}";
|
$file,
|
||||||
|
$line,
|
||||||
|
$message);
|
||||||
|
} else {
|
||||||
|
$output = pht(
|
||||||
|
"Assertion failed, expected values to be equal (at %s:%d).",
|
||||||
|
$file,
|
||||||
|
$line);
|
||||||
}
|
}
|
||||||
|
|
||||||
$output .= "\n";
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue