Skip to content

Commit

Permalink
fix: Complex test name normalization (#139)
Browse files Browse the repository at this point in the history
* Add edge test cases

* Update how dataset and test name are matched

* Use group names for better regex readability

* Repalce multiple non-word character by single hyphen

* Update tests expected output

* Remove useless import

* Move data providers together
  • Loading branch information
flohw authored Dec 23, 2023
1 parent cf514ed commit 7144c6a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
14 changes: 9 additions & 5 deletions src/Browser/Test/LegacyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,19 @@ private static function saveBrowserStates(string $test, string $type): void

private static function normalizeTestName(string $name): string
{
if (!strchr($name, 'with data set')) {
return \strtr($name, '\\:', '-_');
}

// Try to match for a numeric data set index. If it didn't, match for a string one.
if (!\preg_match('#^([\w:\\\]+)(.+\#(\d+))#', $name, $matches)) {
\preg_match('#^([\w:\\\]+)(.+"([^"]+)")?#', $name, $matches);
if (!\preg_match('#^(?<test>[\w:\\\]+) with data set \#(?<dataset>\d+)#', $name, $matches)) {
\preg_match('#^(?<test>[\w:\\\]+) with data set "(?<dataset>.*)"#', $name, $matches);
}

$normalized = \strtr($matches[1], '\\:', '-_');
$normalized = \strtr($matches['test'], '\\:', '-_');

if (isset($matches[3])) {
$normalized .= '__data-set-'.\strtr($matches[3], '\\: ', '-_-');
if (isset($matches['dataset'])) {
$normalized .= '__data-set-'.preg_replace('/\W+/', '-', $matches['dataset']);
}

return $normalized;
Expand Down
62 changes: 46 additions & 16 deletions tests/NormalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@

final class NormalizationTest extends TestCase
{
/**
* @test
* @dataProvider namesProvider
* @dataProvider edgeCaseTestNames
*/
public function can_normalize_test_names(string $testName, string $expectedOutput): void
{
$browser = $this->createMock(Browser::class);
$browser
->expects(self::once())
->method('saveCurrentState')
->with($expectedOutput);

$extension = new LegacyExtension();
$extension->executeBeforeFirstTest();
$extension->executeBeforeTest($testName);
$extension::registerBrowser($browser);
$extension->executeAfterTestError($testName, '', 0);
}

public static function namesProvider(): \Generator
{
$baseTemplate = 'error_'.__METHOD__;
Expand Down Expand Up @@ -52,22 +72,32 @@ public static function namesProvider(): \Generator
];
}

/**
* @test
* @dataProvider namesProvider
*/
public function can_normalize_test_names(string $testName, string $normalizedName): void
public static function edgeCaseTestNames(): \Generator
{
$browser = $this->createMock(Browser::class);
$browser
->expects(self::once())
->method('saveCurrentState')
->with($normalizedName);

$extension = new LegacyExtension();
$extension->executeBeforeFirstTest();
$extension->executeBeforeTest($testName);
$extension::registerBrowser($browser);
$extension->executeAfterTestError($testName, '', 0);
$baseTemplate = \strtr('error_'.__METHOD__."__data-set-", '\\:', '-_');
yield 'self within moustache' => [
'test name' => __METHOD__.' with data set "te{{self}}st" (test set)',
'expected output' => $baseTemplate.'te-self-st__0',
];
yield 'double quoted with space' => [
'test name' => __METHOD__.' with data set "_self.env.setCache("uri://host.net:2121") _self.env.loadTemplate("other-host")" (test set)',
'expected output' => $baseTemplate.'_self-env-setCache-uri-host-net-2121-_self-env-loadTemplate-other-host-__0',
];
yield 'double quotes in moustache' => [
'test name' => __METHOD__.' with data set "te{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}st"',
'expected output' => $baseTemplate.'te-_self-env-registerUndefinedFilterCallback-exec-_self-env-getFilter-id-st__0',
];
yield 'escaped simple quote' => [
'test name' => __METHOD__.' with data set "te{{\'/etc/passwd\'|file_excerpt(1,30)}}st"',
'expected output' => $baseTemplate.'te-etc-passwd-file_excerpt-1-30-st__0',
];
yield 'single quote for array index access' => [
'test name' => __METHOD__.' with data set "te{{[\'id\']|filter(\'system\')}}st"',
'expected output' => $baseTemplate.'te-id-filter-system-st__0',
];
yield 'numeric array access' => [
'test name' => __METHOD__.' with data set "te{{[0]|reduce(\'system\',\'id\')}}st"',
'expected output' => $baseTemplate.'te-0-reduce-system-id-st__0',
];
}
}

0 comments on commit 7144c6a

Please sign in to comment.