]> Git trees. - libqmvoc.git/commitdiff
Add the first real unit test:
authorFrederik Gladhorn <gladhorn@kde.org>
Mon, 17 Sep 2007 22:38:39 +0000 (22:38 +0000)
committerFrederik Gladhorn <gladhorn@kde.org>
Mon, 17 Sep 2007 22:38:39 +0000 (22:38 +0000)
  Create a doc, write it to a temp file and read it again. Tests for the easy tags: author, title, comment, license and category.
Remove all gui elements from keduvocdocument.
This means that when opening a document fails, the apps have to check for the return value of doc->open().
Added an enum for the errors.
With KEduVocDocument::errorDescription a message can be generated to display to the user.
CCMAIL: peter@peterandlinda.com

svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=713642

keduvocdocument/keduvocdocument.cpp
keduvocdocument/keduvocdocument.h
keduvocdocument/tests/keduvocdocumentvalidatortest.cpp

index ffa0ea121a95d2bafb0a189362f10388011cf879..fba62d41a7577fc0afb923cc3a4151764c6758d5 100644 (file)
@@ -25,7 +25,6 @@
 
 #include <klocale.h>
 #include <kdebug.h>
-#include <kmessagebox.h>
 #include <kio/netaccess.h>
 #include <krandomsequence.h>
 #include <kfilterdev.h>
@@ -227,11 +226,12 @@ KEduVocDocument::FileType KEduVocDocument::detectFileType( const QString &fileNa
 }
 
 
-bool KEduVocDocument::open( const KUrl& url )
+int KEduVocDocument::open( const KUrl& url )
 {
     d->init();
-    if ( !url.isEmpty() )
+    if ( !url.isEmpty() ) {
         d->m_url = url;
+    }
 
     bool read = false;
     QString errorMessage = i18n( "<qt>Cannot open file<br /><b>%1</b></qt>", url.path() );
@@ -240,8 +240,8 @@ bool KEduVocDocument::open( const KUrl& url )
         QIODevice * f = KFilterDev::deviceForFile( temporaryFile );
 
         if ( !f->open( QIODevice::ReadOnly ) ) {
-            KMessageBox::error( 0, errorMessage );
-            return false;
+            kError() << errorMessage;
+            return FileCannotRead;
         }
 
         FileType ft = detectFileType( url.path() );
@@ -309,7 +309,9 @@ bool KEduVocDocument::open( const KUrl& url )
 
         if ( !read ) {
             QString msg = i18n( "Could not open or properly read \"%1\"\n(Error reported: %2)", url.path(), errorMessage );
-            KMessageBox::error( 0, msg, i18n( "Error Opening File" ) );
+            kError() << msg << i18n( "Error Opening File" );
+            ///@todo make the readers return int, pass on the error message properly
+            return FileReaderFailed;
         }
 
         f->close();
@@ -332,11 +334,15 @@ bool KEduVocDocument::open( const KUrl& url )
         deleteLesson(defaultLessonNumber, DeleteEmptyLesson);
     }
 
-    return read;
+    if ( !read ) {
+        return FileReaderFailed;
+    }
+
+    return 0;
 }
 
 
