Unit tests for RemoteList are now ready
authorJari Jarvi <t7jaja00@students.oamk.fi>
Mon, 14 Jun 2010 10:32:03 +0000 (13:32 +0300)
committerJari Jarvi <t7jaja00@students.oamk.fi>
Mon, 14 Jun 2010 10:32:46 +0000 (13:32 +0300)
src/remotelist.cpp
src/remotelist.h
ut/ut_remotelist/notxml [deleted file]
ut/ut_remotelist/remotes.xml [deleted file]
ut/ut_remotelist/ut_remotelist.cpp
ut/ut_remotelist/ut_remotelist.h

index 7cfd34d..5d33a3f 100644 (file)
@@ -1,4 +1,5 @@
 #include "remotelist.h"
+
 #include <QFile> 
 #include <QDomElement> 
 #include <QDomNodeList> 
@@ -21,18 +22,6 @@ RemoteList::RemoteList(QDomDocument &doc)
     parse(doc);
 }
 
-RemoteList::RemoteList(const QString &xmlFile)
-{
-    parse(xmlFile);   
-}
-
-RemoteList::RemoteList(QIODevice &in)
-{
-    QDomDocument doc;
-    if (doc.setContent(&in))
-        parse(doc);
-}
-
 RemoteList::~RemoteList()
 {
 }
@@ -44,13 +33,6 @@ void RemoteList::setContent(QDomDocument &doc)
     parse(doc);
 }
 
-void RemoteList::setContent(const QString &xmlFile)
-{
-    mfgMap.clear();
-    modelMap.clear();
-    parse(xmlFile);
-}
-
 bool RemoteList::isValid()
 {
     return valid;
@@ -71,28 +53,6 @@ QList<Model> RemoteList::models(const QString &manufacturer)
     return modelMap.value(manufacturer);
 }
 
-void RemoteList::parse(const QString &xmlFile)
-{
-    QFile file(xmlFile);
-    QDomDocument doc;
-
-    if (!file.open(QIODevice::ReadOnly))
-    {
-        valid = false;
-        return;
-    }
-
-    if (!doc.setContent(&file))
-    {
-        file.close();
-        valid = false;
-        return;
-    }
-    file.close();   
-
-    parse(doc);
-}
-
 void RemoteList::parse(QDomDocument &doc)
 {
     QDomNodeList chars = doc.elementsByTagName("char");
index 4650c09..28481a3 100644 (file)
@@ -13,12 +13,9 @@ public:
     RemoteList();
     RemoteList(const RemoteList &);
     RemoteList(QDomDocument &);
-    RemoteList(const QString &xmlFile);
-    RemoteList(QIODevice &);
     ~RemoteList();
     
     void setContent(QDomDocument &);
-    void setContent(const QString &xmlFile);
 
     // Returns false if document is not set or it's invalid
     bool isValid();
@@ -28,7 +25,6 @@ public:
     QList<Model> models(const QString &manufacturer);
 
 private:
-    void parse(const QString &xmlFile);
     void parse(QDomDocument &doc);
     QStringList parseMfgs(QDomElement &charEl);
     QList<Model> parseModels(QDomElement &mfgEl);
diff --git a/ut/ut_remotelist/notxml b/ut/ut_remotelist/notxml
deleted file mode 100644 (file)
index 9d1eb4c..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-This is just a plain text file.
-
diff --git a/ut/ut_remotelist/remotes.xml b/ut/ut_remotelist/remotes.xml
deleted file mode 100644 (file)
index ea236df..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" ?>
-<db>
-<char id="P">
-    <mfg id="Philips">
-        <model file="15PF4121_00" name="15PF4121"/>
-        <model file="filename" name="modelname"/>
-    </mfg>
-</char>
-<char id="L">
-    <mfg id="Lg">
-        <model file="AKB69680_00" name="AKB69680403"/>
-    </mfg>
-    <mfg id="Leadtek">
-        <model file="Y04G0004_00" name="Y04G0004"/>
-    </mfg>
-</char>
-<char id="T">
-    <mfg id="Topfield">
-        <model file="PVR_5000_00" name="PVR5000"/>
-    </mfg>
-</char>
-<char id="M">
-    <mfg id="Microsoft">
-        <model file="Xbox360_00" name="Xbox360"/>
-    </mfg>
-</char>
-</db>
-
index 3aed64c..8e949a0 100644 (file)
@@ -1,7 +1,6 @@
 #include "ut_remotelist.h"
-#include "remotelist.h"
+
 #include <QtTest>
-#include <QStringList>
 
 void Ut_RemoteList::initTestCase()
 {
@@ -38,42 +37,111 @@ void Ut_RemoteList::initTestCase()
 
     validModels.insert("Topfield", QList<Model>()); 
     validModels["Topfield"].append(Model("PVR5000", "PVR_5000_00"));
+
+    createDocument();
 }
 
 void Ut_RemoteList::cleanupTestCase()
 {
 }
 
-void Ut_RemoteList::init()
+void Ut_RemoteList::createDocument()
+{
+    QDomElement root = document.createElement("db");
+    document.appendChild(root);
+
+    for (int i = 0; i < validMfgs.keys().size(); ++i)
+    {
+        QChar letter = validMfgs.keys().at(i).at(0);
+        QDomElement charEl = document.createElement("char");
+        charEl.setAttribute("id", letter);
+        createMfgElements(letter, charEl);
+        root.appendChild(charEl);
+    }
+}
+
+void Ut_RemoteList::createMfgElements(QChar &letter, QDomElement &parent)
 {
+    for (int i = 0; i < validMfgs[letter].size(); ++i)
+    {
+        QString mfg = validMfgs[letter].at(i);
+        QDomElement mfgEl = document.createElement("mfg");
+        mfgEl.setAttribute("id", mfg);
+        createModelElements(mfg, mfgEl);
+        parent.appendChild(mfgEl);
+    }
 }
 
-void Ut_RemoteList::testSetContent()
+void Ut_RemoteList::createModelElements(const QString &mfg, QDomElement &parent)
 {
+    for (int i = 0; i < validModels[mfg].size(); ++i)
+    {
+        Model m = validModels[mfg].at(i);
+        QDomElement modelEl = document.createElement("model");
+        modelEl.setAttribute("file", m.file);
+        modelEl.setAttribute("name", m.name);
+        parent.appendChild(modelEl);
+    }
 }
 
 void Ut_RemoteList::testIsValid()
 {
-    RemoteList subject("remotes.xml");
+    RemoteList subject(document);
     QCOMPARE(subject.isValid(), true);
 }
 
-void Ut_RemoteList::testIsValid_InvalidFile()
+void Ut_RemoteList::testIsValid_setContent()
 {
-    RemoteList subject("notxml");
+    RemoteList subject;
     QCOMPARE(subject.isValid(), false);
+    subject.setContent(document);
+    QCOMPARE(subject.isValid(), true);
 }
 
-void Ut_RemoteList::testIsValid_FileNotFound()
+void Ut_RemoteList::testLetters()
 {
-    RemoteList subject("thisdoesnotexist");
-    QCOMPARE(subject.isValid(), false);
+    RemoteList subject(document);
+    compareLetters(subject);
 }
-void Ut_RemoteList::testLetters()
+
+void Ut_RemoteList::testManufacturers()
+{
+    RemoteList subject(document);
+    compareManufacturers(subject);
+}
+
+void Ut_RemoteList::testModels()
+{
+    RemoteList subject(document);
+    compareModels(subject);
+}
+
+void Ut_RemoteList::testLetters_setContent()
+{
+    RemoteList subject;
+    subject.setContent(document);
+    compareLetters(subject);
+}
+
+void Ut_RemoteList::testManufacturers_setContent()
+{
+    RemoteList subject;
+    subject.setContent(document);
+    compareManufacturers(subject);
+}
+
+void Ut_RemoteList::testModels_setContent()
+{
+    RemoteList subject;
+    subject.setContent(document);
+    compareModels(subject);
+}
+
+void Ut_RemoteList::compareLetters(RemoteList &subject)
 {
-    QStringList letters = RemoteList("remotes.xml").letters();
+    QStringList letters = subject.letters();
     QStringList validLetters = validMfgs.keys();
+
     QCOMPARE(validLetters.size(), letters.size());
 
     for (int i = 0; i < letters.size(); ++i)
@@ -82,10 +150,8 @@ void Ut_RemoteList::testLetters()
     }
 }
 
