Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit f1cd043

Browse files
committed
Add an example test, make unified diffs match diff output
1 parent 9e18d9a commit f1cd043

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

src/StringDiff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public function getUnifiedDiff(int $context = 3): string {
3838
$hunks = vec[];
3939

4040
$remaining = $this->getDiff();
41+
$last = C\lastx($remaining);
42+
// diff -u ignores trailing newlines
43+
if ($last is DiffKeepOp<_> && $last->getContent() === '') {
44+
$remaining = Vec\slice($remaining, 0, C\count($remaining) - 1);
45+
}
46+
4147
while (!C\is_empty($remaining)) {
4248
$not_keep = C\find_key($remaining, $row ==> !$row instanceof DiffKeepOp);
4349
if ($not_keep === null) {

tests/StringDiffTest.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,41 @@ public function testDiffLines(): void {
3030
);
3131
}
3232

33-
public function testDiffCharacters(): void {
33+
public function testDiffCharacters(): void {
3434
$diff = StringDiff::characters('abb', 'abc')->getDiff();
35-
expect(C\count($diff))->toBeSame(4);
35+
expect(C\count($diff))->toBeSame(4);
3636

37-
expect($diff[0])->toBeInstanceOf(DiffKeepOp::class);
38-
expect($diff[1])->toBeInstanceOf(DiffKeepOp::class);
39-
expect($diff[2])->toBeInstanceOf(DiffDeleteOp::class);
40-
expect($diff[3])->toBeInstanceOf(DiffInsertOp::class);
37+
expect($diff[0])->toBeInstanceOf(DiffKeepOp::class);
38+
expect($diff[1])->toBeInstanceOf(DiffKeepOp::class);
39+
expect($diff[2])->toBeInstanceOf(DiffDeleteOp::class);
40+
expect($diff[3])->toBeInstanceOf(DiffInsertOp::class);
41+
42+
expect(Vec\map($diff, $op ==> $op->getContent()))->toBeSame(
43+
vec['a', 'b', 'b', 'c'],
44+
);
45+
}
46+
47+
public function provideExamples(): vec<varray<string>> {
48+
return Vec\map(
49+
\glob(__DIR__.'/examples/*.a'),
50+
$ex ==> varray[\basename($ex, '.a')],
51+
);
52+
}
4153

42-
expect(Vec\map($diff, $op ==> $op->getContent()))->toBeSame(
43-
vec['a', 'b', 'b', 'c'],
44-
);
45-
}
54+
<<DataProvider('provideExamples')>>
55+
public function testUnifiedDiff(string $name): void {
56+
$base = __DIR__.'/examples/'.$name;
57+
$a = \file_get_contents($base.'.a');
58+
$b = \file_get_contents($base.'.b');
59+
$diff = StringDiff::lines($a, $b)->getUnifiedDiff();
60+
61+
expect($diff)->toBeSame(
62+
\file_get_contents($base.'.udiff.expect'),
63+
'Did not match expected contents '.
64+
'(from diff -u %s %s | tail -n +3 > %s.udiff.expect)',
65+
$base,
66+
$base,
67+
$base,
68+
);
69+
}
4670
}

tests/examples/replace_last.a

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo
2+
bar
3+
baz

tests/examples/replace_last.b

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo
2+
bar
3+
herp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@@ -1,3 +1,3 @@
2+
foo
3+
bar
4+
-baz
5+
+herp

0 commit comments

Comments
 (0)