-bool KEduVocDocument::saveAs( const KUrl & url, FileType ft, const QString & generator )
+int KEduVocDocument::saveAs( const KUrl & url, FileType ft, const QString & generator )
 {
     KUrl tmp( url );
 
@@ -356,8 +362,8 @@ bool KEduVocDocument::saveAs( const KUrl & url, FileType ft, const QString & gen
         QFile f( tmp.path() );
 
         if ( !f.open( QIODevice::WriteOnly ) ) {
-            KMessageBox::error( 0, i18n( "<qt>Cannot write to file<br /><b>%1</b></qt>", tmp.path() ) );
-            return false;
+            kError() << i18n( "Cannot write to file %1", tmp.path() );
+            return FileCannotWrite;
         }
 
         switch ( ft ) {
@@ -386,15 +392,13 @@ bool KEduVocDocument::saveAs( const KUrl & url, FileType ft, const QString & gen
         f.close();
 
         if ( !saved ) {
-            QString msg = i18n( "Could not save \"%1\"\nDo you want to try again?", tmp.path() );
-            int result = KMessageBox::warningContinueCancel( 0, msg, i18n( "Error Saving File" ), KGuiItem( i18n( "&Retry" ) ) );
-            if ( result == KMessageBox::Cancel )
-                return false;
+            kError() << "Error Saving File" << tmp.path();
+            return FileWriterFailed;
         }
     }
     d->m_url = tmp;
     setModified( false );
-    return true;
+    return 0;
 }
 
 void KEduVocDocument::merge( KEduVocDocument *docToMerge, bool matchIdentifiers )
@@ -1194,7 +1198,7 @@ void KEduVocDocument::shuffle()
 }
 
 
-QString KEduVocDocument::pattern( Mode mode )
+QString KEduVocDocument::pattern( FileDialogMode mode )
 {
     static const struct SupportedFilter {
         bool reading;
@@ -1225,6 +1229,31 @@ QString KEduVocDocument::pattern( Mode mode )
     return newfilters.join( "\n" );
 }
 
+QString KEduVocDocument::errorDescription( int errorCode )
+{
+    switch (errorCode) {
+    case NoError:
+        return i18n("No error found.");
+
+    case InvalidXml:
+        return i18n("Invalid XML in document.");
+    case FileTypeUnknown:
+        return i18n("Unknown file type.");
+    case FileCannotWrite:
+        return i18n("File could not be written.");
+    case FileWriterFailed:
+        return i18n("File writer failed.");
+    case FileCannotRead:
+        return i18n("File could not be read.");
+    case FileReaderFailed:
+        return i18n("The file reader failed.");
+    case FileDoesNotExist:
+        return i18n("The file does not exist.");
+    case Unknown:
+    default:
+        return i18n("Unknown error message.");
+    }
+}
 
 KEduVocWordType* KEduVocDocument::wordTypes()
 {
index 6aee9c993ff6e2490e55a5fff3015be4569e83fc..1c520664bfed027b9662fda35fc59bc0a16f0a1e 100644 (file)
@@ -46,8 +46,8 @@ class KEDUVOCDOCUMENT_EXPORT KEduVocDocument : public QObject
     Q_OBJECT
 public:
 
-    enum FileType
-    {
+    /// known vocabulary file types
+    enum FileType {
         KvdNone,
         Automatic,
         Kvtml,
@@ -59,6 +59,33 @@ public:
         Kvtml1
     };
 
+    /// the return code when opening/saving
+    enum ErrorCode {
+        NoError = 0,
+        Unknown,
+        InvalidXml,
+        FileTypeUnknown,
+        FileCannotWrite,
+        FileWriterFailed,
+        FileCannotRead,
+        FileReaderFailed,
+        FileDoesNotExist
+    };
+
+    /// used as parameter for pattern
+    enum FileDialogMode
+    {
+        Reading,
+        Writing
+    };
+
+    /// delete only empty lessons or also if they have entries
+    enum LessonDeletion
+    {
+        DeleteEmptyLesson,
+        DeleteEntriesAndLesson
+    };
+
     /**
      * Constructor for a KdeEdu vocabulary document
      *
@@ -77,9 +104,9 @@ public:
      * Open a document file
      *
      * @param url      url to file to open
-     * @returns        true if successful
+     * @returns        ErrorCode
      */
-    bool open( const KUrl& url );
+    int open( const KUrl& url );
 
     /**
      * Saves the data under the given name
@@ -87,9 +114,9 @@ public:
      * @param url        if url is empty (or NULL) actual name is preserved
      * @param ft         the filetype to be used when saving the document
      * @param generator  the name of the application saving the document
-     * @returns          true if successful
+     * @returns          ErrorCode
      */
-    bool saveAs( const KUrl & url, FileType ft, const QString & generator );
+    int saveAs( const KUrl & url, FileType ft, const QString & generator );
 
     /**
      * Merges data from another document
@@ -464,18 +491,6 @@ public:
 
     static FileType detectFileType( const QString &fileName );
 
-    enum Mode
-    {
-        Reading,
-        Writing
-    };
-
-    enum LessonDeletion
-    {
-        DeleteEmptyLesson,
-        DeleteEntriesAndLesson
-    };
-
     /**
      * Create a string with the supported document types, that can be used
      * as filter in KFileDialog. It includes also an entry to match all the
@@ -484,7 +499,9 @@ public:
      * @param mode             the mode for the supported document types
      * @returns                the filter string
      */
-    static QString pattern( Mode mode );
+    static QString pattern( FileDialogMode mode );
+
+    static QString errorDescription( int errorCode );
 
 Q_SIGNALS:
     void progressChanged( KEduVocDocument *, int curr_percent );
index 52f3d88baafa86152d20900b7f18c42057308cbe..e67d220bf7e2f392ad783a89bada68e127da626d 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "../keduvocdocument.h"
 
+#include <KTemporaryFile>
+
 #include <qtest_kde.h>
 
 #include <qobject.h>
@@ -35,7 +37,36 @@ private slots:
 
 void KEduVocDocumentValidatorTest::testDocumentAboutInfo()
 {
-    QVERIFY( 1==1 );
+    KTemporaryFile temp;
+    temp.setSuffix(".kvtml");
+    temp.open();
+    KUrl fileName = KUrl(temp.fileName());
+    temp.close();
+
+
+//     KUrl fileName = KUrl("/tmp/ValidatorTestDocument.kvtml");
+
+    KEduVocDocument* doc = new KEduVocDocument;
+    doc->setAuthor("Validator Test");
+    doc->setLicense("test license");
+    doc->setDocumentComment("comment");
+    doc->setCategory("test document");
+    doc->setTitle("Validator Test Title");
+
+
+    doc->saveAs(fileName, KEduVocDocument::Kvtml, "Validator Unit Tests");
+    delete doc;
+
+    doc = new KEduVocDocument;
+    doc->open(fileName);
+
+    QVERIFY( doc->author() == "Validator Test" );
+    QVERIFY( doc->license() == "test license" );
+    QVERIFY( doc->documentComment() == "comment" );
+    QVERIFY( doc->category() == "test document" );
+    QVERIFY( doc->title() == "Validator Test Title" );
+
+
 }
 
 QTEST_KDEMAIN_CORE( KEduVocDocumentValidatorTest )