File indexing completed on 2024-04-21 04:40:43
0001 /* 0002 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KOpeningHours/Interval> 0008 #include <KOpeningHours/OpeningHours> 0009 0010 #include <QDirIterator> 0011 #include <QFile> 0012 #include <QLocale> 0013 #include <QProcess> 0014 #include <QTest> 0015 #include <QTimeZone> 0016 0017 using namespace KOpeningHours; 0018 0019 void initLocale() 0020 { 0021 qputenv("LC_ALL", "en_US.utf-8"); 0022 qputenv("LANG", "en_US"); 0023 qputenv("TZ", "Pacific/Auckland"); // way off from what we set below 0024 } 0025 0026 Q_CONSTRUCTOR_FUNCTION(initLocale) 0027 0028 class IterationTest : public QObject 0029 { 0030 Q_OBJECT 0031 private: 0032 QByteArray intervalToString(const Interval &i) const 0033 { 0034 QByteArray b; 0035 i.begin().isValid() ? b += QLocale::c().toString(i.begin(), QStringLiteral("ddd yyyy-MM-dd hh:mm")).toUtf8() : b += "-inf"; 0036 b += " - "; 0037 i.end().isValid() ? b += QLocale::c().toString(i.end(), QStringLiteral("ddd yyyy-MM-dd hh:mm")).toUtf8() : b += "inf"; 0038 if (i.hasOpenEndTime()) { 0039 b += '+'; 0040 } 0041 b += ": "; 0042 switch (i.state()) { 0043 case Interval::Open: 0044 b += "open"; 0045 break; 0046 case Interval::Closed: 0047 b += "closed"; 0048 break; 0049 case Interval::Unknown: 0050 b += "unknown"; 0051 break; 0052 case Interval::Invalid: 0053 Q_UNREACHABLE(); 0054 break; 0055 } 0056 if (!i.comment().isEmpty()) { 0057 b += " (" + i.comment().toUtf8() + ")"; 0058 } 0059 return b + '\n'; 0060 } 0061 0062 private Q_SLOTS: 0063 void initTestCase() 0064 { 0065 QLocale::setDefault(QLocale::c()); 0066 } 0067 0068 void testIterate_data() 0069 { 0070 QTest::addColumn<QString>("inputFile"); 0071 0072 QDirIterator it(QStringLiteral(SOURCE_DIR "/data"), {QStringLiteral("*.intervals")}, QDir::Files | QDir::Readable | QDir::NoSymLinks); 0073 while (it.hasNext()) { 0074 it.next(); 0075 QTest::newRow(it.fileInfo().fileName().toLatin1().constData()) << it.filePath(); 0076 } 0077 } 0078 0079 void testIterate() 0080 { 0081 QFETCH(QString, inputFile); 0082 0083 QFile inFile(inputFile); 0084 QVERIFY(inFile.open(QFile::ReadOnly | QFile::Text)); 0085 0086 const auto expr = inFile.readLine(); 0087 OpeningHours oh(expr); 0088 oh.setLocation(52.5, 13.0); 0089 oh.setRegion(QStringLiteral("DE-BE")); 0090 oh.setTimeZone(QTimeZone("Europe/Berlin")); 0091 QCOMPARE(oh.error(), OpeningHours::NoError); 0092 0093 const auto iterationCount = inFile.readLine().toInt(); 0094 QVERIFY(iterationCount >= 10); 0095 0096 QByteArray b = expr + QByteArray::number(iterationCount) + "\n\n"; 0097 auto interval = oh.interval(QDateTime({2020, 11, 7}, {18, 32, 14})); 0098 b += intervalToString(interval); 0099 for (int i = 0; interval.isValid() && i < iterationCount; ++i) { 0100 interval = oh.nextInterval(interval); 0101 if (interval.isValid()) { 0102 b += intervalToString(interval); 0103 } 0104 } 0105 0106 inFile.seek(0); 0107 const auto refData = inFile.readAll(); 0108 if (refData != b) { 0109 QFile failFile(inputFile + QLatin1String(".fail")); 0110 QVERIFY(failFile.open(QFile::WriteOnly | QFile::Text)); 0111 failFile.write(b); 0112 failFile.close(); 0113 0114 QProcess proc; 0115 proc.setProcessChannelMode(QProcess::ForwardedChannels); 0116 proc.start(QStringLiteral("diff"), {QStringLiteral("-u"), inputFile, failFile.fileName()}); 0117 QVERIFY(proc.waitForFinished()); 0118 } 0119 0120 QCOMPARE(refData, b); 0121 } 0122 }; 0123 0124 QTEST_GUILESS_MAIN(IterationTest) 0125 0126 #include "iterationtest.moc"