Skip to content

Commit 5337d6f

Browse files
authored
test: Add line numbers to the spec file (#1020)
This helps to navigate directly to the spec. Note however that if no title was given, no line number will be found. Maybe it is possible to find it still, but I don't think it is worth my efforts.
1 parent 69ba932 commit 5337d6f

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

tests/SpecFramework/SpecParser.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
use function array_merge;
3030
use function is_int;
3131
use function is_string;
32+
use function Safe\preg_match;
3233
use function sprintf;
34+
use function substr;
35+
use function substr_count;
36+
use const PREG_OFFSET_CAPTURE;
3337

3438
/**
3539
* @internal
@@ -60,6 +64,7 @@ public static function parseSpecFile(
6064
$relativePath = basename($sourceDir).'/'.$file->getRelativePathname();
6165

6266
yield $relativePath.': '.$title => self::parseSpec(
67+
$file->getContents(),
6368
$relativePath,
6469
$meta,
6570
$title,
@@ -91,6 +96,7 @@ private static function checkSpecFileSchema(mixed $specs): void
9196
}
9297

9398
private static function parseSpec(
99+
string $fileContents,
94100
string $file,
95101
Meta $meta,
96102
int|string $title,
@@ -102,6 +108,11 @@ private static function parseSpec(
102108
is_int($title) ? 'spec #'.$title : $title,
103109
);
104110

111+
$lineNumber = self::findLineNumber($fileContents, $title);
112+
if (null !== $lineNumber) {
113+
$file .= ':'.$lineNumber;
114+
}
115+
105116
$specWithConfig = is_string($specWithConfigOrSimpleSpec)
106117
? SpecWithConfig::fromSimpleSpec($specWithConfigOrSimpleSpec)
107118
: $specWithConfigOrSimpleSpec;
@@ -120,6 +131,29 @@ private static function parseSpec(
120131
);
121132
}
122133

134+
/**
135+
* @return positive-int|0
136+
*/
137+
private static function findLineNumber(string $fileContents, int|string $title): ?int
138+
{
139+
if (is_int($title)) {
140+
return null;
141+
}
142+
143+
$titleRegex = sprintf(
144+
'/ *\'%s\' => (?:SpecWithConfig|<<<\'PHP\')/',
145+
$title,
146+
);
147+
148+
if (1 !== preg_match($titleRegex, $fileContents, $matches, PREG_OFFSET_CAPTURE)) {
149+
return null;
150+
}
151+
152+
$titlePosition = $matches[0][1];
153+
154+
return substr_count(substr($fileContents, 0, $titlePosition), "\n") + 1;
155+
}
156+
123157
private static function createSymbolsConfiguration(
124158
SpecWithConfig $specWithConfig,
125159
Meta $meta,

tests/SpecFrameworkTest/SpecParserTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function specProvider(): iterable
8686
'Fixtures/simple-spec-file.php: A spec with a title' => new SpecScenario(
8787
null,
8888
null,
89-
'Fixtures/simple-spec-file.php',
89+
'Fixtures/simple-spec-file.php:33',
9090
'[Example of simple spec file] A spec with a title',
9191
$specCode,
9292
'Humbug',
@@ -108,7 +108,7 @@ public static function specProvider(): iterable
108108
'Fixtures/complete-spec-file.php: Spec with default meta values' => new SpecScenario(
109109
72_000,
110110
83_000,
111-
'Fixtures/complete-spec-file.php',
111+
'Fixtures/complete-spec-file.php:39',
112112
'[Example of simple spec file] Spec with default meta values',
113113
$specCode,
114114
'Humbug',
@@ -132,7 +132,7 @@ public static function specProvider(): iterable
132132
'Fixtures/complete-spec-file.php: Spec with the more verbose form' => new SpecScenario(
133133
72_000,
134134
83_000,
135-
'Fixtures/complete-spec-file.php',
135+
'Fixtures/complete-spec-file.php:49',
136136
'[Example of simple spec file] Spec with the more verbose form',
137137
$specCode,
138138
'Humbug',
@@ -156,7 +156,7 @@ public static function specProvider(): iterable
156156
'Fixtures/complete-spec-file.php: Spec with overridden meta values' => new SpecScenario(
157157
73_000,
158158
82_000,
159-
'Fixtures/complete-spec-file.php',
159+
'Fixtures/complete-spec-file.php:61',
160160
'[Example of simple spec file] Spec with overridden meta values',
161161
$specCode,
162162
'AnotherPrefix',

0 commit comments

Comments
 (0)