Files
yourls-ui/tests/clipboardurl_test.cpp
T
2026-10-01 11:53:34 -04:00

53 lines
2.3 KiB
C++

#include "../clipboardurl.h"
#include <QCoreApplication>
#include <QDebug>
int main (int argc, char **argv) {
QCoreApplication application (argc, argv);
const QUrl expected (QStringLiteral ("https://git.himro.ca/explore/repos"));
int failures = 0;
const auto check = [&failures] (bool condition, const char *description) {
if (!condition) {
qCritical() << description;
++failures;
}
};
for (const auto &text : {
"https://git.himro.ca/explore/repos",
" \nhttps://git.himro.ca/explore/repos\n ",
"'https://git.himro.ca/explore/repos'",
"\"https://git.himro.ca/explore/repos\"",
"<https://git.himro.ca/explore/repos>",
"[https://git.himro.ca/explore/repos](https://git.himro.ca/explore/repos)",
"'[Repositories](https://git.himro.ca/explore/repos)'"}) {
check (ClipboardUrl::parseText (QString::fromUtf8 (text)) == expected, text);
}
check (ClipboardUrl::parseText (QStringLiteral (
"[Other label](https://git.himro.ca/explore/repos?filter=a%26b#section)"))
== QUrl (QStringLiteral ("https://git.himro.ca/explore/repos?filter=a%26b#section")),
"Markdown must use the destination and preserve query and fragment");
check (ClipboardUrl::parseText (QStringLiteral ("[Link](https://example.com/a_(b))"))
== QUrl (QStringLiteral ("https://example.com/a_(b)")),
"Parentheses in URL paths must be preserved");
for (const auto &text : {"", "not a URL", "https://", "[Link](https://)"})
check (!ClipboardUrl::parseText (QString::fromUtf8 (text)).isValid(), text);
QMimeData mimeData;
mimeData.setText (QStringLiteral ("Repositories"));
mimeData.setUrls ({expected});
check (ClipboardUrl::fromMimeData (&mimeData) == expected,
"Browser URL MIME data must take precedence over a link label");
mimeData.clear();
mimeData.setText (expected.toString());
check (ClipboardUrl::fromMimeData (&mimeData) == expected, "Plain text fallback");
mimeData.setUrls ({QUrl (QStringLiteral ("file:///tmp/example"))});
check (ClipboardUrl::fromMimeData (&mimeData) == expected, "Unusable MIME URL fallback");
mimeData.clear();
check (!ClipboardUrl::fromMimeData (&mimeData).isValid(), "Empty clipboard");
check (!ClipboardUrl::fromMimeData (nullptr).isValid(), "Unavailable clipboard");
return failures == 0 ? 0 : 1;
}