HTMLを綺麗に、色んなテスト関係関数の修正
This commit is contained in:
@@ -69,7 +69,7 @@ class DiffViewer {
|
||||
// 各ファイルにHTMLの出力の作成
|
||||
$html = '';
|
||||
foreach ($fileDiffs as $fileName => $diff) {
|
||||
$html .= "<h2>ファイル: ".htmlspecialchars($fileName)."</h2>\n";
|
||||
$html .= "\n <h2>ファイル: ".htmlspecialchars($fileName)."</h2>\n";
|
||||
$html .= $this->generateHtml($diff['leftLines'], $diff['rightLines']);
|
||||
}
|
||||
|
||||
@@ -106,25 +106,25 @@ class DiffViewer {
|
||||
}
|
||||
|
||||
private function generateHtml(array $leftLines, array $rightLines): string {
|
||||
$html = '<table class="diff-table">';
|
||||
$html .= '<tr class="diff-header"><th colspan="2">前</th><th colspan="2">新</th></tr>';
|
||||
$html = " <p>\n <table class=\"diff-table\">\n";
|
||||
$html .= " <tr class=\"diff-header\"><th colspan=\"2\">前</th><th colspan=\"2\">新</th></tr>\n";
|
||||
|
||||
$maxLines = max(count($leftLines), count($rightLines));
|
||||
for ($i = 0; $i < $maxLines; $i++) {
|
||||
$left = isset($leftLines[$i]) ? $leftLines[$i] : ['content' => '', 'type' => 'empty', 'line' => ''];
|
||||
$right = isset($rightLines[$i]) ? $rightLines[$i] : ['content' => '', 'type' => 'empty', 'line' => ''];
|
||||
|
||||
$html .= '<tr>';
|
||||
$html .= " <tr>\n";
|
||||
// 左(変更前)
|
||||
$html .= '<td class="line-number">' . ($left['line'] ?: ' ') . '</td>';
|
||||
$html .= '<td class="' . $left['type'] . '">' . ($left['content'] ?: ' ') . '</td>';
|
||||
$html .= " <td class=\"line-number\">".($left['line'] ?: ' ')."</td>\n";
|
||||
$html .= " <td class=\"{$left['type']}\">".($left['content'] ?: ' ')."</td>\n";
|
||||
// 右(変更後)
|
||||
$html .= '<td class="line-number">' . ($right['line'] ?: ' ') . '</td>';
|
||||
$html .= '<td class="' . $right['type'] . '">' . ($right['content'] ?: ' ') . '</td>';
|
||||
$html .= '</tr>';
|
||||
$html .= " <td class=\"line-number\">".($right['line'] ?: ' ')."</td>\n";
|
||||
$html .= " <td class=\"{$right['type']}\">".($right['content'] ?: ' ')."</td>\n";
|
||||
$html .= " </tr>\n";
|
||||
}
|
||||
|
||||
$html .= '</table>';
|
||||
$html .= " </table>\n </p>\n";
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ class Markdown {
|
||||
if (preg_match('/^```(\w*)$/', $line, $matches)) {
|
||||
if (!$this->inCodeBlock) {
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
$this->inCodeBlock = true;
|
||||
@@ -121,7 +121,7 @@ class Markdown {
|
||||
// テーブルの処理
|
||||
if (preg_match('/^\|(.+)\|$/', $line)) {
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
$cells = array_map('trim', explode('|', trim($line, '|')));
|
||||
@@ -145,17 +145,17 @@ class Markdown {
|
||||
// 水平線の処理
|
||||
if (preg_match('/^([\-\*\_])\1{2,}$/', $line)) {
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
$this->html[] = "<hr>";
|
||||
$this->html[] = " <hr />\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// 引用ブロックの処理
|
||||
if (preg_match('/^>\s(.+)/', $line, $matches)) {
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
$inBlockquote = true;
|
||||
@@ -176,7 +176,7 @@ class Markdown {
|
||||
$inList = false;
|
||||
}
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
|
||||
@@ -191,19 +191,19 @@ class Markdown {
|
||||
$inList = false;
|
||||
}
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
|
||||
$level = strlen($m[1]);
|
||||
$this->html[] = "<h{$level}>".$this->parseInline($m[2])."</h{$level}>";
|
||||
$this->html[] = " <h{$level}>".$this->parseInline($m[2])."</h{$level}>";
|
||||
continue;
|
||||
}
|
||||
|
||||
// 箇条書きリスト
|
||||
if (preg_match('/^(\s*)([\*\-])\s(.+)/', $line, $m)) {
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
$inList = true;
|
||||
@@ -221,7 +221,7 @@ class Markdown {
|
||||
// 番号付きリスト
|
||||
if (preg_match('/^(\s*)\d+\.\s(.+)/', $line, $m)) {
|
||||
if (!empty($currentParagraph)) {
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
$currentParagraph = [];
|
||||
}
|
||||
$inList = true;
|
||||
@@ -246,7 +246,7 @@ class Markdown {
|
||||
$parsedLine = $this->parseInline($line);
|
||||
$currentParagraph[] = $parsedLine;
|
||||
if ($hasBR) {
|
||||
$currentParagraph[] = "<br />";
|
||||
$currentParagraph[] = "<br />\n ";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ class Markdown {
|
||||
if ($inBlockquote) $this->html[] = $this->createBlockquote($blockquoteContent);
|
||||
if ($inTable) $this->html[] = $this->createTable($tableHeaders, $tableRows);
|
||||
if (!empty($currentParagraph))
|
||||
$this->html[] = " <p>".implode("", $currentParagraph)."</p>";
|
||||
$this->html[] = " <p>\n ".implode("", $currentParagraph)."\n </p>";
|
||||
|
||||
return implode("\n", $this->html);
|
||||
}
|
||||
@@ -456,11 +456,11 @@ class Markdown {
|
||||
$currentType = $item['type'];
|
||||
}
|
||||
|
||||
$html .= "<{$currentType}>\n";
|
||||
$html .= " <p>\n <{$currentType}>\n";
|
||||
foreach ($items as $item) {
|
||||
$html .= str_repeat(' ', $currentLevel)." <li>".$item['content']."</li>\n";
|
||||
$html .= str_repeat(' ', $currentLevel)." <li>".$item['content']."</li>\n";
|
||||
}
|
||||
$html .= "</{$currentType}>";
|
||||
$html .= " </{$currentType}>\n </p>";
|
||||
|
||||
while (!empty($listStack)) {
|
||||
$html .= str_repeat(' ', $currentLevel)."</".array_pop($listStack).">\n";
|
||||
@@ -478,7 +478,7 @@ class Markdown {
|
||||
private function createCodeBlock(): string {
|
||||
$code = htmlspecialchars(implode("\n", $this->codeBlockContent));
|
||||
$class = $this->codeBlockLanguage ? " class=\"language-{$this->codeBlockLanguage}\"" : '';
|
||||
return "<pre><code{$class}>{$code}</code></pre>";
|
||||
return " <p>\n <pre><code{$class}>{$code}</code></pre>";
|
||||
// $raw = implode("\n", $this->codeBlockContent);
|
||||
// $lang = $this->codeBlockLanguage ?: 'txt';
|
||||
// $class = $lang === 'txt' ? '' : " class=\"language-{$lang}\"";
|
||||
@@ -487,7 +487,7 @@ class Markdown {
|
||||
}
|
||||
|
||||
private function createBlockquote(array $content): string {
|
||||
return "<blockquote>\n <p>".implode("</p>\n <p>", $content)."</p>\n</blockquote>";
|
||||
return " <p>\n <blockquote>\n ".implode("<br />\n ", $content)."\n </blockquote>\n </p>";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -498,31 +498,31 @@ class Markdown {
|
||||
* @return string HTMLのテーブル
|
||||
*/
|
||||
private function createTable(array $headers, array $rows): string {
|
||||
$html = "<table>\n";
|
||||
$html = " <p>\n <table>\n";
|
||||
|
||||
// ヘッダーを追加
|
||||
if (!empty($headers)) {
|
||||
$html .= " <thead>\n <tr>\n";
|
||||
$html .= " <thead>\n <tr>\n";
|
||||
foreach ($headers as $header) {
|
||||
$html .= " <th>".$this->parseInline($header)."</th>\n";
|
||||
$html .= " <th>".$this->parseInline($header)."</th>\n";
|
||||
}
|
||||
$html .= " </tr>\n </thead>\n";
|
||||
$html .= " </tr>\n </thead>\n";
|
||||
}
|
||||
|
||||
// 行を追加
|
||||
if (!empty($rows)) {
|
||||
$html .= " <tbody>\n";
|
||||
$html .= " <tbody>\n";
|
||||
foreach ($rows as $row) {
|
||||
$html .= " <tr>\n";
|
||||
$html .= " <tr>\n";
|
||||
foreach ($row as $cell) {
|
||||
$html .= " <td>".$this->parseInline($cell)."</td>\n";
|
||||
$html .= " <td>".$this->parseInline($cell)."</td>\n";
|
||||
}
|
||||
$html .= " </tr>\n";
|
||||
$html .= " </tr>\n";
|
||||
}
|
||||
$html .= " </tbody>\n";
|
||||
$html .= " </tbody>\n";
|
||||
}
|
||||
|
||||
$html .= "</table>";
|
||||
$html .= " </table>\n </p>";
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ class Mysql {
|
||||
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
||||
if ($this->socket === false) {
|
||||
$msg = 'ソケットの作成に失敗: '.socket_strerror(socket_last_error());
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ class Mysql {
|
||||
if ($res === false) {
|
||||
$msg = 'ソケットに接続に失敗: '
|
||||
.socket_strerror(socket_last_error($this->socket));
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ class Mysql {
|
||||
$code = unpack('v', substr($response, 1, 2))[1];
|
||||
$mes = substr($response, 3);
|
||||
$msg = "認証応答に失敗: {$code} - {$mes}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ class Mysql {
|
||||
$code = unpack('v', substr($res, 1, 2))[1];
|
||||
$mes = substr($res, 3);
|
||||
$msg = "データベースの選択に失敗: {$code} - {$mes}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -179,7 +179,7 @@ class Mysql {
|
||||
$code = unpack('v', substr($res, 1, 2))[1];
|
||||
$mes = substr($res, 3);
|
||||
$msg = "準備に失敗: {$code} - {$mes}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -236,14 +236,14 @@ class Mysql {
|
||||
|
||||
if (!isset($this->prepared[$statementId])) {
|
||||
$msg = "不正なステートメントID: {$statementId}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
$stmtInfo = $this->prepared[$statementId];
|
||||
if (count($params) != $stmtInfo['num_params']) {
|
||||
$msg = "パラメータ数が一致しません: 期待 {$stmtInfo['num_params']}, 取得 ".count($params);
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ class Mysql {
|
||||
$code = unpack('v', substr($res, 1, 2))[1];
|
||||
$mes = substr($res, 3);
|
||||
$msg = "実行に失敗: {$code} - {$mes}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -389,7 +389,7 @@ class Mysql {
|
||||
$code = unpack('v', substr($res, 1, 2))[1];
|
||||
$mes = substr($res, 3);
|
||||
$msg = "クエリに失敗: {$code} - {$mes}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -402,7 +402,7 @@ class Mysql {
|
||||
// @todo LOCAL INFILEリクエストの処理を実装
|
||||
if (ord($res[0]) === 0xFB) {
|
||||
$msg = "LOCAL INFOリクエストは未対応です";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -490,7 +490,7 @@ class Mysql {
|
||||
$mes = substr($res, 3);
|
||||
$this->close();
|
||||
$msg = "認証に失敗: {$code} - {$mes}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -518,7 +518,7 @@ class Mysql {
|
||||
$eofPacket = $this->readPacket();
|
||||
if (ord($eofPacket[0]) !== 0xFE) {
|
||||
$msg = "フィールド説明の後にEOFパケットが期待されます";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -664,7 +664,7 @@ class Mysql {
|
||||
private function parseOkPacket(string $packet): array {
|
||||
if (strlen($packet) < 2) {
|
||||
$msg = "OKパケットが短すぎます: ".strlen($packet)."バイト";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -678,7 +678,7 @@ class Mysql {
|
||||
|
||||
if (strlen($packet) < $pos + 2) {
|
||||
$msg = "OKパケットにサーバーステータス用のデータが不足しています";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
$serverStatus = unpack('v', substr($packet, $pos, 2))[1];
|
||||
@@ -686,7 +686,7 @@ class Mysql {
|
||||
|
||||
if (strlen($packet) < $pos + 2) {
|
||||
$msg = "OKパケットに警告カウント用のデータが不足しています";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
$warningCount = unpack('v', substr($packet, $pos, 2))[1];
|
||||
@@ -712,7 +712,7 @@ class Mysql {
|
||||
$bytesRead = socket_recv($this->socket, $header, 4, MSG_WAITALL);
|
||||
if ($bytesRead !== 4) {
|
||||
$msg = "パケットヘッダーの読み込みに失敗: 期待 4 バイト, 取得 {$bytesRead}";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -738,7 +738,7 @@ class Mysql {
|
||||
if ($bytesRead === false) {
|
||||
$msg = "パケット内容の読み込みに失敗: エラー "
|
||||
.socket_strerror(socket_last_error($this->socket));
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -811,7 +811,7 @@ class Mysql {
|
||||
$sent = socket_write($this->socket, $header, 4);
|
||||
if ($sent !== 4) {
|
||||
$msg = "パケットヘッダーの送信に失敗";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
@@ -819,7 +819,7 @@ class Mysql {
|
||||
$sent = socket_write($this->socket, $data, $len);
|
||||
if ($sent !== $len) {
|
||||
$msg = "パケットデータの送信に失敗";
|
||||
logger(\LogType::MySQL, $msg);
|
||||
\logger(\LogType::MySQL, $msg);
|
||||
throw new \Exception($msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ class Tester {
|
||||
private bool $colorOutput = true;
|
||||
private bool $verboseOutput = true;
|
||||
private bool $stopOnFailure = false;
|
||||
private bool $skipCurrentDescribe = false;
|
||||
private array $beforeEachCallbacks = [];
|
||||
private array $afterEachCallbacks = [];
|
||||
private array $beforeAllCallbacks = [];
|
||||
@@ -119,6 +120,7 @@ class Tester {
|
||||
*/
|
||||
public function describe(string $description, callable $callback): Tester {
|
||||
$this->currentTestCase = $description;
|
||||
$this->skipCurrentDescribe = false;
|
||||
$this->output($this->colorize('bold', "テストケース: {$description}"));
|
||||
|
||||
try {
|
||||
@@ -149,6 +151,11 @@ class Tester {
|
||||
* @return Tester このインスタンス
|
||||
*/
|
||||
public function it(string $description, callable $callback): Tester {
|
||||
if ($this->skipCurrentDescribe) {
|
||||
$this->skip($description, 'skipAll() が呼び出されました');
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->currentTest = $description;
|
||||
$this->testCount++;
|
||||
|
||||
@@ -435,7 +442,42 @@ class Tester {
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a summary of the test results
|
||||
* コレクションが指定された要素数を持つ事
|
||||
*
|
||||
* @param int $expoected 期待される要素数
|
||||
* @param iterable $collection 配列または Traversable
|
||||
* @param string|null $message 失敗時のカスタムメッセージ
|
||||
* @return Tester
|
||||
*
|
||||
* @throws AssertionFailedException
|
||||
*/
|
||||
public function assertCount(int $expected, iterable $collection, ?string $message = null): Tester {
|
||||
$actual = is_countable($collection) ? count($collection) : iterator_count($collection);
|
||||
|
||||
if ($actual !== $expected) {
|
||||
$message ??= "要素数が {$expected} であることを期待しましたが、{$actual} でした";
|
||||
throw new AssertionFailedException($message);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 現在の describe ブロック内の残りすべての it() をスキップ
|
||||
*
|
||||
* @param string $reason スキップ理由(出力に表示)
|
||||
* @return void
|
||||
*/
|
||||
public function skipAll(string $reason = 'このテストケースはスキップされました'): void {
|
||||
$this->skipCurrentDescribe = true;
|
||||
|
||||
if ($this->verboseOutput) {
|
||||
$this->output($this->colorize('cyan', " スキップAll: {$reason}"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* テスト結果の詳細を表示する
|
||||
*
|
||||
* @return Tester
|
||||
*/
|
||||
|
||||
@@ -61,4 +61,6 @@ $test->describe('プリペアドステートメント', function($test): void {
|
||||
} catch (\Exception $e) {
|
||||
echo 'エラー: '.$e->getMessage()."\n";
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$test->printSummary();
|
||||
Reference in New Issue
Block a user