Add test for examples parsing

Change-Id: Id2ec8afcdbdff97e12b32b836c955552589081c4
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
(cherry picked from commit 87b5176fd2)
This commit is contained in:
Eike Ziller
2023-03-09 11:30:31 +01:00
parent ac17e0e2ad
commit 9144ab75f4
7 changed files with 198 additions and 9 deletions

View File

@@ -229,10 +229,18 @@ expected_str<QList<ExampleItem *>> parseExamples(const FilePath &manifest,
if (!contents)
return make_unexpected(contents.error());
const FilePath path = manifest.parentDir();
return parseExamples(*contents, manifest, examplesInstallPath, demosInstallPath, examples);
}
expected_str<QList<ExampleItem *>> parseExamples(const QByteArray &manifestData,
const Utils::FilePath &manifestPath,
const FilePath &examplesInstallPath,
const FilePath &demosInstallPath,
const bool examples)
{
const FilePath path = manifestPath.parentDir();
QList<ExampleItem *> items;
QXmlStreamReader reader(*contents);
QXmlStreamReader reader(manifestData);
while (!reader.atEnd()) {
switch (reader.readNext()) {
case QXmlStreamReader::StartElement:
@@ -251,7 +259,7 @@ expected_str<QList<ExampleItem *>> parseExamples(const FilePath &manifest,
if (reader.hasError()) {
qDeleteAll(items);
return make_unexpected(QString("Could not parse file \"%1\" as XML document: %2:%3: %4")
.arg(manifest.toUserOutput())
.arg(manifestPath.toUserOutput())
.arg(reader.lineNumber())
.arg(reader.columnNumber())
.arg(reader.errorString()));

View File

@@ -3,6 +3,8 @@
#pragma once
#include "qtsupport_global.h"
#include <coreplugin/welcomepagehelper.h>
#include <utils/expected.h>
#include <utils/filepath.h>
@@ -11,7 +13,7 @@ namespace QtSupport::Internal {
enum InstructionalType { Example = 0, Demo, Tutorial };
class ExampleItem : public Core::ListItem
class QTSUPPORT_EXPORT ExampleItem : public Core::ListItem
{
public:
Utils::FilePath projectPath;
@@ -20,7 +22,6 @@ public:
Utils::FilePath mainFile; /* file to be visible after opening filesToOpen */
Utils::FilePaths dependencies;
InstructionalType type;
int difficulty = 0;
bool hasSourceCode = false;
bool isVideo = false;
bool isHighlighted = false;
@@ -29,10 +30,18 @@ public:
QStringList platforms;
};
Utils::expected_str<QList<ExampleItem *>> parseExamples(const Utils::FilePath &manifest,
const Utils::FilePath &examplesInstallPath,
const Utils::FilePath &demosInstallPath,
bool examples);
QTSUPPORT_EXPORT Utils::expected_str<QList<ExampleItem *>> parseExamples(
const Utils::FilePath &manifest,
const Utils::FilePath &examplesInstallPath,
const Utils::FilePath &demosInstallPath,
bool examples);
QTSUPPORT_EXPORT Utils::expected_str<QList<ExampleItem *>> parseExamples(
const QByteArray &manifestData,
const Utils::FilePath &manifestPath,
const Utils::FilePath &examplesInstallPath,
const Utils::FilePath &demosInstallPath,
bool examples);
} // namespace QtSupport::Internal

View File

@@ -6,6 +6,7 @@ add_subdirectory(cplusplus)
add_subdirectory(debugger)
add_subdirectory(diff)
add_subdirectory(environment)
add_subdirectory(examples)
add_subdirectory(extensionsystem)
add_subdirectory(externaltool)
add_subdirectory(filesearch)

View File

@@ -12,6 +12,7 @@ Project {
"debugger/debugger.qbs",
"diff/diff.qbs",
"environment/environment.qbs",
"examples/examples.qbs",
"extensionsystem/extensionsystem.qbs",
"externaltool/externaltool.qbs",
"filesearch/filesearch.qbs",

View File

@@ -0,0 +1,5 @@
add_qtc_test(tst_examples
DEPENDS Utils Core QtSupport
SOURCES tst_examples.cpp
)

View File

@@ -0,0 +1,9 @@
import qbs
QtcAutotest {
name: "Examples autotest"
Depends { name: "Core" }
Depends { name: "QtSupport" }
Depends { name: "Utils" }
files: "tst_examples.cpp"
}

View File

@@ -0,0 +1,156 @@
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <utils/filepath.h>
#include <qtsupport/examplesparser.h>
#include <QtTest>
using namespace Utils;
using namespace QtSupport::Internal;
class tst_Examples : public QObject
{
Q_OBJECT
public:
tst_Examples();
~tst_Examples();
private slots:
void parsing_data();
void parsing();
};
tst_Examples::tst_Examples() = default;
tst_Examples::~tst_Examples() = default;
static ExampleItem fetchItem()
{
QFETCH(QString, name);
QFETCH(QString, description);
QFETCH(QString, imageUrl);
QFETCH(QStringList, tags);
QFETCH(FilePath, projectPath);
QFETCH(QString, docUrl);
QFETCH(FilePaths, filesToOpen);
QFETCH(FilePath, mainFile);
QFETCH(FilePaths, dependencies);
QFETCH(InstructionalType, type);
QFETCH(bool, hasSourceCode);
QFETCH(bool, isVideo);
QFETCH(bool, isHighlighted);
QFETCH(QString, videoUrl);
QFETCH(QString, videoLength);
QFETCH(QStringList, platforms);
ExampleItem item;
item.name = name;
item.description = description;
item.imageUrl = imageUrl;
item.tags = tags;
item.projectPath = projectPath;
item.docUrl = docUrl;
item.filesToOpen = filesToOpen;
item.mainFile = mainFile;
item.dependencies = dependencies;
item.type = type;
item.hasSourceCode = hasSourceCode;
item.isVideo = isVideo;
item.isHighlighted = isHighlighted;
item.videoUrl = videoUrl;
item.videoLength = videoLength;
item.platforms = platforms;
return item;
}
void tst_Examples::parsing_data()
{
QTest::addColumn<QByteArray>("data");
QTest::addColumn<bool>("isExamples");
QTest::addColumn<QString>("name");
QTest::addColumn<QString>("description");
QTest::addColumn<QString>("imageUrl");
QTest::addColumn<QStringList>("tags");
QTest::addColumn<FilePath>("projectPath");
QTest::addColumn<QString>("docUrl");
QTest::addColumn<FilePaths>("filesToOpen");
QTest::addColumn<FilePath>("mainFile");
QTest::addColumn<FilePaths>("dependencies");
QTest::addColumn<InstructionalType>("type");
QTest::addColumn<bool>("hasSourceCode");
QTest::addColumn<bool>("isVideo");
QTest::addColumn<bool>("isHighlighted");
QTest::addColumn<QString>("videoUrl");
QTest::addColumn<QString>("videoLength");
QTest::addColumn<QStringList>("platforms");
QTest::addRow("example")
<< QByteArray(R"raw(
<examples>
<example docUrl="qthelp://org.qt-project.qtwidgets.660/qtwidgets/qtwidgets-widgets-analogclock-example.html"
imageUrl="qthelp://org.qt-project.qtwidgets.660/qtwidgets/images/analogclock-example.png"
name="Analog Clock" projectPath="widgets/widgets/analogclock/CMakeLists.txt">
<description><![CDATA[The Analog Clock example shows how to draw the contents of a custom widget.]]></description>
<tags>ios,widgets</tags>
<fileToOpen>widgets/widgets/analogclock/main.cpp</fileToOpen>
<fileToOpen>widgets/widgets/analogclock/analogclock.h</fileToOpen>
<fileToOpen mainFile="true">widgets/widgets/analogclock/analogclock.cpp</fileToOpen>
<meta>
<entry name="category">Graphics</entry>
<entry name="tags">widgets</entry>
</meta>
</example>
</examples>
)raw") << /*isExamples=*/true
<< "Analog Clock"
<< "The Analog Clock example shows how to draw the contents of a custom widget."
<< "qthelp://org.qt-project.qtwidgets.660/qtwidgets/images/analogclock-example.png"
<< QStringList{"ios", "widgets"}
<< FilePath::fromUserInput("manifest/widgets/widgets/analogclock/CMakeLists.txt")
<< "qthelp://org.qt-project.qtwidgets.660/qtwidgets/"
"qtwidgets-widgets-analogclock-example.html"
<< FilePaths{FilePath::fromUserInput("manifest/widgets/widgets/analogclock/main.cpp"),
FilePath::fromUserInput("manifest/widgets/widgets/analogclock/analogclock.h"),
FilePath::fromUserInput(
"manifest/widgets/widgets/analogclock/analogclock.cpp")}
<< FilePath::fromUserInput("manifest/widgets/widgets/analogclock/analogclock.cpp")
<< FilePaths() << Example << true << false << false << ""
<< "" << QStringList();
}
void tst_Examples::parsing()
{
QFETCH(QByteArray, data);
QFETCH(bool, isExamples);
const ExampleItem expected = fetchItem();
const expected_str<QList<ExampleItem *>> result
= parseExamples(data,
FilePath("manifest/examples-manifest.xml"),
FilePath("examples"),
FilePath("demos"),
isExamples);
QVERIFY(result);
QCOMPARE(result->size(), 1);
const ExampleItem item = *result->at(0);
QCOMPARE(item.name, expected.name);
QCOMPARE(item.description, expected.description);
QCOMPARE(item.imageUrl, expected.imageUrl);
QCOMPARE(item.tags, expected.tags);
QCOMPARE(item.projectPath, expected.projectPath);
QCOMPARE(item.docUrl, expected.docUrl);
QCOMPARE(item.filesToOpen, expected.filesToOpen);
QCOMPARE(item.mainFile, expected.mainFile);
QCOMPARE(item.dependencies, expected.dependencies);
QCOMPARE(item.type, expected.type);
QCOMPARE(item.hasSourceCode, expected.hasSourceCode);
QCOMPARE(item.isVideo, expected.isVideo);
QCOMPARE(item.isHighlighted, expected.isHighlighted);
QCOMPARE(item.videoUrl, expected.videoUrl);
QCOMPARE(item.videoLength, expected.videoLength);
QCOMPARE(item.platforms, expected.platforms);
qDeleteAll(*result);
}
QTEST_APPLESS_MAIN(tst_Examples)
#include "tst_examples.moc"