Files
AyuGramDesktop/Telegram/SourceFiles/tests/test_markdown_iv.cpp
T
2026-05-19 12:49:08 +04:00

633 lines
16 KiB
C++

#include "iv/markdown/iv_markdown_document.h"
#include "iv/markdown/iv_markdown_parse.h"
#include <QtCore/QByteArray>
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QIODevice>
#include <QtCore/QString>
#include <iostream>
#include <utility>
namespace {
using namespace Iv::Markdown;
constexpr auto kValidationSourceLimit = 4 * 1024 * 1024;
constexpr auto kValidationFormulaLimit = 64 * 1024;
struct Args {
QString markdownPath;
QString latexMarkdownPath;
bool dump = false;
bool ok = true;
QString error;
};
[[nodiscard]] QString FromLatin1(const char *value) {
return QString::fromLatin1(value);
}
void PrintStreamLine(std::ostream &stream, const QString &line) {
const auto bytes = line.toUtf8();
stream.write(bytes.constData(), static_cast<std::streamsize>(bytes.size()));
stream << '\n';
}
void PrintLine(const QString &line) {
PrintStreamLine(std::cout, line);
}
void PrintError(const QString &line) {
PrintStreamLine(std::cerr, line);
}
[[nodiscard]] Args ParseArgs(int argc, char **argv) {
auto result = Args();
for (auto i = 1; i != argc; ++i) {
const auto argument = QString::fromLocal8Bit(argv[i]);
if (argument == FromLatin1("--dump")) {
result.dump = true;
} else if (argument == FromLatin1("--markdown")
|| argument == FromLatin1("--latex-md")) {
if (i + 1 == argc) {
result.ok = false;
result.error = FromLatin1("missing value for ") + argument;
return result;
}
const auto path = QString::fromLocal8Bit(argv[++i]);
if (argument == FromLatin1("--markdown")) {
result.markdownPath = path;
} else {
result.latexMarkdownPath = path;
}
} else {
result.ok = false;
result.error = FromLatin1("unknown argument: ") + argument;
return result;
}
}
return result;
}
[[nodiscard]] QString DefaultFixturePath(const QString &name) {
const auto applicationDir = QDir(QCoreApplication::applicationDirPath());
const auto repoFixtureFromApplication = QDir::cleanPath(
applicationDir.filePath(
FromLatin1("../../Telegram/MarkdownMathProbes/fixtures/") + name));
if (QFileInfo::exists(repoFixtureFromApplication)) {
return repoFixtureFromApplication;
}
const auto repoFixtureFromCurrent = QDir::current().filePath(
FromLatin1("Telegram/MarkdownMathProbes/fixtures/") + name);
if (QFileInfo::exists(repoFixtureFromCurrent)) {
return repoFixtureFromCurrent;
}
const auto applicationCandidate = applicationDir.filePath(name);
if (QFileInfo::exists(applicationCandidate)) {
return applicationCandidate;
}
const auto outDebug = QDir::current().filePath(
FromLatin1("out/Debug/") + name);
if (QFileInfo::exists(outDebug)) {
return outDebug;
}
return outDebug;
}
[[nodiscard]] bool ReadFile(const QString &path, QByteArray *bytes) {
if (!bytes) {
return false;
}
auto file = QFile(path);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
*bytes = file.readAll();
return true;
}
[[nodiscard]] int CountNodes(const MarkdownNode &node) {
auto result = 1;
for (const auto &child : node.children) {
result += CountNodes(child);
}
return result;
}
[[nodiscard]] bool HasKind(const MarkdownNode &node, NodeKind kind) {
if (node.kind == kind) {
return true;
}
for (const auto &child : node.children) {
if (HasKind(child, kind)) {
return true;
}
}
return false;
}
[[nodiscard]] bool HasTaskState(const MarkdownNode &node, TaskState state) {
if (node.taskState == state) {
return true;
}
for (const auto &child : node.children) {
if (HasTaskState(child, state)) {
return true;
}
}
return false;
}
void CollectTables(
const MarkdownNode &node,
std::vector<const MarkdownNode*> *out) {
if (!out) {
return;
}
if (node.kind == NodeKind::Table) {
out->push_back(&node);
}
for (const auto &child : node.children) {
CollectTables(child, out);
}
}
[[nodiscard]] int TableHeaderRowCount(const MarkdownNode &table) {
auto result = 0;
for (const auto &row : table.children) {
if (row.kind == NodeKind::TableRow && row.tableHeader) {
++result;
}
}
return result;
}
[[nodiscard]] bool HasTableHeaderRow(const MarkdownNode &table) {
return TableHeaderRowCount(table) > 0;
}
[[nodiscard]] bool HasSequentialTableColumns(const MarkdownNode &table) {
for (const auto &row : table.children) {
if (row.kind != NodeKind::TableRow) {
return false;
}
auto expectedColumn = 0;
for (const auto &cell : row.children) {
if (cell.kind != NodeKind::TableCell
|| cell.tableColumn != expectedColumn) {
return false;
}
++expectedColumn;
}
}
return !table.children.empty();
}
[[nodiscard]] bool HasTableAlignments(
const MarkdownNode &table,
const std::vector<TableAlignment> &expected) {
if (table.tableAlignments.size() != expected.size()) {
return false;
}
for (auto i = 0, count = int(expected.size()); i != count; ++i) {
if (table.tableAlignments[i] != expected[i]) {
return false;
}
}
return true;
}
[[nodiscard]] int TableColumnCount(const MarkdownNode &table) {
auto result = 0;
for (const auto &row : table.children) {
if (row.kind != NodeKind::TableRow) {
return 0;
}
const auto count = int(row.children.size());
if (count > result) {
result = count;
}
}
return result;
}
[[nodiscard]] int CountDisplayMathNodes(const MarkdownNode &node) {
auto result = (node.kind == NodeKind::DisplayMath) ? 1 : 0;
for (const auto &child : node.children) {
result += CountDisplayMathNodes(child);
}
return result;
}
[[nodiscard]] int CountFormulas(
const PreparedDocument &document,
MathKind kind) {
auto result = 0;
for (const auto &formula : document.formulas) {
if (formula.kind == kind) {
++result;
}
}
return result;
}
[[nodiscard]] bool HasFormulaOnLine(
const PreparedDocument &document,
int line,
const QString &tex) {
for (const auto &formula : document.formulas) {
if (formula.range.available
&& formula.range.startLine == line
&& formula.tex == tex) {
return true;
}
}
return false;
}
[[nodiscard]] bool HasFormulaInLineRange(
const PreparedDocument &document,
int firstLine,
int lastLine) {
if (lastLine < firstLine) {
return false;
}
for (const auto &formula : document.formulas) {
if (!formula.range.available) {
continue;
}
if (formula.range.startLine <= lastLine
&& formula.range.endLine >= firstLine) {
return true;
}
}
return false;
}
[[nodiscard]] QString YesNo(bool value) {
return FromLatin1(value ? "yes" : "no");
}
[[nodiscard]] bool HasBothTaskStates(const PreparedDocument &document) {
return HasTaskState(document.document, TaskState::Checked)
&& HasTaskState(document.document, TaskState::Unchecked);
}
[[nodiscard]] bool ExclusionsPass(const PreparedDocument &document) {
return !HasFormulaInLineRange(document, 281, 281)
&& HasFormulaOnLine(document, 285, FromLatin1("5x + 3"))
&& !HasFormulaInLineRange(document, 332, 340);
}
[[nodiscard]] bool HasFormula(
const PreparedDocument &document,
MathKind kind,
const QString &tex) {
for (const auto &formula : document.formulas) {
if (formula.kind == kind && formula.tex == tex) {
return true;
}
}
return false;
}
void AppendSummaryCounts(QString *line, const PreparedDocument &document) {
line->append(FromLatin1(" nodes="));
line->append(QString::number(document.stats.cmarkNodeCount));
line->append(FromLatin1(" converted="));
line->append(QString::number(CountNodes(document.document)));
line->append(FromLatin1(" formulas_inline="));
line->append(QString::number(CountFormulas(document, MathKind::Inline)));
line->append(FromLatin1(" formulas_display="));
line->append(QString::number(CountFormulas(document, MathKind::Display)));
}
void PrintSummary(const PreparedDocument &document, const QString &label) {
auto line = label;
AppendSummaryCounts(&line, document);
line.append(FromLatin1(" tables="));
line.append(YesNo(HasKind(document.document, NodeKind::Table)));
if (label == FromLatin1("markdown-example.md")) {
line.append(FromLatin1(" tasks="));
line.append(YesNo(HasBothTaskStates(document)));
line.append(FromLatin1(" strike="));
line.append(YesNo(HasKind(document.document, NodeKind::Strike)));
} else if (label == FromLatin1("latex-markdown-test.md")) {
line.append(FromLatin1(" exclusions="));
line.append(YesNo(ExclusionsPass(document)));
}
PrintLine(line);
}
[[nodiscard]] bool ParseFixture(
const QString &path,
const QString &label,
PreparedDocument *document) {
auto bytes = QByteArray();
if (!ReadFile(path, &bytes)) {
PrintError(label + FromLatin1(" read-failed: ") + path);
return false;
}
auto parsed = ParseMarkdownForIv(bytes, ParseOptions{ label });
if (!parsed.ok) {
PrintError(label + FromLatin1(" parse-failed: ") + parsed.error);
return false;
}
PrintSummary(parsed.document, label);
if (document) {
*document = std::move(parsed.document);
}
return true;
}
void Check(bool condition, const QString &message, bool *ok) {
if (condition) {
return;
}
if (ok) {
*ok = false;
}
PrintError(FromLatin1("assertion failed: ") + message);
}
void CheckParseSuccess(
const QByteArray &source,
const QString &label,
bool *ok) {
const auto parsed = ParseMarkdownForIv(source, ParseOptions{ label });
Check(
parsed.ok,
label + FromLatin1(" parse failed: ") + parsed.error,
ok);
}
void CheckParseFailure(
const QByteArray &source,
const QString &label,
const QString &expectedError,
bool *ok) {
const auto parsed = ParseMarkdownForIv(source, ParseOptions{ label });
Check(
!parsed.ok,
label + FromLatin1(" should fail"),
ok);
if (!parsed.ok) {
Check(
parsed.error == expectedError,
label + FromLatin1(" error should be ")
+ expectedError
+ FromLatin1(", got ")
+ parsed.error,
ok);
}
}
void CheckValidationEdges(bool *ok) {
auto utf8BomSource = QByteArray::fromHex("EFBBBF");
utf8BomSource.append("# Title\n");
CheckParseSuccess(
utf8BomSource,
FromLatin1("utf8 bom"),
ok);
CheckParseFailure(
QByteArray::fromHex("FFFE2300"),
FromLatin1("utf16 bom"),
FromLatin1("source-unsupported-bom"),
ok);
CheckParseFailure(
QByteArray("a\0b", 3),
FromLatin1("nul byte"),
FromLatin1("source-binary"),
ok);
CheckParseFailure(
QByteArray::fromHex("C328"),
FromLatin1("invalid utf8"),
FromLatin1("source-invalid-utf8"),
ok);
const auto oversizedSource = QByteArray(kValidationSourceLimit + 1, 'a');
CheckParseFailure(
oversizedSource,
FromLatin1("source size"),
FromLatin1("source-too-large"),
ok);
auto oversizedFormula = QByteArray();
oversizedFormula.reserve(kValidationFormulaLimit + 2);
oversizedFormula.append('$');
oversizedFormula.append(QByteArray(kValidationFormulaLimit + 1, '+'));
oversizedFormula.append('$');
CheckParseFailure(
oversizedFormula,
FromLatin1("formula size"),
FromLatin1("formula-too-large"),
ok);
const auto generated = QByteArray(
"Inline code `$code$`.\n"
"```\n"
"$block$\n"
"```\n"
"Escaped \\$ and price $5.99$.\n"
"Real $x + y$ done.\n");
const auto parsed = ParseMarkdownForIv(
generated,
ParseOptions{ FromLatin1("generated-edge-checks.md") });
Check(
parsed.ok,
FromLatin1("generated exclusions parse failed: ") + parsed.error,
ok);
if (parsed.ok) {
Check(
static_cast<int>(parsed.document.formulas.size()) == 1,
FromLatin1("generated exclusions formula count"),
ok);
Check(
CountFormulas(parsed.document, MathKind::Inline) == 1,
FromLatin1("generated exclusions inline formula count"),
ok);
Check(
CountFormulas(parsed.document, MathKind::Display) == 0,
FromLatin1("generated exclusions display formula count"),
ok);
Check(
HasFormula(parsed.document, MathKind::Inline, FromLatin1("x + y")),
FromLatin1("generated exclusions real formula"),
ok);
}
}
} // namespace
int main(int argc, char **argv) {
auto application = QCoreApplication(argc, argv);
(void)application;
auto args = ParseArgs(argc, argv);
if (!args.ok) {
PrintError(args.error);
return 1;
}
if (args.markdownPath.isEmpty()) {
args.markdownPath = DefaultFixturePath(FromLatin1("markdown-example.md"));
}
if (args.latexMarkdownPath.isEmpty()) {
args.latexMarkdownPath = DefaultFixturePath(
FromLatin1("latex-markdown-test.md"));
}
auto markdown = PreparedDocument();
if (!ParseFixture(
args.markdownPath,
FromLatin1("markdown-example.md"),
&markdown)) {
return 1;
}
if (args.dump) {
PrintLine(DumpForDebug(markdown));
}
auto latex = PreparedDocument();
if (!ParseFixture(
args.latexMarkdownPath,
FromLatin1("latex-markdown-test.md"),
&latex)) {
return 1;
}
if (args.dump) {
PrintLine(DumpForDebug(latex));
}
auto ok = true;
Check(
markdown.stats.cmarkNodeCount == 562,
FromLatin1("markdown-example.md cmark node count"),
&ok);
Check(
CountFormulas(markdown, MathKind::Inline) == 1,
FromLatin1("markdown-example.md inline formula count"),
&ok);
Check(
CountFormulas(markdown, MathKind::Display) == 1,
FromLatin1("markdown-example.md display formula count"),
&ok);
Check(
CountDisplayMathNodes(markdown.document) == 1,
FromLatin1("markdown-example.md display math node count"),
&ok);
Check(
HasKind(markdown.document, NodeKind::Table),
FromLatin1("markdown-example.md table coverage"),
&ok);
Check(
HasKind(markdown.document, NodeKind::Strike),
FromLatin1("markdown-example.md strikethrough coverage"),
&ok);
Check(
HasTaskState(markdown.document, TaskState::Checked),
FromLatin1("markdown-example.md checked task"),
&ok);
Check(
HasTaskState(markdown.document, TaskState::Unchecked),
FromLatin1("markdown-example.md unchecked task"),
&ok);
auto markdownTables = std::vector<const MarkdownNode*>();
CollectTables(markdown.document, &markdownTables);
Check(
int(markdownTables.size()) >= 2,
FromLatin1("markdown-example.md table count"),
&ok);
if (markdownTables.size() >= 2) {
const auto &firstTable = *markdownTables[0];
const auto &secondTable = *markdownTables[1];
Check(
HasTableHeaderRow(firstTable),
FromLatin1("markdown-example.md first table header row"),
&ok);
Check(
TableHeaderRowCount(firstTable) == 1,
FromLatin1("markdown-example.md first table header row count"),
&ok);
Check(
TableColumnCount(firstTable) == 3,
FromLatin1("markdown-example.md first table column count"),
&ok);
Check(
HasSequentialTableColumns(firstTable),
FromLatin1("markdown-example.md first table column order"),
&ok);
Check(
HasTableAlignments(
secondTable,
{
TableAlignment::Left,
TableAlignment::Center,
TableAlignment::Right,
}),
FromLatin1("markdown-example.md second table alignments"),
&ok);
}
Check(
latex.stats.cmarkNodeCount == 532,
FromLatin1("latex-markdown-test.md cmark node count"),
&ok);
Check(
CountFormulas(latex, MathKind::Inline) == 99,
FromLatin1("latex-markdown-test.md inline formula count"),
&ok);
Check(
CountFormulas(latex, MathKind::Display) == 31,
FromLatin1("latex-markdown-test.md display formula count"),
&ok);
Check(
CountDisplayMathNodes(latex.document) == 31,
FromLatin1("latex-markdown-test.md display math node count"),
&ok);
Check(
HasKind(latex.document, NodeKind::Table),
FromLatin1("latex-markdown-test.md table coverage"),
&ok);
auto latexTables = std::vector<const MarkdownNode*>();
CollectTables(latex.document, &latexTables);
Check(
!latexTables.empty(),
FromLatin1("latex-markdown-test.md table count"),
&ok);
if (!latexTables.empty()) {
const auto &table = *latexTables[0];
Check(
HasTableHeaderRow(table),
FromLatin1("latex-markdown-test.md table header row"),
&ok);
Check(
TableColumnCount(table) == 3,
FromLatin1("latex-markdown-test.md table column count"),
&ok);
Check(
HasSequentialTableColumns(table),
FromLatin1("latex-markdown-test.md table column order"),
&ok);
}
Check(
!HasFormulaInLineRange(latex, 281, 281),
FromLatin1("latex-markdown-test.md line 281 exclusion"),
&ok);
Check(
HasFormulaOnLine(latex, 285, FromLatin1("5x + 3")),
FromLatin1("latex-markdown-test.md line 285 formula"),
&ok);
Check(
!HasFormulaInLineRange(latex, 332, 340),
FromLatin1("latex-markdown-test.md lines 332-340 exclusions"),
&ok);
CheckValidationEdges(&ok);
return ok ? 0 : 1;
}