Skip to content

Commit fc3b7cf

Browse files
committed
Add and test additional assertion on consumeRegex
1 parent 7311c4c commit fc3b7cf

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/ParsingInput.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function consumeString(string $value): void
136136
public function consumeRegex(string $pattern): string
137137
{
138138
assert(str_starts_with($pattern, '/^'));
139+
assert(!preg_match('/\$\/[a-z]+$/i', $pattern));
139140

140141
if (preg_match($pattern, $this->remaining(), $matches)) {
141142
$this->position += strlen($matches[0]);

tests/ParsingInputTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use gapple\StructuredFields\ParsingInput;
66
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\Attributes\RequiresSetting;
78
use PHPUnit\Framework\TestCase;
89

910
class ParsingInputTest extends TestCase
@@ -21,6 +22,7 @@ public function testGetEmptyChar(): void
2122
public static function trimProvider(): array
2223
{
2324
return [
25+
// [input string, trim optional white space, expected remaining string]
2426
'space' => [' test ', false, 'test '],
2527
'ows' => [" \t test ", true, 'test '],
2628
'non-ows' => [" \t test ", false, "\t test "],
@@ -51,4 +53,37 @@ public function testConsumeNotMatched(): void
5153
$this->expectExceptionMessage('Unexpected character');
5254
$input->consumeString('foo');
5355
}
56+
57+
/**
58+
* @return array<string, array{string, bool}>
59+
*/
60+
public static function regexProvider(): array
61+
{
62+
return [
63+
'Valid' => ['/^test/', true],
64+
'Valid with modifier' => ['/^test/i', true],
65+
'Missing start anchor' => ['/test/', false],
66+
'End anchor' => ['/test$/', false],
67+
'End anchor and modifier' => ['/test$/i', false],
68+
];
69+
}
70+
71+
#[RequiresSetting('zend.assertions', '1')]
72+
#[DataProvider('regexProvider')]
73+
public function testConsumeRegex(string $regex, bool $expected): void
74+
{
75+
try {
76+
$input = new ParsingInput('test');
77+
$input->consumeRegex($regex);
78+
79+
if (!$expected) {
80+
$this->fail('Expression should not have passed assertions');
81+
}
82+
} catch (\AssertionError $e) {
83+
if ($expected) {
84+
$this->fail('Expression failed assertion: ' . $e->getMessage());
85+
}
86+
}
87+
$this->addToAssertionCount(1);
88+
}
5489
}

0 commit comments

Comments
 (0)