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

Added some additional assertion methods.

Summary:
There are quite a few tests in Arcanist, libphutil and Phabricator that do something similar to `$this->assertEqual(false, ...)` or `$this->assertEqual(true, ...)`.

This is unnecessarily verbose and it would be cleaner if we had `assertFalse` and `assertTrue` methods.

Test Plan: I contemplated adding a unit test for the `getCallerInfo` method but wasn't sure if it was required / where it should live.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley

CC: Korvin, epriestley, aran

Differential Revision: https://secure.phabricator.com/D8460
This commit is contained in:
Joshua Spence 2014-03-08 18:23:14 -08:00 committed by epriestley
parent cf52d88f8b
commit c42bef4e25

View file

@ -25,6 +25,65 @@ abstract class ArcanistPhutilTestCase {
/* -( Making Test Assertions )--------------------------------------------- */ /* -( Making Test Assertions )--------------------------------------------- */
/**
* Assert that a value is false. The test fails if it is not.
*
* @param wild The empirically derived value, generated by executing the
* test.
* @param string A human-readable description of what these values represent,
* and particularly of what a discrepancy means.
*
* @return void
* @task assert
*/
final protected function assertFalse($result, $message = null) {
if ($result === false) {
$this->assertions++;
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);
}
/**
* Assert that a value is true. The test fails if it is not.
*
* @param wild The empirically derived value, generated by executing the
* test.
* @param string A human-readable description of what these values represent,
* and particularly of what a discrepancy means.
*
* @return void
* @task assert
*/
final protected function assertTrue($result, $message = null) {
if ($result === true) {
$this->assertions++;
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);
}
/** /**
* Assert that two values are equal. The test fails if they are not. * Assert that two values are equal. The test fails if they are not.
@ -51,18 +110,9 @@ abstract class ArcanistPhutilTestCase {
$expect = PhutilReadableSerializer::printableValue($expect); $expect = PhutilReadableSerializer::printableValue($expect);
$result = PhutilReadableSerializer::printableValue($result); $result = PhutilReadableSerializer::printableValue($result);
$caller = self::getCallerInfo();
foreach (debug_backtrace() as $location) { $output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
if (!preg_match('/^assert[A-Z]/', idx($location, 'function'))) {
break;
}
$where = $location;
}
$line = idx($where, 'line');
$file = basename(idx($where, 'file'));
$output = "Assertion failed at line {$line} in {$file}";
if ($message) { if ($message) {
$output .= ": {$message}"; $output .= ": {$message}";
@ -582,4 +632,37 @@ abstract class ArcanistPhutilTestCase {
return $this; return $this;
} }
/**
* Returns info about the caller function.
*
* @return map
*/
private static final function getCallerInfo() {
$callee = array();
$caller = array();
$seen = false;
foreach (array_slice(debug_backtrace(), 1) as $location) {
$function = idx($location, 'function');
if (!$seen && preg_match('/^assert[A-Z]/', $function)) {
$seen = true;
$caller = $location;
} else if ($seen && !preg_match('/^assert[A-Z]/', $function)) {
$callee = $location;
break;
}
}
return array(
'file' => basename(idx($caller, 'file')),
'line' => idx($caller, 'line'),
'function' => idx($callee, 'function'),
'class' => idx($callee, 'class'),
'object' => idx($caller, 'object'),
'type' => idx($callee, 'type'),
'args' => idx($caller, 'args'),
);
}
} }