tests/auto/headers/tst_headers.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <QtCore/QtCore>
       
    42 #include <QtTest/QtTest>
       
    43 
       
    44 class tst_Headers: public QObject
       
    45 {
       
    46     Q_OBJECT
       
    47 public:
       
    48     tst_Headers();
       
    49 
       
    50 private slots:
       
    51     void initTestCase();
       
    52 
       
    53     void licenseCheck_data() { allSourceFilesData(); }
       
    54     void licenseCheck();
       
    55 
       
    56     void privateSlots_data() { allHeadersData(); }
       
    57     void privateSlots();
       
    58 
       
    59     void macros_data() { allHeadersData(); }
       
    60     void macros();
       
    61 
       
    62 private:
       
    63     static QStringList getFiles(const QString &path,
       
    64                                 const QStringList dirFilters,
       
    65                                 const QRegExp &exclude);
       
    66     static QStringList getHeaders(const QString &path);
       
    67     static QStringList getSourceFiles(const QString &path);
       
    68 
       
    69     void allSourceFilesData();
       
    70     void allHeadersData();
       
    71     QStringList headers;
       
    72     const QRegExp copyrightPattern;
       
    73     const QRegExp licensePattern;
       
    74     const QRegExp moduleTest;
       
    75     QString qtSrcDir;
       
    76 };
       
    77 
       
    78 tst_Headers::tst_Headers() :
       
    79     copyrightPattern("\\*\\* Copyright \\(C\\) 20[0-9][0-9] Nokia Corporation and/or its subsidiary\\(-ies\\)."),
       
    80     licensePattern("\\*\\* \\$QT_BEGIN_LICENSE:(LGPL|BSD|3RDPARTY)\\$"),
       
    81     moduleTest(QLatin1String("\\*\\* This file is part of the .+ of the Qt Toolkit."))
       
    82 {
       
    83 }
       
    84 
       
    85 QStringList tst_Headers::getFiles(const QString &path,
       
    86                                   const QStringList dirFilters,
       
    87                                   const QRegExp &excludeReg)
       
    88 {
       
    89     const QDir dir(path);
       
    90     const QStringList dirs(dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot));
       
    91     QStringList result;
       
    92 
       
    93     foreach (QString subdir, dirs)
       
    94         result += getFiles(path + "/" + subdir, dirFilters, excludeReg);
       
    95 
       
    96     QStringList entries = dir.entryList(dirFilters, QDir::Files);
       
    97     entries = entries.filter(excludeReg);
       
    98     foreach (QString entry, entries)
       
    99         result += path + "/" + entry;
       
   100 
       
   101     return result;
       
   102 }
       
   103 
       
   104 QStringList tst_Headers::getHeaders(const QString &path)
       
   105 {
       
   106     return getFiles(path, QStringList("*.h"), QRegExp("^(?!ui_)"));
       
   107 }
       
   108 
       
   109 QStringList tst_Headers::getSourceFiles(const QString &path)
       
   110 {
       
   111     return getFiles(path, QStringList("*.cpp"), QRegExp("^(?!(moc_|qrc_))"));
       
   112 }
       
   113 
       
   114 void tst_Headers::initTestCase()
       
   115 {
       
   116     qtSrcDir = QString::fromLocal8Bit(qgetenv("QTSRCDIR").isEmpty()
       
   117                ? qgetenv("QTDIR")
       
   118                : qgetenv("QTSRCDIR"));
       
   119 
       
   120     headers = getHeaders(qtSrcDir + "/src");
       
   121 
       
   122 #ifndef Q_OS_WINCE
       
   123     // Windows CE does not have any headers on the test device
       
   124     QVERIFY2(!headers.isEmpty(), "No headers were found, something is wrong with the auto test setup.");
       
   125 #endif
       
   126 
       
   127     QVERIFY(copyrightPattern.isValid());
       
   128     QVERIFY(licensePattern.isValid());
       
   129 }
       
   130 
       
   131 void tst_Headers::allSourceFilesData()
       
   132 {
       
   133     QTest::addColumn<QString>("sourceFile");
       
   134 
       
   135     QStringList sourceFiles;
       
   136     static char const * const subdirs[] = {
       
   137         "/config.tests",
       
   138         "/demos",
       
   139         "/doc",
       
   140         "/examples",
       
   141         "/mkspecs",
       
   142         "/qmake",
       
   143         "/src",
       
   144         "/tests",
       
   145         "/tools",
       
   146         "/util"
       
   147     };
       
   148 
       
   149     for (int i = 0; i < sizeof(subdirs) / sizeof(subdirs[0]); ++i) {
       
   150         sourceFiles << getSourceFiles(qtSrcDir + subdirs[i]);
       
   151         sourceFiles << getHeaders(qtSrcDir + subdirs[i]);
       
   152     }
       
   153 
       
   154     foreach (QString sourceFile, sourceFiles) {
       
   155         if (sourceFile.contains("/3rdparty/")
       
   156             || sourceFile.contains("/tests/auto/qmake/testdata/bundle-spaces/main.cpp")
       
   157             || sourceFile.contains("/demos/embedded/fluidlauncher/pictureflow.cpp")
       
   158             || sourceFile.contains("/tools/porting/src/")
       
   159             || sourceFile.contains("/tools/assistant/lib/fulltextsearch/")
       
   160             || sourceFile.endsWith("_pch.h.cpp")
       
   161             || sourceFile.endsWith(".ui.h")
       
   162             || sourceFile.endsWith("/src/corelib/global/qconfig.h")
       
   163             || sourceFile.endsWith("/src/corelib/global/qconfig.cpp")
       
   164             || sourceFile.endsWith("/src/tools/uic/qclass_lib_map.h"))
       
   165             continue;
       
   166 
       
   167         QTest::newRow(qPrintable(sourceFile)) << sourceFile;
       
   168     }
       
   169 }
       
   170 
       
   171 void tst_Headers::allHeadersData()
       
   172 {
       
   173     QTest::addColumn<QString>("header");
       
   174 
       
   175     if (headers.isEmpty())
       
   176         QSKIP("can't find any headers in your $QTDIR/src", SkipAll);
       
   177 
       
   178     foreach (QString hdr, headers) {
       
   179         if (hdr.contains("/3rdparty/") || hdr.endsWith("/src/tools/uic/qclass_lib_map.h"))
       
   180             continue;
       
   181 
       
   182         QTest::newRow(qPrintable(hdr)) << hdr;
       
   183     }
       
   184 }
       
   185 
       
   186 void tst_Headers::licenseCheck()
       
   187 {
       
   188     QFETCH(QString, sourceFile);
       
   189 
       
   190     QFile f(sourceFile);
       
   191     QVERIFY(f.open(QIODevice::ReadOnly));
       
   192     QByteArray data = f.readAll();
       
   193     data.replace("\r\n", "\n"); // Windows
       
   194     data.replace('\r', '\n'); // Mac OS9
       
   195     QStringList content = QString::fromLocal8Bit(data).split("\n");
       
   196 
       
   197     if (content.first().contains("generated")) {
       
   198         content.takeFirst();
       
   199         if (content.first().isEmpty())
       
   200             content.takeFirst();
       
   201     }
       
   202 
       
   203     if (sourceFile.endsWith("/tests/auto/linguist/lupdate/testdata/good/merge_ordering/foo.cpp")
       
   204         || sourceFile.endsWith("/tests/auto/linguist/lupdate/testdata/good/mergecpp/finddialog.cpp"))
       
   205     {
       
   206         // These files are meant to start with empty lines.
       
   207         while (content.first().isEmpty() || content.first().startsWith("//"))
       
   208             content.takeFirst();
       
   209     }
       
   210 
       
   211     QVERIFY(licensePattern.exactMatch(content.value(8)) ||
       
   212             licensePattern.exactMatch(content.value(5)));
       
   213     QString licenseType = licensePattern.cap(1);
       
   214 
       
   215     int i = 0;
       
   216 
       
   217     QCOMPARE(content.at(i++), QString("/****************************************************************************"));
       
   218     if (licenseType != "3RDPARTY") {
       
   219         QCOMPARE(content.at(i++), QString("**"));
       
   220         if (sourceFile.endsWith("/tests/auto/qabstractitemmodel/dynamictreemodel.cpp")
       
   221             || sourceFile.endsWith("/tests/auto/qabstractitemmodel/dynamictreemodel.h")
       
   222             || sourceFile.endsWith("/src/network/kernel/qnetworkproxy_p.h"))
       
   223         {
       
   224             // These files are not copyrighted by Nokia.
       
   225             ++i;
       
   226         } else {
       
   227             QVERIFY(copyrightPattern.exactMatch(content.at(i++)));
       
   228         }
       
   229         i++;
       
   230         QCOMPARE(content.at(i++), QString("** Contact: Nokia Corporation (qt-info@nokia.com)"));
       
   231     }
       
   232     QCOMPARE(content.at(i++), QString("**"));
       
   233     QVERIFY(moduleTest.exactMatch(content.at(i++)));
       
   234     QCOMPARE(content.at(i++), QString("**"));
       
   235 }
       
   236 
       
   237 void tst_Headers::privateSlots()
       
   238 {
       
   239     QFETCH(QString, header);
       
   240 
       
   241     if (header.endsWith("/qobjectdefs.h"))
       
   242         return;
       
   243 
       
   244     QFile f(header);
       
   245     QVERIFY(f.open(QIODevice::ReadOnly));
       
   246 
       
   247     QStringList content = QString::fromLocal8Bit(f.readAll()).split("\n");
       
   248     foreach (QString line, content) {
       
   249         if (line.contains("Q_PRIVATE_SLOT("))
       
   250             QVERIFY(line.contains("_q_"));
       
   251     }
       
   252 }
       
   253 
       
   254 void tst_Headers::macros()
       
   255 {
       
   256     QFETCH(QString, header);
       
   257 
       
   258     if (header.endsWith("_p.h") || header.endsWith("_pch.h")
       
   259         || header.contains("global/qconfig-") || header.endsWith("/qconfig.h")
       
   260         || header.contains("/src/tools/") || header.contains("/src/plugins/")
       
   261         || header.endsWith("/qiconset.h") || header.endsWith("/qfeatures.h")
       
   262         || header.endsWith("qt_windows.h"))
       
   263         return;
       
   264 
       
   265     QFile f(header);
       
   266     QVERIFY(f.open(QIODevice::ReadOnly));
       
   267 
       
   268     QByteArray data = f.readAll();
       
   269     QStringList content = QString::fromLocal8Bit(data.replace('\r', "")).split("\n");
       
   270 
       
   271     int beginHeader = content.indexOf("QT_BEGIN_HEADER");
       
   272     int endHeader = content.lastIndexOf("QT_END_HEADER");
       
   273 
       
   274     QVERIFY(beginHeader >= 0);
       
   275     QVERIFY(endHeader >= 0);
       
   276     QVERIFY(beginHeader < endHeader);
       
   277 
       
   278     QVERIFY(content.indexOf(QRegExp("\\bslots\\s*:")) == -1);
       
   279     QVERIFY(content.indexOf(QRegExp("\\bsignals\\s*:")) == -1);
       
   280 
       
   281     if (header.contains("/sql/drivers/") || header.contains("/arch/qatomic")
       
   282         || header.endsWith("qglobal.h")
       
   283         || header.endsWith("qwindowdefs_win.h"))
       
   284         return;
       
   285 
       
   286     int qtmodule = content.indexOf(QRegExp("^QT_MODULE\\(.*\\)$"));
       
   287     QVERIFY(qtmodule != -1);
       
   288     QVERIFY(qtmodule > beginHeader);
       
   289     QVERIFY(qtmodule < endHeader);
       
   290 
       
   291     int beginNamespace = content.indexOf("QT_BEGIN_NAMESPACE");
       
   292     int endNamespace = content.lastIndexOf("QT_END_NAMESPACE");
       
   293     QVERIFY(beginNamespace != -1);
       
   294     QVERIFY(endNamespace != -1);
       
   295     QVERIFY(beginHeader < beginNamespace);
       
   296     QVERIFY(beginNamespace < endNamespace);
       
   297     QVERIFY(endNamespace < endHeader);
       
   298 }
       
   299 
       
   300 QTEST_MAIN(tst_Headers)
       
   301 #include "tst_headers.moc"