feat: adds toHaveLineCountLessThan

This commit is contained in:
Nuno Maduro
2024-07-18 20:43:39 +01:00
parent 135c8a0d46
commit 2e411893d2
5 changed files with 39 additions and 2 deletions

View File

@ -447,6 +447,19 @@ final class Expectation
); );
} }
/**
* Asserts that the given expectation target to have line count less than the given number.
*/
public function toHaveLineCountLessThan(int $lines): ArchExpectation
{
return Targeted::make(
$this,
fn (ObjectDescription $object): bool => count(file($object->path)) < $lines,
sprintf('to have less than %d lines of code', $lines),
FileLineFinder::where(fn (string $line): bool => str_contains($line, '<?php')),
);
}
/** /**
* Asserts that the given expectation target use the "declare(strict_types=1)" declaration. * Asserts that the given expectation target use the "declare(strict_types=1)" declaration.
*/ */

View File

@ -88,6 +88,14 @@ final class OppositeExpectation
); );
} }
/**
* Not supported.
*/
public function toHaveLineCountLessThan(): ArchExpectation
{
throw InvalidExpectation::fromMethods(['not', 'toHaveLineCountLessThan']);
}
/** /**
* Asserts that the given expectation target does not use the "declare(strict_types=1)" declaration. * Asserts that the given expectation target does not use the "declare(strict_types=1)" declaration.
*/ */

View File

@ -797,6 +797,10 @@
✓ it fails ✓ it fails
✓ it fails with message ✓ it fails with message
PASS Tests\Features\Expect\toHaveLineCountLessThan
✓ it passes
✓ it fails
PASS Tests\Features\Expect\toHaveMethod PASS Tests\Features\Expect\toHaveMethod
✓ class has method ✓ class has method
✓ opposite class has method ✓ opposite class has method
@ -1521,4 +1525,4 @@
WARN Tests\Visual\Version WARN Tests\Visual\Version
- visual snapshot of help command output - visual snapshot of help command output
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1066 passed (2611 assertions) Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1068 passed (2614 assertions)

View File

@ -0,0 +1,12 @@
<?php
use Pest\Arch\Exceptions\ArchExpectationFailedException;
use Pest\Expectation;
it('passes', function () {
expect(Expectation::class)->toHaveLineCountLessThan(1000);
});
it('fails', function () {
expect(Expectation::class)->toHaveLineCountLessThan(10);
})->throws(ArchExpectationFailedException::class);

View File

@ -16,7 +16,7 @@ $run = function () {
test('parallel', function () use ($run) { test('parallel', function () use ($run) {
expect($run('--exclude-group=integration')) expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1052 passed (2579 assertions)') ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1054 passed (2582 assertions)')
->toContain('Parallel: 3 processes'); ->toContain('Parallel: 3 processes');
})->skipOnWindows(); })->skipOnWindows();