Skip to content

Commit 39eb968

Browse files
committed
Improves regex reverse compilation tests
Refactors the reverse compilation tests to improve reliability and provide more informative error messages. Specifically, it ensures that sample generation failures are handled gracefully by skipping the sample match test but still validating the recompiled regex. It also replaces direct constant access for preg errors with namespaced access.
1 parent e59bf07 commit 39eb968

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

tests/Integration/ComprehensivePublicAPITest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use RegexParser\Exception\ParserException;
1919
use RegexParser\ReDoS\ReDoSSeverity;
2020
use RegexParser\Regex;
21-
use RegexParser\ValidationResult;
2221

2322
/**
2423
* Comprehensive test suite for the public API of RegexParser.

tests/Integration/ReverseCompilerTest.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace RegexParser\Tests\Integration;
1515

1616
use PHPUnit\Framework\Attributes\DataProvider;
17-
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
1817
use PHPUnit\Framework\TestCase;
1918
use RegexParser\NodeVisitor\CompilerNodeVisitor;
2019
use RegexParser\Regex;
@@ -43,15 +42,15 @@ public function test_round_trip_compilation(string $originalPattern): void
4342
// We suppress errors because we want to catch the false return value
4443
$isValid = @preg_match($recompiled, '');
4544

46-
if ($isValid === false) {
45+
if (false === $isValid) {
4746
$error = preg_last_error();
4847
$msg = match ($error) {
49-
PREG_INTERNAL_ERROR => 'PREG_INTERNAL_ERROR',
50-
PREG_BACKTRACK_LIMIT_ERROR => 'PREG_BACKTRACK_LIMIT_ERROR',
51-
PREG_RECURSION_LIMIT_ERROR => 'PREG_RECURSION_LIMIT_ERROR',
52-
PREG_BAD_UTF8_ERROR => 'PREG_BAD_UTF8_ERROR',
53-
PREG_BAD_UTF8_OFFSET_ERROR => 'PREG_BAD_UTF8_OFFSET_ERROR',
54-
PREG_JIT_STACKLIMIT_ERROR => 'PREG_JIT_STACKLIMIT_ERROR',
48+
\PREG_INTERNAL_ERROR => 'PREG_INTERNAL_ERROR',
49+
\PREG_BACKTRACK_LIMIT_ERROR => 'PREG_BACKTRACK_LIMIT_ERROR',
50+
\PREG_RECURSION_LIMIT_ERROR => 'PREG_RECURSION_LIMIT_ERROR',
51+
\PREG_BAD_UTF8_ERROR => 'PREG_BAD_UTF8_ERROR',
52+
\PREG_BAD_UTF8_OFFSET_ERROR => 'PREG_BAD_UTF8_OFFSET_ERROR',
53+
\PREG_JIT_STACKLIMIT_ERROR => 'PREG_JIT_STACKLIMIT_ERROR',
5554
default => 'Unknown Error'
5655
};
5756
$this->fail("Recompiled regex '$recompiled' from '$originalPattern' is invalid ($msg).");
@@ -63,16 +62,15 @@ public function test_round_trip_compilation(string $originalPattern): void
6362
// Some recursive/complex patterns might hit generator limits, so we wrap in try/catch
6463
$sample = $this->regexService->generate($originalPattern);
6564

66-
if ($sample !== '') {
67-
$this->assertMatchesRegularExpression(
68-
$recompiled,
69-
$sample,
70-
"Recompiled regex '$recompiled' failed to match sample '$sample' generated from '$originalPattern'"
71-
);
72-
}
65+
$this->assertMatchesRegularExpression(
66+
$recompiled,
67+
$sample,
68+
"Recompiled regex '$recompiled' failed to match sample '$sample' generated from '$originalPattern'",
69+
);
7370
} catch (\Exception $e) {
7471
// If generation fails (e.g. infinite recursion), we skip the sample match test
7572
// but the validity test above is still valuable.
73+
$this->markTestSkipped('Sample generation failed for pattern '.$originalPattern.': '.$e->getMessage());
7674
}
7775
}
7876

tests/Integration/SymfonyIntegrationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
1717
use PHPUnit\Framework\TestCase;
18-
use RegexParser\LiteralSet;
1918
use RegexParser\Regex;
2019

2120
/**

0 commit comments

Comments
 (0)