Examples: Read meta data, which includes categories

Task-number: QTCREATORBUG-28546
Change-Id: I11505d4f8b8eaef3f525185cd45757b6f41012ec
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
(cherry picked from commit e060f82fa5)
This commit is contained in:
Eike Ziller
2023-03-09 14:00:08 +01:00
parent 9144ab75f4
commit b542d47907
3 changed files with 39 additions and 1 deletions

View File

@@ -43,6 +43,35 @@ static QStringList trimStringList(const QStringList &stringlist)
return Utils::transform(stringlist, [](const QString &str) { return str.trimmed(); });
}
static QHash<QString, QStringList> parseMeta(QXmlStreamReader *reader)
{
QHash<QString, QStringList> result;
while (!reader->atEnd()) {
switch (reader->readNext()) {
case QXmlStreamReader::StartElement:
if (reader->name() == QLatin1String("entry")) {
const QString key = reader->attributes().value("name").toString();
if (key.isEmpty()) {
reader->raiseError("Tag \"entry\" requires \"name\" attribute");
break;
}
const QString value = reader->readElementText(
QXmlStreamReader::ErrorOnUnexpectedElement);
if (!value.isEmpty())
result[key].append(value);
}
break;
case QXmlStreamReader::EndElement:
if (reader->name() == QLatin1String("meta"))
return result;
break;
default:
break;
}
}
return result;
}
static QList<ExampleItem *> parseExamples(QXmlStreamReader *reader,
const FilePath &projectsOffset,
const FilePath &examplesInstallPath)
@@ -95,6 +124,8 @@ static QList<ExampleItem *> parseExamples(QXmlStreamReader *reader,
item->platforms = trimStringList(
reader->readElementText(QXmlStreamReader::ErrorOnUnexpectedElement)
.split(QLatin1Char(','), Qt::SkipEmptyParts));
} else if (reader->name() == QLatin1String("meta")) {
item->metaData = parseMeta(reader);
}
break;
case QXmlStreamReader::EndElement:

View File

@@ -28,6 +28,7 @@ public:
QString videoUrl;
QString videoLength;
QStringList platforms;
QHash<QString, QStringList> metaData;
};
QTSUPPORT_EXPORT Utils::expected_str<QList<ExampleItem *>> parseExamples(

View File

@@ -25,6 +25,8 @@ private slots:
tst_Examples::tst_Examples() = default;
tst_Examples::~tst_Examples() = default;
using MetaData = QHash<QString, QStringList>;
static ExampleItem fetchItem()
{
QFETCH(QString, name);
@@ -43,6 +45,7 @@ static ExampleItem fetchItem()
QFETCH(QString, videoUrl);
QFETCH(QString, videoLength);
QFETCH(QStringList, platforms);
QFETCH(MetaData, metaData);
ExampleItem item;
item.name = name;
item.description = description;
@@ -60,6 +63,7 @@ static ExampleItem fetchItem()
item.videoUrl = videoUrl;
item.videoLength = videoLength;
item.platforms = platforms;
item.metaData = metaData;
return item;
}
@@ -83,6 +87,7 @@ void tst_Examples::parsing_data()
QTest::addColumn<QString>("videoUrl");
QTest::addColumn<QString>("videoLength");
QTest::addColumn<QStringList>("platforms");
QTest::addColumn<MetaData>("metaData");
QTest::addRow("example")
<< QByteArray(R"raw(
@@ -115,7 +120,7 @@ void tst_Examples::parsing_data()
"manifest/widgets/widgets/analogclock/analogclock.cpp")}
<< FilePath::fromUserInput("manifest/widgets/widgets/analogclock/analogclock.cpp")
<< FilePaths() << Example << true << false << false << ""
<< "" << QStringList();
<< "" << QStringList() << MetaData({{"category", {"Graphics"}}, {"tags", {"widgets"}}});
}
void tst_Examples::parsing()
@@ -148,6 +153,7 @@ void tst_Examples::parsing()
QCOMPARE(item.videoUrl, expected.videoUrl);
QCOMPARE(item.videoLength, expected.videoLength);
QCOMPARE(item.platforms, expected.platforms);
QCOMPARE(item.metaData, expected.metaData);
qDeleteAll(*result);
}