From: Frederik Gladhorn Date: Mon, 17 Sep 2007 22:38:39 +0000 (+0000) Subject: Add the first real unit test: X-Git-Tag: v3.94.0~47 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=29a0edeeb76cb4dd3853e397dd0d46944458ba3d;p=libqmvoc.git Add the first real unit test: 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 --- diff --git a/keduvocdocument/keduvocdocument.cpp b/keduvocdocument/keduvocdocument.cpp index ffa0ea1..fba62d4 100644 --- a/keduvocdocument/keduvocdocument.cpp +++ b/keduvocdocument/keduvocdocument.cpp @@ -25,7 +25,6 @@ #include #include -#include #include #include #include @@ -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( "Cannot open file
%1
", 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( "Cannot write to file
%1
", 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() { diff --git a/keduvocdocument/keduvocdocument.h b/keduvocdocument/keduvocdocument.h index 6aee9c9..1c52066 100644 --- a/keduvocdocument/keduvocdocument.h +++ b/keduvocdocument/keduvocdocument.h @@ -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 ); diff --git a/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp b/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp index 52f3d88..e67d220 100644 --- a/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp +++ b/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp @@ -19,6 +19,8 @@ #include "../keduvocdocument.h" +#include + #include #include @@ -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 )