diff --git a/src/unit/engine/phutil/ArcanistPhutilTestCase.php b/src/unit/engine/phutil/ArcanistPhutilTestCase.php index 419c9b4c..4f1e7c3d 100644 --- a/src/unit/engine/phutil/ArcanistPhutilTestCase.php +++ b/src/unit/engine/phutil/ArcanistPhutilTestCase.php @@ -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); + } + }