File indexing completed on 2024-05-19 16:41:37

0001 /*
0002     SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include <KRunner/AbstractRunnerTest>
0007 #include <KShell>
0008 #include <QMimeData>
0009 #include <QTest>
0010 #include <libqalculate/includes.h>
0011 
0012 class CalculatorRunnerTest : public AbstractRunnerTest
0013 {
0014     Q_OBJECT
0015 
0016 private Q_SLOTS:
0017     void initTestCase();
0018     void testQuery();
0019     void test42();
0020     void testApproximation();
0021     void testQuery_data();
0022 #if QALCULATE_MAJOR_VERSION > 2 || QALCULATE_MINOR_VERSION > 6
0023     void testErrorDetection();
0024 #endif
0025 };
0026 
0027 void CalculatorRunnerTest::initTestCase()
0028 {
0029     initProperties();
0030 }
0031 
0032 void CalculatorRunnerTest::testQuery()
0033 {
0034     QFETCH(QString, query);
0035     QFETCH(QString, result);
0036 
0037     launchQuery(query);
0038     const QList<Plasma::QueryMatch> matches = manager->matches();
0039     QCOMPARE(matches.size(), 1);
0040     QCOMPARE(matches.first().text(), result);
0041 }
0042 
0043 void CalculatorRunnerTest::testQuery_data()
0044 {
0045     QTest::addColumn<QString>("query");
0046     QTest::addColumn<QString>("result");
0047 
0048     // clang-format off
0049     QTest::newRow("simple addition") << "1+1" << "2";
0050     QTest::newRow("simple subtraction") << "2-1" << "1";
0051     QTest::newRow("simple multiplication") << "2*2" << "4";
0052     QTest::newRow("simple division") << "6/2" << "3";
0053     QTest::newRow("simple power") << "2^3" << "8";
0054 
0055     QTest::newRow("x as multiplication sign") << "25x4" << "100";
0056     QTest::newRow("single digit factorial") << "5!" << "120";
0057     QTest::newRow("superscripted number") << "2³"
0058                                           << "8"; // BUG: 435932
0059 
0060     QTest::newRow("hex to decimal lower case") << "0xf" << "15";
0061     QTest::newRow("hex to decimal upper case") << "0xF" << "15";
0062     QTest::newRow("hex to decimal with = at beginning") << "=0xF" << "15";
0063     QTest::newRow("decimal to hex") << "hex=15" << "0xF";
0064     QTest::newRow("decimal to hex with addition") << "hex=15+15" << "0x1E";
0065     QTest::newRow("hex additions") << "0xF+0xF" << "30";
0066     QTest::newRow("hex multiplication") << "0xF*0xF" << "225";
0067     // BUG: 431362
0068     QTest::newRow("hex and decimal addition") << "0x12+1" << "19";
0069     QTest::newRow("hex and decimal addition reversed") << "1+0x12" << "19";
0070     // clang-format on
0071 }
0072 
0073 void CalculatorRunnerTest::testApproximation()
0074 {
0075     launchQuery("5^1234567");
0076     QCOMPARE(manager->matches().size(), 1);
0077     QCOMPARE(manager->matches().constFirst().subtext(), "Approximation");
0078 }
0079 
0080 void CalculatorRunnerTest::test42()
0081 {
0082     launchQuery("life");
0083     QCOMPARE(manager->matches().size(), 1);
0084     QCOMPARE(manager->matches().constFirst().text(), "42");
0085     launchQuery("universe");
0086     QCOMPARE(manager->matches().size(), 1);
0087     QCOMPARE(manager->matches().constFirst().text(), "42");
0088 }
0089 
0090 #if QALCULATE_MAJOR_VERSION > 2 || QALCULATE_MINOR_VERSION > 6
0091 void CalculatorRunnerTest::testErrorDetection()
0092 {
0093     launchQuery("SDL_VIDEODRIVER=");
0094     QVERIFY(manager->matches().isEmpty());
0095 }
0096 #endif
0097 
0098 QTEST_MAIN(CalculatorRunnerTest)
0099 
0100 #include "calculatorrunnertest.moc"