* @task exceptions Exception Handling
* @task hook Hooks for Setup and Teardown
* @task internal Internals
- *
- * @group unitrun
*/
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.
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.
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.
*
$expect = PhutilReadableSerializer::printableValue($expect);
$result = PhutilReadableSerializer::printableValue($result);
$caller = self::getCallerInfo();
-
- $output = "Assertion failed at line {$caller['line']} in {$caller['file']}";
-
- if ($message) {
- $output .= ": {$message}";
+ $file = $caller['file'];
+ $line = $caller['line'];
+
+ 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";
);
}
+
+ /**
+ * 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);
+ }
+
}