File indexing completed on 2024-04-14 04:35:16
0001 /* This file is part of the KDE project 0002 Copyright (C) 2019 Jarosław Staniek <staniek@kde.org> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include <KDb> 0021 0022 #include <QObject> 0023 #include <QTest> 0024 0025 //! Autotests for KDbUtils.cpp 0026 class UtilsTest : public QObject 0027 { 0028 Q_OBJECT 0029 private Q_SLOTS: 0030 void initTestCase(); 0031 0032 //TODO: 0033 // namespace KDbUtils 0034 // { 0035 // KDB_EXPORT bool hasParent(QObject* par, QObject* o); 0036 // template<class type> 0037 // inline type findParent(QObject* o, const char* className = nullptr) 0038 0039 //! QString toISODateStringWithMs(const QTime& time); 0040 //! QString toISODateStringWithMs(const QDateTime& dateTime); 0041 void testTimeToISODateStringAndFromStringWithMs_data(); 0042 void testTimeToISODateStringAndFromStringWithMs(); 0043 0044 //! QTime timeFromISODateStringWithMs(const QString& string); 0045 //! QDateTime dateTimeFromISODateStringWithMs(const QString& string); 0046 void testDateTimeToISODateStringAndFromStringWithMs_data(); 0047 void testDateTimeToISODateStringAndFromStringWithMs(); 0048 0049 // KDB_EXPORT QDateTime stringToHackedQTime(const QString& s); 0050 // KDB_EXPORT void serializeMap(const QMap<QString, QString>& map, QByteArray *array); 0051 // KDB_EXPORT void serializeMap(const QMap<QString, QString>& map, QString *string); 0052 // KDB_EXPORT QMap<QString, QString> deserializeMap(const QByteArray& array); 0053 // KDB_EXPORT QMap<QString, QString> deserializeMap(const QString& string); 0054 // KDB_EXPORT QString stringToFileName(const QString& string); 0055 // KDB_EXPORT void simpleCrypt(QString *string); 0056 // KDB_EXPORT bool simpleDecrypt(QString *string); 0057 // KDB_EXPORT QString pointerToStringInternal(void* pointer, int size); 0058 // KDB_EXPORT void* stringToPointerInternal(const QString& string, int size); 0059 // template<class type> 0060 // QString pointerToString(type *pointer); 0061 // template<class type> 0062 // type* stringToPointer(const QString& string); 0063 // KDB_EXPORT QVariant squeezedValue(const QVariant &value); 0064 // template <class Key, class T> 0065 // class AutodeletedHash : public QHash<Key, T>; 0066 // template <typename T> 0067 // class AutodeletedList : public QList<T>; 0068 // template <typename Key, typename T> 0069 // class CaseInsensitiveHash : public QHash<Key, T>; 0070 // template <typename T> 0071 // QString debugString(const T& object) 0072 // QString findExe(const QString& appname, 0073 // const QString& path = QString(), 0074 // FindExeOptions options = FindExeOption::None); 0075 // class KDB_EXPORT Property { 0076 // public: 0077 // Property(); 0078 // Property(const QVariant &aValue, const QString &aCaption); 0079 // Property(const Property &other); 0080 // bool operator==(const Property &other) const; 0081 // bool operator!=(const Property &other) const { return !operator==(other); } 0082 // bool isNull() const; 0083 // QVariant value() const; 0084 // void setValue(const QVariant &value); 0085 // QString caption() const; 0086 // void setCaption(const QString &caption); 0087 // }; 0088 // class KDB_EXPORT PropertySet 0089 // { 0090 // public: 0091 // PropertySet(); 0092 // PropertySet(const PropertySet &other); 0093 // PropertySet& operator=(const PropertySet &other); 0094 // bool operator==(const PropertySet &other) const; 0095 // bool operator!=(const PropertySet &other) const { return !operator==(other); } 0096 // void insert(const QByteArray &name, const QVariant &value, const QString &caption = QString()); 0097 // void setCaption(const QByteArray &name, const QString &caption); 0098 // void setValue(const QByteArray &name, const QVariant &value); 0099 // void remove(const QByteArray &name); 0100 // Property property(const QByteArray &name) const; 0101 // QList<QByteArray> names() const; 0102 // }; 0103 0104 void cleanupTestCase(); 0105 }; 0106 0107 QTEST_GUILESS_MAIN(UtilsTest) 0108 0109 void UtilsTest::initTestCase() 0110 { 0111 } 0112 0113 void UtilsTest::testTimeToISODateStringAndFromStringWithMs_data() 0114 { 0115 QTest::addColumn<QTime>("time"); 0116 QTest::addColumn<QString>("string"); 0117 0118 QTest::newRow("null") << QTime() << ""; 0119 QTest::newRow("invalid") << QTime(27, 1, 1) << ""; 0120 QTest::newRow("valid1") << QTime(21, 12, 13, 1) << "21:12:13.001"; 0121 QTest::newRow("valid2") << QTime(0, 0, 1, 81) << "00:00:01.081"; 0122 QTest::newRow("valid3") << QTime(21, 12, 13, 981) << "21:12:13.981"; 0123 QTest::newRow("valid4") << QTime(21, 12, 13) << "21:12:13.000"; 0124 } 0125 0126 void UtilsTest::testTimeToISODateStringAndFromStringWithMs() 0127 { 0128 QFETCH(QTime, time); 0129 QFETCH(QString, string); 0130 0131 qDebug() << string << time; 0132 QCOMPARE(KDbUtils::toISODateStringWithMs(time), string); 0133 QCOMPARE(KDbUtils::timeFromISODateStringWithMs(string), time); 0134 } 0135 0136 void UtilsTest::testDateTimeToISODateStringAndFromStringWithMs_data() 0137 { 0138 QTest::addColumn<QDateTime>("dateTime"); 0139 QTest::addColumn<QString>("string"); 0140 0141 QTest::newRow("null") << QDateTime() << ""; 0142 QTest::newRow("invalid") << QDateTime(QDate(0, 1, 1), QTime(27, 1, 1)) << ""; 0143 QTest::newRow("valid1") << QDateTime(QDate(1999, 12, 4), QTime(21, 12, 13, 1)) << "1999-12-04T21:12:13.001"; 0144 QTest::newRow("valid2") << QDateTime(QDate(1999, 12, 4), QTime(0, 0, 1, 81)) << "1999-12-04T00:00:01.081"; 0145 QTest::newRow("valid3") << QDateTime(QDate(1999, 12, 4), QTime(21, 12, 13, 981)) << "1999-12-04T21:12:13.981"; 0146 QTest::newRow("valid4") << QDateTime(QDate(1999, 12, 4), QTime(21, 12, 13)) << "1999-12-04T21:12:13.000"; 0147 } 0148 0149 void UtilsTest::testDateTimeToISODateStringAndFromStringWithMs() 0150 { 0151 QFETCH(QDateTime, dateTime); 0152 QFETCH(QString, string); 0153 0154 qDebug() << string << dateTime << KDbUtils::dateTimeFromISODateStringWithMs(string); 0155 QCOMPARE(KDbUtils::toISODateStringWithMs(dateTime), string); 0156 QCOMPARE(KDbUtils::dateTimeFromISODateStringWithMs(string), dateTime); 0157 } 0158 0159 void UtilsTest::cleanupTestCase() 0160 { 0161 } 0162 0163 #include "UtilsTest.moc"