1 /*
2 SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6 #include <KRunner/AbstractRunnerTest>
7 #include <KShell>
8 #include <QMimeData>
9 #include <QTest>
10
11 class CalculatorRunnerTest : public AbstractRunnerTest
12 {
13 Q_OBJECT
14
15 private Q_SLOTS:
16 void initTestCase();
17 void testQuery();
18 void test42();
19 void testApproximation();
20 void testQuery_data();
21 };
22
initTestCase()23 void CalculatorRunnerTest::initTestCase()
24 {
25 initProperties();
26 }
27
testQuery()28 void CalculatorRunnerTest::testQuery()
29 {
30 QFETCH(QString, query);
31 QFETCH(QString, result);
32
33 launchQuery(query);
34 const QList<Plasma::QueryMatch> matches = manager->matches();
35 QCOMPARE(matches.size(), 1);
36 QCOMPARE(matches.first().text(), result);
37 }
38
testQuery_data()39 void CalculatorRunnerTest::testQuery_data()
40 {
41 QTest::addColumn<QString>("query");
42 QTest::addColumn<QString>("result");
43
44 // clang-format off
45 QTest::newRow("simple addition") << "1+1" << "2";
46 QTest::newRow("simple subtraction") << "2-1" << "1";
47 QTest::newRow("simple multiplication") << "2*2" << "4";
48 QTest::newRow("simple division") << "6/2" << "3";
49 QTest::newRow("simple power") << "2^3" << "8";
50
51 QTest::newRow("x as multiplication sign") << "25x4" << "100";
52 #ifdef ENABLE_QALCULATE
53 QTest::newRow("single digit factorial") << "5!" << "120";
54 QTest::newRow("superscripted number") << "2³"
55 << "8"; // BUG: 435932
56 #endif
57
58 QTest::newRow("hex to decimal lower case") << "0xf" << "15";
59 QTest::newRow("hex to decimal upper case") << "0xF" << "15";
60 QTest::newRow("hex to decimal with = at beginning") << "=0xF" << "15";
61 QTest::newRow("decimal to hex") << "hex=15" << "0xF";
62 QTest::newRow("decimal to hex with addition") << "hex=15+15" << "0x1E";
63 QTest::newRow("hex additions") << "0xF+0xF" << "30";
64 QTest::newRow("hex multiplication") << "0xF*0xF" << "225";
65 // BUG: 431362
66 QTest::newRow("hex and decimal addition") << "0x12+1" << "19";
67 QTest::newRow("hex and decimal addition reversed") << "1+0x12" << "19";
68 // clang-format on
69 }
70
testApproximation()71 void CalculatorRunnerTest::testApproximation()
72 {
73 #ifndef ENABLE_QALCULATE
74 QSKIP("Approximations are only with Qalculate supported");
75 #endif
76 launchQuery("5^1234567");
77 QCOMPARE(manager->matches().size(), 1);
78 QCOMPARE(manager->matches().constFirst().subtext(), "Approximation");
79 }
80
test42()81 void CalculatorRunnerTest::test42()
82 {
83 launchQuery("life");
84 QCOMPARE(manager->matches().size(), 1);
85 QCOMPARE(manager->matches().constFirst().text(), "42");
86 launchQuery("universe");
87 QCOMPARE(manager->matches().size(), 1);
88 QCOMPARE(manager->matches().constFirst().text(), "42");
89 }
90
91 QTEST_MAIN(CalculatorRunnerTest)
92
93 #include "calculatorrunnertest.moc"
94