Skip to content

Commit 547d603

Browse files
committed
Improve spec test harness
1 parent 4e4dcc4 commit 547d603

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

tests/HandlebarsSpecTest.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
/**
1111
* @phpstan-type JsonSpec array{
12-
* file: string, no: int, message: string|null, data: mixed,
13-
* it: string, description: string, expected: string|null, helpers: array<mixed>,
14-
* partials: array<mixed>, compileOptions: array<mixed>, template: string,
15-
* exception: string|null, runtimeOptions: array<mixed>, number: string|null,
12+
* data: mixed, it: string, description: string, expected: string|null,
13+
* helpers: array<mixed>, partials: array<mixed>, compileOptions: array<mixed>,
14+
* template: string, exception: string|true, runtimeOptions: array<mixed>,
1615
* }
1716
*/
1817
class HandlebarsSpecTest extends TestCase
@@ -39,7 +38,7 @@ public function testSpecs(array $spec): void
3938
|| $spec['description'] === 'partials - compat mode'
4039

4140
// stringParams mode was removed from Handlebars in 2015
42-
|| $spec['it'] === 'in string params mode,'
41+
|| isset($spec['compileOptions']['stringParams'])
4342

4443
// Decorators are deprecated: https://github.com/handlebars-lang/handlebars.js/blob/master/docs/decorators-api.md
4544
|| $spec['description'] === 'blocks - decorators'
@@ -64,7 +63,7 @@ public function testSpecs(array $spec): void
6463
$helpersList = '[';
6564
foreach ($spec['helpers'] as $name => $func) {
6665
if (!isset($func['php'])) {
67-
$this->markTestIncomplete("No PHP helper code provided for [{$spec['file']}#{$spec['description']}]#{$spec['no']}");
66+
continue;
6867
}
6968
$helper = self::patchHelperCode($func['php']);
7069
$helpersList .= "\n '$name' => $helper,\n";
@@ -102,11 +101,11 @@ public function testSpecs(array $spec): void
102101
partials: $stringPartials,
103102
));
104103
} catch (\Exception $e) {
105-
if (isset($spec['exception'])) {
104+
if ($spec['exception']) {
106105
$this->expectNotToPerformAssertions();
107106
return;
108107
}
109-
$this->fail("Compile error in {$spec['file']}#{$spec['description']}]#{$spec['no']}:{$spec['it']}\n" . $e->getMessage());
108+
$this->fail("Compile error: {$e->getMessage()}");
110109
}
111110
$renderer = Handlebars::template($php);
112111

@@ -127,30 +126,28 @@ public function testSpecs(array $spec): void
127126
}
128127
$result = $renderer($spec['data'], $ropt);
129128
} catch (\Exception $e) {
130-
if (isset($spec['exception'])) {
129+
if ($spec['exception']) {
131130
$this->expectNotToPerformAssertions();
132131
return;
133132
}
134-
$this->fail("Rendering Error in " . self::getSpecDetails($spec, $php, $helpersList) . "\n\n{$e->getMessage()}");
133+
$this->fail("Rendering error: {$e->getMessage()}\n\n" . self::getSpecDetails($php, $helpersList));
135134
}
136135

137-
if (isset($spec['exception'])) {
138-
$this->fail("Should Fail: " . self::getSpecDetails($spec, $php, $helpersList) . "\n\nResult: $result");
136+
if ($spec['exception']) {
137+
$details = $spec['exception'] === true ? '.' : ": {$spec['exception']}\n";
138+
$this->fail("Expected exception{$details}\nResult: $result\n\n" . self::getSpecDetails($php, $helpersList));
139139
}
140140

141-
$this->assertSame($spec['expected'], $result, self::getSpecDetails($spec, $php, $helpersList));
141+
$this->assertSame($spec['expected'], $result, self::getSpecDetails($php, $helpersList));
142142
}
143143

144-
/**
145-
* @param JsonSpec $spec
146-
*/
147-
private static function getSpecDetails(array $spec, string $code, string $helpers): string
144+
private static function getSpecDetails(string $code, string $helpers): string
148145
{
149-
return "{$spec['file']}#{$spec['description']}]#{$spec['no']}:{$spec['it']}\nHelpers: $helpers\nPHP code:\n$code";
146+
return "Helpers: $helpers\nPHP code:\n$code";
150147
}
151148

152149
/**
153-
* @return list<array{JsonSpec}>
150+
* @return array<string, array{JsonSpec}>
154151
*/
155152
public static function jsonSpecProvider(): array
156153
{
@@ -174,25 +171,26 @@ public static function jsonSpecProvider(): array
174171
if ($contents === false) {
175172
throw new \Exception("Failed to read JSON spec file {$file}");
176173
}
177-
$i = 0;
178174
$json = json_decode($contents, true);
179175

180176
foreach ($json as $spec) {
181-
$ret[] = [[
182-
'file' => $file,
183-
'no' => ++$i,
184-
'message' => $spec['message'] ?? null,
177+
$number = $spec['number'] ?? null;
178+
$key = "{$spec['description']}: {$spec['it']}" . ($number !== null ? " #$number" : '');
179+
if (isset($spec['message'])) {
180+
$key .= ": {$spec['message']}";
181+
}
182+
183+
$ret[$key] = [[
185184
'data' => $spec['data'] ?? null,
186-
'it' => $spec['it'] ?? '',
187-
'description' => $spec['description'] ?? '',
185+
'it' => $spec['it'],
186+
'description' => $spec['description'],
188187
'expected' => $spec['expected'] ?? null,
189188
'helpers' => $spec['helpers'] ?? [],
190189
'partials' => $spec['partials'] ?? [],
191190
'compileOptions' => $spec['compileOptions'] ?? [],
192191
'template' => $spec['template'] ?? '',
193-
'exception' => $spec['exception'] ?? null,
192+
'exception' => $spec['exception'] ?? '',
194193
'runtimeOptions' => $spec['runtimeOptions'] ?? [],
195-
'number' => $spec['number'] ?? null,
196194
]];
197195
}
198196
}

0 commit comments

Comments
 (0)