From: Max Waterman Date: Wed, 24 Feb 2010 18:27:11 +0000 (+0200) Subject: Refactor for testing and a unit test. X-Git-Url: http://git.maemo.org/git/?p=ptas;a=commitdiff_plain;h=968f89d5a6f105b81b9aa36c5482291870388af8 Refactor for testing and a unit test. --- diff --git a/zouba/qt/httpclient.cpp b/zouba/qt/httpclient.cpp index 8a66c95..b77c00f 100644 --- a/zouba/qt/httpclient.cpp +++ b/zouba/qt/httpclient.cpp @@ -1,5 +1,8 @@ +#include "httpclient_p.h" #include "httpclient.h" +#include "routedata.h" + #include "ui_zouba.h" #include @@ -26,6 +29,7 @@ namespace { } HttpClient::HttpClient( Ui::MainWindow *ui ) : + q( new HttpClientPrivate( this ) ), manager( new QNetworkAccessManager(this) ), ui( ui ) { @@ -57,41 +61,8 @@ void HttpClient::get() void HttpClient::replyFinished( QNetworkReply * reply ) { - QXmlStreamReader xml( reply->readAll() ); - - bool inLine = false; - bool inStop = false; - while ( !xml.atEnd() ) { - xml.readNext(); - //qDebug() << xml.name(); - if ( xml.isStartElement() && xml.name() == "LINE" ) { - QString lineCode( xml.attributes().value("code").toString() ); - - qDebug() << "line code" << lineCode; - ui->BusNoDisplay->setText( lineCode ); - - inLine = true; - } else - if ( inLine && xml.name() == "STOP" ) { - inStop = true; - } else - if ( inLine && inStop && xml.name() == "ARRIVAL" ) { - QString arrivalTime( xml.attributes().value("time").toString() ); - - qDebug() << "arrival time" << arrivalTime; - ui->TimeDisplay->setText( arrivalTime ); - - inLine = false; - } else - if ( xml.isEndElement() && xml.name() == "STOP" ) { - inStop = false; - } else - if ( xml.isEndElement() && xml.name() == "LINE" ) { - inLine = false; - } - } + RouteData routeData = q->parseReply( reply->readAll() ); - if ( xml.hasError() ) { - qDebug() << "xml error"; - } + ui->BusNoDisplay->setText( routeData.lineCode ); + ui->TimeDisplay->setText( routeData.arrivalTime ); } diff --git a/zouba/qt/httpclient.h b/zouba/qt/httpclient.h index 28aa9bf..ab648de 100644 --- a/zouba/qt/httpclient.h +++ b/zouba/qt/httpclient.h @@ -7,6 +7,8 @@ #include #include +class HttpClientPrivate; + class HttpClient: public QObject { Q_OBJECT @@ -21,7 +23,9 @@ public Q_SLOTS: void replyFinished(QNetworkReply*); private: + HttpClientPrivate *q; QNetworkAccessManager *manager; Ui::MainWindow *ui; + }; #endif // HTTPCLIENT_H diff --git a/zouba/qt/httpclient_p.cpp b/zouba/qt/httpclient_p.cpp new file mode 100644 index 0000000..25133f9 --- /dev/null +++ b/zouba/qt/httpclient_p.cpp @@ -0,0 +1,54 @@ +#include "httpclient_p.h" + +#include +#include + +HttpClientPrivate::HttpClientPrivate( QObject *parent ) +{ +} + +HttpClientPrivate::~HttpClientPrivate() +{ +} + +RouteData HttpClientPrivate::parseReply( const QByteArray &reply ) +{ + RouteData retVal; + + QXmlStreamReader xml( reply ); + + bool inLine = false; + bool inStop = false; + while ( !xml.atEnd() ) { + xml.readNext(); + if ( xml.isStartElement() && xml.name() == "LINE" ) { + QString lineCode( xml.attributes().value("code").toString() ); + + retVal.lineCode = lineCode; + + inLine = true; + } else + if ( inLine && xml.name() == "STOP" ) { + inStop = true; + } else + if ( inLine && inStop && xml.name() == "ARRIVAL" ) { + QString arrivalTime( xml.attributes().value("time").toString() ); + + retVal.arrivalTime = arrivalTime; + + inLine = false; + } else + if ( xml.isEndElement() && xml.name() == "STOP" ) { + inStop = false; + } else + if ( xml.isEndElement() && xml.name() == "LINE" ) { + inLine = false; + } + } + + if ( xml.hasError() ) { + qDebug() << "xml error"; + } + + return retVal; +} diff --git a/zouba/qt/httpclient_p.h b/zouba/qt/httpclient_p.h new file mode 100644 index 0000000..9c04bd9 --- /dev/null +++ b/zouba/qt/httpclient_p.h @@ -0,0 +1,18 @@ +#ifndef HTTPCLIENT_P_H +#define HTTPCLIENT_P_H + +#include "routedata.h" + +#include + +class HttpClientPrivate: public QObject +{ + Q_OBJECT + +public: + HttpClientPrivate( QObject *parent=0 ); + ~HttpClientPrivate(); + + RouteData parseReply( const QByteArray &reply ); +}; +#endif // HTTPCLIENT_P_H diff --git a/zouba/qt/qt.pro b/zouba/qt/qt.pro index 48c03b7..23f9158 100644 --- a/zouba/qt/qt.pro +++ b/zouba/qt/qt.pro @@ -10,7 +10,9 @@ FORMS = zouba.ui SOURCES = \ main.cpp \ httpclient.cpp \ + httpclient_p.cpp \ HEADERS += \ httpclient.h \ + httpclient_p.h \ diff --git a/zouba/qt/routedata.h b/zouba/qt/routedata.h new file mode 100644 index 0000000..a2abd31 --- /dev/null +++ b/zouba/qt/routedata.h @@ -0,0 +1,12 @@ +#ifndef ROUTEDATA_H +#define ROUTEDATA_H + +#include + +struct RouteData +{ + QString lineCode; + QString arrivalTime; +}; + +#endif // ROUTEDATA_H diff --git a/zouba/qt/tests/ut_httpclient/ut_httpclient.cpp b/zouba/qt/tests/ut_httpclient/ut_httpclient.cpp new file mode 100644 index 0000000..0e00e06 --- /dev/null +++ b/zouba/qt/tests/ut_httpclient/ut_httpclient.cpp @@ -0,0 +1,369 @@ +#include +#include +#include +#include "ut_httpclient.h" + +QByteArray sampleInput( +"\ +\ +\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +\ +" + +); + +void Ut_HttpClient::init() +{ + m_subject = new HttpClientPrivate(); +} + +void Ut_HttpClient::cleanup() +{ + delete m_subject; + m_subject = 0; +} + +void Ut_HttpClient::initTestCase() +{ +} + +void Ut_HttpClient::cleanupTestCase() +{ +} + +void Ut_HttpClient::testParseReply() +{ + RouteData routeData = m_subject->parseReply( sampleInput ); + + QCOMPARE( routeData.lineCode, QString( "2110T 1" ) ); + QCOMPARE( routeData.arrivalTime, QString( "1834" ) ); +} + +QTEST_MAIN(Ut_HttpClient) diff --git a/zouba/qt/tests/ut_httpclient/ut_httpclient.h b/zouba/qt/tests/ut_httpclient/ut_httpclient.h new file mode 100644 index 0000000..b880e5a --- /dev/null +++ b/zouba/qt/tests/ut_httpclient/ut_httpclient.h @@ -0,0 +1,27 @@ +#ifndef UT_HTTPCLIENT_H +#define UT_HTTPCLIENT_H + +#include +#include + +#include + +Q_DECLARE_METATYPE(HttpClientPrivate*); + +class Ut_HttpClient : public QObject +{ + Q_OBJECT + +public: + +private slots: + void init(); + void cleanup(); + void initTestCase(); + void cleanupTestCase(); + void testParseReply(); + +private: + HttpClientPrivate *m_subject; +}; +#endif // UT_HTTPCLIENT_H diff --git a/zouba/qt/tests/ut_httpclient/ut_httpclient.pro b/zouba/qt/tests/ut_httpclient/ut_httpclient.pro new file mode 100644 index 0000000..a8d816d --- /dev/null +++ b/zouba/qt/tests/ut_httpclient/ut_httpclient.pro @@ -0,0 +1,20 @@ +CONFIG += \ + qt \ + debug \ + +QT += \ + testlib \ + +INCLUDEPATH += \ + ../.. \ + +TEMPLATE = app + +SOURCES = \ + ut_httpclient.cpp \ + ../../httpclient_p.cpp \ + +HEADERS += \ + ut_httpclient.h \ + ../../httpclient_p.h \ +