More tests

This commit is contained in:
Nuno Maduro
2023-05-27 14:08:38 +01:00
parent a90b90ad29
commit 26a6e7d712
12 changed files with 101 additions and 15 deletions

View File

@ -347,7 +347,7 @@ final class TestCall
{ {
if (! is_null($this->describing)) { if (! is_null($this->describing)) {
$this->testCaseMethod->describing = $this->describing; $this->testCaseMethod->describing = $this->describing;
$this->testCaseMethod->description = sprintf('`%s` > %s', $this->describing, $this->testCaseMethod->description); $this->testCaseMethod->description = sprintf('`%s` %s', $this->describing, $this->testCaseMethod->description);
} }
$this->testSuite->tests->set($this->testCaseMethod); $this->testSuite->tests->set($this->testCaseMethod);

View File

@ -78,9 +78,7 @@ final class Backtrace
*/ */
public static function file(): string public static function file(): string
{ {
$trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; $trace = self::backtrace();
assert(array_key_exists(self::FILE, $trace));
return $trace[self::FILE]; return $trace[self::FILE];
} }
@ -90,9 +88,7 @@ final class Backtrace
*/ */
public static function dirname(): string public static function dirname(): string
{ {
$trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; $trace = self::backtrace();
assert(array_key_exists(self::FILE, $trace));
return dirname($trace[self::FILE]); return dirname($trace[self::FILE]);
} }
@ -102,8 +98,30 @@ final class Backtrace
*/ */
public static function line(): int public static function line(): int
{ {
$trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; $trace = self::backtrace();
return $trace['line'] ?? 0; return $trace['line'] ?? 0;
} }
/**
* @return array{function: string, line?: int, file: string, class?: class-string, type?: string, args?: mixed[], object?: object}
*/
private static function backtrace(): array
{
$backtrace = debug_backtrace(self::BACKTRACE_OPTIONS);
foreach ($backtrace as $trace) {
if (! isset($trace['file'])) {
continue;
}
if (str_contains($trace['file'], 'pest/src')) {
continue;
}
return $trace;
}
throw ShouldNotHappen::fromMessage('Backtrace not found.');
}
} }

View File

@ -9,10 +9,10 @@
TODO Tests\Features\Describe - 5 todos TODO Tests\Features\Describe - 5 todos
↓ todo ↓ todo
↓ todo on hook > should not fail ↓ todo on hook should not fail
↓ todo on hook > should run ↓ todo on hook should run
↓ todo on describe > should not fail ↓ todo on describe should not fail
↓ todo on describe > should run ↓ todo on describe should run
TODO Tests\Features\Todo - 3 todos TODO Tests\Features\Todo - 3 todos
↓ something todo later ↓ something todo later

View File

@ -7,7 +7,11 @@ beforeEach(function () use ($state) {
}); });
afterEach(function () { afterEach(function () {
$this->state->bar = 2; $this->state->bar = 1;
});
afterEach(function () {
unset($this->state->bar);
}); });
it('does not get executed before the test', function () { it('does not get executed before the test', function () {
@ -18,3 +22,7 @@ it('gets executed after the test', function () {
expect($this->state)->toHaveProperty('bar'); expect($this->state)->toHaveProperty('bar');
expect($this->state->bar)->toBe(2); expect($this->state->bar)->toBe(2);
}); });
afterEach(function () {
$this->state->bar = 2;
});

View File

@ -4,12 +4,24 @@ beforeEach(function () {
$this->bar = 2; $this->bar = 2;
}); });
beforeEach(function () {
$this->bar++;
});
beforeEach(function () {
$this->bar = 0;
});
it('gets executed before each test', function () { it('gets executed before each test', function () {
expect($this->bar)->toBe(2); expect($this->bar)->toBe(1);
$this->bar = 'changed'; $this->bar = 'changed';
}); });
it('gets executed before each test once again', function () { it('gets executed before each test once again', function () {
expect($this->bar)->toBe(2); expect($this->bar)->toBe(1);
});
beforeEach(function () {
$this->bar++;
}); });

View File

@ -68,3 +68,11 @@ it('throws exception if no class nor method has been found', function () {
$testCall->covers('fakeName'); $testCall->covers('fakeName');
})->throws(InvalidArgumentException::class, 'No class or method named "fakeName" has been found.'); })->throws(InvalidArgumentException::class, 'No class or method named "fakeName" has been found.');
describe('a "describe" group of tests', function () {
it('does not append CoversNothing to method attributes', function () {
$phpDoc = (new ReflectionClass($this))->getMethod($this->name());
expect(str_contains($phpDoc->getDocComment(), '* @coversNothing'))->toBeTrue();
});
})->coversNothing();

View File

@ -29,3 +29,9 @@ trait Gettable
get('foo'); // not incomplete because closure is created... get('foo'); // not incomplete because closure is created...
get('foo')->get('bar')->expect(true)->toBeTrue(); get('foo')->get('bar')->expect(true)->toBeTrue();
get('foo')->expect(true)->toBeTrue(); get('foo')->expect(true)->toBeTrue();
describe('a "describe" group of tests', function () {
get('foo'); // not incomplete because closure is created...
get('foo')->get('bar')->expect(true)->toBeTrue();
get('foo')->expect(true)->toBeTrue();
});

View File

@ -15,3 +15,7 @@ it('is not incompleted because of assert')->assertTrue(true);
it('is not incompleted because of test with assertions', function () { it('is not incompleted because of test with assertions', function () {
expect(true)->toBeTrue(); expect(true)->toBeTrue();
}); });
describe('a "describe" group of tests', function () {
it('is incompleted');
});

View File

@ -5,3 +5,11 @@ it('is a test', function () {
}); });
it('is a higher order message test')->expect(true)->toBeTrue(); it('is a higher order message test')->expect(true)->toBeTrue();
describe('a "describe" group of tests', function () {
it('is a test', function () {
expect(['key' => 'foo'])->toHaveKey('key')->key->toBeString();
});
it('is a higher order message test')->expect(true)->toBeTrue();
});

View File

@ -5,3 +5,11 @@ test('notice', function () {
expect(true)->toBeTrue(); expect(true)->toBeTrue();
}); });
describe('a "describe" group of tests', function () {
test('notice', function () {
trigger_error('This is a notice description', E_USER_NOTICE);
expect(true)->toBeTrue();
});
});

View File

@ -9,3 +9,9 @@ it('allows access to the underlying expectNotToPerformAssertions method', functi
it('allows performing no expectations without being risky', function () { it('allows performing no expectations without being risky', function () {
$result = 1 + 1; $result = 1 + 1;
})->throwsNoExceptions(); })->throwsNoExceptions();
describe('a "describe" group of tests', function () {
it('allows performing no expectations without being risky', function () {
$result = 1 + 1;
});
})->throwsNoExceptions();

View File

@ -11,3 +11,11 @@ test('user warning', function () {
expect(true)->toBeTrue(); expect(true)->toBeTrue();
}); });
describe('a "describe" group of tests', function () {
test('user warning', function () {
trigger_error('This is a warning description', E_USER_WARNING);
expect(true)->toBeTrue();
});
});