File indexing completed on 2024-03-24 03:51:02
0001 /* 0002 * SPDX-FileCopyrightText: 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net> 0003 * SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "keduvocconjugation.h" 0007 #include "keduvocdeclension.h" 0008 #include "keduvocdocument.h" 0009 #include "keduvocexpression.h" 0010 #include "keduvoclesson.h" 0011 #include "keduvoctranslation.h" 0012 #include "keduvocwordtype.h" 0013 0014 #include <QTemporaryFile> 0015 0016 #include <qtest_kde.h> 0017 0018 #include <QDomDocument> 0019 #include <QObject> 0020 #include <QValidator> 0021 0022 class KEduVocDocumentValidatorTest : public QObject 0023 { 0024 Q_OBJECT 0025 0026 private slots: 0027 void testDocumentAboutInfo(); 0028 void testLessons(); 0029 void testWordTypes(); 0030 void testTranslations(); 0031 void testConjugations(); 0032 void testDeclensions(); 0033 void testAddRemoveLanguage(); 0034 }; 0035 0036 void KEduVocDocumentValidatorTest::testDocumentAboutInfo() 0037 { 0038 QTemporaryFile temp; 0039 temp.setSuffix(".kvtml"); 0040 temp.open(); 0041 QUrl fileName = QUrl::fromLocalFile(temp.fileName()); 0042 temp.close(); 0043 0044 const QString generator = QString::fromLatin1("Validator Unit Tests"); 0045 const QString author = QString::fromLatin1("Validator Test"); 0046 const QString license = QString::fromLatin1("test license"); 0047 const QString comment = QString::fromLatin1("comment"); 0048 const QString category = QString::fromLatin1("test document"); 0049 const QString title = QString::fromLatin1("Validator Test Title"); 0050 0051 KEduVocDocument doc; 0052 doc.setAuthor(author); 0053 doc.setLicense(license); 0054 doc.setDocumentComment(comment); 0055 doc.setCategory(category); 0056 doc.setTitle(title); 0057 0058 doc.saveAs(fileName, KEduVocDocument::Kvtml, generator); 0059 doc.close(); 0060 0061 KEduVocDocument docRead; 0062 docRead.open(fileName); 0063 0064 QCOMPARE(docRead.generator(), generator); 0065 QCOMPARE(docRead.author(), author); 0066 QCOMPARE(docRead.license(), license); 0067 QCOMPARE(docRead.documentComment(), comment); 0068 QCOMPARE(docRead.category(), category); 0069 QCOMPARE(docRead.title(), title); 0070 } 0071 0072 void KEduVocDocumentValidatorTest::testLessons() 0073 { 0074 const QString title = QString::fromLatin1("Validator Test Title"); 0075 QString lesson0 = QString::fromLatin1("Lesson Root"); 0076 QString lesson1 = QString::fromLatin1("Lesson 1"); 0077 QString lesson1child1 = QString::fromLatin1("Lesson 1.1"); 0078 QString lesson1child2 = QString::fromLatin1("Lesson 1.2"); 0079 QString lesson2 = QString::fromLatin1("Lesson 2"); 0080 QString lesson3 = QString::fromLatin1("Lesson 3"); 0081 0082 KEduVocDocument doc; 0083 doc.lesson()->appendChildContainer(new KEduVocLesson(lesson1, doc.lesson())); 0084 // Order here is significant as setTitle also sets the lesson name. 0085 doc.lesson()->setName(lesson0); 0086 doc.setTitle(title); 0087 QCOMPARE(doc.lesson()->childContainerCount(), 1); 0088 QCOMPARE(doc.lesson()->childContainer(0)->containerType(), KEduVocContainer::Lesson); 0089 QCOMPARE(doc.lesson()->childContainer(0)->parent(), doc.lesson()); 0090 QCOMPARE(doc.lesson()->childContainer(0)->name(), lesson1); 0091 0092 ///@todo decouple document and root lesson title 0093 QEXPECT_FAIL("", "Document and root lesson have the same name", Continue); 0094 QCOMPARE(doc.lesson()->name(), lesson0); 0095 QCOMPARE(doc.title(), title); 0096 0097 doc.lesson()->appendChildContainer(new KEduVocLesson(lesson2, doc.lesson())); 0098 doc.lesson()->appendChildContainer(new KEduVocLesson(lesson3, doc.lesson())); 0099 QCOMPARE(doc.lesson()->childContainerCount(), 3); 0100 0101 doc.lesson()->childContainer(0)->appendChildContainer(new KEduVocLesson(lesson1child1, doc.lesson()->childContainer(0))); 0102 doc.lesson()->childContainer(0)->appendChildContainer(new KEduVocLesson(lesson1child2, doc.lesson()->childContainer(0))); 0103 QCOMPARE(doc.lesson()->childContainer(0)->childContainerCount(), 2); 0104 } 0105 0106 void KEduVocDocumentValidatorTest::testWordTypes() 0107 { 0108 KEduVocDocument doc; 0109 // create doc - has no word types yet 0110 QCOMPARE(doc.wordTypeContainer()->childContainerCount(), 0); 0111 0112 KEduVocWordType *noun; 0113 KEduVocWordType *nounMale; 0114 KEduVocWordType *nounFemale; 0115 KEduVocWordType *verb; 0116 0117 noun = new KEduVocWordType("Noun", doc.wordTypeContainer()); 0118 doc.wordTypeContainer()->appendChildContainer(noun); 0119 QCOMPARE(doc.wordTypeContainer()->childContainerCount(), 1); 0120 nounMale = new KEduVocWordType("Male", noun); 0121 noun->appendChildContainer(nounMale); 0122 nounFemale = new KEduVocWordType("Female", noun); 0123 noun->appendChildContainer(nounFemale); 0124 verb = new KEduVocWordType("Verb", doc.wordTypeContainer()); 0125 doc.wordTypeContainer()->appendChildContainer(verb); 0126 QCOMPARE(doc.wordTypeContainer()->childContainerCount(), 2); 0127 QCOMPARE(doc.wordTypeContainer()->childContainer(0)->childContainerCount(), 2); 0128 0129 // create some entries 0130 for (int i = 0; i < 20; i++) { 0131 KEduVocExpression *e = new KEduVocExpression(QStringList() << QString("lang1 %1").arg(i) << QString("lang2 %1").arg(i)); 0132 doc.lesson()->appendEntry(e); 0133 e->translation(0)->setWordType(noun); 0134 e->translation(1)->setWordType(noun); 0135 } 0136 QCOMPARE(doc.lesson()->entryCount(), 20); 0137 QCOMPARE(noun->entryCount(), 20); 0138 QCOMPARE(verb->entryCount(), 0); 0139 doc.lesson()->entry(0)->translation(0)->setWordType(verb); 0140 // translation 1 is still noun, so it needs to be in both now 0141 QCOMPARE(noun->entryCount(), 20); 0142 QCOMPARE(verb->entryCount(), 1); 0143 doc.lesson()->entry(0)->translation(1)->setWordType(verb); 0144 QCOMPARE(noun->entryCount(), 19); 0145 QCOMPARE(verb->entryCount(), 1); 0146 0147 // delete word type 0148 doc.wordTypeContainer()->deleteChildContainer(1); 0149 // the word type is set to 0 when removed 0150 QVERIFY(doc.lesson()->entry(0)->translation(0)->wordType() == 0); 0151 QVERIFY(doc.lesson()->entry(0)->translation(1)->wordType() == 0); 0152 QCOMPARE(doc.wordTypeContainer()->childContainerCount(), 1); 0153 } 0154 0155 void KEduVocDocumentValidatorTest::testTranslations() 0156 { 0157 KEduVocTranslation *trans1 = new KEduVocTranslation(0, QString("My word")); 0158 QCOMPARE(trans1->text(), QString("My word")); 0159 0160 // copy ctor 0161 KEduVocTranslation *trans2 = new KEduVocTranslation(*trans1); 0162 QCOMPARE(trans2->text(), QString("My word")); 0163 0164 // operator = 0165 KEduVocTranslation *trans3 = new KEduVocTranslation(0); 0166 trans3 = new KEduVocTranslation(*trans1); 0167 QCOMPARE(trans3->text(), QString("My word")); 0168 0169 QCOMPARE((int)trans1->grade(), 0); 0170 trans1->incGrade(); 0171 QCOMPARE((int)trans1->grade(), 1); 0172 QCOMPARE((int)trans3->grade(), 0); 0173 } 0174 0175 void KEduVocDocumentValidatorTest::testDeclensions() 0176 { 0177 KEduVocTranslation translation(0); 0178 QVERIFY(translation.declension() == 0); 0179 KEduVocDeclension *declension = new KEduVocDeclension; 0180 translation.setDeclension(declension); 0181 QCOMPARE(translation.declension(), declension); 0182 } 0183 0184 void KEduVocDocumentValidatorTest::testConjugations() 0185 { 0186 KEduVocConjugation conjugation; 0187 conjugation.setConjugation(KEduVocText("first-singular"), KEduVocWordFlag::First | KEduVocWordFlag::Singular); 0188 QCOMPARE(conjugation.conjugation(KEduVocWordFlag::First | KEduVocWordFlag::Singular).text(), QString("first-singular")); 0189 0190 QDomDocument doc = QDomDocument("test doc"); 0191 QDomElement root = doc.createElement("kvtml"); 0192 doc.appendChild(root); 0193 conjugation.toKVTML2(root, "tense"); 0194 0195 qDebug() << root.text(); 0196 0197 KEduVocConjugation *con2 = KEduVocConjugation::fromKVTML2(root); 0198 0199 QCOMPARE(conjugation.conjugation(KEduVocWordFlag::First | KEduVocWordFlag::Singular).text(), 0200 con2->conjugation(KEduVocWordFlag::First | KEduVocWordFlag::Singular).text()); 0201 delete con2; 0202 } 0203 0204 void KEduVocDocumentValidatorTest::testAddRemoveLanguage() 0205 { 0206 KEduVocDocument doc; 0207 // create some initial languages 0208 doc.appendIdentifier(); 0209 doc.appendIdentifier(); 0210 doc.appendIdentifier(); 0211 doc.appendIdentifier(); 0212 doc.identifier(0).setName("0"); 0213 doc.identifier(1).setName("1"); 0214 doc.identifier(2).setName("2"); 0215 doc.identifier(3).setName("3"); 0216 0217 QCOMPARE(doc.identifierCount(), 4); 0218 KEduVocLesson *lesson = new KEduVocLesson("lesson", doc.lesson()); 0219 doc.lesson()->appendChildContainer(lesson); 0220 lesson->appendEntry(new KEduVocExpression); 0221 lesson->entry(0)->setTranslation(0, "0"); 0222 lesson->entry(0)->setTranslation(1, "1"); 0223 lesson->entry(0)->setTranslation(2, "2"); 0224 lesson->entry(0)->setTranslation(3, "3"); 0225 QCOMPARE(lesson->entry(0)->translationIndices().size(), 4); 0226 0227 // throw away the second language 0228 doc.removeIdentifier(1); 0229 0230 QCOMPARE(doc.identifierCount(), 3); 0231 QCOMPARE(doc.identifier(0).name(), QString("0")); 0232 QCOMPARE(doc.identifier(1).name(), QString("2")); 0233 QCOMPARE(doc.identifier(2).name(), QString("3")); 0234 0235 QCOMPARE(lesson->entry(0)->translationIndices().size(), 3); 0236 QCOMPARE(lesson->entry(0)->translation(0)->text(), QString("0")); 0237 QCOMPARE(lesson->entry(0)->translation(1)->text(), QString("2")); 0238 QCOMPARE(lesson->entry(0)->translation(2)->text(), QString("3")); 0239 } 0240 0241 QTEST_KDEMAIN_CORE(KEduVocDocumentValidatorTest)