File indexing completed on 2024-05-12 05:09:58

0001 /***************************************************************************
0002     Copyright (C) 2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #undef QT_NO_CAST_FROM_ASCII
0026 
0027 #include "fieldtest.h"
0028 
0029 #include "../field.h"
0030 #include "../utils/urlfieldlogic.h"
0031 
0032 #include <KLocalizedString>
0033 
0034 #include <QTest>
0035 
0036 QTEST_APPLESS_MAIN( FieldTest )
0037 
0038 void FieldTest::initTestCase() {
0039   KLocalizedString::setApplicationDomain("tellico");
0040 }
0041 
0042 void FieldTest::testAll() {
0043   Tellico::Data::Field field1(QStringLiteral("name"), QStringLiteral("title"));
0044 
0045   // the default field type is line
0046   QCOMPARE(field1.type(), Tellico::Data::Field::Line);
0047   QCOMPARE(field1.name(), QStringLiteral("name"));
0048   QCOMPARE(field1.title(), QStringLiteral("title"));
0049   // description should match title, to begin with
0050   QCOMPARE(field1.description(), QStringLiteral("title"));
0051   QCOMPARE(field1.flags(), 0);
0052   QCOMPARE(field1.formatType(), Tellico::FieldFormat::FormatNone);
0053   field1.setTitle(QStringLiteral("newtitle"));
0054   QCOMPARE(field1.title(), QStringLiteral("newtitle"));
0055   field1.setCategory(QStringLiteral("category"));
0056   QCOMPARE(field1.category(), QStringLiteral("category"));
0057   field1.setDefaultValue(QStringLiteral("default"));
0058   QCOMPARE(field1.defaultValue(), QStringLiteral("default"));
0059   QCOMPARE(field1.isSingleCategory(), false);
0060 
0061   Tellico::Data::Field field1a = field1;
0062   QCOMPARE(field1.type(), field1a.type());
0063   QCOMPARE(field1.category(), field1a.category());
0064   QCOMPARE(field1.propertyList(), field1a.propertyList());
0065 
0066   Tellico::Data::Field field2(QStringLiteral("table"), QStringLiteral("Table"), Tellico::Data::Field::Table2);
0067   // should be converted to Table type
0068   QCOMPARE(field2.type(), Tellico::Data::Field::Table);
0069   QCOMPARE(field2.property(QStringLiteral("columns")), QStringLiteral("2"));
0070   field2.setProperty(QStringLiteral("columns"), QStringLiteral("1"));
0071   QCOMPARE(field2.property(QStringLiteral("columns")), QStringLiteral("1"));
0072   QCOMPARE(field2.isSingleCategory(), true);
0073   QCOMPARE(field2.flags(), int(Tellico::Data::Field::AllowMultiple));
0074   QVERIFY(field2.hasFlag(Tellico::Data::Field::AllowMultiple));
0075 
0076   QStringList allowed;
0077   allowed << QStringLiteral("choice1");
0078   Tellico::Data::Field field3(QStringLiteral("choice"), QStringLiteral("Choice"), allowed);
0079   QCOMPARE(field3.type(), Tellico::Data::Field::Choice);
0080   QCOMPARE(field3.allowed(), allowed);
0081   field3.setDefaultValue(QStringLiteral("default"));
0082   QCOMPARE(field3.defaultValue(), QString());
0083   QCOMPARE(field3.flags(), 0);
0084 
0085   Tellico::Data::Field field4(QStringLiteral("derived"), QStringLiteral("Derived"), Tellico::Data::Field::Dependent);
0086   // should be converted to Line type
0087   QCOMPARE(field4.type(), Tellico::Data::Field::Line);
0088   QCOMPARE(field4.isSingleCategory(), false);
0089   QCOMPARE(field4.flags(), int(Tellico::Data::Field::Derived));
0090   QVERIFY(field4.hasFlag(Tellico::Data::Field::Derived));
0091 
0092   Tellico::Data::Field field5(QStringLiteral("readonly"), QStringLiteral("Readonly"), Tellico::Data::Field::ReadOnly);
0093   // should be converted to Line type
0094   QCOMPARE(field5.type(), Tellico::Data::Field::Line);
0095   QCOMPARE(field5.isSingleCategory(), false);
0096   QCOMPARE(field5.flags(), int(Tellico::Data::Field::NoEdit));
0097   QVERIFY(field5.hasFlag(Tellico::Data::Field::NoEdit));
0098 }
0099 
0100 void FieldTest::testUrlFieldLogic() {
0101   Tellico::UrlFieldLogic logic;
0102   // starts out with absolute urls
0103   QCOMPARE(logic.isRelative(), false);
0104 
0105   // use a local data file to test
0106   QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("data/test.ris"));
0107   // since the logic is still absolute, the url should be unchanged
0108   QCOMPARE(logic.urlText(u), u.url());
0109 
0110   logic.setRelative(true);
0111   // since the base url is not set, the url should be unchanged
0112   QCOMPARE(logic.urlText(u), u.url());
0113 
0114   logic.setBaseUrl(QUrl(QStringLiteral("http://tellico-project.org")));
0115   // since the base url is not local, the url should be unchanged
0116   QCOMPARE(logic.urlText(u), u.url());
0117 
0118   // now use the local parent directory
0119   QUrl base = u.adjusted(QUrl::RemoveFilename);
0120   logic.setBaseUrl(base);
0121   // url text should just be the file name since it's in the same folder
0122   QCOMPARE(logic.urlText(u), QStringLiteral("test.ris"));
0123 
0124   // the base url is actually a tellico data file
0125   base = QUrl::fromLocalFile(QFINDTESTDATA("data/with-image.tc"));
0126   logic.setBaseUrl(base);
0127   // url text should still be the file name since it's in the same folder
0128   QCOMPARE(logic.urlText(u), QStringLiteral("test.ris"));
0129 
0130   // check a relative file one folder deep
0131   u = QUrl::fromLocalFile(QFINDTESTDATA("data/alexandria/0060574623.yaml"));
0132   QCOMPARE(logic.urlText(u), QStringLiteral("alexandria/0060574623.yaml"));
0133 
0134   logic.setRelative(false);
0135   QCOMPARE(logic.urlText(u), u.url());
0136 
0137   // test relative to url for unknown document
0138   logic.setBaseUrl(QUrl(QStringLiteral("file:Unknown")));
0139   logic.setRelative(true);
0140   // logic result should not be a relative path, but full path
0141   QCOMPARE(logic.urlText(u), u.url());
0142 }