File indexing completed on 2024-07-21 03:42:08

0001 /*
0002     This file is part of KNewStuff2.
0003     SPDX-FileCopyrightText: 2008 Jeremy Whiting <jpwhiting@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 // unit test for entry
0009 
0010 #include <QDomDocument>
0011 #include <QString>
0012 #include <QTest>
0013 #include <QXmlStreamReader>
0014 
0015 #include "entry.h"
0016 
0017 #include <knewstuffcore_debug.h>
0018 
0019 const char *entryXML =
0020     "<stuff category=\"Category\"> "
0021     "<name>Name</name>"
0022     "<providerid>https://api.opendesktop.org/v1/</providerid>"
0023     "<author homepage=\"http://testhomepage\">testauthor</author>"
0024     "<homepage>https://testhomepage</homepage>"
0025     "<licence>3</licence>" // krazy:exclude=spelling
0026     "<version>4.0</version>"
0027     "<rating>82</rating>"
0028     "<downloads>128891</downloads>"
0029     "<installedfile>/some/test/path.jpg</installedfile>"
0030     "<id>12345</id>"
0031     "<releasedate>2008-08-12<!--randomcomment--></releasedate>"
0032     "<summary>new version 3.0</summary>"
0033     "<!--randomcomment-->"
0034     "<changelog>Updated</changelog>"
0035     "<preview>https://testpreview</preview>"
0036     "<previewBig>https://testpreview</previewBig>"
0037     "<payload>http://testpayload</payload>"
0038     "<status>"
0039     "<!--randomcomment-->"
0040     "installed"
0041     "<!--randomcomment-->"
0042     "</status>"
0043     "<tags>ghns_excluded=1</tags>"
0044     "</stuff>";
0045 
0046 const QString name = QStringLiteral("Name");
0047 const QString category = QStringLiteral("Category");
0048 const QString summary = QStringLiteral("new version 3.0");
0049 const QString version = QStringLiteral("4.0");
0050 const QString license = QStringLiteral("3");
0051 
0052 class testEntry : public QObject
0053 {
0054     Q_OBJECT
0055 private:
0056     KNSCore::Entry createEntryOld();
0057     KNSCore::Entry createEntry();
0058 private Q_SLOTS:
0059     void testProperties();
0060     void testCopy();
0061     void testAssignment();
0062     void testDomImplementation();
0063 };
0064 
0065 KNSCore::Entry testEntry::createEntryOld()
0066 {
0067     QDomDocument document;
0068     document.setContent(QString::fromLatin1(entryXML));
0069     QDomElement node = document.documentElement();
0070     KNSCore::Entry entry;
0071     bool xmlResult = entry.setEntryXML(node);
0072     qCDebug(KNEWSTUFFCORE) << "Created entry from XML " << xmlResult;
0073     return entry;
0074 }
0075 
0076 KNSCore::Entry testEntry::createEntry()
0077 {
0078     QXmlStreamReader reader;
0079     reader.addData(entryXML);
0080     KNSCore::Entry entry;
0081     bool xmlResult = reader.readNextStartElement() && entry.setEntryXML(reader);
0082     qCDebug(KNEWSTUFFCORE) << "Created entry from XML " << xmlResult;
0083     return entry;
0084 }
0085 
0086 void testEntry::testProperties()
0087 {
0088     KNSCore::Entry entry = createEntry();
0089 
0090     QCOMPARE(entry.name(), name);
0091     QCOMPARE(entry.category(), category);
0092     QCOMPARE(entry.license(), license);
0093     QCOMPARE(entry.summary(), summary);
0094     QCOMPARE(entry.version(), version);
0095 }
0096 
0097 void testEntry::testCopy()
0098 {
0099     KNSCore::Entry entry = createEntry();
0100     KNSCore::Entry entry2(entry);
0101 
0102     QCOMPARE(entry.name(), entry2.name());
0103     QCOMPARE(entry.category(), entry2.category());
0104     QCOMPARE(entry.license(), entry2.license());
0105     QCOMPARE(entry.summary(), entry2.summary());
0106     QCOMPARE(entry.version(), entry2.version());
0107 }
0108 
0109 void testEntry::testAssignment()
0110 {
0111     KNSCore::Entry entry = createEntry();
0112     KNSCore::Entry entry2 = entry;
0113 
0114     QCOMPARE(entry.name(), entry2.name());
0115     QCOMPARE(entry.category(), entry2.category());
0116     QCOMPARE(entry.license(), entry2.license());
0117     QCOMPARE(entry.summary(), entry2.summary());
0118     QCOMPARE(entry.version(), entry2.version());
0119 }
0120 
0121 void testEntry::testDomImplementation()
0122 {
0123     KNSCore::Entry entry = createEntry();
0124     KNSCore::Entry entry2 = createEntryOld();
0125 
0126     QCOMPARE(entry.name(), entry2.name());
0127     QCOMPARE(entry.category(), entry2.category());
0128     QCOMPARE(entry.license(), entry2.license());
0129     QCOMPARE(entry.summary(), entry2.summary());
0130     QCOMPARE(entry.version(), entry2.version());
0131 }
0132 
0133 QTEST_GUILESS_MAIN(testEntry)
0134 #include "knewstuffentrytest.moc"