File indexing completed on 2024-11-10 04:55:18

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 KRunner;
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     QVERIFY2(manager->matches().constFirst().text().contains(QLatin1String("Canadian dollars (CAD)")), qUtf8Printable(manager->matches().constFirst().text()));
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     QTest::newRow("test symbols (other than currencies)") << QStringLiteral("1000 µs as year") << QStringLiteral("3.17098E-11 year (y)");
0098     QTest::newRow("test negative value") << QStringLiteral("-4m as cm") << QStringLiteral("-400 centimeters (cm)");
0099     QTest::newRow("test fractions") << QStringLiteral("6/3m>cm") << QStringLiteral("200 centimeters (cm)");
0100     QTest::newRow("test case insensitive units") << QStringLiteral("1Liter in ML") << QStringLiteral("1,000 milliliters (ml)");
0101     // megaseconds (Ms) and milliseconds (ms)
0102     QTest::newRow("test case sensitive units") << QStringLiteral("1Ms as ms") << QStringLiteral("1,000,000,000 milliseconds (ms)");
0103     QTest::newRow("test case sensitive units") << QStringLiteral("1,000,000,000milliseconds>Ms") << QStringLiteral("1 megasecond (Ms)");
0104 }
0105 
0106 void ConverterRunnerTest::testQuery()
0107 {
0108     QFETCH(QString, query);
0109     QFETCH(QString, expectedText);
0110 
0111     launchQuery(query);
0112     const QList<QueryMatch> matches = manager->matches();
0113     QCOMPARE(matches.count(), 1);
0114     QCOMPARE(matches.first().text(), expectedText);
0115 }
0116 
0117 void ConverterRunnerTest::testInvalidQuery_data()
0118 {
0119     QTest::addColumn<QString>("query");
0120 
0121     QTest::newRow("test invalid fraction without unit") << QStringLiteral("1/2");
0122     QTest::newRow("test invalid fraction without unit but valid target unit") << QStringLiteral("4/4>cm");
0123     QTest::newRow("test invalid currency") << QStringLiteral("4us$>abc$");
0124 }
0125 
0126 void ConverterRunnerTest::testInvalidQuery()
0127 {
0128     QFETCH(QString, query);
0129     launchQuery(query);
0130     QCOMPARE(manager->matches().count(), 0);
0131 }
0132 
0133 void ConverterRunnerTest::testRoundingOfCurrencies()
0134 {
0135     launchQuery(QStringLiteral("40000000000000000000000000000000000000000000000000$"));
0136     QVERIFY(!manager->matches().isEmpty());
0137     QVERIFY(!manager->matches().constFirst().text().startsWith("-"));
0138     launchQuery(QStringLiteral("50.123$"));
0139     QVERIFY(!manager->matches().isEmpty());
0140     QRegularExpression hasTwoDecimalPrescision(QStringLiteral(R"(^\d+\.\d\d)"));
0141     QVERIFY2(manager->matches().constFirst().text().contains(hasTwoDecimalPrescision), qUtf8Printable(manager->matches().constFirst().text()));
0142 }
0143 
0144 QTEST_MAIN(ConverterRunnerTest)
0145 
0146 #include "converterrunnertest.moc"