マークダウンのユニットテスト
This commit is contained in:
234
src/Site/Test/LibMarkdown.php
Normal file
234
src/Site/Test/LibMarkdown.php
Normal file
@@ -0,0 +1,234 @@
|
||||
<?php
|
||||
namespace Site\Test;
|
||||
|
||||
require_once __DIR__.'/../../../autoload.php';
|
||||
|
||||
use Site\Lib\Tester;
|
||||
use Site\Lib\Markdown;
|
||||
|
||||
$test = new Tester([
|
||||
'colorOutput' => true,
|
||||
'verboseOutput' => true
|
||||
]);
|
||||
|
||||
function mkfile(string $body): void {
|
||||
$post = fopen(ROOT."/blog/dietrannynigger.md", "w");
|
||||
fwrite($post, "title: 【】\n");
|
||||
fwrite($post, "uuid: ".uuid()."\n");
|
||||
fwrite($post, "author: 諏訪子\n");
|
||||
fwrite($post, "date: ".date("Y-m-d H:i:s")."\n");
|
||||
fwrite($post, "thumbnail: \n");
|
||||
fwrite($post, "thumborient: center\n");
|
||||
fwrite($post, "category: \n");
|
||||
fwrite($post, "----\n");
|
||||
fwrite($post, $body);
|
||||
fclose($post);
|
||||
}
|
||||
|
||||
function rmfile(): void {
|
||||
unlink(ROOT."/blog/dietrannynigger.md");
|
||||
}
|
||||
|
||||
class Constants {
|
||||
public const START = "<p> ";
|
||||
public const END = " </p>";
|
||||
}
|
||||
|
||||
$test->describe('マークダウン', function($test): void {
|
||||
|
||||
$test->it('メタデータをパーシング出来るはず', function($test): void {
|
||||
$md = new Markdown('feature-test', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$meta = $md->getMetadata();
|
||||
$test->assertNotNull($meta);
|
||||
|
||||
$expect = new \stdClass;
|
||||
$expect->title = '【マークダウン】関数';
|
||||
$expect->uuid = 'a8c04518-4181-4ec6-9ef0-3f88f23b84b6';
|
||||
$expect->author = '諏訪子';
|
||||
$expect->date = '2025-11-07 20:33:42';
|
||||
$expect->thumbnail = 'o_53803618dc1691.28179609-orig.jpg';
|
||||
$expect->thumborient = 'center';
|
||||
$expect->category = ['test', 'おちんちん', 'でっかいおっぱい'];
|
||||
$expect->css = ['code', 'blockquote', 'table', 'blink'];
|
||||
|
||||
$test->assertEquals($expect, $meta);
|
||||
});
|
||||
|
||||
$test->it('横線はHTMLに交換出来るはず', function($test): void {
|
||||
mkfile('----');
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = "<hr />";
|
||||
$actual = str_replace("\n", '', trim($body));
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
});
|
||||
|
||||
$test->it('ヘッダーはHTMLに交換出来るはず', function($test): void {
|
||||
mkfile('# ケロケロ');
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = "<h1>ケロケロ</h1>";
|
||||
$actual = trim($body);
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
|
||||
// ---------------
|
||||
|
||||
mkfile('## ケロケロ');
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = "<h2>ケロケロ</h2>";
|
||||
$actual = trim($body);
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
|
||||
// ---------------
|
||||
|
||||
mkfile('### ケロケロ');
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = "<h3>ケロケロ</h3>";
|
||||
$actual = trim($body);
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
|
||||
// ---------------
|
||||
|
||||
mkfile('#### ケロケロ');
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = "<h4>ケロケロ</h4>";
|
||||
$actual = trim($body);
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
|
||||
// ---------------
|
||||
|
||||
mkfile('##### ケロケロ');
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = "<h5>ケロケロ</h5>";
|
||||
$actual = trim($body);
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
|
||||
// ---------------
|
||||
|
||||
mkfile('###### ケロケロ');
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = "<h6>ケロケロ</h6>";
|
||||
$actual = trim($body);
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
});
|
||||
|
||||
$test->skip('コメントはHTMLに交換出来るはず', '作成中・・・');
|
||||
|
||||
$test->it('画像、音楽、動画はHTMLに交換出来るはず', function($test): void {
|
||||
mkfile("\n
|
||||
\n
|
||||
\n
|
||||
$[audio/ogg](https://ass.technicalsuwako.moe/砕月着信音.ogg)\n
|
||||
#[video/mp4](https://ass.technicalsuwako.moe/フリーズ.mp4)");
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = '<p><img style="width: 100%; " src="https://ass.technicalsuwako.moe/banner.png" alt="" /> </p> '
|
||||
. '<p><img style="width: 100%; " src="https://ass.technicalsuwako.moe/banner.png" alt="ケロケロ" /> </p> '
|
||||
. '<p><img style="width: 100%; max-width: 200px;" src="https://ass.technicalsuwako.moe/banner.png" alt="ケロケロ" /> </p> '
|
||||
. '<p><audio controls><source src="https://ass.technicalsuwako.moe/砕月着信音.ogg" type="audio/ogg" /></audio> </p> '
|
||||
. '<p><video style="max-width: 100%;" controls><source src="https://ass.technicalsuwako.moe/フリーズ.mp4" type="video/mp4" /></video> </p>';
|
||||
$actual = str_replace("\n", '', str_replace(' ', '', trim($body)));
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
});
|
||||
|
||||
$test->it('基本的なマークダウンはHTMLに交換出来るはず', function($test): void {
|
||||
mkfile("**太文字**\n
|
||||
*イタリック*\n
|
||||
_下線_\n
|
||||
~取消線~\n
|
||||
!:(5)明滅文字:!\n
|
||||
^(72)フォントサイズ^\n
|
||||
%(ff00ff)フォントカラー%");
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = '<p> <strong>太文字</strong> </p> '
|
||||
. '<p> <em>イタリック</em> </p> '
|
||||
. '<p> <u>下線</u> </p> '
|
||||
. '<p> <s>取消線</s> </p> '
|
||||
. '<p> <span class="blink" style="animation-duration: 5s;">明滅文字</span> </p> '
|
||||
. '<p> <span style="font-size: 72px;">フォントサイズ</span> </p> '
|
||||
. '<p> <span style="color: #ff00ff;">フォントカラー</span> </p>';
|
||||
$actual = str_replace("\n", '', trim($body));
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
});
|
||||
|
||||
$test->it('複数マークダウンはHTMLに交換出来るはず', function($test): void {
|
||||
mkfile("[^(500)<God>(すわこ)](https://techsuwako.jp/)^");
|
||||
|
||||
$md = new Markdown('dietrannynigger', '/blog/');
|
||||
$test->assertNotNull($md);
|
||||
|
||||
$body = $md->parse();
|
||||
$test->assertNotNull($body);
|
||||
|
||||
$expect = Constants::START.'<a href="https://techsuwako.jp/"><span style="font-size: 500px;"><ruby>God<rp>(</rp><rt>すわこ</rt><rp>)</rp></ruby></a></span>'.Constants::END;
|
||||
$actual = str_replace("\n", '', trim($body));
|
||||
|
||||
$test->assertEquals($expect, $actual);
|
||||
});
|
||||
|
||||
rmfile();
|
||||
});
|
||||
|
||||
$test->printSummary();
|
||||
Reference in New Issue
Block a user