File indexing completed on 2024-04-21 03:48:17

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Andreas Xavier <andxav at zoho dot com>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #ifndef READERTESTHELPERS_H
0007 #define READERTESTHELPERS_H
0008 
0009 #include "keduvocdocument.h"
0010 #include <QBuffer>
0011 #include <QTemporaryFile>
0012 
0013 /** These are macros and functions common to all of the file reader tests*/
0014 namespace ReaderTestHelpersUnitTest
0015 {
0016 
0017 /** Class to manage creation/destruction of a temp doc*/
0018 class TestDoc : public QTemporaryFile
0019 {
0020 public:
0021     /** Create the file, fix the suffix and instantiate it.*/
0022     explicit TestDoc(const QString &str)
0023     {
0024         this->open(QFile::WriteOnly);
0025         QTextStream out(this);
0026         out << str;
0027         this->close();
0028     }
0029     /** Destructor*/
0030     ~TestDoc() override
0031     {
0032     }
0033 };
0034 
0035 // These macros are to force the QCOMPARE/QVERIFY to be in the test function.
0036 // In order to produce useful out QCOMPARE must be in the test function
0037 // since it is a macro
0038 
0039 // Check that a parse returns errcode
0040 #define KVOCREADER_EXPECT_CORE(str, eError, eType, isGood)                                                                                                     \
0041     do {                                                                                                                                                       \
0042         QByteArray array(str.toLatin1());                                                                                                                      \
0043         QBuffer buffer(&array);                                                                                                                                \
0044         buffer.open(QIODevice::ReadOnly);                                                                                                                      \
0045         ReaderManager::ReaderPtr reader(ReaderManager::reader(buffer));                                                                                        \
0046         KEduVocDocument docRead;                                                                                                                               \
0047         KEduVocDocument::ErrorCode actualError(reader->read(docRead));                                                                                         \
0048         KEduVocDocument::FileType actualType(reader->fileTypeHandled());                                                                                       \
0049         if (!isGood && (actualError != eError || actualType != eType)) {                                                                                       \
0050             qDebug() << str;                                                                                                                                   \
0051         }                                                                                                                                                      \
0052         QCOMPARE(int(actualError), int(eError));                                                                                                               \
0053         QCOMPARE(int(actualType), int(eType));                                                                                                                 \
0054         ReaderTestHelpersUnitTest::TestDoc tempfile(str);                                                                                                      \
0055         KEduVocDocument doc;                                                                                                                                   \
0056         QCOMPARE(int(doc.detectFileType(tempfile.fileName())), int(eType));                                                                                    \
0057     } while (0)
0058 
0059 // Check that a parse returns errcode.
0060 #define KVOCREADER_EXPECT(str, expectedError, expectedType)                                                                                                    \
0061     do {                                                                                                                                                       \
0062         KVOCREADER_EXPECT_CORE(str, expectedError, expectedType, true);                                                                                        \
0063     } while (0)
0064 
0065 // Check that a parse returns errcode. This ignores the error.
0066 #define KVOCREADER_DONT_EXPECT(str, expectedError, expectedType)                                                                                               \
0067     do {                                                                                                                                                       \
0068         QEXPECT_FAIL("", " This is a known bug.", Continue);                                                                                                   \
0069         KVOCREADER_EXPECT_CORE(str, expectedError, expectedType, false);                                                                                       \
0070     } while (0)
0071 }
0072 
0073 #endif // READERTESTHELPERS_H