From 3927177b23d61cedd69eefd402a56090c7e8dfe8 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Tue, 5 Sep 2023 20:36:18 -0300 Subject: [PATCH 1/2] finishing the code --- overrides/Runner/TestSuiteLoader.php | 2 +- src/Expectation.php | 2 +- src/Factories/Annotations/Depends.php | 2 +- src/Factories/TestCaseFactory.php | 4 ++-- src/Mixins/Expectation.php | 2 +- src/PendingCalls/TestCall.php | 5 +++-- src/Support/Str.php | 10 +++++++++- tests/Features/Describe.php | 20 ++++++++++++++++++++ 8 files changed, 38 insertions(+), 9 deletions(-) diff --git a/overrides/Runner/TestSuiteLoader.php b/overrides/Runner/TestSuiteLoader.php index 9c1a0935..cc36904c 100644 --- a/overrides/Runner/TestSuiteLoader.php +++ b/overrides/Runner/TestSuiteLoader.php @@ -132,7 +132,7 @@ final class TestSuiteLoader continue; } - if ($class->isAbstract() || ($class->getFileName() !== $suiteClassFile)) { + if ($class->isAbstract() || ($suiteClassFile !== $class->getFileName())) { if (! str_contains($class->getFileName(), 'TestCaseFactory.php')) { continue; } diff --git a/src/Expectation.php b/src/Expectation.php index ef3c3950..979dde28 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -239,7 +239,7 @@ final class Expectation $index = isset($callbacks[$index + 1]) ? $index + 1 : 0; } - if (count($callbacks) > $valuesCount) { + if ($valuesCount < count($callbacks)) { throw new OutOfRangeException('Sequence expectations are more than the iterable items.'); } diff --git a/src/Factories/Annotations/Depends.php b/src/Factories/Annotations/Depends.php index 24fb03cc..2da826ea 100644 --- a/src/Factories/Annotations/Depends.php +++ b/src/Factories/Annotations/Depends.php @@ -19,7 +19,7 @@ final class Depends implements AddsAnnotations public function __invoke(TestCaseMethodFactory $method, array $annotations): array { foreach ($method->depends as $depend) { - $depend = Str::evaluable($depend); + $depend = Str::evaluable($method->describing !== null ? Str::describe($method->describing, $depend) : $depend); $annotations[] = "@depends $depend"; } diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index cbe50985..f6f518c1 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -241,7 +241,7 @@ final class TestCaseFactory throw ShouldNotHappen::fromMessage('The test description may not be empty.'); } - if (Str::evaluable($method->description) === $methodName) { + if ($methodName === Str::evaluable($method->description)) { return true; } } @@ -259,7 +259,7 @@ final class TestCaseFactory throw ShouldNotHappen::fromMessage('The test description may not be empty.'); } - if (Str::evaluable($method->description) === $methodName) { + if ($methodName === Str::evaluable($method->description)) { return $method; } } diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 2617fea4..400095f9 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -950,7 +950,7 @@ final class Expectation } if (! class_exists($exception)) { - if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") { + if ($e instanceof Error && "Class \"$exception\" not found" === $e->getMessage()) { Assert::assertTrue(true); throw $e; diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 9b1edabc..6ade188b 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -16,6 +16,7 @@ use Pest\Support\Backtrace; use Pest\Support\Exporter; use Pest\Support\HigherOrderCallables; use Pest\Support\NullClosure; +use Pest\Support\Str; use Pest\TestSuite; use PHPUnit\Framework\TestCase; @@ -209,7 +210,7 @@ final class TestCall */ private function skipOn(string $osFamily, string $message): self { - return PHP_OS_FAMILY === $osFamily + return $osFamily === PHP_OS_FAMILY ? $this->skip($message) : $this; } @@ -361,7 +362,7 @@ final class TestCall { if (! is_null($this->describing)) { $this->testCaseMethod->describing = $this->describing; - $this->testCaseMethod->description = sprintf('`%s` → %s', $this->describing, $this->testCaseMethod->description); + $this->testCaseMethod->description = Str::describe($this->describing, $this->testCaseMethod->description); // @phpstan-ignore-line } $this->testSuite->tests->set($this->testCaseMethod); diff --git a/src/Support/Str.php b/src/Support/Str.php index ee4e7231..35b109c3 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -51,7 +51,7 @@ final class Str return true; } - return substr($target, -$length) === $search; + return $search === substr($target, -$length); } /** @@ -92,4 +92,12 @@ final class Str { return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0]; } + + /** + * Creates a describe block as `$right` → `$left` format. + */ + public static function describe(string $right, string $left): string + { + return sprintf('`%s` → %s', $right, $left); + } } diff --git a/tests/Features/Describe.php b/tests/Features/Describe.php index 6173a4dd..64bf3453 100644 --- a/tests/Features/Describe.php +++ b/tests/Features/Describe.php @@ -76,3 +76,23 @@ describe('with on describe', function () { expect($foo)->toBe(3); }); })->with([3]); + +describe('depends on describe', function () { + test('foo', function () { + expect('foo')->toBe('foo'); + }); + + test('bar', function () { + expect('bar')->toBe('bar'); + })->depends('foo'); +}); + +describe('depends on describe using with', function () { + test('foo', function ($foo) { + expect($foo)->toBe(3); + }); + + test('bar', function ($foo) { + expect($foo + $foo)->toBe(6); + })->depends('foo'); +})->with([3]); From ae0a23004650ac3c7598f354064a0fd8125ac39d Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 6 Sep 2023 11:48:53 +0100 Subject: [PATCH 2/2] chore: improves readability --- src/Support/Str.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Support/Str.php b/src/Support/Str.php index 35b109c3..c0c5a51b 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -94,10 +94,10 @@ final class Str } /** - * Creates a describe block as `$right` → `$left` format. + * Creates a describe block as `$describeDescription` → `$testDescription` format. */ - public static function describe(string $right, string $left): string + public static function describe(string $describeDescription, string $testDescription): string { - return sprintf('`%s` → %s', $right, $left); + return sprintf('`%s` → %s', $describeDescription, $testDescription); } }