diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2018-12-17 15:09:23 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2019-10-04 18:35:29 +0200 |
commit | da284ef10e0ef80776b9fc9b7bb0e6dc8d71ba63 (patch) | |
tree | 8617f85d03841c09a078825bca7ceaf272217f35 /tests/auto/tools/moc/tst_moc.cpp | |
parent | 64473c9320b8c521e3ea7030aee5c1c8193661b1 (diff) |
Add support for machine-readable JSON output to the MOC
The --output-json parameter will make moc produce a .json file next to
the regular output file. With --collect-json the .json files for a
module can be merged into a single one.
Task-number: QTBUG-68796
Change-Id: I0e8fb802d47bd22da219701a8df947973d4bd7b5
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/tools/moc/tst_moc.cpp')
-rw-r--r-- | tests/auto/tools/moc/tst_moc.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index 89f563f11d..9fc00288fe 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -33,6 +33,7 @@ #include <stdio.h> #include <qobject.h> #include <qmetaobject.h> +#include <qjsondocument.h> #include "using-namespaces.h" #include "assign-namespace.h" @@ -717,6 +718,7 @@ private slots: void testQNamespace(); void cxx17Namespaces(); void cxxAttributes(); + void mocJsonOutput(); signals: void sigWithUnsignedArg(unsigned foo); @@ -3971,6 +3973,57 @@ void tst_Moc::cxxAttributes() QCOMPARE(meta.keyCount(), 7); } +void tst_Moc::mocJsonOutput() +{ + const auto readFile = [](const QString &fileName) { + QFile f(fileName); + f.open(QIODevice::ReadOnly); + return QJsonDocument::fromJson(f.readAll()); + }; + + const QString actualFile = QStringLiteral(":/allmocs.json"); + const QString expectedFile = QStringLiteral(":/allmocs_baseline.json"); + + QVERIFY2(QFile::exists(actualFile), qPrintable(actualFile)); + QVERIFY2(QFile::exists(expectedFile), qPrintable(expectedFile)); + + QJsonDocument actualOutput = readFile(QLatin1String(":/allmocs.json")); + QJsonDocument expectedOutput = readFile(QLatin1String(":/allmocs_baseline.json")); + + const auto showPotentialDiff = [](const QJsonDocument &actual, const QJsonDocument &expected) -> QByteArray { +#if defined(Q_OS_UNIX) + QByteArray actualStr = actual.toJson(); + QByteArray expectedStr = expected.toJson(); + + QTemporaryFile actualFile; + if (!actualFile.open()) + return "Error opening actual temp file"; + actualFile.write(actualStr); + actualFile.flush(); + + QTemporaryFile expectedFile; + if (!expectedFile.open()) + return "Error opening expected temp file"; + expectedFile.write(expectedStr); + expectedFile.flush(); + + QProcess diffProc; + diffProc.setProgram("diff"); + diffProc.setArguments(QStringList() << "-ub" << expectedFile.fileName() << actualFile.fileName()); + diffProc.start(); + if (!diffProc.waitForStarted()) + return "Error waiting for diff process to start."; + if (!diffProc.waitForFinished()) + return "Error waiting for diff process to finish."; + return diffProc.readAllStandardOutput(); +#else + return "Cannot launch diff. Please check allmocs.json and allmocs_baseline.json on disk."; +#endif + }; + + QVERIFY2(actualOutput == expectedOutput, showPotentialDiff(actualOutput, expectedOutput).constData()); +} + QTEST_MAIN(tst_Moc) // the generated code must compile with QT_NO_KEYWORDS |