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

0001 /*
0002   SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "importexportautocorrectionlibreofficetest.h"
0008 #include "export/exportlibreofficeautocorrection.h"
0009 #include "import/importlibreofficeautocorrection.h"
0010 #include <QStandardPaths>
0011 #include <QTemporaryFile>
0012 #include <QTest>
0013 
0014 QTEST_MAIN(ImportExportAutocorrectionLibreOfficeTest)
0015 ImportExportAutocorrectionLibreOfficeTest::ImportExportAutocorrectionLibreOfficeTest(QObject *parent)
0016     : QObject{parent}
0017 {
0018     QStandardPaths::setTestModeEnabled(true);
0019 }
0020 
0021 void ImportExportAutocorrectionLibreOfficeTest::shouldImportExport()
0022 {
0023     QFETCH(QString, filename);
0024 
0025     const QString originalFile = QLatin1String(AUTOCORRECTION_DATA_DIR) + QLatin1Char('/') + filename;
0026 
0027     // First step
0028     TextAutoCorrectionCore::ImportLibreOfficeAutocorrection import;
0029     QString messageError;
0030     if (import.import(originalFile, messageError, TextAutoCorrectionCore::ImportAbstractAutocorrection::All)) {
0031         const auto mUpperCaseExceptions = import.upperCaseExceptions();
0032         const auto mTwoUpperLetterExceptions = import.twoUpperLetterExceptions();
0033         const auto mAutocorrectEntries = import.autocorrectEntries();
0034 
0035         // Test export
0036         TextAutoCorrectionCore::ExportLibreOfficeAutocorrection correct;
0037         correct.setAutocorrectEntries(mAutocorrectEntries);
0038         correct.setUpperCaseExceptions(mUpperCaseExceptions);
0039         correct.setTwoUpperLetterExceptions(mTwoUpperLetterExceptions);
0040         QString message;
0041 
0042         QTemporaryFile temporaryShareFile;
0043         temporaryShareFile.open();
0044 
0045         if (!correct.exportData(QString(), temporaryShareFile.fileName(), message, QString())) {
0046             QVERIFY(false);
0047             qWarning() << "We can't save in file :" << temporaryShareFile.fileName();
0048         }
0049         // Close temporary file
0050         temporaryShareFile.close();
0051 
0052         // Reimport
0053         TextAutoCorrectionCore::ImportLibreOfficeAutocorrection importTwoStep;
0054         if (import.import(temporaryShareFile.fileName(), messageError, TextAutoCorrectionCore::ImportAbstractAutocorrection::All)) {
0055             const auto mUpperCaseExceptionsStep2 = import.upperCaseExceptions();
0056             const auto mTwoUpperLetterExceptionsStep2 = import.twoUpperLetterExceptions();
0057             const auto mAutocorrectEntriesStep2 = import.autocorrectEntries();
0058             QCOMPARE(mUpperCaseExceptionsStep2, mUpperCaseExceptions);
0059             QCOMPARE(mTwoUpperLetterExceptionsStep2, mTwoUpperLetterExceptions);
0060             QCOMPARE(mAutocorrectEntriesStep2, mAutocorrectEntries);
0061         } else {
0062             qWarning() << "Impossible to import " << temporaryShareFile.fileName();
0063             QVERIFY(false);
0064         }
0065     } else {
0066         qWarning() << "Impossible to import " << originalFile;
0067         QVERIFY(false);
0068     }
0069 }
0070 
0071 void ImportExportAutocorrectionLibreOfficeTest::shouldImportExport_data()
0072 {
0073     QTest::addColumn<QString>("filename");
0074     QTest::newRow("disable") << QStringLiteral("acor_fr.dat");
0075 }
0076 
0077 #include "moc_importexportautocorrectionlibreofficetest.cpp"