Commit 1894722f authored by Jakob Petsovits's avatar Jakob Petsovits
Browse files

[runners/converter] Reject conversion queries without source unit

Commit 6e6c5707 made it so that "1/2" gets correctly rejected
(used to match in certain locales such as German), but broke
unit tests by matching invalid fraction queries like "4/4>mm".

With this commit, both of these invalid queries get rejected.
Also adding more fraction tests with units left out.
parent ebc941dc
......@@ -40,7 +40,9 @@ private Q_SLOTS:
void testLettersAndCurrency();
void testInvalidCurrency();
void testFractions();
void testInvalidFractions();
void testFractionsWithoutSpecifiedTarget();
void testInvalidFractionsWithoutSourceUnit();
void testInvalidFractionsWithoutAnyUnit();
void testSymbolsInUnits();
void testNegativeValue();
......@@ -163,7 +165,7 @@ void ConverterRunnerTest::testInvalidCurrency()
}
/**
* Test if the factions are correctly parsed
* Test if the fractions are correctly parsed
*/
void ConverterRunnerTest::testFractions()
{
......@@ -175,10 +177,24 @@ void ConverterRunnerTest::testFractions()
QCOMPARE(context.matches().first().text(), QStringLiteral("200 centimeters (cm)"));
}
/**
* Test if fractions with source unit, but without target unit get parsed
*/
void ConverterRunnerTest::testFractionsWithoutSpecifiedTarget()
{
Plasma::RunnerContext context;
context.setQuery(QStringLiteral("6/3 m"));
runner->match(context);
Converter converter;
const auto lengthCategory = converter.category(KUnitConversion::LengthCategory);
QCOMPARE(context.matches().count(), lengthCategory.mostCommonUnits().count() - 1);
}
/**
* Test if an invalid query with a fraction gets rejected
*/
void ConverterRunnerTest::testInvalidFractions()
void ConverterRunnerTest::testInvalidFractionsWithoutSourceUnit()
{
Plasma::RunnerContext context;
context.setQuery(QStringLiteral("4/4>cm"));
......@@ -187,6 +203,18 @@ void ConverterRunnerTest::testInvalidFractions()
QCOMPARE(context.matches().count(), 0);
}
/**
* Test if an invalid query with a fraction but no unit gets rejected
*/
void ConverterRunnerTest::testInvalidFractionsWithoutAnyUnit()
{
Plasma::RunnerContext context;
context.setQuery(QStringLiteral("1/2"));
runner->match(context);
QCOMPARE(context.matches().count(), 0);
}
/**
* Test if symbols (other than currencies) are accepted
*/
......
......@@ -79,14 +79,8 @@ void ConverterRunner::match(Plasma::RunnerContext &context)
const QString inputValueString = valueRegexMatch.captured(1);
// Get the different units by splitting up the query with the regex
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
QStringList unitStrings = context.query().simplified().remove(valueRegex)
.split(unitSeperatorRegex, QString::SkipEmptyParts);
#else
QStringList unitStrings = context.query().simplified().remove(valueRegex)
.split(unitSeperatorRegex, Qt::SkipEmptyParts);
#endif
if (unitStrings.isEmpty()) {
QStringList unitStrings = context.query().simplified().remove(valueRegex).split(unitSeperatorRegex);
if (unitStrings.isEmpty() || unitStrings.at(0).isEmpty()) {
return;
}
// Check if unit is valid, otherwise check for the value in the compatibleUnits map
......@@ -94,9 +88,6 @@ void ConverterRunner::match(Plasma::RunnerContext &context)
KUnitConversion::UnitCategory inputCategory = converter.categoryForUnit(inputUnitString);
if (inputCategory.id() == KUnitConversion::InvalidCategory) {
inputUnitString = compatibleUnits.value(inputUnitString.toUpper());
if (inputUnitString.isEmpty()) {
return;
}
inputCategory = converter.categoryForUnit(inputUnitString);
if (inputCategory.id() == KUnitConversion::InvalidCategory) {
return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment