sieveeditorgraphicalmodewidgettest.cpp 13.3 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
    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);
67
    //First parsing
Laurent Montel's avatar
Laurent Montel committed
68
    bool result = false;
Laurent Montel's avatar
Laurent Montel committed
69
    QString doc = KSieveUi::ParsingUtil::parseScript(script, result);
Laurent Montel's avatar
Laurent Montel committed
70
71
72
73
74
    QCOMPARE(success, result);
    if (success) {
        QString error;
        w.loadScript(doc, error);
        QCOMPARE(hasError, !error.isEmpty());
75
76
77
78
79
80
81
82
83
84
85

        QString generatedScript = w.currentscript();

        doc = KSieveUi::ParsingUtil::parseScript(generatedScript, result);
        QCOMPARE(success, result);

        //second parsing
        w.loadScript(doc, error);
        QCOMPARE(hasError, !error.isEmpty());
        generatedScript = w.currentscript();

Laurent Montel's avatar
Laurent Montel committed
86
87
88
89
90
91
        //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
92
93
        //qDebug() << " generatedScript" << generatedScript;

Laurent Montel's avatar
Laurent Montel committed
94
95
        // compare to reference file
        QStringList args = QStringList()
Laurent Montel's avatar
Laurent Montel committed
96
97
98
                           << QStringLiteral("-u")
                           << refFile
                           << generatedFile;
Laurent Montel's avatar
Laurent Montel committed
99
100
101
102
103
        QProcess proc;
        proc.setProcessChannelMode(QProcess::ForwardedChannels);
        proc.start(QStringLiteral("diff"), args);
        QVERIFY(proc.waitForFinished());

Laurent Montel's avatar
Laurent Montel committed
104
        QEXPECT_FAIL("regexp", "Expected failure on regexp file. Problem with header + comparator", Continue);
105
        QEXPECT_FAIL("test-current-date", "Expected failure on current-date file. We depend against current date", Continue);
106
        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
107
        QEXPECT_FAIL("foreverypart", "Expected failure on foreverypart file. A lot of parsing errors.", Continue);
Laurent Montel's avatar
Laurent Montel committed
108

Laurent Montel's avatar
Laurent Montel committed
109
        QEXPECT_FAIL("failed-1", "Expected failure on failed-1 file. anyof + allof.", Continue);
Laurent Montel's avatar
Laurent Montel committed
110
        QEXPECT_FAIL("failed-if-in-if", "Expected failure on failed-if-in-if file. Problem with if in another if.", Continue);
111
        QEXPECT_FAIL("test-comment2", "Expected failure on test-comment2 file. Problem with # and /* .", Continue);
Laurent Montel's avatar
Laurent Montel committed
112
        QEXPECT_FAIL("test-comment7", "Expected failure on test-comment7 file. Problem comment after an action .", Continue);
Laurent Montel's avatar
Laurent Montel committed
113
        QEXPECT_FAIL("test-date1", "Expected failure on test-date1 file. Problem with * in date.", Continue);
114
        QEXPECT_FAIL("wierd-script", "Missing ';' at the end of requires => invalid script", Continue);
Laurent Montel's avatar
Laurent Montel committed
115
116
        QCOMPARE(proc.exitCode(), 0);
    }
117
118
}

Laurent Montel's avatar
Laurent Montel committed
119
120
121
122
123
void SieveEditorGraphicalModeWidgetTest::shouldLoadScripts_data()
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<bool>("hasError");
    QTest::addColumn<bool>("success");
Laurent Montel's avatar
Laurent Montel committed
124
    QTest::newRow("test-vacation") << QStringLiteral("test-vacation") << false << true;
Laurent Montel's avatar
Laurent Montel committed
125

Laurent Montel's avatar
Laurent Montel committed
126
    QTest::newRow("test-foreach") << QStringLiteral("test-foreach") << false << true;
Laurent Montel's avatar
Laurent Montel committed
127
    QTest::newRow("not-condition") << QStringLiteral("not-condition") << false << true;
Laurent Montel's avatar
skip++    
Laurent Montel committed
128
    QTest::newRow("test-envelop-without-part") << QStringLiteral("test-envelop-without-part") << false << true;
Laurent Montel's avatar
Laurent Montel committed
129
    QTest::newRow("include") << QStringLiteral("include") << false << true;
Laurent Montel's avatar
skip++    
Laurent Montel committed
130
131
132
133
134
135
136
137
138
139
    QTest::newRow("notify") << QStringLiteral("notify") << false << true;
    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("test-forwarding") << QStringLiteral("test-forwarding") << false << true;
    QTest::newRow("test-elseif") << QStringLiteral("test-elseif") << false << true;
    QTest::newRow("test-address-order") << QStringLiteral("test-address-order") << false << true;
    QTest::newRow("test-regexp") << QStringLiteral("test-regexp") << false << true;
Laurent Montel's avatar
Laurent Montel committed
140
    QTest::newRow("emptyscript") << QStringLiteral("empty") << false << true;
Laurent Montel's avatar
Laurent Montel committed
141
    QTest::newRow("simplescript") << QStringLiteral("simple") << false << true;
Laurent Montel's avatar
Laurent Montel committed
142
    QTest::newRow("bodywithlist") << QStringLiteral("body") << false << true;
Laurent Montel's avatar
Laurent Montel committed
143
144
    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
145
146
147
    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;
Laurent Montel's avatar
Laurent Montel committed
148

Laurent Montel's avatar
Laurent Montel committed
149
150
151
    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
152
    QTest::newRow("test-address-order") << QStringLiteral("test-address-order") << false << true;
153
    QTest::newRow("test-regexp") << QStringLiteral("test-regexp") << false << true;
154
    QTest::newRow("test-envelop-without-part") << QStringLiteral("test-envelop-without-part") << false << true;
Laurent Montel's avatar
Laurent Montel committed
155
    QTest::newRow("test-anti-slash") << QStringLiteral("test-anti-slash") << false << true;
Laurent Montel's avatar
Laurent Montel committed
156
    QTest::newRow("test-current-date") << QStringLiteral("test-current-date") << false << true;
Laurent Montel's avatar
skip++    
Laurent Montel committed
157

158
    QTest::newRow("delete-headers") << QStringLiteral("delete-headers") << false << true;
Laurent Montel's avatar
Laurent Montel committed
159
160
    QTest::newRow("mailboxexist") << QStringLiteral("mailboxexist") << false << true;
    QTest::newRow("extract-text") << QStringLiteral("extract-text") << false << true;
161
162
163
    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;
Laurent Montel's avatar
Laurent Montel committed
164

Laurent Montel's avatar
Laurent Montel committed
165
    QTest::newRow("variable-lower") << QStringLiteral("variable-lower") << false << true;
Laurent Montel's avatar
Laurent Montel committed
166
167
168
169
170
171
    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;
Laurent Montel's avatar
skip++    
Laurent Montel committed
172

Laurent Montel's avatar
Laurent Montel committed
173
174
175
176
177
178
    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
179
180
181
    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
182
    QTest::newRow("test-spam") << QStringLiteral("test-spam") << false << true;
Laurent Montel's avatar
Laurent Montel committed
183
184
185
    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
186
187
    QTest::newRow("vacation-active-discard") << QStringLiteral("vacation-active-discard") << false << true;
    QTest::newRow("vacation-multiple") << QStringLiteral("vacation-multiple") << false << true;
188
    QTest::newRow("vacation-multiple-with-undefined-command") << QStringLiteral("vacation-multiple-with-undefined-command") << true << true;
Laurent Montel's avatar
Laurent Montel committed
189
    QTest::newRow("full-example1") << QStringLiteral("full-example1") << false << true;
Laurent Montel's avatar
Laurent Montel committed
190
191
    //We need to fix it.
    QTest::newRow("regexp") << QStringLiteral("regexp") << true << true;
Laurent Montel's avatar
Laurent Montel committed
192
    QTest::newRow("comparator") << QStringLiteral("comparator") << false << true;
Laurent Montel's avatar
Laurent Montel committed
193
    QTest::newRow("test-comment") << QStringLiteral("test-comment") << false << true;
