Changeset View
Changeset View
Standalone View
Standalone View
src/unit/engine/phutil/ArcanistPhutilTestCase.php
| Show First 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | if ($message !== null) { | ||||
| 'Assertion failed, expected values to be equal (at %s:%d).', | 'Assertion failed, expected values to be equal (at %s:%d).', | ||||
| $file, | $file, | ||||
| $line); | $line); | ||||
| } | } | ||||
| $output .= "\n"; | $output .= "\n"; | ||||
| if (strpos($expect, "\n") === false && strpos($result, "\n") === false) { | if (strpos($expect, "\n") === false && strpos($result, "\n") === false) { | ||||
| $output .= "Expected: {$expect}\n"; | $output .= pht("Expected: %s\n Actual: %s", $expect, $result); | ||||
| $output .= " Actual: {$result}"; | |||||
| } else { | } else { | ||||
| $output .= "Expected vs Actual Output Diff\n"; | $output .= pht( | ||||
| $output .= ArcanistDiffUtils::renderDifferences( | "Expected vs Actual Output Diff\n%s", | ||||
| ArcanistDiffUtils::renderDifferences( | |||||
| $expect, | $expect, | ||||
| $result, | $result, | ||||
| $lines = 0xFFFF); | $lines = 0xFFFF)); | ||||
| } | } | ||||
| $this->failTest($output); | $this->failTest($output); | ||||
| throw new ArcanistPhutilTestTerminatedException($output); | throw new ArcanistPhutilTestTerminatedException($output); | ||||
| } | } | ||||
| /** | /** | ||||
| ▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | /* -( Exception Handling )------------------------------------------------- */ | ||||
| final protected function tryTestCases( | final protected function tryTestCases( | ||||
| array $inputs, | array $inputs, | ||||
| array $expect, | array $expect, | ||||
| $callable, | $callable, | ||||
| $exception_class = 'Exception') { | $exception_class = 'Exception') { | ||||
| if (count($inputs) !== count($expect)) { | if (count($inputs) !== count($expect)) { | ||||
| $this->assertFailure( | $this->assertFailure( | ||||
| 'Input and expectations must have the same number of values.'); | pht('Input and expectations must have the same number of values.')); | ||||
| } | } | ||||
| $labels = array_keys($inputs); | $labels = array_keys($inputs); | ||||
| $inputs = array_values($inputs); | $inputs = array_values($inputs); | ||||
| $expecting = array_values($expect); | $expecting = array_values($expect); | ||||
| foreach ($inputs as $idx => $input) { | foreach ($inputs as $idx => $input) { | ||||
| $expect = $expecting[$idx]; | $expect = $expecting[$idx]; | ||||
| $label = $labels[$idx]; | $label = $labels[$idx]; | ||||
| Show All 10 Lines | foreach ($inputs as $idx => $input) { | ||||
| } | } | ||||
| $caught = $ex; | $caught = $ex; | ||||
| } | } | ||||
| $actual = !($caught instanceof Exception); | $actual = !($caught instanceof Exception); | ||||
| if ($expect === $actual) { | if ($expect === $actual) { | ||||
| if ($expect) { | if ($expect) { | ||||
| $message = "Test case '{$label}' did not throw, as expected."; | $message = pht("Test case '%s' did not throw, as expected.", $label); | ||||
| } else { | } else { | ||||
| $message = "Test case '{$label}' threw, as expected."; | $message = pht("Test case '%s' threw, as expected.", $label); | ||||
| } | } | ||||
| } else { | } else { | ||||
| if ($expect) { | if ($expect) { | ||||
| $message = "Test case '{$label}' was expected to succeed, but it ". | $message = pht( | ||||
| "raised an exception of class ".get_class($ex)." with ". | "Test case '%s' was expected to succeed, but it ". | ||||
| "message: ".$ex->getMessage(); | "raised an exception of class %s with message: %s", | ||||
| $label, | |||||
| get_class($ex), | |||||
| $ex->getMessage()); | |||||
| } else { | } else { | ||||
| $message = "Test case '{$label}' was expected to raise an ". | $message = pht( | ||||
| "exception, but it did not throw anything."; | "Test case '%s' was expected to raise an ". | ||||
| "exception, but it did not throw anything.", | |||||
| $label); | |||||
| } | } | ||||
| } | } | ||||
| $this->assertEqual($expect, $actual, $message); | $this->assertEqual($expect, $actual, $message); | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 227 Lines • ▼ Show 20 Lines | foreach ($methods as $method) { | ||||
| $exceptions['Shutdown'] = $ex; | $exceptions['Shutdown'] = $ex; | ||||
| } | } | ||||
| if ($exceptions) { | if ($exceptions) { | ||||
| if (count($exceptions) == 1) { | if (count($exceptions) == 1) { | ||||
| throw head($exceptions); | throw head($exceptions); | ||||
| } else { | } else { | ||||
| throw new PhutilAggregateException( | throw new PhutilAggregateException( | ||||
| 'Multiple exceptions were raised during test execution.', | pht('Multiple exceptions were raised during test execution.'), | ||||
| $exceptions); | $exceptions); | ||||
| } | } | ||||
| } | } | ||||
| if (!$this->assertions) { | if (!$this->assertions) { | ||||
| $this->failTest( | $this->failTest( | ||||
| pht( | pht( | ||||
| 'This test case made no assertions. Test cases must make at '. | 'This test case made no assertions. Test cases must make at '. | ||||
| 'least one assertion.')); | 'least one assertion.')); | ||||
| } | } | ||||
| } catch (ArcanistPhutilTestTerminatedException $ex) { | } catch (ArcanistPhutilTestTerminatedException $ex) { | ||||
| // Continue with the next test. | // Continue with the next test. | ||||
| } catch (ArcanistPhutilTestSkippedException $ex) { | } catch (ArcanistPhutilTestSkippedException $ex) { | ||||
| // Continue with the next test. | // Continue with the next test. | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| $ex_class = get_class($ex); | $ex_class = get_class($ex); | ||||
| $ex_message = $ex->getMessage(); | $ex_message = $ex->getMessage(); | ||||
| $ex_trace = $ex->getTraceAsString(); | $ex_trace = $ex->getTraceAsString(); | ||||
| $message = "EXCEPTION ({$ex_class}): {$ex_message}\n{$ex_trace}"; | $message = sprintf( | ||||
| "%s (%s): %s\n%s", | |||||
| pht('EXCEPTION'), | |||||
| $ex_class, | |||||
| $ex_message, | |||||
| $ex_trace); | |||||
| $this->failTest($message); | $this->failTest($message); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| $this->didRunTests(); | $this->didRunTests(); | ||||
| return $this->results; | return $this->results; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | final private function endCoverage() { | ||||
| } | } | ||||
| return $coverage; | return $coverage; | ||||
| } | } | ||||
| final private function assertCoverageAvailable() { | final private function assertCoverageAvailable() { | ||||
| if (!function_exists('xdebug_start_code_coverage')) { | if (!function_exists('xdebug_start_code_coverage')) { | ||||
| throw new Exception( | throw new Exception( | ||||
| "You've enabled code coverage but XDebug is not installed."); | pht("You've enabled code coverage but XDebug is not installed.")); | ||||
| } | } | ||||
| } | } | ||||
| final public function setProjectRoot($project_root) { | final public function setProjectRoot($project_root) { | ||||
| $this->projectRoot = $project_root; | $this->projectRoot = $project_root; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 94 Lines • Show Last 20 Lines | |||||