aboutsummaryrefslogtreecommitdiff
path: root/autotests/kconfig_compiler/kconfigcompiler_test.cpp
blob: bd8a0ef780cad09fd8243443f0ee5c8244b3fd2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
    Tests for KConfig Compiler

    SPDX-FileCopyrightText: 2005 Duncan Mac-Vicar <duncan@kde.org>
    SPDX-FileCopyrightText: 2009 Pino Toscano <pino@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "kconfigcompiler_test.h"
#include <QDir>
#include <QProcess>
#include <QString>
#include <QTest>
#include <qstandardpaths.h>

// QT5 TODO QTEST_GUILESS_MAIN(KConfigCompiler_Test)
QTEST_MAIN(KConfigCompiler_Test)

typedef const char *CompilerTestSet[];

static CompilerTestSet testCases = {"test1.cpp",
                                    "test1.h",
                                    "test2.cpp",
                                    "test2.h",
                                    "test3.cpp",
                                    "test3.h",
                                    "test3a.cpp",
                                    "test3a.h",
                                    "test4.cpp",
                                    "test4.h",
                                    "test5.cpp",
                                    "test5.h",
                                    "test6.cpp",
                                    "test6.h",
                                    "test7.cpp",
                                    "test7.h",
                                    "test8a.cpp",
                                    "test8a.h",
                                    "test8b.cpp",
                                    "test8b.h",
                                    "test8c.cpp",
                                    "test8c.h",
                                    "test9.h",
                                    "test9.cpp",
                                    "test10.h",
                                    "test10.cpp",
                                    "test11.h",
                                    "test11.cpp",
                                    "test11a.h",
                                    "test11a.cpp",
                                    "test12.h",
                                    "test12.cpp",
                                    "test13.h",
                                    "test13.cpp",
                                    "test_dpointer.cpp",
                                    "test_dpointer.h",
                                    "test_qdebugcategory.cpp",
                                    "test_qdebugcategory.h",
                                    "test_signal.cpp",
                                    "test_signal.h",
                                    "test_notifiers.cpp",
                                    "test_notifiers.h",
                                    "test_translation_kde.cpp",
                                    "test_translation_kde.h",
                                    "test_translation_kde_domain.cpp",
                                    "test_translation_kde_domain.h",
                                    "test_translation_qt.cpp",
                                    "test_translation_qt.h",
                                    "test_emptyentries.cpp",
                                    "test_emptyentries.h",
                                    "test_properties_minmax.cpp",
                                    "test_properties_minmax.h",
                                    "test_param_minmax.cpp",
                                    "test_param_minmax.h",
                                    "test_subgroups.cpp",
                                    "test_subgroups.h",
                                    nullptr};

static CompilerTestSet testCasesToRun = {"test1",
                                         "test2",
                                         "test3",
                                         "test3a",
                                         "test4",
                                         "test5",
                                         "test6",
                                         "test7",
                                         "test8",
                                         "test9",
                                         "test10",
                                         "test11",
                                         "test12",
                                         "test13",
                                         "test_enums_and_properties",
                                         "test_dpointer",
                                         "test_qdebugcategory",
                                         "test_signal",
                                         "test_translation_kde",
                                         "test_translation_kde_domain",
                                         "test_translation_qt",
                                         "test_emptyentries",
                                         "test_properties_minmax",
                                         "test_param_minmax",
                                         "test_subgroups",
                                         nullptr};

#if 0
static CompilerTestSet willFailCases = {
    // where is that QDir coming from?
    //"test9.cpp", NULL
    NULL
};
#endif

void KConfigCompiler_Test::initTestCase()
{
    m_diffExe = QStandardPaths::findExecutable(QStringLiteral("diff"));
    if (m_diffExe.isEmpty()) {
        qDebug() << "diff command not found, detailed info on comparison failure will not be available.";
    }
}

void KConfigCompiler_Test::testBaselineComparison_data()
{
    QTest::addColumn<QString>("testName");

    for (const char **it = testCases; *it; ++it) {
        QTest::newRow(*it) << QString::fromLatin1(*it);
    }
}

void KConfigCompiler_Test::testBaselineComparison()
{
    QFETCH(QString, testName);

    QFile file(QFINDTESTDATA(testName));
    QFile fileRef(QFINDTESTDATA(testName + QLatin1String(".ref")));

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open" << file.fileName() << "(" << testName << ")";
        QFAIL("Can't open file for comparison");
    }
    if (!fileRef.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open" << fileRef.fileName() << "(" << testName << ".ref )";
        QFAIL("Can't open file for comparison");
    }
    const QByteArray content = file.readAll();
    const QByteArray contentRef = fileRef.readAll();

    if (content != contentRef) {
        appendFileDiff(fileRef.fileName(), file.fileName());
    }

    QVERIFY(content == contentRef);
}

void KConfigCompiler_Test::testRunning_data()
{
    QTest::addColumn<QString>("testName");

    for (const char **it = testCasesToRun; *it; ++it) {
        QTest::newRow(*it) << QString::fromLatin1(*it);
    }
}

void KConfigCompiler_Test::testRunning()
{
    QFETCH(QString, testName);

#ifdef Q_OS_WIN
    testName += QLatin1String{".exe"};
#endif

    QString program = QFINDTESTDATA(testName);
    QVERIFY2(!program.isEmpty(), qPrintable(testName + QLatin1String(" must exist!")));
    QVERIFY2(QFile::exists(program), qPrintable(program + QLatin1String(" must exist!")));
    QProcess process;
    process.start(program, QStringList(), QIODevice::ReadOnly);
    if (process.waitForStarted()) {
        QVERIFY(process.waitForFinished());
    }
    QCOMPARE((int)process.error(), (int)QProcess::UnknownError);
    QCOMPARE(process.exitCode(), 0);

#ifdef Q_OS_HURD
    QTest::qWait(10);
#endif
}

void KConfigCompiler_Test::appendFileDiff(const QString &oldFile, const QString &newFile)
{
    if (m_diffExe.isEmpty()) {
        return;
    }

    QStringList args({QStringLiteral("-u"), QFileInfo(oldFile).absoluteFilePath(), QFileInfo(newFile).absoluteFilePath()});

    QProcess process;
    process.start(m_diffExe, args, QIODevice::ReadOnly);
    process.waitForStarted();
    process.waitForFinished();

    if (process.exitCode() == 1) {
        const QString diffFileName = oldFile + QStringLiteral(".diff");
        QFile diffFile(diffFileName);
        QVERIFY2(diffFile.open(QIODevice::WriteOnly), qPrintable(QLatin1String("Could not save diff file for ") + oldFile));
        diffFile.write(process.readAllStandardOutput());

        // force a failure to print the flename where we stored the diff.
        QVERIFY2(false, qPrintable(QLatin1String("This test failed, look at the following file for details: ") + diffFileName));
    }
}