Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Plasma
Plasma Add-ons
Commits
0129de13
Verified
Commit
0129de13
authored
Feb 13, 2021
by
Alexander Lohnau
💬
Browse files
GIT_SILENT Revert "Rewrite converter runner tests"
This reverts commit
b9b961b4
.
parent
abf7023a
Changes
5
Hide whitespace changes
Inline
Side-by-side
runners/converter/CMakeLists.txt
View file @
0129de13
add_definitions
(
-DTRANSLATION_DOMAIN=\"plasma_runner_converterrunner\"
)
add_library
(
krunner_converter MODULE converterrunner.cpp
)
kcoreaddons_desktop_to_json
(
krunner_converter plasma-runner-converter.desktop
)
target_link_libraries
(
krunner_converter
set
(
krunner_converter_SRCS converterrunner.cpp
)
add_library
(
krunner_converter_static STATIC
${
krunner_converter_SRCS
}
)
target_link_libraries
(
krunner_converter_static
KF5::I18n
KF5::Runner
KF5::UnitConversion
Qt::Widgets
)
add_library
(
krunner_converter MODULE plugin.cpp
)
kcoreaddons_desktop_to_json
(
krunner_converter plasma-runner-converter.desktop
)
target_link_libraries
(
krunner_converter krunner_converter_static
)
install
(
TARGETS krunner_converter DESTINATION
${
KDE_INSTALL_PLUGINDIR
}
/kf5/krunner
)
if
(
BUILD_TESTING
)
...
...
runners/converter/autotests/CMakeLists.txt
View file @
0129de13
...
...
@@ -2,5 +2,4 @@ remove_definitions(-DQT_NO_CAST_FROM_ASCII)
include
(
ECMAddTests
)
ecm_add_test
(
converterrunnertest.cpp TEST_NAME converterrunnertest LINK_LIBRARIES Qt::Test KF5::Runner KF5::UnitConversion
)
configure_krunner_test
(
converterrunnertest unitconverter
)
ecm_add_test
(
converterrunnertest.cpp TEST_NAME converterrunnertest LINK_LIBRARIES Qt::Test krunner_converter_static
)
runners/converter/autotests/converterrunnertest.cpp
View file @
0129de13
...
...
@@ -4,34 +4,45 @@
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#undef KRUNNER_TEST_RUNNER_PLUGIN_NAME
#define KRUNNER_TEST_RUNNER_PLUGIN_NAME "unitconverter"
#include <KRunner/AbstractRunnerTest>
#include <KUnitConversion/Converter>
#include <KUnitConversion/UnitCategory>
#include <QTest>
#include "../converterrunner.h"
#include <clocale>
using
namespace
KUnitConversion
;
class
ConverterRunnerTest
:
public
AbstractRunnerTes
t
class
ConverterRunnerTest
:
public
QObjec
t
{
Q_OBJECT
private
Q_SLOTS
:
void
initTestCase
();
void
testMostCommonUnits
();
void
testSpecificTargetUnit
();
void
testUnitsCaseInsensitive
();
void
testCaseSensitiveUnits
();
void
testCurrency
();
void
testLettersAndCurrency
();
void
testInvalidCurrency
();
void
testFractions
();
void
testFractionsWithoutSpecifiedTarget
();
void
testQuery_data
();
void
testQuery
();
void
testInvalidQuery_data
();
void
testInvalidQuery
();
void
testInvalidFractionsWithoutSourceUnit
();
void
testInvalidFractionsWithoutAnyUnit
();
void
testSymbolsInUnits
();
void
testNegativeValue
();
private:
ConverterRunner
*
runner
=
nullptr
;
};
void
ConverterRunnerTest
::
initTestCase
()
{
initProperties
();
setlocale
(
LC_ALL
,
"C.utf8"
);
qputenv
(
"LANG"
,
"en_US"
);
QLocale
::
setDefault
(
QLocale
::
English
);
runner
=
new
ConverterRunner
(
this
,
KPluginMetaData
(),
QVariantList
());
runner
->
init
();
}
/**
...
...
@@ -39,11 +50,57 @@ void ConverterRunnerTest::initTestCase()
*/
void
ConverterRunnerTest
::
testMostCommonUnits
()
{
launchQuery
(
QStringLiteral
(
"1m"
));
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"1m"
));
runner
->
match
(
context
);
Converter
converter
;
const
auto
lengthCategory
=
converter
.
category
(
KUnitConversion
::
LengthCategory
);
QCOMPARE
(
manager
->
matches
().
count
(),
lengthCategory
.
mostCommonUnits
().
count
()
-
1
);
QCOMPARE
(
context
.
matches
().
count
(),
lengthCategory
.
mostCommonUnits
().
count
()
-
1
);
}
/*
* Test if specifying a target unit works
*/
void
ConverterRunnerTest
::
testSpecificTargetUnit
()
{
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"1m > cm"
));
runner
->
match
(
context
);
QCOMPARE
(
context
.
matches
().
count
(),
1
);
QCOMPARE
(
context
.
matches
().
constFirst
().
text
(),
QStringLiteral
(
"100 centimeters (cm)"
));
}
/**
* Test if the units are case insensitive
*/
void
ConverterRunnerTest
::
testUnitsCaseInsensitive
()
{
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"1Liter in ML"
));
runner
->
match
(
context
);
QCOMPARE
(
context
.
matches
().
count
(),
1
);
}
/**
* Test if the units that are case sensitive are correctly parsed
*/
void
ConverterRunnerTest
::
testCaseSensitiveUnits
()
{
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"1Ms as ms"
));
runner
->
match
(
context
);
QCOMPARE
(
context
.
matches
().
count
(),
1
);
QCOMPARE
(
context
.
matches
().
constFirst
().
text
(),
QStringLiteral
(
"1,000,000,000 milliseconds (ms)"
));
Plasma
::
RunnerContext
context2
;
context2
.
setQuery
(
QStringLiteral
(
"1,000,000,000milliseconds>Ms"
));
runner
->
match
(
context2
);
QCOMPARE
(
context2
.
matches
().
count
(),
1
);
QCOMPARE
(
context2
.
matches
().
constFirst
().
text
(),
"1 megasecond (Ms)"
);
}
/**
...
...
@@ -51,7 +108,9 @@ void ConverterRunnerTest::testMostCommonUnits()
*/
void
ConverterRunnerTest
::
testCurrency
()
{
launchQuery
(
QStringLiteral
(
"1$"
));
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"1$"
));
runner
->
match
(
context
);
Converter
converter
;
const
auto
currencyCategory
=
converter
.
category
(
KUnitConversion
::
CurrencyCategory
);
...
...
@@ -62,7 +121,7 @@ void ConverterRunnerTest::testCurrency()
if
(
localCurrency
.
isValid
()
&&
!
currencyUnits
.
contains
(
localCurrency
))
{
currencyUnits
<<
localCurrency
;
}
QCOMPARE
(
manager
->
matches
().
count
(),
currencyUnits
.
count
()
-
1
);
QCOMPARE
(
context
.
matches
().
count
(),
currencyUnits
.
count
()
-
1
);
}
/**
...
...
@@ -70,10 +129,37 @@ void ConverterRunnerTest::testCurrency()
*/
void
ConverterRunnerTest
::
testLettersAndCurrency
()
{
launchQuery
(
QStringLiteral
(
"4us$>ca$"
));
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"4us$>ca$"
));
runner
->
match
(
context
);
QCOMPARE
(
manager
->
matches
().
count
(),
1
);
QVERIFY
(
manager
->
matches
().
constFirst
().
text
().
contains
(
QLatin1String
(
"Canadian dollars (CAD)"
)));
QCOMPARE
(
context
.
matches
().
count
(),
1
);
QVERIFY
(
context
.
matches
().
constFirst
().
text
().
contains
(
QLatin1String
(
"Canadian dollars (CAD)"
)));
}
/**
* Test a query that matches the regex but is not valid
*/
void
ConverterRunnerTest
::
testInvalidCurrency
()
{
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"4us$>abc$"
));
runner
->
match
(
context
);
QCOMPARE
(
context
.
matches
().
count
(),
0
);
}
/**
* Test if the fractions are correctly parsed
*/
void
ConverterRunnerTest
::
testFractions
()
{
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"6/3m>cm"
));
runner
->
match
(
context
);
QCOMPARE
(
context
.
matches
().
count
(),
1
);
QCOMPARE
(
context
.
matches
().
constFirst
().
text
(),
QStringLiteral
(
"200 centimeters (cm)"
));
}
/**
...
...
@@ -81,53 +167,62 @@ void ConverterRunnerTest::testLettersAndCurrency()
*/
void
ConverterRunnerTest
::
testFractionsWithoutSpecifiedTarget
()
{
launchQuery
(
QStringLiteral
(
"6/3 m"
));
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"6/3 m"
));
runner
->
match
(
context
);
Converter
converter
;
const
auto
lengthCategory
=
converter
.
category
(
KUnitConversion
::
LengthCategory
);
QCOMPARE
(
manager
->
matches
().
count
(),
lengthCategory
.
mostCommonUnits
().
count
()
-
1
);
QCOMPARE
(
context
.
matches
().
count
(),
lengthCategory
.
mostCommonUnits
().
count
()
-
1
);
}
void
ConverterRunnerTest
::
testQuery_data
()
/**
* Test if an invalid query with a fraction gets rejected
*/
void
ConverterRunnerTest
::
testInvalidFractionsWithoutSourceUnit
()
{
QTest
::
addColumn
<
QString
>
(
"query"
);
QTest
::
addColumn
<
QString
>
(
"expectedText"
);
QTest
::
newRow
(
"test specific target unit"
)
<<
QStringLiteral
(
"1m > cm"
)
<<
QStringLiteral
(
"100 centimeters (cm)"
);
QTest
::
newRow
(
"test symbols (other than currencies)"
)
<<
QStringLiteral
(
"1000 µs as year"
)
<<
QStringLiteral
(
"3.17098e-11 year (y)"
);
QTest
::
newRow
(
"test negative value"
)
<<
QStringLiteral
(
"-4m as cm"
)
<<
QStringLiteral
(
"-400 centimeters (cm)"
);
QTest
::
newRow
(
"test fractions"
)
<<
QStringLiteral
(
"6/3m>cm"
)
<<
QStringLiteral
(
"200 centimeters (cm)"
);
QTest
::
newRow
(
"test case insensitive units"
)
<<
QStringLiteral
(
"1Liter in ML"
)
<<
QStringLiteral
(
"1,000 milliliters (ml)"
);
// megaseconds (Ms) and milliseconds (ms)
QTest
::
newRow
(
"test case sensitive units"
)
<<
QStringLiteral
(
"1Ms as ms"
)
<<
QStringLiteral
(
"1,000,000,000 milliseconds (ms)"
);
QTest
::
newRow
(
"test case sensitive units"
)
<<
QStringLiteral
(
"1,000,000,000milliseconds>Ms"
)
<<
QStringLiteral
(
"1 megasecond (Ms)"
);
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"4/4>cm"
));
runner
->
match
(
context
);
QCOMPARE
(
context
.
matches
().
count
(),
0
);
}
void
ConverterRunnerTest
::
testQuery
()
/**
* Test if an invalid query with a fraction but no unit gets rejected
*/
void
ConverterRunnerTest
::
testInvalidFractionsWithoutAnyUnit
()
{
QFETCH
(
QString
,
query
);
QFETCH
(
QString
,
expectedText
);
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"1/2"
));
runner
->
match
(
context
);
launchQuery
(
query
);
const
QList
<
Plasma
::
QueryMatch
>
matches
=
manager
->
matches
();
QCOMPARE
(
matches
.
count
(),
1
);
QCOMPARE
(
matches
.
first
().
text
(),
expectedText
);
QCOMPARE
(
context
.
matches
().
count
(),
0
);
}
void
ConverterRunnerTest
::
testInvalidQuery_data
()
/**
* Test if symbols (other than currencies) are accepted
*/
void
ConverterRunnerTest
::
testSymbolsInUnits
()
{
QTest
::
addColumn
<
QString
>
(
"query"
);
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"1000 µs as year"
));
runner
->
match
(
context
);
QTest
::
newRow
(
"test invalid fraction without unit"
)
<<
QStringLiteral
(
"1/2"
);
QTest
::
newRow
(
"test invalid fraction without unit but valid target unit"
)
<<
QStringLiteral
(
"4/4>cm"
);
QTest
::
newRow
(
"test invalid currency"
)
<<
QStringLiteral
(
"4us$>abc$"
);
QCOMPARE
(
context
.
matches
().
count
(),
1
);
}
void
ConverterRunnerTest
::
testInvalidQuery
()
/**
* Test if negative values are accepted
*/
void
ConverterRunnerTest
::
testNegativeValue
()
{
QFETCH
(
QString
,
query
);
launchQuery
(
query
);
QCOMPARE
(
manager
->
matches
().
count
(),
0
);
Plasma
::
RunnerContext
context
;
context
.
setQuery
(
QStringLiteral
(
"-4m as cm"
));
runner
->
match
(
context
);
QCOMPARE
(
context
.
matches
().
count
(),
1
);
QCOMPARE
(
context
.
matches
().
constFirst
().
text
(),
"-400 centimeters (cm)"
);
}
QTEST_MAIN
(
ConverterRunnerTest
)
...
...
runners/converter/converterrunner.cpp
View file @
0129de13
...
...
@@ -15,8 +15,6 @@
#include <cmath>
K_EXPORT_PLASMA_RUNNER_WITH_JSON
(
ConverterRunner
,
"plasma-runner-converter.json"
)
ConverterRunner
::
ConverterRunner
(
QObject
*
parent
,
const
KPluginMetaData
&
metaData
,
const
QVariantList
&
args
)
:
Plasma
::
AbstractRunner
(
parent
,
metaData
,
args
)
{
...
...
@@ -227,4 +225,3 @@ void ConverterRunner::insertCompatibleUnits()
}
}
}
#include "converterrunner.moc"
runners/converter/plugin.cpp
0 → 100644
View file @
0129de13
/*
* SPDX-FileCopyrightText: 2007, 2008 Petri Damstén <damu@iki.fi>
* SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "converterrunner.h"
K_EXPORT_PLASMA_RUNNER_WITH_JSON
(
ConverterRunner
,
"plasma-runner-converter.json"
)
#include "plugin.moc"
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment