kdenote/lib/tests/test_frontmatter.cpp

149 lines
4.6 KiB
C++

#include <QObject>
#include <QTest>
#include "denote_frontmatter.h"
class TestFrontMatter : public QObject
{
Q_OBJECT
private Q_SLOTS:
void parseYaml()
{
QByteArray content =
"---\n"
"title: My Test Note\n"
"tags: [journal, daily]\n"
"identifier: 20240315T120000\n"
"signature: 1a\n"
"---\n"
"Body content here.\n";
auto fm = Denote::parseFrontMatter(content, Denote::FileType::MdYaml);
QCOMPARE(fm.title, QStringLiteral("My Test Note"));
QCOMPARE(fm.tags, QStringList({QStringLiteral("journal"), QStringLiteral("daily")}));
QCOMPARE(fm.identifier, QStringLiteral("20240315T120000"));
QCOMPARE(fm.signature, QStringLiteral("1a"));
}
void parseYamlListTags()
{
QByteArray content =
"---\n"
"title: Note\n"
"tags:\n"
" - alpha\n"
" - beta\n"
"identifier: 20240101T090000\n"
"---\n";
auto fm = Denote::parseFrontMatter(content, Denote::FileType::MdYaml);
QCOMPARE(fm.title, QStringLiteral("Note"));
QCOMPARE(fm.tags, QStringList({QStringLiteral("alpha"), QStringLiteral("beta")}));
}
void parseOrg()
{
QByteArray content =
"#+title: Org Note Title\n"
"#+filetags: :emacs:org:\n"
"#+identifier: 20240315T120000\n"
"#+signature: 2b\n"
"\n"
"* Heading\n"
"Body text.\n";
auto fm = Denote::parseFrontMatter(content, Denote::FileType::Org);
QCOMPARE(fm.title, QStringLiteral("Org Note Title"));
QCOMPARE(fm.tags, QStringList({QStringLiteral("emacs"), QStringLiteral("org")}));
QCOMPARE(fm.identifier, QStringLiteral("20240315T120000"));
QCOMPARE(fm.signature, QStringLiteral("2b"));
}
void parseTxt()
{
QByteArray content =
"title: Plain Text Note\n"
"tags: notes, plain\n"
"identifier: 20240315T120000\n"
"--\n"
"The body.\n";
auto fm = Denote::parseFrontMatter(content, Denote::FileType::Txt);
QCOMPARE(fm.title, QStringLiteral("Plain Text Note"));
QCOMPARE(fm.tags, QStringList({QStringLiteral("notes"), QStringLiteral("plain")}));
QCOMPARE(fm.identifier, QStringLiteral("20240315T120000"));
}
void marshalYaml()
{
Denote::FrontMatter fm;
fm.title = QStringLiteral("Test");
fm.tags = {QStringLiteral("a"), QStringLiteral("b")};
fm.identifier = QStringLiteral("20240101T000000");
QByteArray result = Denote::marshalFrontMatter(fm, Denote::FileType::MdYaml);
QVERIFY(result.contains("title: Test"));
QVERIFY(result.contains("tags: [a, b]"));
QVERIFY(result.contains("identifier: 20240101T000000"));
QVERIFY(result.startsWith("---\n"));
QVERIFY(result.endsWith("---\n"));
}
void marshalOrg()
{
Denote::FrontMatter fm;
fm.title = QStringLiteral("Org Test");
fm.tags = {QStringLiteral("x")};
fm.identifier = QStringLiteral("20240101T000000");
QByteArray result = Denote::marshalFrontMatter(fm, Denote::FileType::Org);
QVERIFY(result.contains("#+title: Org Test"));
QVERIFY(result.contains("#+filetags: :x:"));
QVERIFY(result.contains("#+identifier: 20240101T000000"));
}
void extractBodyYaml()
{
QByteArray content =
"---\n"
"title: X\n"
"---\n"
"Body here.\n";
QString body = Denote::extractBody(content, Denote::FileType::MdYaml);
QCOMPARE(body.trimmed(), QStringLiteral("Body here."));
}
void extractBodyOrg()
{
QByteArray content =
"#+title: X\n"
"#+filetags: :a:\n"
"\n"
"* Heading\n";
QString body = Denote::extractBody(content, Denote::FileType::Org);
QVERIFY(body.contains(QStringLiteral("Heading")));
}
void fileTypeDetection()
{
QCOMPARE(Denote::fileTypeFromExtension(QStringLiteral(".md")), Denote::FileType::MdYaml);
QCOMPARE(Denote::fileTypeFromExtension(QStringLiteral(".org")), Denote::FileType::Org);
QCOMPARE(Denote::fileTypeFromExtension(QStringLiteral(".txt")), Denote::FileType::Txt);
QCOMPARE(Denote::fileTypeFromExtension(QStringLiteral(".pdf")), Denote::FileType::Unknown);
QVERIFY(Denote::supportsFrontMatter(QStringLiteral(".md")));
QVERIFY(!Denote::supportsFrontMatter(QStringLiteral(".png")));
}
};
QTEST_MAIN(TestFrontMatter)
#include "test_frontmatter.moc"