From da284ef10e0ef80776b9fc9b7bb0e6dc8d71ba63 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Mon, 17 Dec 2018 15:09:23 +0100 Subject: 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 Reviewed-by: Ulf Hermann --- tests/auto/tools/moc/tst_moc.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'tests/auto/tools/moc/tst_moc.cpp') 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 #include #include +#include #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 -- cgit v1.2.3