Skip to content

Commit 973fecb

Browse files
committed
Add some textual renderer tests
Signed-off-by: Jack Cherng <[email protected]>
1 parent 232356f commit 973fecb

19 files changed

+184
-77
lines changed

tests/DiffHelperTest.php

Lines changed: 76 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Jfcherng\Diff\DiffHelper;
88
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Finder\Finder;
10+
use Symfony\Component\Finder\SplFileInfo;
911

1012
/**
1113
* @coversNothing
@@ -15,102 +17,99 @@
1517
final class DiffHelperTest extends TestCase
1618
{
1719
/**
18-
* Data provider for DiffHelper::calculate.
20+
* Test renderer output.
1921
*
20-
* @return array the data provider
22+
* @covers \Jfcherng\Diff\DiffHelper::calculate
23+
* @covers \Jfcherng\Diff\Renderer\Text\Context
24+
* @covers \Jfcherng\Diff\Renderer\Text\Unified
25+
*
26+
* @dataProvider rendererOutputDataProvider
27+
*
28+
* @param string $rendererName The renderer name
29+
* @param int $idx The index
30+
* @param SplFileInfo[] $testFiles The test files
2131
*/
22-
public function calculateDataProvider(): array
32+
public function testRendererOutput(string $rendererName, int $idx, array $testFiles): void
2333
{
24-
return [
25-
[
26-
<<<'EOT'
27-
apples
28-
oranges
29-
kiwis
30-
carrots
31-
EOT
32-
,
33-
<<<'EOT'
34-
apples
35-
kiwis
36-
carrots
37-
grapefruits
38-
EOT
39-
,
40-
[
41-
'Unified' => <<<'EOT'
42-
@@ -1,4 +1,4 @@
43-
apples
44-
-oranges
45-
kiwis
46-
carrots
47-
+grapefruits
48-
49-
EOT
50-
,
51-
'Context' => <<<'EOT'
52-
***************
53-
*** 1,4 ****
54-
apples
55-
- oranges
56-
kiwis
57-
carrots
58-
--- 1,4 ----
59-
apples
60-
kiwis
61-
carrots
62-
+ grapefruits
63-
64-
EOT
65-
,
66-
],
67-
],
68-
];
34+
if (!isset($testFiles['old'], $testFiles['new'], $testFiles['result'])) {
35+
static::markTestSkipped("Renderer output test '{$rendererName}' #{$idx} is imcomplete.");
36+
}
37+
38+
$result = DiffHelper::calculate(
39+
$testFiles['old']->getContents(),
40+
$testFiles['new']->getContents(),
41+
$rendererName
42+
);
43+
44+
static::assertSame(
45+
$testFiles['result']->getContents(),
46+
$result,
47+
"Renderer output test '{$rendererName}' #{$idx} failed..."
48+
);
6949
}
7050

7151
/**
72-
* Test the DiffHelper::calculate with the 'Unified' renderer.
73-
*
74-
* @covers \Jfcherng\Diff\DiffHelper::calculate
75-
* @dataProvider calculateDataProvider
52+
* Test the DiffHelper::getStyleSheet.
7653
*
77-
* @param string $old the old
78-
* @param string $new the new
79-
* @param array $expecteds the expecteds
54+
* @covers \Jfcherng\Diff\DiffHelper::getStyleSheet
8055
*/
81-
public function testCalculateUnified(string $old, string $new, array $expecteds): void
56+
public function testGetStyleSheet(): void
8257
{
83-
static::assertSame(
84-
$expecteds['Unified'],
85-
DiffHelper::calculate($old, $new, 'Unified')
86-
);
58+
static::assertIsString(DiffHelper::getStyleSheet());
8759
}
8860

8961
/**
90-
* Test the DiffHelper::calculate with the 'Context' renderer.
91-
*
92-
* @covers \Jfcherng\Diff\DiffHelper::calculate
93-
* @dataProvider calculateDataProvider
94-
*
95-
* @param string $old the old
96-
* @param string $new the new
97-
* @param array $expecteds the expecteds
62+
* Data provider for self::testRendererOutput.
9863
*/
99-
public function testCalculateContext(string $old, string $new, array $expecteds): void
64+
public function rendererOutputDataProvider(): array
10065
{
101-
static::assertSame(
102-
$expecteds['Context'],
103-
DiffHelper::calculate($old, $new, 'Context')
104-
);
66+
$rendererNames = DiffHelper::getAvailableRenderers();
67+
68+
$data = [];
69+
70+
foreach ($rendererNames as $rendererName) {
71+
$tests = $this->findRendererOutputTestFiles($rendererName);
72+
73+
foreach ($tests as $idx => $files) {
74+
$data[] = [$rendererName, $idx, $files];
75+
}
76+
}
77+
78+
return $data;
10579
}
10680

10781
/**
108-
* Test the DiffHelper::getStyleSheet.
82+
* Find renderer output test files.
10983
*
110-
* @covers \Jfcherng\Diff\DiffHelper::getStyleSheet
84+
* The structure is like [
85+
* 1 => ['old' => SplFileInfo, 'new' => SplFileInfo, 'result' => SplFileInfo],
86+
* ...
87+
* ]
88+
*
89+
* @param string $rendererName The renderer name
11190
*/
112-
public function testGetStyleSheet(): void
91+
protected function findRendererOutputTestFiles(string $rendererName): array
11392
{
114-
static::assertIsString(DiffHelper::getStyleSheet());
93+
$rendererNameRegex = \preg_quote($rendererName, '/');
94+
$fileNameRegex = "/{$rendererNameRegex}-(?P<idx>[0-9]+)-(?P<name>[^.\-]+)\.txt$/u";
95+
96+
$finder = (new Finder())
97+
->files()
98+
->name($fileNameRegex)
99+
->in(__DIR__ . '/data/renderer_outputs');
100+
101+
$ret = [];
102+
103+
/** @var SplFileInfo $file */
104+
foreach ($finder as $file) {
105+
\preg_match($fileNameRegex, $file->getFilename(), $matches);
106+
$idx = (int) $matches['idx'];
107+
$name = $matches['name'];
108+
109+
$ret[$idx] = $ret[$idx] ?? [];
110+
$ret[$idx][$name] = $file;
111+
}
112+
113+
return $ret;
115114
}
116115
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; simple test
2+
apples
3+
kiwis
4+
carrots
5+
grapefruits
6+
equal-line
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; simple test
2+
apples
3+
oranges
4+
kiwis
5+
carrots
6+
equal-line
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
***************
2+
*** 1,6 ****
3+
; simple test
4+
apples
5+
- oranges
6+
kiwis
7+
carrots
8+
equal-line
9+
--- 1,6 ----
10+
; simple test
11+
apples
12+
kiwis
13+
carrots
14+
+ grapefruits
15+
equal-line
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; test EOL at EOF
2+
A
3+
C
4+
X
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; test EOL at EOF
2+
A
3+
B
4+
X
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
***************
2+
*** 1,4 ****
3+
; test EOL at EOF
4+
A
5+
! B
6+
! X
7+
\ No newline at end of file
8+
--- 1,4 ----
9+
; test EOL at EOF
10+
A
11+
! C
12+
! X
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; pure OP_EQ blocks should be omitted
2+
A
3+
B
4+
M
5+
X
6+
Y
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; pure OP_EQ blocks should be omitted
2+
A
3+
B
4+
X
5+
Y
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
***************
2+
*** 1,5 ****
3+
--- 1,6 ----
4+
; pure OP_EQ blocks should be omitted
5+
A
6+
B
7+
+ M
8+
X
9+
Y

0 commit comments

Comments
 (0)