Skip to content

Commit 7144c6a

Browse files
authored
fix: Complex test name normalization (#139)
* 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
1 parent cf514ed commit 7144c6a

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

src/Browser/Test/LegacyExtension.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,19 @@ private static function saveBrowserStates(string $test, string $type): void
113113

114114
private static function normalizeTestName(string $name): string
115115
{
116+
if (!strchr($name, 'with data set')) {
117+
return \strtr($name, '\\:', '-_');
118+
}
119+
116120
// Try to match for a numeric data set index. If it didn't, match for a string one.
117-
if (!\preg_match('#^([\w:\\\]+)(.+\#(\d+))#', $name, $matches)) {
118-
\preg_match('#^([\w:\\\]+)(.+"([^"]+)")?#', $name, $matches);
121+
if (!\preg_match('#^(?<test>[\w:\\\]+) with data set \#(?<dataset>\d+)#', $name, $matches)) {
122+
\preg_match('#^(?<test>[\w:\\\]+) with data set "(?<dataset>.*)"#', $name, $matches);
119123
}
120124

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

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

127131
return $normalized;

tests/NormalizationTest.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717

1818
final class NormalizationTest extends TestCase
1919
{
20+
/**
21+
* @test
22+
* @dataProvider namesProvider
23+
* @dataProvider edgeCaseTestNames
24+
*/
25+
public function can_normalize_test_names(string $testName, string $expectedOutput): void
26+
{
27+
$browser = $this->createMock(Browser::class);
28+
$browser
29+
->expects(self::once())
30+
->method('saveCurrentState')
31+
->with($expectedOutput);
32+
33+
$extension = new LegacyExtension();
34+
$extension->executeBeforeFirstTest();
35+
$extension->executeBeforeTest($testName);
36+
$extension::registerBrowser($browser);
37+
$extension->executeAfterTestError($testName, '', 0);
38+
}
39+
2040
public static function namesProvider(): \Generator
2141
{
2242
$baseTemplate = 'error_'.__METHOD__;
@@ -52,22 +72,32 @@ public static function namesProvider(): \Generator
5272
];
5373
}
5474

55-
/**
56-
* @test
57-
* @dataProvider namesProvider
58-
*/
59-
public function can_normalize_test_names(string $testName, string $normalizedName): void
75+
public static function edgeCaseTestNames(): \Generator
6076
{
61-
$browser = $this->createMock(Browser::class);
62-
$browser
63-
->expects(self::once())
64-
->method('saveCurrentState')
65-
->with($normalizedName);
66-
67-
$extension = new LegacyExtension();
68-
$extension->executeBeforeFirstTest();
69-
$extension->executeBeforeTest($testName);
70-
$extension::registerBrowser($browser);
71-
$extension->executeAfterTestError($testName, '', 0);
77+
$baseTemplate = \strtr('error_'.__METHOD__."__data-set-", '\\:', '-_');
78+
yield 'self within moustache' => [
79+
'test name' => __METHOD__.' with data set "te{{self}}st" (test set)',
80+
'expected output' => $baseTemplate.'te-self-st__0',
81+
];
82+
yield 'double quoted with space' => [
83+
'test name' => __METHOD__.' with data set "_self.env.setCache("uri://host.net:2121") _self.env.loadTemplate("other-host")" (test set)',
84+
'expected output' => $baseTemplate.'_self-env-setCache-uri-host-net-2121-_self-env-loadTemplate-other-host-__0',
85+
];
86+
yield 'double quotes in moustache' => [
87+
'test name' => __METHOD__.' with data set "te{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}st"',
88+
'expected output' => $baseTemplate.'te-_self-env-registerUndefinedFilterCallback-exec-_self-env-getFilter-id-st__0',
89+
];
90+
yield 'escaped simple quote' => [
91+
'test name' => __METHOD__.' with data set "te{{\'/etc/passwd\'|file_excerpt(1,30)}}st"',
92+
'expected output' => $baseTemplate.'te-etc-passwd-file_excerpt-1-30-st__0',
93+
];
94+
yield 'single quote for array index access' => [
95+
'test name' => __METHOD__.' with data set "te{{[\'id\']|filter(\'system\')}}st"',
96+
'expected output' => $baseTemplate.'te-id-filter-system-st__0',
97+
];
98+
yield 'numeric array access' => [
99+
'test name' => __METHOD__.' with data set "te{{[0]|reduce(\'system\',\'id\')}}st"',
100+
'expected output' => $baseTemplate.'te-0-reduce-system-id-st__0',
101+
];
72102
}
73103
}

0 commit comments

Comments
 (0)