File indexing completed on 2024-12-22 04:28:31

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "converttexttest.h"
0008 #include "converttext.h"
0009 #include <QTest>
0010 #include <QTextDocument>
0011 
0012 QTEST_GUILESS_MAIN(ConvertTextTest)
0013 ConvertTextTest::ConvertTextTest(QObject *parent)
0014     : QObject{parent}
0015 {
0016 }
0017 
0018 void ConvertTextTest::shouldHaveDefaultValues()
0019 {
0020     {
0021         const QString str(QStringLiteral("foo"));
0022         QCOMPARE(TextUtils::ConvertText::normalize(QStringView(str)), QStringLiteral("foo"));
0023     }
0024     {
0025         const QString str(QStringLiteral("numérique"));
0026         QCOMPARE(TextUtils::ConvertText::normalize(QStringView(str)), QStringLiteral("numerique"));
0027     }
0028     {
0029         const QString str(QStringLiteral("téléphone"));
0030         QCOMPARE(TextUtils::ConvertText::normalize(QStringView(str)), QStringLiteral("telephone"));
0031     }
0032     {
0033         const QString str(QStringLiteral("noël"));
0034         QCOMPARE(TextUtils::ConvertText::normalize(QStringView(str)), QStringLiteral("noel"));
0035     }
0036 }
0037 
0038 void ConvertTextTest::testUpperCase_data()
0039 {
0040     QTest::addColumn<QString>("input");
0041     QTest::addColumn<QString>("result");
0042     QTest::addColumn<int>("startcursorposition");
0043     QTest::addColumn<int>("endcursorposition");
0044     QTest::newRow("onelinewithoutselection") << QStringLiteral("foo") << QStringLiteral("foo") << -1 << -1;
0045     QTest::newRow("onelinewithselection") << QStringLiteral("foo") << QStringLiteral("FOO") << 0 << 3;
0046     QTest::newRow("onelinewithselectionuppercase") << QStringLiteral("FOO") << QStringLiteral("FOO") << 0 << 3;
0047 }
0048 
0049 void ConvertTextTest::testUpperCase()
0050 {
0051     QFETCH(QString, input);
0052     QFETCH(QString, result);
0053     QFETCH(int, startcursorposition);
0054     QFETCH(int, endcursorposition);
0055     auto document = new QTextDocument(this);
0056     document->setPlainText(input);
0057     QTextCursor textCursor(document);
0058     if (startcursorposition != -1 && endcursorposition != -1) {
0059         textCursor.setPosition(startcursorposition);
0060         textCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endcursorposition - startcursorposition);
0061     }
0062     TextUtils::ConvertText::upperCase(textCursor);
0063     QCOMPARE(textCursor.document()->toPlainText(), result);
0064     delete document;
0065 }
0066 
0067 void ConvertTextTest::testLowerCase_data()
0068 {
0069     QTest::addColumn<QString>("input");
0070     QTest::addColumn<QString>("result");
0071     QTest::addColumn<int>("startcursorposition");
0072     QTest::addColumn<int>("endcursorposition");
0073     QTest::newRow("onelinewithoutselection") << QStringLiteral("foo") << QStringLiteral("foo") << -1 << -1;
0074     QTest::newRow("onelinewithselection") << QStringLiteral("foo") << QStringLiteral("foo") << 0 << 3;
0075     QTest::newRow("onelinewithselectionuppercase") << QStringLiteral("FOO") << QStringLiteral("foo") << 0 << 3;
0076 }
0077 
0078 void ConvertTextTest::testLowerCase()
0079 {
0080     QFETCH(QString, input);
0081     QFETCH(QString, result);
0082     QFETCH(int, startcursorposition);
0083     QFETCH(int, endcursorposition);
0084     auto document = new QTextDocument(this);
0085     document->setPlainText(input);
0086     QTextCursor textCursor(document);
0087     if (startcursorposition != -1 && endcursorposition != -1) {
0088         textCursor.setPosition(startcursorposition);
0089         textCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endcursorposition - startcursorposition);
0090     }
0091     TextUtils::ConvertText::lowerCase(textCursor);
0092     QCOMPARE(textCursor.document()->toPlainText(), result);
0093     delete document;
0094 }
0095 
0096 void ConvertTextTest::testSentenceCase_data()
0097 {
0098     QTest::addColumn<QString>("input");
0099     QTest::addColumn<QString>("result");
0100     QTest::addColumn<int>("startcursorposition");
0101     QTest::addColumn<int>("endcursorposition");
0102     QTest::newRow("onelinewithoutselection") << QStringLiteral("foo") << QStringLiteral("foo") << -1 << -1;
0103     QTest::newRow("onelinewithselection") << QStringLiteral("foo") << QStringLiteral("Foo") << 0 << 3;
0104     QTest::newRow("bigline") << QStringLiteral("foo bla foo.\nddd") << QStringLiteral("Foo bla foo.\nDdd") << 0 << 16;
0105 }
0106 
0107 void ConvertTextTest::testSentenceCase()
0108 {
0109     QFETCH(QString, input);
0110     QFETCH(QString, result);
0111     QFETCH(int, startcursorposition);
0112     QFETCH(int, endcursorposition);
0113     auto document = new QTextDocument(this);
0114     document->setPlainText(input);
0115     QTextCursor textCursor(document);
0116     if (startcursorposition != -1 && endcursorposition != -1) {
0117         textCursor.setPosition(startcursorposition);
0118         textCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endcursorposition - startcursorposition);
0119     }
0120     TextUtils::ConvertText::sentenceCase(textCursor);
0121     QCOMPARE(textCursor.document()->toPlainText(), result);
0122     delete document;
0123 }
0124 
0125 void ConvertTextTest::testReverseCase_data()
0126 {
0127     QTest::addColumn<QString>("input");
0128     QTest::addColumn<QString>("result");
0129     QTest::addColumn<int>("startcursorposition");
0130     QTest::addColumn<int>("endcursorposition");
0131     QTest::newRow("onelinewithoutselection") << QStringLiteral("foo") << QStringLiteral("foo") << -1 << -1;
0132     QTest::newRow("onelinewithselection") << QStringLiteral("foo") << QStringLiteral("FOO") << 0 << 3;
0133     QTest::newRow("reverseCase") << QStringLiteral("fOo bla\tfOO") << QStringLiteral("FoO BLA\tFoo") << 0 << 12;
0134     QTest::newRow("notallineselected") << QStringLiteral("fOo bla\tfOO") << QStringLiteral("FoO bla\tfOO") << 0 << 3;
0135 }
0136 
0137 void ConvertTextTest::testReverseCase()
0138 {
0139     QFETCH(QString, input);
0140     QFETCH(QString, result);
0141     QFETCH(int, startcursorposition);
0142     QFETCH(int, endcursorposition);
0143 
0144     auto document = new QTextDocument(this);
0145     document->setPlainText(input);
0146     QTextCursor textCursor(document);
0147     if (startcursorposition != -1 && endcursorposition != -1) {
0148         textCursor.setPosition(startcursorposition);
0149         textCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endcursorposition - startcursorposition);
0150     }
0151     TextUtils::ConvertText::reverseCase(textCursor);
0152     QCOMPARE(textCursor.document()->toPlainText(), result);
0153     delete document;
0154 }
0155 
0156 #include "moc_converttexttest.cpp"