sieveeditorgraphicalmodewidgettest.cpp 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
   Copyright (C) 2017 Laurent Montel <montel@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "sieveeditorgraphicalmodewidgettest.h"
Laurent Montel's avatar
Laurent Montel committed
21 22
#include "../sieveeditorgraphicalmodewidget.h"
#include "scriptsparsing/parsingutil.h"
Laurent Montel's avatar
Laurent Montel committed
23
#include <PimCommon/SieveSyntaxHighlighterUtil>
24
#include <QTest>
Laurent Montel's avatar
Laurent Montel committed
25 26
#include <QStandardPaths>
#include <QProcess>
27

Laurent Montel's avatar
Laurent Montel committed
28 29 30 31 32
#ifndef Q_OS_WIN
void initLocale()
{
    setenv("LC_ALL", "en_US.utf-8", 1);
}
Laurent Montel's avatar
Laurent Montel committed
33

Laurent Montel's avatar
Laurent Montel committed
34 35 36
Q_CONSTRUCTOR_FUNCTION(initLocale)
#endif

37 38 39
SieveEditorGraphicalModeWidgetTest::SieveEditorGraphicalModeWidgetTest(QObject *parent)
    : QObject(parent)
{
Laurent Montel's avatar
Laurent Montel committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    QStandardPaths::setTestModeEnabled(true);
}

QString readSieveFile(const QString &sieveFile)
{
    QFile file(sieveFile);
    file.open(QIODevice::ReadOnly);
    Q_ASSERT(file.isOpen());
    const QString data = QString::fromUtf8(file.readAll());
    return data;
}

void SieveEditorGraphicalModeWidgetTest::shouldLoadScripts()
{
    QFETCH(QString, input);
    QFETCH(bool, hasError);
    QFETCH(bool, success);

    KSieveUi::SieveEditorGraphicalModeWidget w;
Laurent Montel's avatar
Laurent Montel committed
59 60 61 62
    PimCommon::SieveSyntaxHighlighterUtil sieveHighlighterutil;
    const QStringList capabilities = sieveHighlighterutil.fullCapabilities();

    w.setSieveCapabilities(capabilities);
Laurent Montel's avatar
Laurent Montel committed
63 64 65 66 67 68 69 70 71 72 73 74
    const QString originalFile = QLatin1String(KSIEVEUI_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral(".siv");
    const QString refFile = QLatin1String(KSIEVEUI_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral("-ref.siv");
    const QString generatedFile = QLatin1String(KSIEVEUI_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral("-generated.siv");
    QString script = readSieveFile(originalFile);
    bool result = false;
    const QDomDocument doc = KSieveUi::ParsingUtil::parseScript(script, result);
    QCOMPARE(success, result);
    if (success) {
        QString error;
        w.loadScript(doc, error);
        QCOMPARE(hasError, !error.isEmpty());
        const QString generatedScript = w.currentscript();
75

Laurent Montel's avatar
Laurent Montel committed
76 77 78 79 80 81
        //Create generated file
        QFile f(generatedFile);
        QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
        f.write(generatedScript.toUtf8());
        f.close();

Laurent Montel's avatar
Laurent Montel committed
82 83
        //qDebug() << " generatedScript" << generatedScript;

Laurent Montel's avatar
Laurent Montel committed
84 85
        // compare to reference file
        QStringList args = QStringList()
Laurent Montel's avatar
Laurent Montel committed
86 87 88
                           << QStringLiteral("-u")
                           << refFile
                           << generatedFile;
Laurent Montel's avatar
Laurent Montel committed
89 90 91 92 93
        QProcess proc;
        proc.setProcessChannelMode(QProcess::ForwardedChannels);
        proc.start(QStringLiteral("diff"), args);
        QVERIFY(proc.waitForFinished());

Laurent Montel's avatar
Laurent Montel committed
94
        QEXPECT_FAIL("regexp", "Expected failure on regexp file. Problem with header + comparator", Continue);
95
        QEXPECT_FAIL("test-current-date", "Expected failure on current-date file. We depend against current date", Continue);
96
        QEXPECT_FAIL("delete-headers-with-index", "Expected failure on delete-headers-with-index file. We depend against \"index\" feature", Continue);
Laurent Montel's avatar
Laurent Montel committed
97
        QEXPECT_FAIL("foreverypart", "Expected failure on foreverypart file. A lot of parsing errors.", Continue);
Laurent Montel's avatar
Laurent Montel committed
98

Laurent Montel's avatar
Laurent Montel committed
99
        QEXPECT_FAIL("failed-1", "Expected failure on failed-1 file. anyof + allof.", Continue);
Laurent Montel's avatar
Laurent Montel committed
100 101
        QEXPECT_FAIL("failed-2", "Expected failure on failed-2 file. Problem with body + text.", Continue);
        QEXPECT_FAIL("failed-if-in-if", "Expected failure on failed-if-in-if file. Problem with if in another if.", Continue);
102
        QEXPECT_FAIL("test-comment2", "Expected failure on test-comment2 file. Problem with # and /* .", Continue);
Laurent Montel's avatar
Laurent Montel committed
103 104
        QCOMPARE(proc.exitCode(), 0);
    }
105 106
}

Laurent Montel's avatar
Laurent Montel committed
107 108 109 110 111 112
void SieveEditorGraphicalModeWidgetTest::shouldLoadScripts_data()
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<bool>("hasError");
    QTest::addColumn<bool>("success");

Laurent Montel's avatar
Laurent Montel committed
113
    QTest::newRow("emptyscript") << QStringLiteral("empty") << false << true;
Laurent Montel's avatar
Laurent Montel committed
114
    QTest::newRow("simplescript") << QStringLiteral("simple") << false << true;
Laurent Montel's avatar
Laurent Montel committed
115
    QTest::newRow("bodywithlist") << QStringLiteral("body") << false << true;
Laurent Montel's avatar
Laurent Montel committed
116 117
    QTest::newRow("add-flags") << QStringLiteral("add-flags") << false << true;
    QTest::newRow("test-virus") << QStringLiteral("test-virus") << false << true;
Laurent Montel's avatar
Laurent Montel committed
118 119 120 121
    QTest::newRow("replace") << QStringLiteral("replace") << false << true;
    QTest::newRow("test-envelop") << QStringLiteral("test-envelop") << false << true;
    QTest::newRow("test-servermetadataexists") << QStringLiteral("test-servermetadataexists") << false << true;
    QTest::newRow("test-foreach") << QStringLiteral("test-foreach") << false << true;
Laurent Montel's avatar
Laurent Montel committed
122 123 124
    QTest::newRow("test-vacation") << QStringLiteral("test-vacation") << false << true;
    QTest::newRow("test-forwarding") << QStringLiteral("test-forwarding") << false << true;
    QTest::newRow("test-elseif") << QStringLiteral("test-elseif") << false << true;
Laurent Montel's avatar
Laurent Montel committed
125
    QTest::newRow("test-address-order") << QStringLiteral("test-address-order") << false << true;
126
    QTest::newRow("test-regexp") << QStringLiteral("test-regexp") << false << true;
127
    QTest::newRow("test-envelop-without-part") << QStringLiteral("test-envelop-without-part") << false << true;
Laurent Montel's avatar
Laurent Montel committed
128
    QTest::newRow("test-anti-slash") << QStringLiteral("test-anti-slash") << false << true;
Laurent Montel's avatar
Laurent Montel committed
129 130
    QTest::newRow("test-current-date") << QStringLiteral("test-current-date") << false << true;
    QTest::newRow("not-condition") << QStringLiteral("not-condition") << false << true;
131
    QTest::newRow("delete-headers") << QStringLiteral("delete-headers") << false << true;
Laurent Montel's avatar
Laurent Montel committed
132 133
    QTest::newRow("mailboxexist") << QStringLiteral("mailboxexist") << false << true;
    QTest::newRow("extract-text") << QStringLiteral("extract-text") << false << true;
134 135 136 137
    QTest::newRow("enclose") << QStringLiteral("enclose") << false << true;
    QTest::newRow("add-single-flag") << QStringLiteral("add-single-flag") << false << true;
    QTest::newRow("fileinfo") << QStringLiteral("fileinfo") << false << true;
    QTest::newRow("include") << QStringLiteral("include") << false << true;
Laurent Montel's avatar
Laurent Montel committed
138
    QTest::newRow("variable-lower") << QStringLiteral("variable-lower") << false << true;
Laurent Montel's avatar
Laurent Montel committed
139 140 141 142 143 144 145
    QTest::newRow("test-environnement") << QStringLiteral("test-environnement") << false << true;
    QTest::newRow("test-has-flags") << QStringLiteral("test-has-flags") << false << true;
    QTest::newRow("test-spam-plus") << QStringLiteral("test-spam-plus") << false << true;
    QTest::newRow("remove-flags") << QStringLiteral("remove-flags") << false << true;
    QTest::newRow("variable") << QStringLiteral("variable") << false << true;
    QTest::newRow("break") << QStringLiteral("break") << false << true;
    QTest::newRow("notify") << QStringLiteral("notify") << false << true;
Laurent Montel's avatar
Laurent Montel committed
146 147 148 149 150 151
    QTest::newRow("test-exists") << QStringLiteral("test-exists") << false << true;
    QTest::newRow("vacation") << QStringLiteral("vacation") << false << true;
    QTest::newRow("test-metadataexists") << QStringLiteral("test-metadataexists") << false << true;
    QTest::newRow("test-date") << QStringLiteral("test-date") << false << true;
    QTest::newRow("redirect") << QStringLiteral("redirect") << false << true;
    QTest::newRow("convert-action") << QStringLiteral("convert-action") << false << true;
Laurent Montel's avatar
Laurent Montel committed
152 153 154
    QTest::newRow("address-with-default-all-parameter") << QStringLiteral("address-with-default-all-parameter") << false << true;
    QTest::newRow("not-condition2") << QStringLiteral("not-condition2") << false << true;
    QTest::newRow("address-with-default-is-parameter") << QStringLiteral("address-with-default-is-parameter") << false << true;
Laurent Montel's avatar
Laurent Montel committed
155
    QTest::newRow("test-spam") << QStringLiteral("test-spam") << false << true;
Laurent Montel's avatar
Laurent Montel committed
156 157 158
    QTest::newRow("reject") << QStringLiteral("reject") << false << true;
    QTest::newRow("keep") << QStringLiteral("keep") << false << true;
    QTest::newRow("test-headers") << QStringLiteral("test-headers") << false << true;
Laurent Montel's avatar
Laurent Montel committed
159 160
    QTest::newRow("vacation-active-discard") << QStringLiteral("vacation-active-discard") << false << true;
    QTest::newRow("vacation-multiple") << QStringLiteral("vacation-multiple") << false << true;
161
    QTest::newRow("vacation-multiple-with-undefined-command") << QStringLiteral("vacation-multiple-with-undefined-command") << true << true;
Laurent Montel's avatar
Laurent Montel committed
162
    QTest::newRow("full-example1") << QStringLiteral("full-example1") << false << true;
Laurent Montel's avatar
Laurent Montel committed
163 164
    //We need to fix it.
    QTest::newRow("regexp") << QStringLiteral("regexp") << true << true;
Laurent Montel's avatar
Laurent Montel committed
165
    QTest::newRow("comparator") << QStringLiteral("comparator") << false << true;
Laurent Montel's avatar
Laurent Montel committed
166
    QTest::newRow("test-comment") << QStringLiteral("test-comment") << false << true;
Laurent Montel's avatar
Laurent Montel committed
167 168
    QTest::newRow("hasflag") << QStringLiteral("hasflag") << false << true;
    QTest::newRow("setflag") << QStringLiteral("setflag") << false << true;
Laurent Montel's avatar
Laurent Montel committed
169
    QTest::newRow("add-header") << QStringLiteral("add-header") << false << true;
170
    QTest::newRow("problem-with-inferior-char") << QStringLiteral("problem-with-inferior-char") << false << true;
171 172 173

    //We need to implement index support
    QTest::newRow("delete-headers-with-index") << QStringLiteral("delete-headers-with-index") << true << true;
Laurent Montel's avatar
Laurent Montel committed
174 175

    QTest::newRow("notify-2") << QStringLiteral("notify-2") << false << true;
Laurent Montel's avatar
Laurent Montel committed
176

177
    //Need to fix it
Laurent Montel's avatar
Laurent Montel committed
178
    QTest::newRow("foreverypart") << QStringLiteral("foreverypart") << true << true;
179 180

    QTest::newRow("test-foreverypart-complex") << QStringLiteral("test-foreverypart-complex") << false << true;
Laurent Montel's avatar
Laurent Montel committed
181
    QTest::newRow("test-envelop-subadress") << QStringLiteral("test-envelop-subadress") << false << true;
Laurent Montel's avatar
Laurent Montel committed
182
    QTest::newRow("test-current-date-custom") << QStringLiteral("test-current-date-custom") << false << true;
Laurent Montel's avatar
Laurent Montel committed
183 184 185

    //Failed
    QTest::newRow("failed-1") << QStringLiteral("failed-1") << true << true;
Laurent Montel's avatar
Laurent Montel committed
186
    QTest::newRow("failed-2") << QStringLiteral("failed-2") << true << true;
Laurent Montel's avatar
Laurent Montel committed
187
    QTest::newRow("failed-if-in-if") << QStringLiteral("failed-if-in-if") << true << true;
188 189

    QTest::newRow("test-comment2") << QStringLiteral("test-comment2") << false << true;
Laurent Montel's avatar
Laurent Montel committed
190
    QTest::newRow("test-comment3") << QStringLiteral("test-comment3") << false << true;
191
    QTest::newRow("test-comment4") << QStringLiteral("test-comment4") << false << true;
Laurent Montel's avatar
Laurent Montel committed
192 193
}

194
QTEST_MAIN(SieveEditorGraphicalModeWidgetTest)