QOwnNotes/tests/unit_tests/testcases/app/test_utilsmisc.cpp

166 lines
5 KiB
C++
Raw Normal View History

2020-01-27 13:45:07 +05:00
#include "test_utilsmisc.h"
#include <QTest>
#include "utils/misc.h"
2020-01-27 13:45:07 +05:00
using namespace Utils::Misc;
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::initTestCase() {}
2020-01-27 13:45:07 +05:00
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testRemoveIfStartsWith() {
2020-01-27 13:45:07 +05:00
QString s = QStringLiteral("--*--testString");
QString result = removeIfStartsWith(s, QStringLiteral("--*--"));
QString expected = QStringLiteral("testString");
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testRemoveIfEndsWith() {
2020-01-27 13:45:07 +05:00
QString s = QStringLiteral("testString--*--");
QString result = removeIfEndsWith(s, QStringLiteral("--*--"));
QString expected = QStringLiteral("testString");
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testPrependIfDoesNotStartWith() {
2020-01-27 13:45:07 +05:00
QString s = QStringLiteral("testString");
QString result = prependIfDoesNotStartWith(s, QStringLiteral("--*--"));
QString expected = QStringLiteral("--*--testString");
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testAppendIfDoesNotEndWith() {
2020-01-27 13:45:07 +05:00
QString s = QStringLiteral("testString");
QString result = appendIfDoesNotEndWith(s, QStringLiteral("--*--"));
QString expected = QStringLiteral("testString--*--");
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testStartDetachedProcess() {
2020-01-27 13:45:07 +05:00
#ifdef Q_OS_UNIX
auto result = startDetachedProcess("pwd", {});
QVERIFY2(result == true, "Failed to start detached process");
#else
QSKIP("This test is skipped on non unix OS");
#endif
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testShorten() {
2020-01-27 13:45:07 +05:00
QString s = QStringLiteral("A Long test string with lots of words");
const auto result = shorten(s, 10);
QString expected = QStringLiteral("A Long ...");
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testCycleTextCase() {
// test empty string
2020-01-27 13:45:07 +05:00
QString s = "";
QString result = cycleTextCase(s);
QString expected = "";
QVERIFY(result == expected);
2020-02-10 18:23:25 +01:00
// lower
2020-01-27 13:45:07 +05:00
s = "lower case sentence";
result = cycleTextCase(s);
expected = "LOWER CASE SENTENCE";
QVERIFY(result == expected);
2020-02-10 18:23:25 +01:00
// upper
2020-01-27 13:45:07 +05:00
s = "LOWER CASE SENTENCE";
result = cycleTextCase(s);
expected = "Lower Case Sentence";
QVERIFY(result == expected);
2020-02-10 18:23:25 +01:00
// start case
2020-01-27 13:45:07 +05:00
s = "Lower Case Sentence";
result = cycleTextCase(s);
expected = "Lower case sentence";
QVERIFY(result == expected);
2020-02-10 18:23:25 +01:00
// sentence case
2020-01-27 13:45:07 +05:00
s = "Lower case sentence";
result = cycleTextCase(s);
expected = "lower case sentence";
QVERIFY(result == expected);
2020-02-10 18:23:25 +01:00
// random 1
2020-01-27 13:45:07 +05:00
s = "LOWER CASE Sentence";
result = cycleTextCase(s);
expected = "lower case sentence";
QVERIFY(result == expected);
2020-02-10 18:23:25 +01:00
// random 2
2020-01-27 13:45:07 +05:00
s = "lower CASE Sentence";
result = cycleTextCase(s);
expected = "lower case sentence";
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testHtmlToMarkdown() {
2020-01-27 13:45:07 +05:00
QString html = "<head><meta charset=\"utf-8\"></head>";
html += "<script>script</script>";
html += "<style>style</style>";
html += "<h1>Heading <em>italic</em></h1>";
html += "<h2>Heading <strong>italic</strong></h2>";
html += "<h3>Heading <em>italic</em></h3>";
html += "<h4>Heading <em>italic</em></h4";
html += "<h5>Heading <em>italic</em></h5>";
html += "<h6>Heading <em>italic</em></h6>";
html += "<code>hello</code>";
html += "<i>hello</i>";
QString result = htmlToMarkdown(html);
2020-02-10 18:23:25 +01:00
QString expected =
"\n# Heading *italic*\n\n## Heading **italic**\n\n### Heading "
"*italic*\n<h4>Heading *italic*</h4\n##### Heading *italic*\n\n###### "
"Heading *italic*\n\n```\nhello\n```\n*hello*";
2020-01-27 13:45:07 +05:00
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testParseTaskList() {
2020-01-27 13:45:07 +05:00
QString t1 = "<li> [ ] task 1</li>";
QString r1 = parseTaskList(t1, true);
2020-02-10 18:23:25 +01:00
QString expec =
2020-05-21 08:57:54 +02:00
"<li style=\"list-style-type:square\"> <a class=\"task-list-item-checkbox\" "
2020-02-10 18:23:25 +01:00
"href=\"checkbox://_0\">&#9744;</a> task 1</li>";
2020-01-27 13:45:07 +05:00
QVERIFY(r1 == expec);
QString t2 = "<li> [x] task 2</li>";
QString r2 = parseTaskList(t2, true);
2020-02-10 18:23:25 +01:00
expec =
"<li> <a class=\"task-list-item-checkbox\" "
"href=\"checkbox://_0\">&#9745;</a> task 2</li>";
2020-01-27 13:45:07 +05:00
QVERIFY(r2 == expec);
QString t3 = "<li> [x] task 3</li>";
QString r3 = parseTaskList(t3, false);
expec = "<li> &#9745; task 3</li>";
QVERIFY(r3 == expec);
QString t4 = "<li> [ ] task 4</li>";
QString r4 = parseTaskList(t4, false);
expec = "<li> &#9744; task 4</li>";
QVERIFY(r4 == expec);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testUnescapeHtml() {
2020-01-27 13:45:07 +05:00
QString html = "<h1>hello</h1>\n<p>hello</p>";
QString result = unescapeHtml(html);
QString expected = "hello\n\n\nhello";
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testHtmlSpecialChars() {
2020-01-27 13:45:07 +05:00
QString str = "& \" \' < >";
QString result = htmlspecialchars(str);
QString expected = "&amp; &quot; &apos; &lt; &gt;";
QVERIFY(result == expected);
}
2020-02-10 18:23:25 +01:00
void TestUtilsMisc::testToHumanReadableByteSize() {
2020-01-27 13:45:07 +05:00
qint64 bytes = 8712492;
QString result = toHumanReadableByteSize(bytes);
QString expected = "8.31 MB";
QVERIFY(result == expected);
}