-void Ut_RemoteList::testManufacturers()
+void Ut_RemoteList::compareManufacturers(RemoteList &subject)
 {
-    RemoteList subject("remotes.xml");
-
     for (int i = 0; i < validMfgs.keys().size(); ++i)
     {
         QString letter = validMfgs.keys().at(i);
@@ -97,10 +163,8 @@ void Ut_RemoteList::testManufacturers()
     }
 }
 
-void Ut_RemoteList::testModels()
+void Ut_RemoteList::compareModels(RemoteList &subject)
 {
-    RemoteList subject("remotes.xml");
-
     for (int i = 0; i < validModels.keys().size(); ++i)
     {
         QString mfg = validModels.keys().at(i);
index 478ca28..7db7764 100644 (file)
@@ -5,7 +5,10 @@
 #include <QStringList>
 #include <QMap>
 #include <QList>
+#include <QDomElement>
+
 #include "model.h"
+#include "remotelist.h"
 
 class Ut_RemoteList: public QObject
 {
@@ -14,19 +17,32 @@ class Ut_RemoteList: public QObject
 private slots:
     void initTestCase();
     void cleanupTestCase();
-    void init();
-    
-    void testSetContent();
+
     void testIsValid();
-    void testIsValid_InvalidFile();
-    void testIsValid_FileNotFound();
+    void testIsValid_setContent();
     void testLetters();
     void testManufacturers();
     void testModels();
+    void testLetters_setContent();
+    void testManufacturers_setContent();
+    void testModels_setContent();
+
+private:
+    // Creates the xml document containing the test data
+    void createDocument();
+
+    // Creates element for each mfg which name starts with the given letter.
+    void createMfgElements(QChar &letter, QDomElement &parent);
+    void createModelElements(const QString &mfg, QDomElement &parent);
+
+    void compareLetters(RemoteList &subject);
+    void compareManufacturers(RemoteList &subject);
+    void compareModels(RemoteList &subject);
 
 private:
-    QMap<QString, QStringList> validMfgs;       // letter -- mfgs
-    QMap<QString, QList<Model> > validModels;   // mfg ----- models
+    QDomDocument document;
+    QMap<QString, QStringList> validMfgs;
+    QMap<QString, QList<Model> > validModels;
 };
 
 #endif // UT_REMOTELIST_H