File indexing completed on 2024-05-19 11:37:30

0001 /*
0002     This file is part of the kholidays library.
0003 
0004     SPDX-FileCopyrightText: 2010 John Layt <john@layt.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "testholidayregion.h"
0010 
0011 #include <QDebug>
0012 #include <QFile>
0013 #include <QFileInfo>
0014 #include <QString>
0015 #include <QTest>
0016 
0017 QTEST_MAIN(HolidayRegionTest)
0018 
0019 void HolidayRegionTest::printMetadata(const KHolidays::HolidayRegion &region)
0020 {
0021     if (region.isValid()) {
0022         qDebug() << "This regionCode = " << region.regionCode();
0023         qDebug() << "Is valid? = " << region.isValid();
0024         qDebug() << "Country code = " << region.countryCode();
0025         qDebug() << "Language code = " << region.languageCode();
0026         qDebug() << "Name = " << region.name();
0027         qDebug() << "Description = " << region.description();
0028     } else {
0029         qDebug() << "Not Valid!";
0030     }
0031     qDebug() << "";
0032 }
0033 
0034 void HolidayRegionTest::printHolidays(const KHolidays::Holiday::List &holidays)
0035 {
0036     if (!holidays.isEmpty()) {
0037         for (const KHolidays::Holiday &holiday : holidays) {
0038             qDebug().nospace() << "Date = " << holiday.observedStartDate().toString(Qt::ISODate) << " Duration = " << holiday.duration()
0039                                << " Name = " << holiday.name().leftJustified(20, /*QChar fill=*/QLatin1Char(' '), /*bool truncate=*/false)
0040                                << " Category = " << holiday.categoryList();
0041         }
0042     } else {
0043         qDebug() << "No holidays";
0044     }
0045 }
0046 
0047 void HolidayRegionTest::parseRegionCalendarYear(const KHolidays::HolidayRegion &region, int year, const QString &calendarType)
0048 {
0049     qDebug() << "Parsing region = " << region.regionCode() << " year = " << year << " calendar = " << calendarType;
0050     printHolidays(region.rawHolidaysWithAstroSeasons(year));
0051     qDebug() << "";
0052 }
0053 
0054 void HolidayRegionTest::parseRegionDateRange(const KHolidays::HolidayRegion &region, const QDate &startDate, const QDate &endDate)
0055 {
0056     qDebug() << "Parsing regionCode = " << region.regionCode() << " start date = " << startDate.toString(Qt::ISODate)
0057              << " end date = " << endDate.toString(Qt::ISODate);
0058     printHolidays(region.rawHolidaysWithAstroSeasons(startDate, endDate));
0059     qDebug() << "";
0060 }
0061 
0062 void HolidayRegionTest::parseRawRegionDateRange(const KHolidays::HolidayRegion &region, const QDate &startDate, const QDate &endDate)
0063 {
0064     qDebug() << "Parsing regionCode = " << region.regionCode() << " start date = " << startDate.toString(Qt::ISODate)
0065              << " end date = " << endDate.toString(Qt::ISODate);
0066     printHolidays(region.rawHolidaysWithAstroSeasons(startDate, endDate));
0067     qDebug() << "";
0068 }
0069 
0070 void HolidayRegionTest::parseRegionDate(const KHolidays::HolidayRegion &region, const QDate &date)
0071 {
0072     qDebug() << "Parsing regionCode = " << region.regionCode() << " date = " << date.toString(Qt::ISODate);
0073     printHolidays(region.rawHolidaysWithAstroSeasons(date));
0074     qDebug() << "";
0075 }
0076 
0077 void HolidayRegionTest::parseRegionDateRangeCategory(const KHolidays::HolidayRegion &region,
0078                                                      const QDate &startDate,
0079                                                      const QDate &endDate,
0080                                                      const QString &category)
0081 {
0082     qDebug() << "Parsing regionCode = " << region.regionCode() << " start date = " << startDate.toString(Qt::ISODate)
0083              << " end date = " << endDate.toString(Qt::ISODate) << " category = " << category;
0084     printHolidays(region.rawHolidays(startDate, endDate, category));
0085     qDebug() << "";
0086 }
0087 
0088 void HolidayRegionTest::testLoadFileCalendarSystems()
0089 {
0090     KHolidays::HolidayRegion region(QFileInfo(KDESRCDIR "/holiday_calendar_systems"));
0091     printMetadata(region);
0092     parseRegionDateRangeCategory(region, QDate(2020, 7, 1), QDate(2021, 6, 30), "seasonal");
0093     parseRegionDateRangeCategory(region, QDate(2020, 1, 1), QDate(2022, 12, 31), "seasonal");
0094     parseRegionDateRangeCategory(region, QDate(2020, 1, 1), QDate(2020, 12, 31), "public");
0095     parseRegionCalendarYear(region, 2022);
0096     parseRegionCalendarYear(region, 2023);
0097     parseRegionCalendarYear(region, 2024);
0098     parseRegionCalendarYear(region, 2025);
0099     qDebug() << "";
0100 }
0101 
0102 void HolidayRegionTest::testLoadFile()
0103 {
0104     KHolidays::HolidayRegion region(QFileInfo(KDESRCDIR "/holiday_gb-eaw_en-gb_Test"));
0105     printMetadata(region);
0106     parseRegionCalendarYear(region, 2020, QStringLiteral("gregorian"));
0107     parseRegionCalendarYear(region, 2020);
0108     parseRegionCalendarYear(region, 2021);
0109     parseRegionCalendarYear(region, 2022);
0110     parseRegionCalendarYear(region, 2023);
0111     parseRegionCalendarYear(region, 2024);
0112     parseRegionCalendarYear(region, 2025);
0113     qDebug() << "";
0114 }
0115 
0116 void HolidayRegionTest::testGb()
0117 {
0118     KHolidays::HolidayRegion region(QStringLiteral("gb-eaw_en-gb"));
0119     printMetadata(region);
0120     parseRegionDateRange(region, QDate(2020, 7, 1), QDate(2021, 6, 30));
0121     parseRegionDateRange(region, QDate(2020, 1, 1), QDate(2022, 12, 31));
0122     parseRegionDateRange(region, QDate(2020, 1, 1), QDate(2020, 12, 31));
0123     parseRegionDate(region, QDate(2020, 1, 1));
0124     parseRegionCalendarYear(region, 2020);
0125     parseRegionCalendarYear(region, 2021);
0126     parseRegionCalendarYear(region, 2022);
0127     parseRegionCalendarYear(region, 2023);
0128     parseRegionCalendarYear(region, 2024);
0129     parseRegionCalendarYear(region, 2025);
0130 }
0131 
0132 void HolidayRegionTest::testIran()
0133 {
0134     KHolidays::HolidayRegion region(QStringLiteral("ir_en-us"));
0135     printMetadata(region);
0136     parseRegionCalendarYear(region, 2020);
0137     parseRegionCalendarYear(region, 2021);
0138     parseRegionCalendarYear(region, 2022);
0139     parseRegionCalendarYear(region, 2023);
0140     parseRegionCalendarYear(region, 2024);
0141     parseRegionCalendarYear(region, 2025);
0142 }
0143 
0144 void HolidayRegionTest::testIsrael()
0145 {
0146     KHolidays::HolidayRegion region(QStringLiteral("il_en-us"));
0147     printMetadata(region);
0148     parseRegionCalendarYear(region, 2020);
0149     parseRegionCalendarYear(region, 2021);
0150     parseRegionCalendarYear(region, 2022);
0151     parseRegionCalendarYear(region, 2023);
0152     parseRegionCalendarYear(region, 2024);
0153     parseRegionCalendarYear(region, 2025);
0154 }
0155 
0156 void HolidayRegionTest::testRegions()
0157 {
0158     qDebug() << "Available region codes:";
0159     const QStringList regionCodes = KHolidays::HolidayRegion::regionCodes();
0160     for (const QString &regionCode : regionCodes) {
0161         KHolidays::HolidayRegion testRegion(regionCode);
0162         qDebug() << regionCode << " = " << testRegion.name();
0163     }
0164     qDebug() << "";
0165 
0166     qDebug() << "This years holidays:";
0167     for (const QString &regionCode : regionCodes) {
0168         KHolidays::HolidayRegion testRegion(regionCode);
0169         printMetadata(testRegion);
0170         parseRegionCalendarYear(testRegion, QDate::currentDate().year());
0171         qDebug() << "";
0172     }
0173     qDebug() << "";
0174 }
0175 
0176 void HolidayRegionTest::testDefaultRegions()
0177 {
0178     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("be", "nl"), QString("be_nl"));
0179     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("be", "fr"), QString("be_fr"));
0180     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("be", "xx"), QString("be_fr"));
0181     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("xx", "fr_BE"), QString("be_fr"));
0182     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("xx", "xx"), QString());
0183     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("au", "en_GB"), QString("au-act_en-gb"));
0184     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("au-VIC", "en_GB"), QString("au-vic_en-gb"));
0185     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("DE-BE"), QLatin1String("de-be_de"));
0186     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("DE"), QLatin1String("de_de"));
0187     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("AT-9"), QLatin1String("at_de"));
0188     QCOMPARE(KHolidays::HolidayRegion::defaultRegionCode("US-CA"), QLatin1String("us_en-us"));
0189 }
0190 
0191 void HolidayRegionTest::testSolistaleInHolidays()
0192 {
0193     KHolidays::HolidayRegion region(QFileInfo(KDESRCDIR "/holiday_solstice_in_holidays"));
0194 
0195     auto holidays = region.rawHolidaysWithAstroSeasons(QDate(2020, 7, 1), QDate(2021, 6, 30));
0196     QCOMPARE(holidays.size(), 5);
0197     QCOMPARE(holidays.first().observedStartDate(), QDate(2020, 9, 22));
0198     QCOMPARE(holidays.first().name(), QLatin1String("September Equinox"));
0199 
0200     holidays = region.rawHolidays(QDate(2020, 7, 1), QDate(2021, 6, 30));
0201     QCOMPARE(holidays.size(), 1);
0202     QCOMPARE(holidays.first().observedStartDate(), QDate(2021, 1, 1));
0203     QCOMPARE(holidays.first().name(), QLatin1String("New Years"));
0204 
0205     holidays = region.rawHolidays(QDate(2020, 7, 1), QDate(2021, 6, 30), QLatin1String("public"));
0206     QCOMPARE(holidays.size(), 1);
0207     QCOMPARE(holidays.first().observedStartDate(), QDate(2021, 1, 1));
0208     QCOMPARE(holidays.first().name(), QLatin1String("New Years"));
0209 
0210     holidays = region.rawHolidays(QDate(2020, 7, 1), QDate(2021, 6, 30), QLatin1String("seasonal"));
0211     QCOMPARE(holidays.size(), 4);
0212     QCOMPARE(holidays.first().observedStartDate(), QDate(2020, 9, 22));
0213     QCOMPARE(holidays.first().name(), QLatin1String("September Equinox"));
0214 
0215     holidays = region.rawHolidaysWithAstroSeasons(2020);
0216     QCOMPARE(holidays.size(), 5);
0217     QCOMPARE(holidays.first().observedStartDate(), QDate(2020, 1, 1));
0218     QCOMPARE(holidays.first().name(), QLatin1String("New Years"));
0219     QCOMPARE(holidays.last().observedStartDate(), QDate(2020, 12, 21));
0220     QCOMPARE(holidays.last().name(), QLatin1String("December Solstice"));
0221 
0222     holidays = region.rawHolidaysWithAstroSeasons(QDate(2021, 3, 20));
0223     QCOMPARE(holidays.size(), 1);
0224     QCOMPARE(holidays.first().observedStartDate(), QDate(2021, 3, 20));
0225     QCOMPARE(holidays.first().name(), QLatin1String("March Equinox"));
0226 }
0227 
0228 void HolidayRegionTest::testLoadFileCalendarSystemsForPlasma()
0229 {
0230     KHolidays::HolidayRegion region(QFileInfo(KDESRCDIR "/holiday_calendar_systems"));
0231     printMetadata(region);
0232     parseRawRegionDateRange(region, QDate(2020, 7, 1), QDate(2021, 6, 30));
0233     parseRawRegionDateRange(region, QDate(2020, 1, 1), QDate(2020, 12, 31));
0234     parseRawRegionDateRange(region, QDate(2020, 11, 1), QDate(2021, 1, 2));
0235     parseRawRegionDateRange(region, QDate(2020, 12, 25), QDate(2021, 7, 2));
0236     parseRawRegionDateRange(region, QDate(2020, 12, 25), QDate(2022, 1, 2));
0237 }
0238 
0239 void HolidayRegionTest::testDominicanRepublicDiadelaConstitucion()
0240 {
0241     KHolidays::HolidayRegion region(QStringLiteral("do_es"));
0242     printMetadata(region);
0243     auto holidays = region.rawHolidays(QDate(2020, 10, 20), QDate(2020, 12, 15));
0244     QCOMPARE(holidays.first().observedStartDate(), QDate(2020, 11, 9));
0245     holidays = region.rawHolidays(QDate(2021, 10, 20), QDate(2021, 12, 15), QLatin1String("public"));
0246     QCOMPARE(holidays.first().observedStartDate(), QDate(2021, 11, 6));
0247     holidays = region.rawHolidays(QDate(2022, 10, 20), QDate(2022, 12, 15), QLatin1String("public"));
0248     QCOMPARE(holidays.first().observedStartDate(), QDate(2022, 11, 6));
0249     holidays = region.rawHolidays(QDate(2023, 10, 20), QDate(2023, 12, 15), QLatin1String("public"));
0250     QCOMPARE(holidays.first().observedStartDate(), QDate(2023, 11, 6));
0251     holidays = region.rawHolidays(QDate(2024, 10, 20), QDate(2024, 12, 15), QLatin1String("public"));
0252     QCOMPARE(holidays.first().observedStartDate(), QDate(2024, 11, 4));
0253     holidays = region.rawHolidays(QDate(2025, 10, 20), QDate(2025, 12, 15), QLatin1String("public"));
0254     QCOMPARE(holidays.first().observedStartDate(), QDate(2025, 11, 10));
0255     holidays = region.rawHolidays(QDate(2026, 10, 20), QDate(2026, 12, 15), QLatin1String("public"));
0256     QCOMPARE(holidays.first().observedStartDate(), QDate(2026, 11, 9));
0257     holidays = region.rawHolidays(QDate(2027, 10, 20), QDate(2027, 12, 15), QLatin1String("public"));
0258     QCOMPARE(holidays.first().observedStartDate(), QDate(2027, 11, 6));
0259     holidays = region.rawHolidays(QDate(2028, 10, 20), QDate(2028, 12, 15), QLatin1String("public"));
0260     QCOMPARE(holidays.first().observedStartDate(), QDate(2028, 11, 6));
0261     holidays = region.rawHolidays(QDate(2029, 10, 20), QDate(2029, 12, 15), QLatin1String("public"));
0262     QCOMPARE(holidays.first().observedStartDate(), QDate(2029, 11, 5));
0263     holidays = region.rawHolidays(QDate(2030, 10, 20), QDate(2030, 12, 15), QLatin1String("public"));
0264     QCOMPARE(holidays.first().observedStartDate(), QDate(2030, 11, 4));
0265     holidays = region.rawHolidays(QDate(2031, 10, 20), QDate(2031, 12, 15), QLatin1String("public"));
0266     QCOMPARE(holidays.first().observedStartDate(), QDate(2031, 11, 10));
0267     holidays = region.rawHolidays(QDate(2035, 10, 20), QDate(2035, 12, 15), QLatin1String("public"));
0268     QCOMPARE(holidays.first().observedStartDate(), QDate(2035, 11, 5));
0269 }
0270 
0271 void HolidayRegionTest::testUsAmericanJuneteenth()
0272 {
0273     KHolidays::HolidayRegion region(QStringLiteral("us_en-us"));
0274     printMetadata(region);
0275     auto holidays = region.rawHolidays(QDate(2023, 6, 19), QDate(2023, 6, 19));
0276     QCOMPARE(holidays.first().observedStartDate(), QDate(2023, 6, 19));
0277     holidays = region.rawHolidays(QDate(2024, 6, 1), QDate(2024, 6, 25), QLatin1String("public"));
0278     QCOMPARE(holidays.first().observedStartDate(), QDate(2024, 6, 19));
0279     holidays = region.rawHolidays(QDate(2027, 6, 1), QDate(2027, 6, 25), QLatin1String("public"));
0280     QCOMPARE(holidays.first().observedStartDate(), QDate(2027, 6, 18));
0281     holidays = region.rawHolidays(QDate(2033, 6, 20), QDate(2033, 6, 25), QLatin1String("public"));
0282     QCOMPARE(holidays.first().observedStartDate(), QDate(2033, 6, 20));
0283 }
0284 
0285 #include "moc_testholidayregion.cpp"