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

247 lines
10 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-05-21 09:04:10 +02:00
const auto listTag = QStringLiteral("<li style=\"list-style-type:square\">");
const QString &t1 = "<li> [ ] task 1</li>";
const QString &r1 = parseTaskList(t1, true);
QString expec = listTag +
" <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);
2020-05-21 09:04:10 +02:00
const QString &t2 = "<li> [x] task 2</li>";
const QString &r2 = parseTaskList(t2, true);
expec = listTag +
" <a class=\"task-list-item-checkbox\" "
2020-02-10 18:23:25 +01:00
"href=\"checkbox://_0\">&#9745;</a> task 2</li>";
2020-01-27 13:45:07 +05:00
QVERIFY(r2 == expec);
2020-05-21 09:04:10 +02:00
const QString &t3 = "<li> [x] task 3</li>";
const QString &r3 = parseTaskList(t3, false);
expec = listTag + " &#9745; task 3</li>";
2020-01-27 13:45:07 +05:00
QVERIFY(r3 == expec);
2020-05-21 09:04:10 +02:00
const QString &t4 = "<li> [ ] task 4</li>";
const QString &r4 = parseTaskList(t4, false);
expec = listTag + " &#9744; task 4</li>";
2020-01-27 13:45:07 +05:00
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);
}
void TestUtilsMisc::testTransformEvernoteImportCodeBlock() {
QString content = R"(<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export2.dtd">
<en-export export-date="20181021T174838Z" application="Evernote/Windows" version="6.x">
<note><title>test</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note><div>This is a test note with some code blocks</div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>&lt;?php</div><div><br/></div><div>phpinfo();</div><div><br/></div><div>?&gt;</div></div><div><br/></div></en-note>]]></content><created>20181021T111619Z</created><updated>20181021T111738Z</updated><note-attributes><author>myemail@example.com</author><source>desktop.win</source><source-application>evernote.win32</source-application></note-attributes></note></en-export>
)";
QCOMPARE(testEvernoteImportText(content), R"(This is a test note with some code blocks
```
<?php
phpinfo();
?>
```)");
}
2021-09-27 21:51:14 +02:00
void TestUtilsMisc::testTransformEvernoteImportCodeBlock2() {
QString content = R"(<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export2.dtd">
<en-export export-date="20181029T161628Z" application="Evernote/Windows" version="6.x">
<note><title>File manager</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note><div><br/></div><div style="-en-codeblock: true; box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;"><div>&nbsp; &nbsp; /**</div><div>&nbsp; &nbsp; &nbsp;* Gets the configuration path depending on the operation system</div><div>&nbsp; &nbsp; &nbsp;*</div><div>&nbsp; &nbsp; &nbsp;* @return bool|string</div><div>&nbsp; &nbsp; &nbsp;*/</div><div>&nbsp; &nbsp; private static function getConfigPath()</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $file_manager = new pm_ServerFileManager();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if ($file_manager-&gt;fileExists(&apos;/etc/httpd/conf.d/pagespeed.conf&apos;)) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // CentOS</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &apos;/etc/httpd/conf.d/pagespeed.conf&apos;;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; } elseif ($file_manager-&gt;fileExists(&apos;/etc/apache2/mods-available/pagespeed.conf&apos;)) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Ubuntu</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &apos;/etc/apache2/mods-available/pagespeed.conf&apos;;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return false;</div><div>&nbsp; &nbsp; }</div></div><div><br/></div></en-note>]]></content><created>20170703T093015Z</created><updated>20170703T093022Z</updated><note-attributes><author>myemail@domain.com</author><source>desktop.win</source><source-application>evernote.win32</source-application></note-attributes></note></en-export>
)";
QCOMPARE(testEvernoteImportText(content), R"(```
/**
* Gets the configuration path depending on the operation system
*
* @return bool|string
*/
private static function getConfigPath()
{
$file_manager = new pm_ServerFileManager();
if ($file_manager->fileExists('/etc/httpd/conf.d/pagespeed.conf')) {
// CentOS
return '/etc/httpd/conf.d/pagespeed.conf';
} elseif ($file_manager->fileExists('/etc/apache2/mods-available/pagespeed.conf')) {
// Ubuntu
return '/etc/apache2/mods-available/pagespeed.conf';
}
return false;
}
```)");
}
void TestUtilsMisc::testTransformEvernoteImportRussian() {
QString content = R"(<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export2.dtd">
<en-export export-date="20191111T145250Z" application="Evernote/Windows" version="6.x">
<note><title>15-3.1</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>&#1059; &#1084;&#1077;&#1085;&#1103; &#1076;&#1086;&#1084;&#1072; &#1076;&#1074;&#1077; &#1082;&#1086;&#1084;&#1085;&#1072;&#1090;&#1099;, &#1082;&#1091;&#1093;&#1085;&#1103;, &#1090;&#1091;&#1072;&#1083;&#1077;&#1090; &#1080; &#1074;&#1072;&#1085;&#1085;&#1072;.</div></en-note>]]></content><created>20150714T065518Z</created><updated>20150714T073706Z</updated><note-attributes><source>mobile.android</source></note-attributes></note></en-export>
)";
qDebug() << testEvernoteImportText(content);
QCOMPARE(testEvernoteImportText(content), R"(У меня дома две комнаты, кухня, туалет и ванна.)");
}