File indexing completed on 2024-04-14 04:35:57

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <map/scene/penwidthutil_p.h>
0008 #include <map/style/mapcssdeclaration_p.h>
0009 #include <osm/element.h>
0010 
0011 #include <QTest>
0012 
0013 using namespace KOSMIndoorMap;
0014 
0015 Q_DECLARE_METATYPE(KOSMIndoorMap::Unit)
0016 
0017 class PenWidthUtilTest: public QObject
0018 {
0019     Q_OBJECT
0020 private Q_SLOTS:
0021     void penWidthExplicit_data()
0022     {
0023         QTest::addColumn<double>("cssValue");
0024         QTest::addColumn<QString>("cssUnit");
0025         QTest::addColumn<double>("expectedWidth");
0026         QTest::addColumn<Unit>("expectedUnit");
0027 
0028         QTest::newRow("none") << 42.0 << QString() << 42.0 << Unit::Pixel;
0029         QTest::newRow("css meter") << 42.0 << "m" << 42.0 << Unit::Meter;
0030         QTest::newRow("css px") << 42.0 << "px" << 42.0 << Unit::Pixel;
0031     }
0032     void penWidthExplicit()
0033     {
0034         QFETCH(double, cssValue);
0035         QFETCH(QString, cssUnit);
0036         QFETCH(double, expectedWidth);
0037         QFETCH(Unit, expectedUnit);
0038 
0039         MapCSSDeclaration decl(MapCSSDeclaration::PropertyDeclaration);
0040         decl.setDoubleValue(cssValue);
0041         if (!cssUnit.isEmpty()) {
0042             decl.setUnit(qPrintable(cssUnit), cssUnit.size());
0043         }
0044 
0045         OSM::Node n;
0046         Unit resultUnit = Unit::Pixel;
0047         const auto result = PenWidthUtil::penWidth(OSM::Element(&n), &decl, resultUnit);
0048         QCOMPARE(result, expectedWidth);
0049         QCOMPARE(resultUnit, expectedUnit);
0050     }
0051 
0052     void penWidthFromTagTest_data()
0053     {
0054         QTest::addColumn<QString>("keyName");
0055         QTest::addColumn<QString>("tagValue");
0056         QTest::addColumn<double>("expectedWidth");
0057         QTest::addColumn<Unit>("penUnit");
0058 
0059         QTest::newRow("meter explicit") << "width" << "42m" << 42.0 << Unit::Meter;
0060         QTest::newRow("meter implicit") << "width" << "42" << 42.0 << Unit::Meter;
0061         QTest::newRow("feet 1") << "width" << "42ft" << 12.8016 << Unit::Meter;
0062         QTest::newRow("feet 2") << "width" << "42 ft" << 12.8016 << Unit::Meter;
0063         QTest::newRow("implicit mm") << "gauge" << "1435" << 1.435 << Unit::Meter;
0064         QTest::newRow("gauge explicit") << "gauge" << "1m" << 1.0 << Unit::Meter;
0065         QTest::newRow("multi gauge") << "gauge" << "1000;1435" << 1.435 << Unit::Meter;
0066         QTest::newRow("multi gauge 2") << "gauge" << "1435;1000" << 1.435 << Unit::Meter;
0067         QTest::newRow("cm") << "width" << "100 cm" << 1.0 << Unit::Meter;
0068     }
0069 
0070     void penWidthFromTagTest()
0071     {
0072         QFETCH(QString, keyName);
0073         QFETCH(QString, tagValue);
0074         QFETCH(double, expectedWidth);
0075         QFETCH(Unit, penUnit);
0076 
0077         OSM::DataSet dataSet;
0078         const auto tagKey = dataSet.makeTagKey(qPrintable(keyName));
0079         OSM::Node node;
0080         node.id = -1;
0081         OSM::setTagValue(node, tagKey, tagValue.toUtf8());
0082 
0083         MapCSSDeclaration decl(MapCSSDeclaration::PropertyDeclaration);
0084         decl.setPropertyName("width", 5);
0085         decl.setIdentifierValue(qPrintable(keyName), keyName.size());
0086         decl.compile(dataSet);
0087 
0088         Unit resultUnit;
0089         const auto w = PenWidthUtil::penWidth(OSM::Element(&node), &decl, resultUnit);
0090         QCOMPARE(w, expectedWidth);
0091         QCOMPARE(penUnit, resultUnit);
0092     }
0093 
0094     void penWidthFromTagErrors_data()
0095     {
0096         QTest::addColumn<QString>("tagValue");
0097 
0098         QTest::newRow("empty") << QString();
0099         QTest::newRow("non-number") << "foo";
0100         QTest::newRow("unit only") << "ft";
0101     }
0102 
0103     void penWidthFromTagErrors()
0104     {
0105         QFETCH(QString, tagValue);
0106 
0107         OSM::DataSet dataSet;
0108         const auto tagKey = dataSet.makeTagKey("width");
0109         OSM::Node node;
0110         node.id = -1;
0111         OSM::setTagValue(node, tagKey, tagValue.toUtf8());
0112 
0113         MapCSSDeclaration decl(MapCSSDeclaration::PropertyDeclaration);
0114         decl.setPropertyName("width", 5);
0115         decl.setIdentifierValue("width", 5);
0116         decl.compile(dataSet);
0117 
0118         Unit resultUnit;
0119         const auto w = PenWidthUtil::penWidth(OSM::Element(&node), &decl, resultUnit);
0120         QCOMPARE(w, 0.0);
0121     }
0122 };
0123 
0124 QTEST_GUILESS_MAIN(PenWidthUtilTest)
0125 
0126 #include "penwidthutiltest.moc"