Skip to content

Lazy assert types gathering in TypeInferenceTestCase #1991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use function array_map;
use function array_merge;
use function count;
use function implode;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -74,6 +75,40 @@ public function processFile(
);
}

/**
* @param callable(): array<string, array<mixed>> $assertionsCallback
* @api
*/
public function assertAssertions(callable $assertionsCallback): void
{
$assertions = $assertionsCallback();

$errors = [];
foreach ($assertions as $args) {
$type = $args[0];
$file = $args[1];
if ($type === 'type') {
$expectedType = $args[2];
$this->assertInstanceOf(ConstantScalarType::class, $expectedType);
$expected = $expectedType->getValue();
$actual = $args[3]->describe(VerbosityLevel::precise());
$line = $args[4];
if ($expected !== $actual) {
$errors[] = sprintf('Expected type %s, got type %s in %s on line %d.', $expected, $actual, $file, $line);
}
} elseif ($type === 'variableCertainty') {
$expectedCertainty = $args[2];
$actualCertainty = $args[3];
$variableName = $args[4];
$line = $args[5];
if (!$expectedCertainty->equals($actualCertainty)) {
$errors[] = sprintf('Expected %s, actual certainty of variable $%s is %s in %s on line %d.', $expectedCertainty->describe(), $variableName, $actualCertainty->describe(), $file, $line);
}
}
}
$this->assertEmpty($errors, implode("\n", $errors));
}

/**
* @api
* @param mixed ...$args
Expand Down Expand Up @@ -106,6 +141,15 @@ public function assertFileAsserts(
}
}

/**
* @api
* @return iterable<string, array{callable(): array<string, array<mixed>>}>
*/
public function gatherAssertions(string $file): iterable
{
yield $file => [fn () => $this->gatherAssertTypes($file)];
}

/**
* @api
* @return array<string, mixed[]>
Expand Down
Loading