Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Complex test name normalization #139

Merged
merged 7 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
];
}
}