Commit 7b904337 authored by Volker Krause's avatar Volker Krause
Browse files

Add an IFOPT identifier validator

This is the first step towards properly making use of IFOPT data from OTP,
EFA or ASS sources, similar to how we already use IBNR and UIC station ids.
parent db9e79b0
......@@ -11,6 +11,7 @@ ecm_add_test(kgraphqlminimizertest.cpp LINK_LIBRARIES Qt5::Test KGraphQL)
ecm_add_test(indexeddatatabletest.cpp LINK_LIBRARIES Qt5::Test)
ecm_add_test(polylinetest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(ifopttest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(mergeutiltest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(locationtest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
ecm_add_test(linetest.cpp LINK_LIBRARIES Qt5::Test KPublicTransport)
......
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "../src/lib/ifopt/ifoptutil.cpp"
#include <QJsonDocument>
#include <QJsonObject>
#include <QTest>
#define s(x) QStringLiteral(x)
using namespace KPublicTransport;
class IfoptTest: public QObject
{
Q_OBJECT
private Q_SLOTS:
void testIsValid_data()
{
QTest::addColumn<QString>("ifopt");
QTest::addColumn<bool>("valid");
QTest::newRow("empty") << QString() << false;
QTest::newRow("quay") << s("de:08115:4512:5:B") << true;
QTest::newRow("prefix") << s("hbg:de:08115:4512:2:1") << false;
QTest::newRow("level") << s("de:08115:4512:1") << true;
QTest::newRow("stop_place") << s("de:05315:11201") << true;
QTest::newRow("admin_area") << s("de:05111") << false;
QTest::newRow("country") << s("de") << false;
QTest::newRow("colons") << s("::::") << false;
QTest::newRow("missing element") << s("de:08115::5:B") << false;
QTest::newRow("missing country") << s(":05111:18995:2:2") << false;
QTest::newRow("trailing colon") << s("de:05315:11201:") << false;
QTest::newRow("too many elements") << s("de:08115:4512:2:1:42") << false;
}
void testIsValid()
{
QFETCH(QString, ifopt);
QFETCH(bool, valid);
QCOMPARE(IfoptUtil::isValid(ifopt), valid);
}
};
QTEST_APPLESS_MAIN(IfoptTest)
#include "ifopttest.moc"
......@@ -85,6 +85,8 @@ set(kpublictransport_srcs
gtfs/hvt.cpp
ifopt/ifoptutil.cpp
knowledgedb/linemetadata.cpp
models/abstractquerymodel.cpp
......
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "ifoptutil.h"
#include <QDebug>
#include <QStringView>
using namespace KPublicTransport;
bool IfoptUtil::isValid(QStringView ifopt)
{
if (ifopt.size() < 6) {
return false;
}
qsizetype pos = 0;
int elementCount = 1;
do {
const auto idx = ifopt.indexOf(QLatin1Char(':'), pos);
elementCount += (idx > pos) ? 1 : 0;
if (idx == pos || idx == ifopt.size() - 1 || (elementCount < 3 && idx < 0)) {
return false;
}
pos = idx + 1;
} while (pos != 0 && pos < ifopt.size());
return elementCount >= 3 && elementCount <= 5 && ifopt[0].isLetter() && ifopt[1].isLetter() && ifopt[2] == QLatin1Char(':');
}
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KPUBLICTRANSPORT_IFOPTUTIL_H
#define KPUBLICTRANSPORT_IFOPTUTIL_H
class QStringView;
namespace KPublicTransport {
/** Utility functions for dealing with IFOPT (Identification of Fixed Objects in Public Transport).
* @see https://wiki.openstreetmap.org/wiki/Key:ref:IFOPT
* @see https://en.wikipedia.org/wiki/Identification_of_Fixed_Objects_in_Public_Transport
*/
namespace IfoptUtil
{
/** Check if @p ifopt is a valid IFOPT identifier. */
bool isValid(QStringView ifopt);
}
}
#endif // KPUBLICTRANSPORT_IFOPTUTIL_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment