File indexing completed on 2024-05-12 16:46:17

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 "../gui/urlfieldlogic.h"
0031 
0032 #include <QTest>
0033 
0034 QTEST_APPLESS_MAIN( FieldTest )
0035 
0036 void FieldTest::testAll() {
0037   Tellico::Data::Field field1(QStringLiteral("name"), QStringLiteral("title"));
0038 
0039   // the default field type is line
0040   QCOMPARE(field1.type(), Tellico::Data::Field::Line);
0041   QCOMPARE(field1.name(), QStringLiteral("name"));
0042   QCOMPARE(field1.title(), QStringLiteral("title"));
0043   // description should match title, to begin with
0044   QCOMPARE(field1.description(), QStringLiteral("title"));
0045   QCOMPARE(field1.flags(), 0);
0046   QCOMPARE(field1.formatType(), Tellico::FieldFormat::FormatNone);
0047   field1.setTitle(QStringLiteral("newtitle"));
0048   QCOMPARE(field1.title(), QStringLiteral("newtitle"));
0049   field1.setCategory(QStringLiteral("category"));
0050   QCOMPARE(field1.category(), QStringLiteral("category"));
0051   field1.setDefaultValue(QStringLiteral("default"));
0052   QCOMPARE(field1.defaultValue(), QStringLiteral("default"));
0053   QCOMPARE(field1.isSingleCategory(), false);
0054 
0055   Tellico::Data::Field field1a = field1;
0056   QCOMPARE(field1.type(), field1a.type());
0057   QCOMPARE(field1.category(), field1a.category());
0058   QCOMPARE(field1.propertyList(), field1a.propertyList());
0059 
0060   Tellico::Data::Field field2(QStringLiteral("table"), QStringLiteral("Table"), Tellico::Data::Field::Table2);
0061   // should be converted to Table type
0062   QCOMPARE(field2.type(), Tellico::Data::Field::Table);
0063   QCOMPARE(field2.property(QStringLiteral("columns")), QStringLiteral("2"));
0064   field2.setProperty(QStringLiteral("columns"), QStringLiteral("1"));
0065   QCOMPARE(field2.property(QStringLiteral("columns")), QStringLiteral("1"));
0066   QCOMPARE(field2.isSingleCategory(), true);
0067   QCOMPARE(field2.flags(), int(Tellico::Data::Field::AllowMultiple));
0068   QVERIFY(field2.hasFlag(Tellico::Data::Field::AllowMultiple));
0069 
0070   QStringList allowed;
0071   allowed << QStringLiteral("choice1");
0072   Tellico::Data::Field field3(QStringLiteral("choice"), QStringLiteral("Choice"), allowed);
0073   QCOMPARE(field3.type(), Tellico::Data::Field::Choice);
0074   QCOMPARE(field3.allowed(), allowed);
0075   field3.setDefaultValue(QStringLiteral("default"));
0076   QCOMPARE(field3.defaultValue(), QString());
0077   QCOMPARE(field3.flags(), 0);
0078 
0079   Tellico::Data::Field field4(QStringLiteral("derived"), QStringLiteral("Derived"), Tellico::Data::Field::Dependent);
0080   // should be converted to Line type
0081   QCOMPARE(field4.type(), Tellico::Data::Field::Line);
0082   QCOMPARE(field4.isSingleCategory(), false);
0083   QCOMPARE(field4.flags(), int(Tellico::Data::Field::Derived));
0084   QVERIFY(field4.hasFlag(Tellico::Data::Field::Derived));
0085 
0086   Tellico::Data::Field field5(QStringLiteral("readonly"), QStringLiteral("Readonly"), Tellico::Data::Field::ReadOnly);
0087   // should be converted to Line type
0088   QCOMPARE(field5.type(), Tellico::Data::Field::Line);
0089   QCOMPARE(field5.isSingleCategory(), false);
0090   QCOMPARE(field5.flags(), int(Tellico::Data::Field::NoEdit));
0091   QVERIFY(field5.hasFlag(Tellico::Data::Field::NoEdit));
0092 }
0093 
0094 void FieldTest::testUrlFieldLogic() {
0095   Tellico::UrlFieldLogic logic;
0096   // starts out with absolute urls
0097   QCOMPARE(logic.isRelative(), false);
0098 
0099   // use a local data file to test
0100   QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("data/test.ris"));
0101   // since the logic is still absolute, the url should be unchanged
0102   QCOMPARE(logic.urlText(u), u.url());
0103 
0104   logic.setRelative(true);
0105   // since the base url is not set, the url should be unchanged
0106   QCOMPARE(logic.urlText(u), u.url());
0107 
0108   logic.setBaseUrl(QUrl(QStringLiteral("http://tellico-project.org")));
0109   // since the base url is not local, the url should be unchanged
0110   QCOMPARE(logic.urlText(u), u.url());
0111 
0112   // now use the local parent directory
0113   QUrl base = u.adjusted(QUrl::RemoveFilename);
0114   logic.setBaseUrl(base);
0115   // url text should just be the file name since it's in the same folder
0116   QCOMPARE(logic.urlText(u), QStringLiteral("test.ris"));
0117 
0118   // the base url is actually a tellico data file
0119   base = QUrl::fromLocalFile(QFINDTESTDATA("data/with-image.tc"));
0120   logic.setBaseUrl(base);
0121   // url text should still be the file name since it's in the same folder
0122   QCOMPARE(logic.urlText(u), QStringLiteral("test.ris"));
0123 
0124   // check a relative file one folder deep
0125   u = QUrl::fromLocalFile(QFINDTESTDATA("data/alexandria/0060574623.yaml"));
0126   QCOMPARE(logic.urlText(u), QStringLiteral("alexandria/0060574623.yaml"));
0127 
0128   logic.setRelative(false);
0129   QCOMPARE(logic.urlText(u), u.url());
0130 }