Skip to content

Commit 41a9b32

Browse files
committed
Add testcase method's name to the test's title.
Resolves nette#448.
1 parent 7fd3b98 commit 41a9b32

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/Framework/Helpers.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function findCommonDirectory(array $paths): string
7171

7272

7373
/**
74-
* Parse phpDoc comment.
74+
* Parse the first docblock encountered in the provided string.
7575
* @internal
7676
*/
7777
public static function parseDocComment(string $s): array
@@ -81,10 +81,14 @@ public static function parseDocComment(string $s): array
8181
return [];
8282
}
8383

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

90+
// Parse @tags and their arguments. If there are multiple identically
91+
// named tags, their arguments will be returned as a list of string.
8892
preg_match_all('#^[ \t\*]*@(\w+)([^\w\r\n].*)?#mi', $content[1], $matches, PREG_SET_ORDER);
8993
foreach ($matches as $match) {
9094
$ref = &$options[strtolower($match[1])];

src/Runner/Test.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Test
4343
public function __construct(string $file, ?string $title = null)
4444
{
4545
$this->file = $file;
46-
$this->title = $title;
46+
$this->title = $title !== null ? trim($title) : null;
4747
}
4848

4949

@@ -100,6 +100,29 @@ public function getOutput(): string
100100
}
101101

102102

103+
public function withAppendedTitle(string $title): self
104+
{
105+
if ($this->hasResult()) {
106+
throw new \LogicException('Cannot append title to test which already has a result.');
107+
}
108+
109+
$title = trim($title);
110+
$me = clone $this;
111+
112+
if ($title === '') {
113+
return $me;
114+
}
115+
116+
// At this point we're sure both $me->title and $title do not have
117+
// leading/trailing whitespace.
118+
$me->title = $me->title !== null
119+
? "{$me->title} {$title}"
120+
: $title;
121+
122+
return $me;
123+
}
124+
125+
103126
/**
104127
* @return static
105128
*/

src/Runner/TestHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ private function initiateTestCase(Test $test, $foo, PhpInterpreter $interpreter)
228228
}
229229

230230
return array_map(
231-
fn(string $method): Test => $test->withArguments(['method' => $method]),
231+
fn(string $method): Test => $test
232+
// Add the testcase method's name to the test's title.
233+
->withAppendedTitle($method)
234+
->withArguments(['method' => $method]),
232235
$methods,
233236
);
234237
}

tests/Runner/Job.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,29 @@ test('', function () {
2828
Assert::contains('Nette Tester', $job->getHeaders());
2929
}
3030
});
31+
32+
33+
test('Appending title to a Test object w/o initial title', function () {
34+
$testA = (new Test('Job.test.phptx'));
35+
Assert::null($testA->title);
36+
37+
$testB = $testA->withAppendedTitle('title B');
38+
Assert::notSame($testB, $testA);
39+
Assert::same('title B', $testB->title);
40+
41+
$testC = $testB->withAppendedTitle(" \t title C ");
42+
Assert::notSame($testC, $testB);
43+
Assert::same('title B title C', $testC->title);
44+
});
45+
46+
47+
test('Appending title to a Test object w/ initial title', function () {
48+
$testA = (new Test('Job.test.phptx', 'Initial title '));
49+
Assert::same('Initial title', $testA->title);
50+
51+
$testB = $testA->withAppendedTitle(' ');
52+
Assert::same('Initial title', $testB->title);
53+
54+
$testC = $testB->withAppendedTitle(" \t MEGATITLE ");
55+
Assert::same('Initial title MEGATITLE', $testC->title);
56+
});

0 commit comments

Comments
 (0)