Skip to content
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

Add testcase method's name to the test's title #451

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
6 changes: 5 additions & 1 deletion src/Framework/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function findCommonDirectory(array $paths): string


/**
* Parse phpDoc comment.
* Parse the first docblock encountered in the provided string.
* @internal
*/
public static function parseDocComment(string $s): array
Expand All @@ -81,10 +81,14 @@ public static function parseDocComment(string $s): array
return [];
}

// The first non-@tagged string encountered in a multiline docblock will
// be stored at index 0.
if (preg_match('#^[ \t\*]*+([^\s@].*)#mi', $content[1], $matches)) {
$options[0] = trim($matches[1]);
}

// Parse @tags and their arguments. If there are multiple identically
// named tags, their arguments will be returned as a list of string.
preg_match_all('#^[ \t\*]*@(\w+)([^\w\r\n].*)?#mi', $content[1], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$ref = &$options[strtolower($match[1])];
Expand Down
25 changes: 24 additions & 1 deletion src/Runner/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Test
public function __construct(string $file, ?string $title = null)
{
$this->file = $file;
$this->title = $title;
$this->title = $title !== null ? trim($title) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case is the constructor called with a title that has extra spaces?

}


Expand Down Expand Up @@ -100,6 +100,29 @@ public function getOutput(): string
}


public function withAppendedTitle(string $title): self
{
if ($this->hasResult()) {
throw new \LogicException('Cannot append title to test which already has a result.');
}

$title = trim($title);
$me = clone $this;

if ($title === '') {
return $me;
}

// At this point we're sure both $me->title and $title do not have
// leading/trailing whitespace.
$me->title = $me->title !== null
? "{$me->title} {$title}"
: $title;

return $me;
}


/**
* @return static
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Runner/TestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ private function initiateTestCase(Test $test, $foo, PhpInterpreter $interpreter)
}

return array_map(
fn(string $method): Test => $test->withArguments(['method' => $method]),
fn(string $method): Test => $test
// Add the testcase method's name to the test's title.
->withAppendedTitle($method)
->withArguments(['method' => $method]),
$methods,
);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Runner/Job.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,29 @@ test('', function () {
Assert::contains('Nette Tester', $job->getHeaders());
}
});


test('Appending title to a Test object w/o initial title', function () {
$testA = (new Test('Job.test.phptx'));
Assert::null($testA->title);

$testB = $testA->withAppendedTitle('title B');
Assert::notSame($testB, $testA);
Assert::same('title B', $testB->title);

$testC = $testB->withAppendedTitle(" \t title C ");
Assert::notSame($testC, $testB);
Assert::same('title B title C', $testC->title);
});


test('Appending title to a Test object w/ initial title', function () {
$testA = (new Test('Job.test.phptx', 'Initial title '));
Assert::same('Initial title', $testA->title);

$testB = $testA->withAppendedTitle(' ');
Assert::same('Initial title', $testB->title);

$testC = $testB->withAppendedTitle(" \t MEGATITLE ");
Assert::same('Initial title MEGATITLE', $testC->title);
});
Loading