File indexing completed on 2024-04-14 03:46:36

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Andreas Xavier <andxav at zoho dot com>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include "keduvocdocument.h"
0007 #include "keduvocwqlreader.h"
0008 #include "readermanager.h"
0009 
0010 #include "readerTestHelpers.h"
0011 
0012 #include <QDebug>
0013 #include <QObject>
0014 #include <QTest>
0015 
0016 namespace WordQuizReaderTests
0017 {
0018 
0019 /**
0020  * @file readerwordquiztest.cpp
0021  * Unit Tests of parsing of WordQuiz WQL Files
0022  *
0023  * Each test creates a QString in WQL format
0024  * */
0025 
0026 /**
0027  * @brief Unit Tests of parsing of WQL
0028  *
0029  * Each test creates a string
0030  * reads, parses and verifies the parse.
0031  *
0032  * Most of the WQL file is ignored in the parser
0033  * , so it is replaced in test with a=1 etc.
0034  * */
0035 
0036 class WqlReaderTest : public QObject
0037 {
0038     Q_OBJECT
0039 
0040 private slots:
0041     /** Smallest passing File
0042      * */
0043     void testParseMinimalWQL();
0044     /** Missing Font Info*/
0045     void testParseMissingFontInfo();
0046 
0047 private:
0048 };
0049 
0050 /**
0051  * @brief WQLGenerator builds the WQL doc
0052  * This is to minimize repetition in the unit tests.
0053  * */
0054 class WQLGenerator
0055 {
0056 public:
0057     WQLGenerator();
0058     ~WQLGenerator();
0059 
0060     // String Data
0061     const QString headerText, fontTag, gridTag, vocabTag;
0062     const QString lang0loc, lang1loc;
0063     const QString word0Left, word0Right;
0064 
0065     // Generators of parts of the WQL Doc
0066     WQLGenerator &preamble();
0067     WQLGenerator &minimalFontInfo();
0068     WQLGenerator &minimalGridInfo();
0069     WQLGenerator &minimalVocab();
0070 
0071     /** Convert to the QIODevice that the reader expects*/
0072     QIODevice *toQIODevice();
0073     QByteArray m_barray;
0074     QBuffer *m_buffer;
0075     QString m_string;
0076 
0077     KEduVocDocument::FileType myType;
0078 };
0079 
0080 WQLGenerator::WQLGenerator()
0081     : headerText(QStringLiteral("WordQuiz\n5\n"))
0082     , fontTag(QStringLiteral("[Font Info]"))
0083     , gridTag(QStringLiteral("[Grid Info]"))
0084     , vocabTag(QStringLiteral("[Vocabulary]"))
0085     , lang0loc(QStringLiteral("en"))
0086     , lang1loc(QStringLiteral("de"))
0087     , word0Left(QStringLiteral("dog"))
0088     , word0Right(QStringLiteral("Hund"))
0089     , m_buffer(nullptr)
0090     , m_string(QLatin1String(""))
0091     , myType(KEduVocDocument::Wql)
0092 
0093 {
0094 }
0095 WQLGenerator::~WQLGenerator()
0096 {
0097     if (m_buffer) {
0098         delete m_buffer;
0099     }
0100 }
0101 
0102 WQLGenerator &WQLGenerator::preamble()
0103 {
0104     m_string = headerText;
0105     return *this;
0106 }
0107 
0108 WQLGenerator &WQLGenerator::minimalFontInfo()
0109 {
0110     m_string += fontTag + "\na=1\nb=1\nc=1\nd=1\n";
0111     return *this;
0112 }
0113 
0114 WQLGenerator &WQLGenerator::minimalGridInfo()
0115 {
0116     m_string += gridTag + "\na=1\nb=1\nc=1\n";
0117     return *this;
0118 }
0119 
0120 WQLGenerator &WQLGenerator::minimalVocab()
0121 {
0122     m_string += vocabTag + '\n' + word0Left + "[\n" + word0Right + '\n';
0123     return *this;
0124 }
0125 
0126 void WqlReaderTest::testParseMinimalWQL()
0127 {
0128     WQLGenerator gen;
0129     gen.preamble().minimalFontInfo().minimalGridInfo().minimalVocab();
0130 
0131     KVOCREADER_EXPECT(gen.m_string, KEduVocDocument::NoError, gen.myType);
0132 }
0133 
0134 void WqlReaderTest::testParseMissingFontInfo()
0135 {
0136     WQLGenerator gen;
0137     gen.preamble();
0138 
0139     KVOCREADER_EXPECT(gen.m_string, KEduVocDocument::FileReaderFailed, gen.myType);
0140 }
0141 
0142 }
0143 
0144 QTEST_MAIN(WordQuizReaderTests::WqlReaderTest)
0145 
0146 #include "readerwordquiztest.moc"