File indexing completed on 2022-09-20 12:43:20

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KCountry>
0008 #include <KCountrySubdivision>
0009 
0010 #include <QObject>
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 void initEnvironment()
0015 {
0016     qputenv("LANG", "fr_CH");
0017     QStandardPaths::setTestModeEnabled(true);
0018 }
0019 
0020 Q_CONSTRUCTOR_FUNCTION(initEnvironment)
0021 
0022 class KCountrySubdivisionTest : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void testEmpty()
0027     {
0028         KCountrySubdivision s;
0029         QVERIFY(!s.isValid());
0030         QVERIFY(!s.country().isValid());
0031         QVERIFY(s.code().isEmpty());
0032         QVERIFY(!s.parent().isValid());
0033         QVERIFY(s.subdivisions().isEmpty());
0034         QVERIFY(s.timeZoneIds().isEmpty());
0035     }
0036 
0037     void testLookup()
0038     {
0039         auto s = KCountrySubdivision::fromCode(u"DE-BE");
0040         QVERIFY(s.isValid());
0041         QCOMPARE(s.country().alpha2(), QLatin1String("DE"));
0042         QVERIFY(!s.parent().isValid());
0043         QCOMPARE(s.name(), QStringLiteral("Berlin"));
0044         QCOMPARE(s.code(), QLatin1String("DE-BE"));
0045 
0046         s = KCountrySubdivision::fromCode("AT-9");
0047         QVERIFY(s.isValid());
0048         QCOMPARE(s.country().alpha2(), QLatin1String("AT"));
0049         QVERIFY(!s.parent().isValid());
0050         QCOMPARE(s.name(), QStringLiteral("Vienne"));
0051         QCOMPARE(s.code(), QLatin1String("AT-9"));
0052 
0053         s = KCountrySubdivision::fromCode(u"FR-ARA");
0054         QVERIFY(s.isValid());
0055         QCOMPARE(s.country().alpha2(), QLatin1String("FR"));
0056         QVERIFY(!s.parent().isValid());
0057         QCOMPARE(s.name(), QStringLiteral("Auvergne-Rhône-Alpes"));
0058         QCOMPARE(s.code(), QLatin1String("FR-ARA"));
0059         QCOMPARE(KCountrySubdivision::fromCode(u"FR-aRa"), s);
0060 
0061         s = KCountrySubdivision::fromCode(u"CZ-20A");
0062         QVERIFY(s.isValid());
0063         QCOMPARE(s.country().alpha2(), QLatin1String("CZ"));
0064         QCOMPARE(s.code(), QLatin1String("CZ-20A"));
0065 
0066         s = s.parent();
0067         QVERIFY(s.isValid());
0068         QCOMPARE(s.country().alpha2(), QLatin1String("CZ"));
0069         QVERIFY(!s.parent().isValid());
0070         QCOMPARE(s.code(), QLatin1String("CZ-20"));
0071         QCOMPARE(KCountrySubdivision::fromCode("cz-20"), s);
0072 
0073         s = KCountrySubdivision::fromCode("us-or");
0074         QVERIFY(s.isValid());
0075         QCOMPARE(s.country().alpha2(), QLatin1String("US"));
0076         QVERIFY(!s.parent().isValid());
0077         QCOMPARE(s.code(), QLatin1String("US-OR"));
0078         QCOMPARE(s.name(), QLatin1String("Oregon"));
0079 
0080         QVERIFY(!KCountrySubdivision::fromCode(u"ZZ-ABC").isValid());
0081         QVERIFY(!KCountrySubdivision::fromCode("NZ-999").isValid());
0082         QVERIFY(!KCountrySubdivision::fromCode("AT-9-9").isValid());
0083         QVERIFY(!KCountrySubdivision::fromCode("").isValid());
0084         QVERIFY(!KCountrySubdivision::fromCode(nullptr).isValid());
0085     }
0086 
0087     void testSubList()
0088     {
0089         auto s = KCountrySubdivision::fromCode(u"CZ-20");
0090         QVERIFY(s.isValid());
0091         const auto l = s.subdivisions();
0092         QVERIFY(l.size() > 10);
0093         for (auto sub : l) {
0094             QVERIFY(sub.isValid());
0095             QCOMPARE(sub.parent(), s);
0096             QCOMPARE(sub.country().alpha2(), QLatin1String("CZ"));
0097         }
0098 
0099         s = KCountrySubdivision::fromCode(u"DE-BE");
0100         QCOMPARE(s.subdivisions().size(), 0);
0101     }
0102 
0103     void testTopList()
0104     {
0105         auto c = KCountry::fromAlpha2(u"CZ");
0106         QVERIFY(c.isValid());
0107         auto l = c.subdivisions();
0108         QVERIFY(l.size() > 10);
0109         for (auto s : l) {
0110             QVERIFY(s.isValid());
0111             QVERIFY(!s.parent().isValid());
0112             QCOMPARE(s.country(), c);
0113         }
0114 
0115         c = KCountry::fromAlpha2(u"DE");
0116         QVERIFY(c.isValid());
0117         l = c.subdivisions();
0118         QCOMPARE(l.size(), 16);
0119         for (auto s : l) {
0120             QVERIFY(s.isValid());
0121             QVERIFY(!s.parent().isValid());
0122             QCOMPARE(s.subdivisions().size(), 0);
0123             QCOMPARE(s.country(), c);
0124         }
0125     }
0126 
0127     void testTimezone()
0128     {
0129         auto tzs = KCountrySubdivision::fromCode("DE-BE").timeZoneIds();
0130         QCOMPARE(tzs.size(), 1);
0131         QCOMPARE(tzs.at(0), "Europe/Berlin");
0132 
0133         tzs = KCountrySubdivision::fromCode("FR-IDF").timeZoneIds();
0134         QCOMPARE(tzs.size(), 1);
0135         QCOMPARE(tzs.at(0), "Europe/Paris");
0136         tzs = KCountrySubdivision::fromCode("NL-SX").timeZoneIds();
0137         QCOMPARE(tzs.size(), 1);
0138         QCOMPARE(tzs.at(0), "America/Lower_Princes");
0139 
0140         tzs = KCountrySubdivision::fromCode("ES-CN").timeZoneIds();
0141         QCOMPARE(tzs.size(), 1);
0142         QCOMPARE(tzs.at(0), "Atlantic/Canary");
0143 
0144         tzs = KCountrySubdivision::fromCode("US-OR").timeZoneIds();
0145         QCOMPARE(tzs.size(), 2);
0146         QCOMPARE(tzs.at(0), "America/Los_Angeles");
0147         QCOMPARE(tzs.at(1), "America/Boise");
0148     }
0149 
0150     void testFromLocation_data()
0151     {
0152         QTest::addColumn<float>("lat");
0153         QTest::addColumn<float>("lon");
0154         QTest::addColumn<QString>("code");
0155 
0156         QTest::newRow("invalid") << -91.0f << 361.0f << QString();
0157         QTest::newRow("out-of-coverage") << 90.0f << 0.0f << QString();
0158 
0159         QTest::newRow("US-AK") << 65.0f << -155.0f << QStringLiteral("US-AK");
0160         QTest::newRow("US-CA") << 37.7f << -122.0f << QStringLiteral("US-CA");
0161         QTest::newRow("FR-IDF") << 48.7f << 2.5f << QStringLiteral("FR-IDF");
0162         QTest::newRow("DE-BW") << 48.7f << 9.0f << QStringLiteral("DE-BW");
0163     }
0164 
0165     void testFromLocation()
0166     {
0167         QFETCH(float, lat);
0168         QFETCH(float, lon);
0169         QFETCH(QString, code);
0170 
0171         const auto s = KCountrySubdivision::fromLocation(lat, lon);
0172         QCOMPARE(s.code(), code);
0173         const auto c = KCountry::fromLocation(lat, lon);
0174         QCOMPARE(s.country(), c);
0175     }
0176 };
0177 
0178 QTEST_GUILESS_MAIN(KCountrySubdivisionTest)
0179 
0180 #include "kcountrysubdivisiontest.moc"