master
ブランチ master
LittleBeast / src / Std / Test / LibDiffViewer.php
6.4 KB、133行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Test;
39
40require_once __DIR__.'/../../../autoload.php';
41
42use Std\Lib\Tester;
43use Std\Lib\DiffViewer;
44
45$test = new Tester([
46 'colorOutput' => true,
47 'verboseOutput' => true
48]);
49
50$tmpFiles = [];
51$view = function(string $diff) use (&$tmpFiles): string {
52 $f = tempnam(sys_get_temp_dir(), 'lbdiff_');
53 file_put_contents($f, $diff);
54 $tmpFiles[] = $f;
55 return (new DiffViewer($f))->displaySideBySide();
56};
57// <td>の行を [クラス, 内容] の配列にする
58$cells = function(string $html): array {
59 preg_match_all('#<td class="([\w-]+)">(.*?)</td>#s', $html, $m, PREG_SET_ORDER);
60 return array_map(fn($x) => [$x[1], $x[2]], $m);
61};
62
63$test->describe('差分ビューア', function($test) use ($view, $cells): void {
64 $test->it('無いファイルは例外のはず', function($test): void {
65 $test->assertThrows(fn() => new DiffViewer('/no/such/file.diff'), \Exception::class);
66 });
67
68 $test->it('git形式の差分を左右に並べるはず', function($test) use ($view, $cells): void {
69 $html = $view("diff --git a/main.c b/main.c\nindex 1..2 100644\n--- a/main.c\n+++ b/main.c\n"
70 ."@@ -1,3 +1,3 @@\n #include <stdio.h>\n-int a;\n+int b;\n return 0;\n");
71 $test->assertStringContains('<h2>ファイル: a/main.c | b/main.c</h2>', $html);
72 $test->assertStringContains('<td colspan="4">@@ -1,3 +1,3 @@</td>', $html);
73
74 $c = $cells($html);
75 $test->assertEquals(['line-number', '1'], $c[0]);
76 $test->assertEquals(['context', '#include &lt;stdio.h&gt;'], $c[1]);
77 $test->assertEquals(['removed', 'int a;'], $c[5]);
78 $test->assertEquals(['added', 'int b;'], $c[7]);
79 $test->assertEquals(['line-number', '2'], $c[6]);
80 });
81
82 $test->it('削除と追加の数が違う場合は空のセルで埋めるはず', function($test) use ($view): void {
83 $html = $view("--- a.txt\n+++ b.txt\n@@ -1,2 +1,1 @@\n-x\n-y\n+z\n");
84 $test->assertStringContains('<td class="empty">&nbsp;</td>', $html);
85 $test->assertEquals(1, substr_count($html, 'class="empty"'));
86 });
87
88 $test->it('「---」で始まる削除行をファイルヘッダーと間違えないはず', function($test) use ($view): void {
89 $html = $view("--- a/x.md\n+++ b/x.md\n@@ -1,2 +1,1 @@\n---- 区切り\n keep\n");
90 $test->assertEquals(1, substr_count($html, '<h2>'));
91 $test->assertStringContains('<td class="removed">--- 区切り</td>', $html);
92 });
93
94 $test->it('行数が省略されたハンクと新しいファイルを扱えるはず', function($test) use ($view, $cells): void {
95 $html = $view("diff --git a/new.txt b/new.txt\nnew file mode 100644\n--- /dev/null\n+++ b/new.txt\n@@ -0,0 +1 @@\n+hello\n\\ No newline at end of file\n");
96 $test->assertStringContains('<h2>ファイル: /dev/null | b/new.txt</h2>', $html);
97 $c = $cells($html);
98 $test->assertEquals(['added', 'hello'], $c[3]);
99 $test->assertEquals(['line-number', '1'], $c[2]);
100 });
101
102 $test->it('複数のファイルと内容の無い変更を扱えるはず', function($test) use ($view): void {
103 $html = $view("diff --git a/a.png b/a.png\nBinary files a/a.png and b/a.png differ\n"
104 ."diff --git a/old.sh b/new.sh\nsimilarity index 100%\nrename from old.sh\nrename to new.sh\n"
105 ."diff --git a/c.txt b/c.txt\n--- a/c.txt\n+++ b/c.txt\n@@ -1 +1 @@\n-1\n+2\n");
106 $test->assertEquals(3, substr_count($html, '<h2>'));
107 $test->assertStringContains('<p>バイナリファイル</p>', $html);
108 $test->assertStringContains('<h2>ファイル: a/old.sh | b/new.sh</h2>', $html);
109 $test->assertStringContains('類似度:100% / 名前の変更元:old.sh / 名前の変更先:new.sh', $html);
110 });
111
112 $test->it('HTMLをエスケープするはず', function($test) use ($view): void {
113 $html = $view("--- a/<b>.html\n+++ b/<b>.html\n@@ -1 +1 @@\n-<script>alert(1)</script>\n+&amp;\n");
114 $test->assertFalse(str_contains($html, '<script>'));
115 $test->assertFalse(str_contains($html, '<b>'));
116 $test->assertStringContains('&lt;script&gt;', $html);
117 $test->assertStringContains('&amp;amp;', $html);
118 });
119
120 $test->it('CRLFの差分も扱えるはず', function($test) use ($view): void {
121 $html = $view("--- a.txt\r\n+++ b.txt\r\n@@ -1 +1 @@\r\n-x\r\n+y\r\n");
122 $test->assertStringContains('<td class="removed">x</td>', $html);
123 $test->assertStringContains('<td class="added">y</td>', $html);
124 });
125
126 $test->it('空の差分は何も出力しないはず', function($test) use ($view): void {
127 $test->assertEquals('', $view(''));
128 });
129});
130
131foreach ($tmpFiles as $f) @unlink($f);
132
133$test->printSummary();