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

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "autocorrectiontest.h"
0008 #include "../autocorrection.h"
0009 #include "settings/textautocorrectionsettings.h"
0010 #include <QStandardPaths>
0011 #include <QTest>
0012 #include <QTextDocument>
0013 QTEST_MAIN(AutoCorrectionTest)
0014 
0015 #ifndef Q_OS_WIN
0016 void initLocale()
0017 {
0018     setenv("LC_ALL", "en_US.utf-8", 1);
0019 }
0020 
0021 Q_CONSTRUCTOR_FUNCTION(initLocale)
0022 #endif
0023 
0024 AutoCorrectionTest::AutoCorrectionTest()
0025 {
0026     QStandardPaths::setTestModeEnabled(true);
0027     mConfig = KSharedConfig::openConfig(QStringLiteral("autocorrectiontestrc"));
0028     TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->setSharedConfig(mConfig);
0029     TextAutoCorrectionCore::TextAutoCorrectionSettings::self()->load();
0030 }
0031 
0032 AutoCorrectionTest::~AutoCorrectionTest() = default;
0033 
0034 void AutoCorrectionTest::shouldHaveDefaultValue()
0035 {
0036     TextAutoCorrectionCore::AutoCorrection autocorrection;
0037     QVERIFY(!autocorrection.autoCorrectionSettings()->isEnabledAutoCorrection());
0038     QVERIFY(!autocorrection.autoCorrectionSettings()->isUppercaseFirstCharOfSentence());
0039     QVERIFY(!autocorrection.autoCorrectionSettings()->isFixTwoUppercaseChars());
0040     QVERIFY(!autocorrection.autoCorrectionSettings()->isSingleSpaces());
0041     QVERIFY(!autocorrection.autoCorrectionSettings()->isAutoFractions());
0042     QVERIFY(!autocorrection.autoCorrectionSettings()->isCapitalizeWeekDays());
0043     QVERIFY(!autocorrection.autoCorrectionSettings()->isReplaceDoubleQuotes());
0044     QVERIFY(!autocorrection.autoCorrectionSettings()->isReplaceSingleQuotes());
0045     QVERIFY(!autocorrection.autoCorrectionSettings()->isAdvancedAutocorrect());
0046     QVERIFY(!autocorrection.autoCorrectionSettings()->isAutoFormatUrl());
0047     QVERIFY(!autocorrection.autoCorrectionSettings()->isAutoBoldUnderline());
0048     QVERIFY(!autocorrection.autoCorrectionSettings()->isSuperScript());
0049     QVERIFY(!autocorrection.autoCorrectionSettings()->isAddNonBreakingSpace());
0050     QVERIFY(!autocorrection.autoCorrectionSettings()->isReplaceDoubleQuotesByFrenchQuotes());
0051 }
0052 
0053 void AutoCorrectionTest::shouldRestoreValue()
0054 {
0055     TextAutoCorrectionCore::AutoCorrection autocorrection;
0056     // TODO
0057 }
0058 
0059 void AutoCorrectionTest::shouldUpperCaseFirstCharOfSentence()
0060 {
0061     TextAutoCorrectionCore::AutoCorrection autocorrection;
0062     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0063     settings->setEnabledAutoCorrection(true);
0064     settings->setUppercaseFirstCharOfSentence(true);
0065     autocorrection.setAutoCorrectionSettings(settings);
0066 
0067     // Uppercase here.
0068     QTextDocument doc;
0069     QString text = QStringLiteral("foo");
0070     doc.setPlainText(text);
0071     int position = text.length();
0072     autocorrection.autocorrect(false, doc, position);
0073     QCOMPARE(doc.toPlainText(), QStringLiteral("Foo"));
0074 
0075     // IT's not first char -> not uppercase
0076     text = QStringLiteral(" foo");
0077     doc.setPlainText(text);
0078     position = text.length();
0079     autocorrection.autocorrect(false, doc, position);
0080     QCOMPARE(doc.toPlainText(), text);
0081 
0082     // It's already uppercase
0083     text = QStringLiteral("Foo");
0084     doc.setPlainText(text);
0085     position = text.length();
0086     autocorrection.autocorrect(false, doc, position);
0087     QCOMPARE(doc.toPlainText(), text);
0088 
0089     // Word is after a ". "
0090     text = QStringLiteral("Foo. foo");
0091     doc.setPlainText(text);
0092     position = text.length();
0093     autocorrection.autocorrect(false, doc, position);
0094     QCOMPARE(doc.toPlainText(), QStringLiteral("Foo. Foo"));
0095     QCOMPARE(position, text.length());
0096 }
0097 
0098 void AutoCorrectionTest::shouldFixTwoUpperCaseChars()
0099 {
0100     TextAutoCorrectionCore::AutoCorrection autocorrection;
0101     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0102     settings->setEnabledAutoCorrection(true);
0103     settings->setFixTwoUppercaseChars(true);
0104     autocorrection.setAutoCorrectionSettings(settings);
0105 
0106     // Remove two uppercases
0107     QTextDocument doc;
0108     QString text = QStringLiteral("FOo");
0109     doc.setPlainText(text);
0110     int position = text.length();
0111     autocorrection.autocorrect(false, doc, position);
0112     QCOMPARE(doc.toPlainText(), QStringLiteral("Foo"));
0113 
0114     // There is not two uppercase
0115     text = QStringLiteral("foo");
0116     doc.setPlainText(text);
0117     position = text.length();
0118     autocorrection.autocorrect(false, doc, position);
0119     QCOMPARE(doc.toPlainText(), text);
0120 
0121     text = QStringLiteral("Foo");
0122     doc.setPlainText(text);
0123     position = text.length();
0124     autocorrection.autocorrect(false, doc, position);
0125     QCOMPARE(doc.toPlainText(), text);
0126 
0127     // There is a uppercase word
0128     text = QStringLiteral("FOO");
0129     doc.setPlainText(text);
0130     position = text.length();
0131     autocorrection.autocorrect(false, doc, position);
0132     QCOMPARE(doc.toPlainText(), text);
0133 
0134     // Exclude 2 upper letter
0135     text = QStringLiteral("ABc");
0136     doc.setPlainText(text);
0137     position = text.length();
0138     autocorrection.autocorrect(false, doc, position);
0139     QCOMPARE(doc.toPlainText(), QStringLiteral("Abc"));
0140 
0141     QSet<QString> exception;
0142     exception.insert(QStringLiteral("ABc"));
0143     settings->setTwoUpperLetterExceptions(exception);
0144     autocorrection.setAutoCorrectionSettings(settings);
0145     text = QStringLiteral("ABc");
0146     doc.setPlainText(text);
0147     position = text.length();
0148     autocorrection.autocorrect(false, doc, position);
0149     QCOMPARE(doc.toPlainText(), text);
0150 }
0151 
0152 void AutoCorrectionTest::shouldReplaceSingleQuote()
0153 {
0154     TextAutoCorrectionCore::AutoCorrection autocorrection;
0155     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0156     settings->setEnabledAutoCorrection(true);
0157     settings->setReplaceSingleQuotes(true);
0158     autocorrection.setAutoCorrectionSettings(settings);
0159 
0160     TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes simpleQuote;
0161     simpleQuote.begin = QLatin1Char('A');
0162     simpleQuote.end = QLatin1Char('B');
0163 
0164     settings->setTypographicSingleQuotes(simpleQuote);
0165     autocorrection.setAutoCorrectionSettings(settings);
0166 
0167     QTextDocument doc;
0168     QString text = QStringLiteral("sss");
0169     doc.setPlainText(QStringLiteral("'") + text);
0170     int position = text.length();
0171     autocorrection.autocorrect(false, doc, position);
0172     QCOMPARE(doc.toPlainText(), QString(simpleQuote.begin + text));
0173 
0174     text = QStringLiteral("sss");
0175     doc.setPlainText(text + QStringLiteral("'"));
0176     position = text.length();
0177     autocorrection.autocorrect(false, doc, position);
0178     QCOMPARE(doc.toPlainText(), QString(text + simpleQuote.end));
0179 
0180     text = QStringLiteral("sss");
0181     doc.setPlainText(QStringLiteral("'") + text + QStringLiteral("'"));
0182     position = text.length();
0183     autocorrection.autocorrect(false, doc, position);
0184     QCOMPARE(doc.toPlainText(), QString(simpleQuote.begin + text + simpleQuote.end));
0185 }
0186 
0187 void AutoCorrectionTest::shouldReplaceDoubleQuote()
0188 {
0189     TextAutoCorrectionCore::AutoCorrection autocorrection;
0190     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0191     settings->setEnabledAutoCorrection(true);
0192     settings->setReplaceDoubleQuotes(true);
0193     TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes doubleQuote;
0194     doubleQuote.begin = QLatin1Char('A');
0195     doubleQuote.end = QLatin1Char('B');
0196 
0197     settings->setTypographicDoubleQuotes(doubleQuote);
0198     autocorrection.setAutoCorrectionSettings(settings);
0199 
0200     QTextDocument doc;
0201     QString text = QStringLiteral("sss");
0202 
0203     doc.setPlainText(QLatin1Char('"') + text);
0204     int position = text.length();
0205     autocorrection.autocorrect(false, doc, position);
0206     QCOMPARE(doc.toPlainText(), QString(doubleQuote.begin + text));
0207 
0208     text = QStringLiteral("sss");
0209     doc.setPlainText(text + QStringLiteral("\""));
0210     position = text.length();
0211     autocorrection.autocorrect(false, doc, position);
0212     QCOMPARE(doc.toPlainText(), QString(text + doubleQuote.end));
0213 
0214     text = QStringLiteral("sss");
0215     doc.setPlainText(QStringLiteral("\"") + text + QStringLiteral("\""));
0216     position = text.length();
0217     autocorrection.autocorrect(false, doc, position);
0218     QCOMPARE(doc.toPlainText(), QString(doubleQuote.begin + text + doubleQuote.end));
0219 }
0220 
0221 void AutoCorrectionTest::shouldNotReplaceUppercaseLetter()
0222 {
0223     TextAutoCorrectionCore::AutoCorrection autocorrection;
0224     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0225     settings->setEnabledAutoCorrection(true);
0226     settings->setFixTwoUppercaseChars(true);
0227     autocorrection.setAutoCorrectionSettings(settings);
0228     QSet<QString> exceptions;
0229     exceptions.insert(QStringLiteral("ABc"));
0230     settings->setTwoUpperLetterExceptions(exceptions);
0231     autocorrection.setAutoCorrectionSettings(settings);
0232 
0233     QTextDocument doc;
0234     QString text = QStringLiteral("foo ABc");
0235     doc.setPlainText(text);
0236     int position = text.length();
0237     autocorrection.autocorrect(false, doc, position);
0238     QCOMPARE(doc.toPlainText(), text);
0239 }
0240 
0241 void AutoCorrectionTest::shouldReplaceToTextFormat()
0242 {
0243     TextAutoCorrectionCore::AutoCorrection autocorrection;
0244     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0245     settings->setEnabledAutoCorrection(true);
0246     settings->setAutoBoldUnderline(true);
0247     autocorrection.setAutoCorrectionSettings(settings);
0248 
0249     QTextDocument doc;
0250     // We don't use html => don't change it.
0251     QString text = QStringLiteral("*foo*");
0252     doc.setHtml(text);
0253     int position = text.length();
0254     autocorrection.autocorrect(false, doc, position);
0255     QCOMPARE(doc.toPlainText(), text);
0256 
0257     QString result = QStringLiteral("foo");
0258     doc.setHtml(text);
0259     position = text.length();
0260     autocorrection.autocorrect(true, doc, position);
0261     QCOMPARE(doc.toPlainText(), result);
0262 
0263     text = QStringLiteral("*foo*");
0264     doc.setHtml(text);
0265     position = text.length();
0266     autocorrection.autocorrect(true, doc, position);
0267     QCOMPARE(doc.toPlainText(), result);
0268     QTextCursor cursor(&doc);
0269     cursor.setPosition(2);
0270     QTextCharFormat charFormat = cursor.charFormat();
0271     QCOMPARE(charFormat.font().underline(), false);
0272     QCOMPARE(charFormat.font().bold(), true);
0273     QCOMPARE(charFormat.font().strikeOut(), false);
0274 
0275     text = QStringLiteral("_foo_");
0276     doc.setHtml(text);
0277     position = text.length();
0278     autocorrection.autocorrect(true, doc, position);
0279     QCOMPARE(doc.toPlainText(), result);
0280     cursor = QTextCursor(&doc);
0281     cursor.setPosition(2);
0282     charFormat = cursor.charFormat();
0283     QCOMPARE(charFormat.font().underline(), true);
0284     QCOMPARE(charFormat.font().bold(), false);
0285     QCOMPARE(charFormat.font().strikeOut(), false);
0286 
0287     text = QStringLiteral("-foo-");
0288     doc.setHtml(text);
0289     position = text.length();
0290     autocorrection.autocorrect(true, doc, position);
0291     QCOMPARE(doc.toPlainText(), result);
0292     cursor = QTextCursor(&doc);
0293     cursor.setPosition(2);
0294     charFormat = cursor.charFormat();
0295     QCOMPARE(charFormat.font().underline(), false);
0296     QCOMPARE(charFormat.font().bold(), false);
0297     QCOMPARE(charFormat.font().strikeOut(), true);
0298 
0299     // Don't convert it.
0300     text = QStringLiteral("-foo1");
0301     doc.setHtml(text);
0302     position = text.length();
0303     autocorrection.autocorrect(true, doc, position);
0304     QCOMPARE(doc.toPlainText(), text);
0305 
0306     text = QStringLiteral("*foo* blabla");
0307     position = 5;
0308     doc.setHtml(text);
0309     autocorrection.autocorrect(true, doc, position);
0310     result = QStringLiteral("foo blabla");
0311     QCOMPARE(doc.toPlainText(), result);
0312     QCOMPARE(position, 3);
0313 
0314     // TODO QCOMPARE(doc.toHtml(), text);
0315 }
0316 
0317 void AutoCorrectionTest::shouldReplaceAutoFraction()
0318 {
0319     TextAutoCorrectionCore::AutoCorrection autocorrection;
0320     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0321     settings->setEnabledAutoCorrection(true);
0322     settings->setAutoFractions(true);
0323     autocorrection.setAutoCorrectionSettings(settings);
0324 
0325     QTextDocument doc;
0326     QString text = QStringLiteral("1/2");
0327     doc.setPlainText(text);
0328     int position = text.length();
0329     autocorrection.autocorrect(false, doc, position);
0330 
0331     QCOMPARE(doc.toPlainText(), QStringLiteral("½"));
0332 
0333     QString suffix = QStringLiteral(" after");
0334     text = QStringLiteral("1/2");
0335     position = 3;
0336     text += suffix;
0337     doc.setPlainText(text);
0338     autocorrection.autocorrect(false, doc, position);
0339     QCOMPARE(doc.toPlainText(), QString(QStringLiteral("½") + suffix));
0340     QCOMPARE(position, 1);
0341 }
0342 
0343 void AutoCorrectionTest::shouldNotAddSpaceWhenWeAlreadyHaveASpace()
0344 {
0345     TextAutoCorrectionCore::AutoCorrection autocorrection;
0346     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0347     settings->setEnabledAutoCorrection(true);
0348     settings->setSingleSpaces(true);
0349     autocorrection.setAutoCorrectionSettings(settings);
0350     QTextDocument doc;
0351     // We already a space => don't allow to add more
0352     QString text = QStringLiteral("FOO ");
0353     doc.setPlainText(text);
0354     int position = text.length();
0355     bool result = autocorrection.autocorrect(false, doc, position);
0356     QCOMPARE(result, false);
0357 
0358     // We can add a space
0359     text = QStringLiteral("FOO");
0360     doc.setPlainText(text);
0361     position = text.length();
0362     result = autocorrection.autocorrect(false, doc, position);
0363     QCOMPARE(result, true);
0364 
0365     // We have a space => don't add it.
0366     text = QStringLiteral("FOO ");
0367     position = text.length();
0368     QString fullText = text + QStringLiteral("FOO");
0369     doc.setPlainText(fullText);
0370     result = autocorrection.autocorrect(false, doc, position);
0371     QCOMPARE(result, false);
0372 }
0373 
0374 void AutoCorrectionTest::shouldAutocorrectWord()
0375 {
0376     TextAutoCorrectionCore::AutoCorrection autocorrection;
0377     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0378     settings->setEnabledAutoCorrection(true);
0379     settings->setAdvancedAutocorrect(true);
0380     autocorrection.setAutoCorrectionSettings(settings);
0381 
0382     QTextDocument doc;
0383     // No changes
0384     QString text = QStringLiteral("FOOAA");
0385     doc.setPlainText(text);
0386     int position = text.length();
0387     int oldPosition = position;
0388     autocorrection.autocorrect(false, doc, position);
0389     QCOMPARE(doc.toPlainText(), text);
0390     QCOMPARE(position, oldPosition);
0391 
0392     // Convert word
0393     QHash<QString, QString> entries;
0394     const QString convertWord = QStringLiteral("BLABLA");
0395     entries.insert(text, convertWord);
0396     settings->setAutocorrectEntries(entries);
0397     autocorrection.setAutoCorrectionSettings(settings);
0398     text = QStringLiteral("FOOAA");
0399     doc.setPlainText(text);
0400     position = text.length();
0401     autocorrection.autocorrect(false, doc, position);
0402     QCOMPARE(doc.toPlainText(), convertWord);
0403     QCOMPARE(position, convertWord.length());
0404 
0405     QString suffix = QStringLiteral(" TOTO");
0406     text = QStringLiteral("FOOAA");
0407     position = text.length();
0408     text += suffix;
0409     doc.setPlainText(text);
0410     autocorrection.autocorrect(false, doc, position);
0411     QCOMPARE(doc.toPlainText(), QString(convertWord + suffix));
0412     // FIXME ? QCOMPARE(position, convertWord.length());
0413 }
0414 
0415 void AutoCorrectionTest::shouldNotUpperCaseFirstCharOfSentence()
0416 {
0417     TextAutoCorrectionCore::AutoCorrection autocorrection;
0418     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0419     settings->setEnabledAutoCorrection(true);
0420     settings->setUppercaseFirstCharOfSentence(true);
0421     autocorrection.setAutoCorrectionSettings(settings);
0422     QSet<QString> lst;
0423     lst.insert(QStringLiteral("Foo."));
0424     settings->setUpperCaseExceptions(lst);
0425     autocorrection.setAutoCorrectionSettings(settings);
0426 
0427     // Uppercase here.
0428     QTextDocument doc;
0429     QString text = QStringLiteral("foo. blabla Foo. tt");
0430     doc.setPlainText(text);
0431     int position = text.length();
0432     autocorrection.autocorrect(false, doc, position);
0433     QCOMPARE(doc.toPlainText(), text);
0434 
0435     settings->setUpperCaseExceptions(QSet<QString>());
0436     autocorrection.setAutoCorrectionSettings(settings);
0437 
0438     doc.setPlainText(text);
0439     position = text.length();
0440     autocorrection.autocorrect(false, doc, position);
0441     QString result = QStringLiteral("foo. blabla Foo. Tt");
0442     QCOMPARE(doc.toPlainText(), result);
0443 }
0444 
0445 using mapAutoCorrect = QHash<QString, QString>;
0446 Q_DECLARE_METATYPE(mapAutoCorrect)
0447 
0448 void AutoCorrectionTest::shouldAutocorrectMultiWord_data()
0449 {
0450     QTest::addColumn<QString>("originalString");
0451     QTest::addColumn<QString>("convertedString");
0452     QTest::addColumn<mapAutoCorrect>("convertStringHash");
0453 
0454     mapAutoCorrect map;
0455     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0456     QTest::newRow("simpleReplace") << QStringLiteral("boo") << QStringLiteral("bla") << map;
0457 
0458     map.clear();
0459     map.insert(QStringLiteral("a boo"), QStringLiteral("b bla"));
0460     QTest::newRow("multiword") << QStringLiteral("a boo") << QStringLiteral("b bla") << map;
0461 
0462     map.clear();
0463     map.insert(QStringLiteral("a boo"), QStringLiteral("b bla"));
0464     map.insert(QStringLiteral("a booeeeeeeeeeeeeeeeeeee"), QStringLiteral("b blaeee"));
0465     QTest::newRow("multiword-2") << QStringLiteral("toto. a boo") << QStringLiteral("toto. b bla") << map;
0466 
0467     map.clear();
0468     QTest::newRow("multiword-2 without replace") << QStringLiteral("toto. a boo") << QStringLiteral("toto. a boo") << map;
0469 
0470     map.clear();
0471     map.insert(QStringLiteral("a boo"), QStringLiteral("b bla"));
0472     QTest::newRow("multiword-2 with uppercase") << QStringLiteral("toto. A boo") << QStringLiteral("toto. B bla") << map;
0473 
0474     map.clear();
0475     map.insert(QStringLiteral("a boo1111111111"), QStringLiteral("b bla"));
0476     QTest::newRow("multiword-3") << QStringLiteral("a boo") << QStringLiteral("a boo") << map;
0477 
0478     map.clear();
0479     map.insert(QStringLiteral("boo"), QStringLiteral("Bla"));
0480     QTest::newRow("withuppercase") << QStringLiteral("Boo") << QStringLiteral("Bla") << map;
0481 
0482     map.clear();
0483     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0484     QTest::newRow("withuppercase-2") << QStringLiteral("Boo") << QStringLiteral("Bla") << map;
0485 
0486     map.clear();
0487     map.insert(QStringLiteral("boo"), QStringLiteral("Bla"));
0488     QTest::newRow("withuppercase-3") << QStringLiteral("Boo") << QStringLiteral("Bla") << map;
0489 
0490     map.clear();
0491     map.insert(QStringLiteral("boo"), QStringLiteral("Bla"));
0492     QTest::newRow("withuppercase-4") << QStringLiteral("boo") << QStringLiteral("bla") << map;
0493 
0494     map.clear();
0495     map.insert(QStringLiteral("booooo"), QStringLiteral("bla"));
0496     QTest::newRow("nofindtext") << QStringLiteral("boo") << QStringLiteral("boo") << map;
0497 
0498     map.clear();
0499     map.insert(QStringLiteral("boosss"), QStringLiteral("Blasss"));
0500     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0501     QTest::newRow("withponct") << QStringLiteral("boo!") << QStringLiteral("bla!") << map;
0502 
0503     map.clear();
0504     map.insert(QStringLiteral("boosss"), QStringLiteral("Blasss"));
0505     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0506     QTest::newRow("withponct-2") << QStringLiteral("lolo. boo!") << QStringLiteral("lolo. bla!") << map;
0507 
0508     map.clear();
0509     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0510     QTest::newRow("dontreplaceinpieceofword") << QStringLiteral("voitureboo") << QStringLiteral("voitureboo") << map;
0511 
0512     map.clear();
0513     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0514     QTest::newRow("dontreplaceall") << QStringLiteral("Boo boo boo") << QStringLiteral("Boo boo bla") << map;
0515 
0516     map.clear();
0517     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0518     map.insert(QStringLiteral("boo boo"), QStringLiteral("bli"));
0519     QTest::newRow("replace-1") << QStringLiteral("Boo boo boo") << QStringLiteral("Boo bli") << map;
0520 
0521     map.clear();
0522     map.insert(QStringLiteral("boo boo"), QStringLiteral("bli"));
0523     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0524     QTest::newRow("replace-1-order") << QStringLiteral("Boo boo boo") << QStringLiteral("Boo bli") << map;
0525 
0526     map.clear();
0527     map.insert(QStringLiteral("au delà"), QStringLiteral("au-delà"));
0528     map.insert(QStringLiteral("boo boo"), QStringLiteral("bli"));
0529     QTest::newRow("replace-2") << QStringLiteral("voilà au delà") << QStringLiteral("voilà au-delà") << map;
0530 
0531     map.clear();
0532     map.insert(QStringLiteral("au delà"), QStringLiteral("au-delà"));
0533     map.insert(QStringLiteral("avant JC"), QStringLiteral("avant J.-C."));
0534     QTest::newRow("replace-3") << QStringLiteral("il est né avant JC") << QStringLiteral("il est né avant J.-C.") << map;
0535 }
0536 
0537 void AutoCorrectionTest::shouldAutocorrectMultiWord()
0538 {
0539     QFETCH(QString, originalString);
0540     QFETCH(QString, convertedString);
0541     QFETCH(mapAutoCorrect, convertStringHash);
0542 
0543     TextAutoCorrectionCore::AutoCorrection autocorrection;
0544     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0545     settings->setEnabledAutoCorrection(true);
0546     settings->setAdvancedAutocorrect(true);
0547     settings->setAutocorrectEntries(convertStringHash);
0548     autocorrection.setAutoCorrectionSettings(settings);
0549 
0550     QTextDocument doc;
0551     doc.setPlainText(originalString);
0552     int position = originalString.length();
0553     autocorrection.autocorrect(false, doc, position);
0554     QCOMPARE(doc.toPlainText(), convertedString);
0555 }
0556 
0557 void AutoCorrectionTest::shouldAddNonBreakingSpace_data()
0558 {
0559     QTest::addColumn<QString>("originalString");
0560     QTest::addColumn<QString>("convertedString");
0561     QTest::addColumn<QString>("language");
0562     QTest::addColumn<bool>("enableAddNonBreakingSpace");
0563     QTest::newRow("convert1") << QStringLiteral("boo !") << QStringLiteral("boob!") << QStringLiteral("fr") << true;
0564     QTest::newRow("disable") << QStringLiteral("boo !") << QStringLiteral("boo !") << QStringLiteral("fr") << false;
0565     QTest::newRow("nonchanges") << QStringLiteral("boo") << QStringLiteral("boo") << QStringLiteral("fr") << true;
0566     QTest::newRow("convert2") << QStringLiteral("boo ;") << QStringLiteral("boob;") << QStringLiteral("fr") << true;
0567     QTest::newRow("convert3") << QStringLiteral("boo ?") << QStringLiteral("boob?") << QStringLiteral("fr") << true;
0568     QTest::newRow("convert4") << QStringLiteral("boo :") << QStringLiteral("boob:") << QStringLiteral("fr") << true;
0569     QTest::newRow("nonfrenchlanguage") << QStringLiteral("boo :") << QStringLiteral("boo :") << QStringLiteral("ge") << true;
0570     QTest::newRow("onecharacter") << QStringLiteral(":") << QStringLiteral(":") << QStringLiteral("fr") << true;
0571     QTest::newRow("onecharacter2") << QStringLiteral(" ") << QStringLiteral(" ") << QStringLiteral("fr") << true;
0572     QTest::newRow("percentage") << QStringLiteral("50 %") << QStringLiteral("50b%") << QStringLiteral("fr") << true;
0573     QTest::newRow("degrees") << QStringLiteral("50 °C") << QStringLiteral("50b°C") << QStringLiteral("fr") << true;
0574     QTest::newRow("simplespace") << QStringLiteral(" ") << QStringLiteral(" ") << QStringLiteral("fr") << true;
0575 }
0576 
0577 void AutoCorrectionTest::shouldAddNonBreakingSpace()
0578 {
0579     QFETCH(QString, originalString);
0580     QFETCH(QString, convertedString);
0581     QFETCH(QString, language);
0582     QFETCH(bool, enableAddNonBreakingSpace);
0583 
0584     TextAutoCorrectionCore::AutoCorrection autocorrection;
0585     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0586     settings->setEnabledAutoCorrection(true);
0587     settings->setAddNonBreakingSpace(enableAddNonBreakingSpace);
0588 
0589     settings->setLanguage(language);
0590     settings->setNonBreakingSpace(QChar(QLatin1Char('b')));
0591     autocorrection.setAutoCorrectionSettings(settings);
0592 
0593     QTextDocument doc;
0594     doc.setPlainText(originalString);
0595     int position = originalString.length();
0596     autocorrection.autocorrect(false, doc, position);
0597     QCOMPARE(doc.toPlainText(), convertedString);
0598 }
0599 
0600 void AutoCorrectionTest::shouldReplaceWithMultiOption_data()
0601 {
0602     QTest::addColumn<QString>("originalString");
0603     QTest::addColumn<QString>("convertedString");
0604     QTest::addColumn<mapAutoCorrect>("convertStringHash");
0605 
0606     QTest::addColumn<bool>("enable");
0607     QTest::addColumn<bool>("uppercaseFirstCharOfSentence");
0608     QTest::addColumn<bool>("advancedAutocorrect");
0609     QTest::addColumn<bool>("fixtwouppercase");
0610     QTest::addColumn<int>("position");
0611     mapAutoCorrect map;
0612     map.insert(QStringLiteral("boo"), QStringLiteral("bla"));
0613     map.insert(QStringLiteral(":j2:"), QStringLiteral("TV"));
0614     map.insert(QStringLiteral("i"), QStringLiteral("I"));
0615     map.insert(QStringLiteral("Noel"), QStringLiteral("noal"));
0616     // create a big word otherwise we can't reproduce bug
0617     map.insert(QStringLiteral("anticonstitutionnellement"), QStringLiteral("2"));
0618 
0619     QTest::newRow("disable") << QStringLiteral("Boo boo boo") << QStringLiteral("Boo boo boo") << map << false << false << false << false << -1;
0620     QTest::newRow("enablebutdisablealloptions") << QStringLiteral("Boo boo boo") << QStringLiteral("Boo boo boo") << map << true << false << false << false
0621                                                 << -1;
0622     QTest::newRow("enableandenableuppercase") << QStringLiteral("Boo boo boo") << QStringLiteral("Boo boo boo") << map << true << true << false << false << -1;
0623     QTest::newRow("enableandenableuppercaseandadvanced")
0624         << QStringLiteral("Boo boo boo") << QStringLiteral("Boo boo bla") << map << true << true << true << false << -1;
0625 
0626     QTest::newRow("enableandenableuppercaseandadvanced-2")
0627         << QStringLiteral("Boo boo. boo") << QStringLiteral("Boo boo. Bla") << map << true << true << true << false << -1;
0628     QTest::newRow("enableandenableuppercaseandadvanced-3")
0629         << QStringLiteral("blablobli") << QStringLiteral("Blablobli") << map << true << true << true << false << -1;
0630     QTest::newRow("enableandenableuppercaseandadvanced-4")
0631         << QStringLiteral("blablobli. foo") << QStringLiteral("blablobli. Foo") << map << true << true << true << false << -1;
0632 
0633     QTest::newRow("enableandenablefixtowuppercase") << QStringLiteral("Boo boo. BOo") << QStringLiteral("Boo boo. Boo") << map << true << true << false << true
0634                                                     << -1;
0635     QTest::newRow("enableandenablefixtowuppercase-2") << QStringLiteral("Boo BOo") << QStringLiteral("Boo Boo") << map << true << true << false << true << -1;
0636 
0637     QTest::newRow(":j2:") << QStringLiteral(":j2:") << QStringLiteral("TV") << map << true << false << true << false << -1;
0638     QTest::newRow(":j2: bla") << QStringLiteral(":j2: bla") << QStringLiteral(":j2: bla") << map << true << false << true << false << -1;
0639     QTest::newRow(":j2: bla 1") << QStringLiteral(":j2: bla") << QStringLiteral("TV bla") << map << true << false << true << false << 4;
0640     QTest::newRow(":j2: bla 2") << QStringLiteral(":j2: :j2:") << QStringLiteral(":j2: TV") << map << true << false << true << false << -1;
0641     QTest::newRow("La mais il n est pas ici ") << QStringLiteral("La mais il n est pas ici ") << QStringLiteral("La mais il n est pas ici ") << map << true
0642                                                << false << true << false << 25;
0643     QTest::newRow("a noel") << QStringLiteral("a noel") << QStringLiteral("a noal") << map << true << false << true << false << 6;
0644     // TODO add more
0645 }
0646 
0647 void AutoCorrectionTest::shouldReplaceWithMultiOption()
0648 {
0649     QFETCH(QString, originalString);
0650     QFETCH(QString, convertedString);
0651     QFETCH(mapAutoCorrect, convertStringHash);
0652     QFETCH(bool, enable);
0653     QFETCH(bool, uppercaseFirstCharOfSentence);
0654     QFETCH(bool, advancedAutocorrect);
0655     QFETCH(bool, fixtwouppercase);
0656     QFETCH(int, position);
0657 
0658     TextAutoCorrectionCore::AutoCorrection autocorrection;
0659     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0660     settings->setEnabledAutoCorrection(enable);
0661     settings->setAdvancedAutocorrect(advancedAutocorrect);
0662     settings->setAutocorrectEntries(convertStringHash);
0663     settings->setUppercaseFirstCharOfSentence(uppercaseFirstCharOfSentence);
0664     settings->setFixTwoUppercaseChars(fixtwouppercase);
0665     autocorrection.setAutoCorrectionSettings(settings);
0666 
0667     QTextDocument doc;
0668     doc.setPlainText(originalString);
0669     position = (position == -1) ? originalString.length() : position;
0670     autocorrection.autocorrect(false, doc, position);
0671     QCOMPARE(doc.toPlainText(), convertedString);
0672 }
0673 
0674 void AutoCorrectionTest::shouldAddNonBreakingSpaceBeforeAfterQuote()
0675 {
0676     TextAutoCorrectionCore::AutoCorrection autocorrection;
0677     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0678     settings->setEnabledAutoCorrection(true);
0679     settings->setReplaceDoubleQuotes(true);
0680     settings->setReplaceSingleQuotes(true);
0681     settings->setLanguage(QStringLiteral("fr"));
0682     settings->setAddNonBreakingSpace(true);
0683     // TODO fix me verify why it doesn't use no breaking space
0684     const QChar nbsp = QChar(/*QChar::Nbsp*/ QLatin1Char('b'));
0685     settings->setNonBreakingSpace(nbsp);
0686     autocorrection.setAutoCorrectionSettings(settings);
0687 
0688     TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes doubleQuote;
0689     doubleQuote.begin = QLatin1Char('A');
0690     doubleQuote.end = QLatin1Char('B');
0691     settings->setTypographicDoubleQuotes(doubleQuote);
0692 
0693     TextAutoCorrectionCore::AutoCorrectionUtils::TypographicQuotes simpleQuote;
0694     simpleQuote.begin = QLatin1Char('A');
0695     simpleQuote.end = QLatin1Char('B');
0696 
0697     settings->setTypographicSingleQuotes(simpleQuote);
0698     autocorrection.setAutoCorrectionSettings(settings);
0699 
0700     QTextDocument doc;
0701     QString text = QStringLiteral("sss");
0702 
0703     doc.setPlainText(QLatin1Char('"') + text);
0704     int position = text.length();
0705     autocorrection.autocorrect(false, doc, position);
0706 
0707     QCOMPARE(doc.toPlainText(), QString(doubleQuote.begin + nbsp + text));
0708 
0709     text = QStringLiteral("sss");
0710     doc.setPlainText(text + QStringLiteral("\""));
0711     position = text.length();
0712     autocorrection.autocorrect(false, doc, position);
0713     QCOMPARE(doc.toPlainText(), QString(text + nbsp + doubleQuote.end));
0714 
0715     // Simple quote
0716     text = QStringLiteral("sss");
0717     doc.setPlainText(text + QStringLiteral("\'"));
0718     position = text.length();
0719     autocorrection.autocorrect(false, doc, position);
0720     QCOMPARE(doc.toPlainText(), QString(text + nbsp + simpleQuote.end));
0721 
0722     text = QStringLiteral("sss");
0723     doc.setPlainText(QStringLiteral("\"") + text + QStringLiteral("\""));
0724     position = text.length();
0725     autocorrection.autocorrect(false, doc, position);
0726     QCOMPARE(doc.toPlainText(), QString(doubleQuote.begin + nbsp + text + nbsp + doubleQuote.end));
0727 
0728     // Simple quote
0729     text = QStringLiteral("sss");
0730     doc.setPlainText(QStringLiteral("\'") + text + QStringLiteral("\'"));
0731     position = text.length();
0732     autocorrection.autocorrect(false, doc, position);
0733     QCOMPARE(doc.toPlainText(), QString(simpleQuote.begin + nbsp + text + nbsp + simpleQuote.end));
0734 
0735     text = QLatin1Char('(');
0736     doc.setPlainText(QStringLiteral("\"") + text + QStringLiteral("\""));
0737     position = text.length();
0738     autocorrection.autocorrect(false, doc, position);
0739     QCOMPARE(doc.toPlainText(), QString(doubleQuote.begin + nbsp + text + nbsp + doubleQuote.end));
0740 }
0741 
0742 void AutoCorrectionTest::shouldAutoFormatURLs()
0743 {
0744     TextAutoCorrectionCore::AutoCorrection autocorrection;
0745     auto settings = new TextAutoCorrectionCore::AutoCorrectionSettings;
0746     settings->setAutoFormatUrl(true);
0747     autocorrection.setAutoCorrectionSettings(settings);
0748     // autocorrection.autocorrect(true, doc, position);
0749 
0750     //    QTextCursor cursor(&doc);
0751     //    cursor.setPosition(2);
0752     //    QTextCharFormat charFormat = cursor.charFormat();
0753     //    QCOMPARE(charFormat.font().underline(), false);
0754     //    QCOMPARE(charFormat.font().bold(), true);
0755     //    QCOMPARE(charFormat.font().strikeOut(), false);
0756 }
0757 
0758 #include "moc_autocorrectiontest.cpp"