Changeset View
Changeset View
Standalone View
Standalone View
src/unit/engine/__tests__/PhutilUnitTestEngineTestCase.php
| Show All 12 Lines | final class PhutilUnitTestEngineTestCase extends ArcanistTestCase { | ||||
| protected function willRunTests() { | protected function willRunTests() { | ||||
| self::$allTestsCounter++; | self::$allTestsCounter++; | ||||
| } | } | ||||
| protected function didRunTests() { | protected function didRunTests() { | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| 1, | 1, | ||||
| self::$allTestsCounter, | self::$allTestsCounter, | ||||
| 'Expect willRunTests() has been called once.'); | pht( | ||||
| 'Expect %s has been called once.', | |||||
| 'willRunTests()')); | |||||
| self::$allTestsCounter--; | self::$allTestsCounter--; | ||||
| $actual_test_count = 4; | $actual_test_count = 4; | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $actual_test_count, | $actual_test_count, | ||||
| count(self::$distinctWillRunTests), | count(self::$distinctWillRunTests), | ||||
| 'Expect willRunOneTest() was called once for each test.'); | pht( | ||||
| 'Expect %s was called once for each test.', | |||||
| 'willRunOneTest()')); | |||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $actual_test_count, | $actual_test_count, | ||||
| count(self::$distinctDidRunTests), | count(self::$distinctDidRunTests), | ||||
| 'Expect didRunOneTest() was called once for each test.'); | pht( | ||||
| 'Expect %s was called once for each test.', | |||||
| 'didRunOneTest()')); | |||||
| $this->assertEqual( | $this->assertEqual( | ||||
| self::$distinctWillRunTests, | self::$distinctWillRunTests, | ||||
| self::$distinctDidRunTests, | self::$distinctDidRunTests, | ||||
| 'Expect same tests had pre- and post-run callbacks invoked.'); | pht('Expect same tests had pre-run and post-run callbacks invoked.')); | ||||
| } | } | ||||
| public function __destruct() { | public function __destruct() { | ||||
| if (self::$allTestsCounter !== 0) { | if (self::$allTestsCounter !== 0) { | ||||
| throw new Exception( | throw new Exception( | ||||
| 'didRunTests() was not called correctly after tests completed!'); | pht( | ||||
| '%s was not called correctly after tests completed!', | |||||
| 'didRunTests()')); | |||||
| } | } | ||||
| } | } | ||||
| protected function willRunOneTest($test) { | protected function willRunOneTest($test) { | ||||
| self::$distinctWillRunTests[$test] = true; | self::$distinctWillRunTests[$test] = true; | ||||
| self::$oneTestCounter++; | self::$oneTestCounter++; | ||||
| } | } | ||||
| protected function didRunOneTest($test) { | protected function didRunOneTest($test) { | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| 1, | 1, | ||||
| self::$oneTestCounter, | self::$oneTestCounter, | ||||
| 'Expect willRunOneTest depth to be one.'); | pht('Expect %s depth to be one.', 'willRunOneTest()')); | ||||
| self::$distinctDidRunTests[$test] = true; | self::$distinctDidRunTests[$test] = true; | ||||
| self::$oneTestCounter--; | self::$oneTestCounter--; | ||||
| } | } | ||||
| public function testPass() { | public function testPass() { | ||||
| $this->assertEqual(1, 1, 'This test is expected to pass.'); | $this->assertEqual(1, 1, pht('This test is expected to pass.')); | ||||
| } | } | ||||
| public function testFailSkip() { | public function testFailSkip() { | ||||
| $failed = 0; | $failed = 0; | ||||
| $skipped = 0; | $skipped = 0; | ||||
| $test_case = new ArcanistPhutilTestCaseTestCase(); | $test_case = new ArcanistPhutilTestCaseTestCase(); | ||||
| foreach ($test_case->run() as $result) { | foreach ($test_case->run() as $result) { | ||||
| if ($result->getResult() == ArcanistUnitTestResult::RESULT_FAIL) { | if ($result->getResult() == ArcanistUnitTestResult::RESULT_FAIL) { | ||||
| $failed++; | $failed++; | ||||
| } else if ($result->getResult() == ArcanistUnitTestResult::RESULT_SKIP) { | } else if ($result->getResult() == ArcanistUnitTestResult::RESULT_SKIP) { | ||||
| $skipped++; | $skipped++; | ||||
| } else { | } else { | ||||
| $this->assertFailure('These tests should either fail or skip.'); | $this->assertFailure(pht('These tests should either fail or skip.')); | ||||
| } | } | ||||
| } | } | ||||
| $this->assertEqual(1, $failed, 'One test was expected to fail.'); | $this->assertEqual(1, $failed, pht('One test was expected to fail.')); | ||||
| $this->assertEqual(1, $skipped, 'One test was expected to skip.'); | $this->assertEqual(1, $skipped, pht('One test was expected to skip.')); | ||||
| } | } | ||||
| public function testTryTestCases() { | public function testTryTestCases() { | ||||
| $this->tryTestCases( | $this->tryTestCases( | ||||
| array( | array( | ||||
| true, | true, | ||||
| false, | false, | ||||
| ), | ), | ||||
| Show All 10 Lines | $this->tryTestCaseMap( | ||||
| 1 => true, | 1 => true, | ||||
| 0 => false, | 0 => false, | ||||
| ), | ), | ||||
| array($this, 'throwIfFalsey')); | array($this, 'throwIfFalsey')); | ||||
| } | } | ||||
| protected function throwIfFalsey($input) { | protected function throwIfFalsey($input) { | ||||
| if (!$input) { | if (!$input) { | ||||
| throw new Exception('This is a negative test case!'); | throw new Exception(pht('This is a negative test case!')); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||