Laurent Montel's avatar
Laurent Montel committed
194
195
    QTest::newRow("hasflag") << QStringLiteral("hasflag") << false << true;
    QTest::newRow("setflag") << QStringLiteral("setflag") << false << true;
Laurent Montel's avatar
Laurent Montel committed
196
    QTest::newRow("add-header") << QStringLiteral("add-header") << false << true;
197
    QTest::newRow("problem-with-inferior-char") << QStringLiteral("problem-with-inferior-char") << false << true;
198
199
200

    //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
201
202

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

204
    //Need to fix it
Laurent Montel's avatar
Laurent Montel committed
205
    QTest::newRow("foreverypart") << QStringLiteral("foreverypart") << true << true;
206
207

    QTest::newRow("test-foreverypart-complex") << QStringLiteral("test-foreverypart-complex") << false << true;
Laurent Montel's avatar
Laurent Montel committed
208
    QTest::newRow("test-envelop-subadress") << QStringLiteral("test-envelop-subadress") << false << true;
Laurent Montel's avatar
Laurent Montel committed
209
    QTest::newRow("test-current-date-custom") << QStringLiteral("test-current-date-custom") << false << true;
Laurent Montel's avatar
Laurent Montel committed
210
211
212

    //Failed
    QTest::newRow("failed-1") << QStringLiteral("failed-1") << true << true;
Laurent Montel's avatar
Laurent Montel committed
213
    QTest::newRow("failed-2") << QStringLiteral("failed-2") << false << true;
Laurent Montel's avatar
Laurent Montel committed
214
    QTest::newRow("failed-if-in-if") << QStringLiteral("failed-if-in-if") << true << true;
215
216

    QTest::newRow("test-comment2") << QStringLiteral("test-comment2") << false << true;
Laurent Montel's avatar
Laurent Montel committed
217
    QTest::newRow("test-comment3") << QStringLiteral("test-comment3") << false << true;
218
    QTest::newRow("test-comment4") << QStringLiteral("test-comment4") << false << true;
219
220

    QTest::newRow("test-value-as-list-quoted") << QStringLiteral("test-value-as-list-quoted") << false << true;
Laurent Montel's avatar
Laurent Montel committed
221
    QTest::newRow("test-header-value") << QStringLiteral("test-header-value") << false << true;
Laurent Montel's avatar
Laurent Montel committed
222
223

    QTest::newRow("test-comment5") << QStringLiteral("test-comment5") << false << true;
Laurent Montel's avatar
Laurent Montel committed
224
    QTest::newRow("test-comment6") << QStringLiteral("test-comment6") << false << true;
Laurent Montel's avatar
Laurent Montel committed
225
    QTest::newRow("test-comment7") << QStringLiteral("test-comment7") << false << true;
Laurent Montel's avatar
Laurent Montel committed
226
    QTest::newRow("test-comment8") << QStringLiteral("test-comment8") << false << true;
227
    QTest::newRow("test-comment9") << QStringLiteral("test-comment9") << false << true;
Laurent Montel's avatar
Laurent Montel committed
228
229

    QTest::newRow("test-date1") << QStringLiteral("test-date1") << false << true;
Laurent Montel's avatar
Laurent Montel committed
230
231

    QTest::newRow("test-header-multilist") << QStringLiteral("test-header-multilist") << false << true;
Laurent Montel's avatar
Laurent Montel committed
232
    QTest::newRow("test-header-multilist-comment") << QStringLiteral("test-header-multilist-comment") << false << true;
Laurent Montel's avatar
Laurent Montel committed
233
234

    QTest::newRow("wierd-script") << QStringLiteral("wierd-script") << false << true;
235
236

    QTest::newRow("test-text-with-backslash") << QStringLiteral("test-text-with-backslash") << false << true;
237
238
    //Bug 383756
    QTest::newRow("body-without-bodytype") << QStringLiteral("body-without-bodytype") << false << true;
Laurent Montel's avatar
Laurent Montel committed
239
240
}

241
QTEST_MAIN(SieveEditorGraphicalModeWidgetTest)