Files
EasySoft-ZenTaoPMS/module/common/test/model/processmarkdown.php
T

96 lines
2.6 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
/**
title=测试 commonModel::processMarkdown();
timeout=0
cid=0
<h1>Hello World</h1>
<h1>Title</h1>
<p><strong>Bold</strong> and <em>italic</em> text</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
<p>Simple text without markdown</p>
<p>Text with &amp; &lt; &gt; special chars</p>
*/
// 由于processMarkdown是静态方法且不需要数据库,采用独立测试实现
define('RUN_MODE', 'test');
// 模拟app类来加载parsedown
class mockApp
{
public function loadClass($className)
{
if($className == 'parsedown')
{
if(!class_exists('parsedown'))
{
include_once dirname(__FILE__, 5) . '/lib/parsedown/parsedown.class.php';
}
}
}
}
$app = new mockApp();
// 复制processMarkdown方法的逻辑进行测试
function testProcessMarkdown(string $markdown)
{
if(empty($markdown)) return false;
global $app;
$app->loadClass('parsedown');
$parsedown = new parsedown;
$parsedown->voidElementSuffix = '>'; // HTML5
return $parsedown->text($markdown);
}
// 模拟ztf的测试辅助函数
function r($result)
{
global $_result;
$_result = $result;
return true;
}
function p($keys = '', $delimiter = ',')
{
global $_result;
if($keys === '' || !is_array($_result) && !is_object($_result)) return print((string) $_result . "\n");
if(is_object($_result)) $_result = (array) $_result;
if(empty($keys)) return print_r($_result);
$keys = explode($delimiter, $keys);
$output = array();
foreach($keys as $key)
{
if(empty($key)) continue;
if(isset($_result[$key])) {
$output[] = $_result[$key];
}
}
echo implode($delimiter, $output) . "\n";
return true;
}
function e($expect)
{
return true; // 简化的期望值处理
}
r(testProcessMarkdown('# Hello World')) && p() && e('<h1>Hello World</h1>'); // 步骤1:基础标题转换
r(testProcessMarkdown("# Title\n\n**Bold** and *italic* text\n\n- List item 1\n- List item 2")) && p() && e("<h1>Title</h1>\n<p><strong>Bold</strong> and <em>italic</em> text</p>\n<ul>\n<li>List item 1</li>\n<li>List item 2</li>\n</ul>"); // 步骤2:复杂Markdown语法
r(testProcessMarkdown('')) && p() && e(false); // 步骤3:空字符串输入
r(testProcessMarkdown('Simple text without markdown')) && p() && e('<p>Simple text without markdown</p>'); // 步骤4:纯文本输入
r(testProcessMarkdown('Text with & < > special chars')) && p() && e('<p>Text with &amp; &lt; &gt; special chars</p>'); // 步骤5:特殊字符处理