File indexing completed on 2024-05-12 16:59:39

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include <KRunner/AbstractRunnerTest>
0008 #include <KUnitConversion/Converter>
0009 #include <KUnitConversion/UnitCategory>
0010 #include <QRegularExpression>
0011 #include <QTest>
0012 
0013 using namespace KUnitConversion;
0014 using namespace Plasma;
0015 
0016 class ConverterRunnerTest : public AbstractRunnerTest
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void initTestCase();
0021     void testMostCommonUnits();
0022     void testCurrency();
0023     void testLettersAndCurrency();
0024     void testFractionsWithoutSpecifiedTarget();
0025     void testQuery_data();
0026     void testQuery();
0027     void testInvalidQuery_data();
0028     void testInvalidQuery();
0029     void testRoundingOfCurrencies();
0030 };
0031 
0032 void ConverterRunnerTest::initTestCase()
0033 {
0034     initProperties();
0035 }
0036 
0037 /**
0038  * Test if the most common units are displayed
0039  */
0040 void ConverterRunnerTest::testMostCommonUnits()
0041 {
0042     launchQuery(QStringLiteral("1m"));
0043 
0044     Converter converter;
0045     const auto lengthCategory = converter.category(KUnitConversion::LengthCategory);
0046     QCOMPARE(manager->matches().count(), lengthCategory.mostCommonUnits().count() - 1);
0047 }
0048 
0049 /**
0050  * Test of a currency gets converted to the most common currencies
0051  */
0052 void ConverterRunnerTest::testCurrency()
0053 {
0054     launchQuery(QStringLiteral("1$"));
0055 
0056     Converter converter;
0057     const auto currencyCategory = converter.category(KUnitConversion::CurrencyCategory);
0058     QList<Unit> currencyUnits = currencyCategory.mostCommonUnits();
0059 
0060     const QString currencyIsoCode = QLocale().currencySymbol(QLocale::CurrencyIsoCode);
0061     const KUnitConversion::Unit localCurrency = currencyCategory.unit(currencyIsoCode);
0062     if (localCurrency.isValid() && !currencyUnits.contains(localCurrency)) {
0063         currencyUnits << localCurrency;
0064     }
0065     QCOMPARE(manager->matches().count(), currencyUnits.count() - 1);
0066 }
0067 
0068 /**
0069  * Test a combination of currency symbols and letters that is not directly supported by the conversion backend
0070  */
0071 void ConverterRunnerTest::testLettersAndCurrency()
0072 {
0073     launchQuery(QStringLiteral("4us$>ca$"));
0074 
0075     QCOMPARE(manager->matches().count(), 1);
0076     QVERIFY(manager->matches().constFirst().text().contains(QLatin1String("Canadian dollars (CAD)")));
0077 }
0078 
0079 /**
0080  * Test if fractions with source unit, but without target unit get parsed
0081  */
0082 void ConverterRunnerTest::testFractionsWithoutSpecifiedTarget()
0083 {
0084     launchQuery(QStringLiteral("6/3 m"));
0085 
0086     Converter converter;
0087     const auto lengthCategory = converter.category(KUnitConversion::LengthCategory);
0088     QCOMPARE(manager->matches().count(), lengthCategory.mostCommonUnits().count() - 1);
0089 }
0090 
0091 void ConverterRunnerTest::testQuery_data()
0092 {
0093     QTest::addColumn<QString>("query");
0094     QTest::addColumn<QString>("expectedText");
0095 
0096     QTest::newRow("test specific target unit") << QStringLiteral("1m > cm") << QStringLiteral("100 centimeters (cm)");
0097 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0098     QTest::newRow("test symbols (other than currencies)") << QStringLiteral("1000 µs as year") << QStringLiteral("3.17098e-11 year (y)");
0099 #else
0100     QTest::newRow("test symbols (other than currencies)") << QStringLiteral("1000 µs as year") << QStringLiteral("3.17098E-11 year (y)");
0101 #endif
0102     QTest::newRow("test negative value") << QStringLiteral("-4m as cm") << QStringLiteral("-400 centimeters (cm)");
0103     QTest::newRow("test fractions") << QStringLiteral("6/3m>cm") << QStringLiteral("200 centimeters (cm)");
0104     QTest::newRow("test case insensitive units") << QStringLiteral("1Liter in ML") << QStringLiteral("1,000 milliliters (ml)");
0105     // megaseconds (Ms) and milliseconds (ms)
0106     QTest::newRow("test case sensitive units") << QStringLiteral("1Ms as ms") << QStringLiteral("1,000,000,000 milliseconds (ms)");
0107     QTest::newRow("test case sensitive units") << QStringLiteral("1,000,000,000milliseconds>Ms") << QStringLiteral("1 megasecond (Ms)");
0108 }
0109 
0110 void ConverterRunnerTest::testQuery()
0111 {
0112     QFETCH(QString, query);
0113     QFETCH(QString, expectedText);
0114 
0115     launchQuery(query);
0116     const QList<QueryMatch> matches = manager->matches();
0117     QCOMPARE(matches.count(), 1);
0118     QCOMPARE(matches.first().text(), expectedText);
0119 }
0120 
0121 void ConverterRunnerTest::testInvalidQuery_data()
0122 {
0123     QTest::addColumn<QString>("query");
0124 
0125     QTest::newRow("test invalid fraction without unit") << QStringLiteral("1/2");
0126     QTest::newRow("test invalid fraction without unit but valid target unit") << QStringLiteral("4/4>cm");
0127     QTest::newRow("test invalid currency") << QStringLiteral("4us$>abc$");
0128 }
0129 
0130 void ConverterRunnerTest::testInvalidQuery()
0131 {
0132     QFETCH(QString, query);
0133     launchQuery(query);
0134     QCOMPARE(manager->matches().count(), 0);
0135 }
0136 
0137 void ConverterRunnerTest::testRoundingOfCurrencies()
0138 {
0139     launchQuery(QStringLiteral("40000000000000000000000000000000000000000000000000$"));
0140     QVERIFY(!manager->matches().isEmpty());
0141     QVERIFY(!manager->matches().constFirst().text().startsWith("-"));
0142     launchQuery(QStringLiteral("50.123$"));
0143     QVERIFY(!manager->matches().isEmpty());
0144     QRegularExpression hasTwoDecimalPrescision(QStringLiteral(R"(^\d+\.\d\d)"));
0145     QVERIFY(manager->matches().constFirst().text().contains(hasTwoDecimalPrescision));
0146 }
0147 
0148 QTEST_MAIN(ConverterRunnerTest)
0149 
0150 #include "converterrunnertest.moc"