Implemented street names/numbers parses and unit tests for it routing_container_classes
authorSami Rämö <sami.ramo@ixonos.com>
Mon, 12 Jul 2010 10:58:00 +0000 (13:58 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Mon, 12 Jul 2010 10:58:00 +0000 (13:58 +0300)
src/routing/routesegment.cpp
tests/routing/route/testroute.cpp
tests/routing/routesegment/testroutesegment.cpp

index f0e2219..4c08739 100644 (file)
@@ -20,6 +20,7 @@
 */
 
 #include <QDebug>
+#include <QRegExp>
 
 #include "routingcommon.h"
 
@@ -144,8 +145,59 @@ QString RouteSegment::street() const
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    /// @todo Implement parser
-    return QString();
+    // regular expressions for matching/replacing instructions
+    const QString REGEXP_1ST_SEGMENT
+        = "^Head (north|northeast|east|southeast|south|southwest|west|northwest)";
+    const QString REGEXP_CONTINUE = "^Continue";
+    const QString REGEXP_ROUNDABOUT
+        = "^At the roundabout, take the \\d(st|nd|rd|th) exit";
+    const QString REGEXP_TURNS = "^(Turn|Slight|Sharp) (left|right)";
+    const QString REGEXP_U_TURN = "^Make a U-turn";
+
+    QString result = m_instruction;
+
+    // cut beginning of the instruction
+    // (but only if it matches with the expected value)
+
+    if (m_turnType == "") {
+        // first segment (without turn type code)
+        QRegExp regexp(REGEXP_1ST_SEGMENT);
+        result.replace(regexp, "");
+
+    } else if (m_turnType == "C") {
+        // continue straight
+        QRegExp regexp(REGEXP_CONTINUE);
+        result.replace(regexp, "");
+
+    } else if ((m_turnType == "TL") || (m_turnType == "TSLL")
+               || (m_turnType == "TSHL") || (m_turnType == "TR")
+               || (m_turnType == "TSLR") || (m_turnType == "TSHR")) {
+        // turns
+        QRegExp regexp(REGEXP_TURNS);
+        result.replace(regexp, "");
+
+    } else if (m_turnType == "TU") {
+        // u-turn
+        QRegExp regexp(REGEXP_U_TURN);
+        result.replace(regexp, "");
+
+    } else if (m_turnType.startsWith("EXIT")) {
+        // roundabout
+        QRegExp regexp(REGEXP_ROUNDABOUT);
+        result.replace(regexp, "");
+
+    } else {
+        // no match, parsing failed
+        qCritical() << __PRETTY_FUNCTION__ << "PARSING FAILED! ( turn type:"
+                    << m_turnType << "instruction:" << m_instruction << ")";
+    }
+
+    // replace on/at/onto words at the beginning of the result
+    // with zero or one leading space and one following space
+    QRegExp regexp("^ ?(on|at|onto) ");
+    result.replace(regexp, "");
+
+    return result;
 }
 
 int RouteSegment::time() const
index 3b686d0..1f2a2d4 100644 (file)
@@ -31,19 +31,12 @@ class TestRoute : public QObject
 {
     Q_OBJECT
 
-public:
-    TestRoute();
-
 private Q_SLOTS:
     void geometryPoints();
     void segments();
     void settersAndGetters();
 };
 
-TestRoute::TestRoute()
-{
-}
-
 void TestRoute::geometryPoints()
 {
     // test points
index a760dc1..4571b88 100644 (file)
@@ -29,19 +29,12 @@ class TestRouteSegment : public QObject
 {
     Q_OBJECT
 
-public:
-    TestRouteSegment();
-
 private Q_SLOTS:
     void settersAndGetters();
     void streetNameParser();
     void streetNameParser_data();
 };
 
-TestRouteSegment::TestRouteSegment()
-{
-}
-
 void TestRouteSegment::settersAndGetters()
 {
     // test data
@@ -83,12 +76,59 @@ void TestRouteSegment::settersAndGetters()
 
 void TestRouteSegment::streetNameParser()
 {
-    QVERIFY2(false, "Not yet implemented!");
+    QFETCH(QString, turnType);
+    QFETCH(QString, instruction);
+    QFETCH(QString, result);
+
+    RouteSegment segment;
+    segment.setTurnType(turnType);
+    segment.setInstruction(instruction);
+
+    QCOMPARE(segment.street(), result);
 }
 
 void TestRouteSegment::streetNameParser_data()
 {
-
+    QTest::addColumn<QString>("turnType");
+    QTest::addColumn<QString>("instruction");
+    QTest::addColumn<QString>("result");
+
+    QTest::newRow("first segment")
+            << "" << "Head northwest on Kiviharjunlenkki" << "Kiviharjunlenkki";
+    QTest::newRow("continue (go straight)")
+            << "C" << "Continue on 847/Heikinkatu" << "847/Heikinkatu";
+    QTest::newRow("turn left")
+            << "TL" << "Turn left at Kajaanintie" << "Kajaanintie";
+    QTest::newRow("turn slight left")
+            << "TSLL" << "Slight left at 847/Uusikatu" << "847/Uusikatu";
+    QTest::newRow("turn sharp left")
+            << "TSHL" << "Sharp left at Erottajankatu" << "Erottajankatu";
+    QTest::newRow("turn right")
+            << "TR" << "Turn right at Linnakatu" << "Linnakatu";
+    QTest::newRow("turn slight right")
+            << "TSLR" << "Slight right at 847/Lävistäjä" << "847/Lävistäjä";
+    QTest::newRow("turn sharp right")
+            << "TSHR" << "Sharp right at Arkadiankatu" << "Arkadiankatu";
+    QTest::newRow("U-turn")
+            << "TU" << "Make a U-turn" << "";
+
+    QTest::newRow("first segment, road name with numbers")
+            << "" << "Head west on 4;9;13;23/Vaajakoskentie" << "4;9;13;23/Vaajakoskentie";
+    QTest::newRow("first segment, unknown road")
+            << "" << "Head northeast" << "";
+
+    QTest::newRow("roundabout, 1st exit")
+            << "EXIT1" << "At the roundabout, take the 1st exit onto Kaitoväylä"
+            << "Kaitoväylä";
+    QTest::newRow("roundabout, 2nd exit")
+            << "EXIT2" << "At the roundabout, take the 2nd exit onto 4;9;13;23"
+            << "4;9;13;23";
+    QTest::newRow("roundabout, 3rd exit")
+            << "EXIT3" << "At the roundabout, take the 3rd exit onto 4;9;13;23"
+            << "4;9;13;23";
+    QTest::newRow("roundabout, 5th exit")
+            << "EXIT5" << "At the roundabout, take the 5th exit onto 4;9;13;23"
+            << "4;9;13;23";
 }
 
 QTEST_APPLESS_MAIN(TestRouteSegment);