/* SPDX-FileCopyrightText: 2020 Mladen Milinkovic SPDX-License-Identifier: GPL-2.0-or-later */ #include "richdocumenttest.h" #include "helpers/common.h" #include #include #include using namespace SubtitleComposer; enum Format { Plain = 1, Html = 2, Both = Plain | Html }; Q_DECLARE_METATYPE(Format) void RichDocumentTest::testCursor() { const QString backslashes = $("oXXo"); doc.setHtml(backslashes); QTextCursor c(&doc); QEXPECT_FAIL("", "Already at doc Start", Continue); QVERIFY(c.movePosition(QTextCursor::Start)); QVERIFY(c.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1)); QCOMPARE(c.selectedText(), $("o")); QEXPECT_FAIL("", "Dunno why... some internal anchor == position?", Continue); QVERIFY(c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 2)); c.clearSelection(); QVERIFY(c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 2)); QVERIFY(c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor)); QCOMPARE(c.selectedText(), $("o")); } void RichDocumentTest::testHtml_data() { QTest::addColumn("html"); QTest::addColumn("htmlOut"); QTest::addColumn("textOut"); QTest::newRow("bold styles") << "

It has bold and strong styles.

" << "It has bold and strong styles." << "It has bold and strong styles."; QTest::newRow("italic styles") << "
It has italic and em styles.
" << "It has italic and em styles." << "It has italic and em styles."; QTest::newRow("underline/strikethrough styles") << "It has underline and strike styles." << "It has underline and strike styles." << "It has underline and strike styles."; QTest::newRow("color styles") << "

Try red and black and blue color.

" << "Try red and black and blue color." << "Try red and black and blue color."; QTest::newRow("unicode literal") << "An unicode ♪ char." << "An unicode \u266A char." << "An unicode \u266A char."; QTest::newRow("misc tags, breaks etc") << "

A non breakable space. " "A newline.\n" "A line break.
" "And a

different paragraph

" "and some

unclosed and bad tags." << "A non\u00A0breakable space. " "A newline.
\n" "A line break.
\n" "And a
\ndifferent paragraph
\n" "and some
\nunclosed and bad tags." << "A non breakable space. " "A newline.\n" "A line break.\n" "And a\ndifferent paragraph\n" "and some \nunclosed and bad tags."; } void RichDocumentTest::testHtml() { QFETCH(QString, html); QFETCH(QString, htmlOut); QFETCH(QString, textOut); doc.setHtml(html); QCOMPARE(doc.toHtml(), htmlOut); QCOMPARE(doc.toPlainText(), textOut); doc.setHtml(htmlOut); QCOMPARE(doc.toHtml(), htmlOut); QCOMPARE(doc.toPlainText(), textOut); } void RichDocumentTest::testRegExpReplace_data() { QTest::addColumn("inType"); QTest::addColumn("input"); QTest::addColumn("re"); QTest::addColumn("repType"); QTest::addColumn("replacement"); QTest::addColumn("expected"); QTest::newRow("t1") << Plain << $("xx12xx345xx6xx7xx") << RE$("x+") << Plain << QString() << QString(); QTest::newRow("t2") << Plain << $("xx12xx345xx6xx7xx") << RE$("x+") << Plain << $("y") << QString(); QTest::newRow("t3") << Plain << $("xx12xx345xx6xx7xx") << RE$("((6x)|x+)") << Plain << $("\\2") << QString(); QTest::newRow("t4") << Plain << $("xx12xx345xx6xx7xx") << RE$("((6x)|x+)") << Plain << $("o\\2o") << QString(); QTest::newRow("t5") << Plain << $("xx12xx345xx6xx7xx") << RE$("((\\d)x+)+") << Plain << $("o\\2o") << QString(); QTest::newRow("swaps") << Plain << $("aa12bb345cc6xx7yy") << RE$("([a-z]+)\\d+([a-z]+)") << Plain << $("\\2 swapped \\1") << QString(); // these are failing cause replacer is not very good with replacing tags yet // QTest::newRow("styles1") // << Html << $("amazing super stuff") // << RE$("\\b(\\w+)\\b") // << Plain << $("X\\1X") // << $("XamazingX XsuperX XstuffX"); // QTest::newRow("styles2") // << Html << $("amazing super stuff") // << RE$("\\b\\w+\\b") // << Plain << $("word") // << $("word word word"); } void RichDocumentTest::testRegExpReplace() { QFETCH(Format, inType); QFETCH(QString, input); QFETCH(QRegularExpression, re); QFETCH(Format, repType); QFETCH(QString, replacement); QFETCH(QString, expected); if(inType == Plain) doc.setPlainText(input, true); else doc.setHtml(input, true); if(expected.isEmpty()) expected = QString(input).replace(re, replacement); if(inType == Plain && repType == Plain) { doc.replace(re, replacement, false); QCOMPARE(doc.toPlainText(), expected); } doc.undo(); doc.replace(re, replacement, true); QString docOut = inType == Plain && repType == Plain ? doc.toPlainText() : doc.toHtml(); QCOMPARE(docOut, expected); } void RichDocumentTest::testIndexReplace_data() { QTest::addColumn("inType"); QTest::addColumn("input"); QTest::addColumn("index"); QTest::addColumn("length"); QTest::addColumn("repType"); QTest::addColumn("replacement"); QTest::addColumn("expected"); QTest::newRow("start") << Plain << $("xx12xx345xx6xx7xx") << 0 << 4 << Plain << $("YYY") << $("YYYxx345xx6xx7xx"); QTest::newRow("middle") << Plain << $("xx12xx345xx6xx7xx") << 6 << 3 << Plain << $("YYY") << $("xx12xxYYYxx6xx7xx"); QTest::newRow("end") << Plain << $("xx12xx345xx6xx7xx") << 12 << 5 << Plain << $("YYY") << $("xx12xx345xx6YYY"); } void RichDocumentTest::testIndexReplace() { QFETCH(Format, inType); QFETCH(QString, input); QFETCH(int, index); QFETCH(int, length); QFETCH(Format, repType); QFETCH(QString, replacement); QFETCH(QString, expected); if(inType == Plain) doc.setPlainText(input, true); else doc.setHtml(input, true); if(expected.isEmpty()) expected = QString(input).replace(index, length, replacement); if(inType == Plain && repType == Plain) { doc.replace(index, length, replacement); QCOMPARE(doc.toPlainText(), expected); } doc.undo(); doc.replace(index, length, replacement); QString docOut = inType == Plain && repType == Plain ? doc.toPlainText() : doc.toHtml(); QCOMPARE(docOut, expected); } void RichDocumentTest::testCleanupSpaces_data() { QTest::addColumn("inType"); QTest::addColumn("input"); QTest::addColumn("expected"); QTest::newRow("t1") << Html << $("♪ It \t \t seems \t today
that
all \t you see ♪") << $("\u266A It seems today\nthat all you see \u266A"); QTest::newRow("t2") << Plain << $("\n\n \t This \t is\tsome \t \n \t \n\t \t\n\n \t good subtitle\t\n\ttext. \t") << $("This is some\ngood subtitle\ntext."); QTest::newRow("t3") << Plain << $(" \t \nline text\n \t ") << $("line text"); QTest::newRow("t4") << Html << $(" \t  
 

line text

\n \t


 
") << $("line text"); } void RichDocumentTest::testCleanupSpaces() { QFETCH(Format, inType); QFETCH(QString, input); QFETCH(QString, expected); if(inType == Plain) doc.setPlainText(input); else doc.setHtml(input); doc.cleanupSpaces(); QCOMPARE(doc.toPlainText(), expected); } void RichDocumentTest::testUpperLower() { doc.setPlainText($("some čćžšđ òàùèì lowercase \u266A♪\\!! text")); doc.toUpper(); QCOMPARE(doc.toPlainText(), $("SOME ČĆŽŠĐ ÒÀÙÈÌ LOWERCASE \u266A♪\\!! TEXT")); doc.toLower(); QCOMPARE(doc.toPlainText(), $("some čćžšđ òàùèì lowercase \u266A♪\\!! text")); } void RichDocumentTest::testSentence_data() { QTest::addColumn("sentenceStart"); QTest::addColumn("input"); QTest::addColumn("expected"); QTest::newRow("t1") << true << $("begINNING of ♪ some \t \u266A SENTENCE,") << $("Beginning of ♪ some \t \u266A sentence,"); QTest::newRow("t2") << false << $("Which CONTINUES? to finish¿\nthe SENTENCE") << $("which continues? To finish¿\nThe sentence"); QTest::newRow("t3") << false << $("BREAKS: LINE. AND PUP'S ENDS HERE!") << $("breaks: line. And pup's ends here!"); } void RichDocumentTest::testSentence() { QFETCH(bool, sentenceStart); QFETCH(QString, input); QFETCH(QString, expected); doc.setPlainText(input); doc.toSentenceCase(&sentenceStart); QCOMPARE(doc.toPlainText(), expected); } void RichDocumentTest::testTitle_data() { QTest::addColumn("sentenceStart"); QTest::addColumn("input"); QTest::addColumn("expected"); QTest::newRow("t1") << true << $("begINNING of ♪ s-o-m-e \t \u266A SENTENCE,") << $("Beginning Of ♪ S-o-m-e \t \u266A Sentence,"); QTest::newRow("t2") << false << $("Whi_ch CONT:INUES? to finish¿\nthe SENTENCE") << $("Whi_ch Cont:Inues? To Finish¿\nThe Sentence"); QTest::newRow("t3") << false << $("BRE+AKS: LINE. AND PUP'S ENDS HERE!") << $("Bre+aks: Line. And Pup's Ends Here!"); } void RichDocumentTest::testTitle() { QFETCH(bool, sentenceStart); QFETCH(QString, input); QFETCH(QString, expected); doc.setPlainText(input); doc.toSentenceCase(&sentenceStart, true, true); QCOMPARE(doc.toPlainText(), expected); } QTEST_GUILESS_MAIN(RichDocumentTest)