add tests for pattern functions some simplifications

This commit is contained in:
Evgenii Alekseev 2016-06-25 03:11:21 +03:00
parent f6a6704fd2
commit faf259e339
7 changed files with 207 additions and 77 deletions

View File

@ -204,10 +204,10 @@ void AWKeys::reinitKeys(const QStringList currentKeys)
qCDebug(LOG_AW) << "Update found keys by using list" << currentKeys; qCDebug(LOG_AW) << "Update found keys by using list" << currentKeys;
// append lists // append lists
m_foundBars m_foundBars = AWPatternFunctions::findKeys(keyOperator->pattern(),
= AWPatternFunctions::findBars(keyOperator->pattern(), currentKeys); currentKeys, true);
m_foundKeys m_foundKeys = AWPatternFunctions::findKeys(keyOperator->pattern(),
= AWPatternFunctions::findKeys(keyOperator->pattern(), currentKeys); currentKeys, false);
m_foundLambdas = AWPatternFunctions::findLambdas(keyOperator->pattern()); m_foundLambdas = AWPatternFunctions::findLambdas(keyOperator->pattern());
// generate list of required keys for bars // generate list of required keys for bars
QStringList barKeys; QStringList barKeys;
@ -215,7 +215,7 @@ void AWKeys::reinitKeys(const QStringList currentKeys)
GraphicalItem *item = keyOperator->giByKey(bar); GraphicalItem *item = keyOperator->giByKey(bar);
if (item->isCustom()) if (item->isCustom())
item->setUsedKeys( item->setUsedKeys(
AWPatternFunctions::findKeys(item->bar(), currentKeys)); AWPatternFunctions::findKeys(item->bar(), currentKeys, false));
else else
item->setUsedKeys(QStringList() << item->bar()); item->setUsedKeys(QStringList() << item->bar());
barKeys.append(item->usedKeys()); barKeys.append(item->usedKeys());

View File

@ -86,7 +86,8 @@ QString AWPatternFunctions::expandTemplates(QString code)
} }
QVariantList AWPatternFunctions::findFunctionCalls(const QString function, QList<AWPatternFunctions::AWFunction>
AWPatternFunctions::findFunctionCalls(const QString function,
const QString code) const QString code)
{ {
qCDebug(LOG_AW) << "Looking for function" << function << "in" << code; qCDebug(LOG_AW) << "Looking for function" << function << "in" << code;
@ -102,16 +103,16 @@ QVariantList AWPatternFunctions::findFunctionCalls(const QString function,
.arg(function)); .arg(function));
regex.setPatternOptions(QRegularExpression::DotMatchesEverythingOption); regex.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
QVariantList foundFunctions; QList<AWPatternFunctions::AWFunction> foundFunctions;
QRegularExpressionMatchIterator it = regex.globalMatch(code); QRegularExpressionMatchIterator it = regex.globalMatch(code);
while (it.hasNext()) { while (it.hasNext()) {
QRegularExpressionMatch match = it.next(); QRegularExpressionMatch match = it.next();
QVariantHash metadata; AWPatternFunctions::AWFunction metadata;
// work with args // work with args
QString argsString = match.captured(QString("args")); QString argsString = match.captured(QString("args"));
if (argsString.isEmpty()) { if (argsString.isEmpty()) {
metadata[QString("args")] = QStringList(); metadata.args = QStringList();
} else { } else {
// replace '$,' to 0x1d // replace '$,' to 0x1d
argsString.replace(QString("$,"), QString(0x1d)); argsString.replace(QString("$,"), QString(0x1d));
@ -119,11 +120,11 @@ QVariantList AWPatternFunctions::findFunctionCalls(const QString function,
std::for_each(args.begin(), args.end(), [](QString &arg) { std::for_each(args.begin(), args.end(), [](QString &arg) {
arg.replace(QString(0x1d), QString(",")); arg.replace(QString(0x1d), QString(","));
}); });
metadata[QString("args")] = args; metadata.args = args;
} }
// other variables // other variables
metadata[QString("body")] = match.captured(QString("body")); metadata.body = match.captured(QString("body"));
metadata[QString("what")] = match.captured(); metadata.what = match.captured();
foundFunctions.append(metadata); foundFunctions.append(metadata);
} }
@ -136,22 +137,17 @@ QString AWPatternFunctions::insertAllKeys(QString code, const QStringList keys)
qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list" qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list"
<< keys; << keys;
QVariantList found QList<AWPatternFunctions::AWFunction> found
= AWPatternFunctions::findFunctionCalls(QString("aw_all"), code); = AWPatternFunctions::findFunctionCalls(QString("aw_all"), code);
for (auto function : found) { for (auto function : found) {
QVariantHash metadata = function.toHash();
QString separator QString separator
= metadata[QString("args")].toStringList().isEmpty() = function.args.isEmpty() ? QString(",") : function.args.at(0);
? QString(",") QStringList required = keys.filter(QRegExp(function.body));
: metadata[QString("args")].toStringList().at(0);
QStringList required
= keys.filter(QRegExp(metadata[QString("body")].toString()));
std::for_each(required.begin(), required.end(), [](QString &value) { std::for_each(required.begin(), required.end(), [](QString &value) {
value = QString("%1: $%1").arg(value); value = QString("%1: $%1").arg(value);
}); });
code.replace(metadata[QString("what")].toString(), code.replace(function.what, required.join(separator));
required.join(separator));
} }
return code; return code;
@ -163,15 +159,12 @@ QString AWPatternFunctions::insertKeyCount(QString code, const QStringList keys)
qCDebug(LOG_AW) << "Looking for count in code" << code << "using list" qCDebug(LOG_AW) << "Looking for count in code" << code << "using list"
<< keys; << keys;
QVariantList found QList<AWPatternFunctions::AWFunction> found
= AWPatternFunctions::findFunctionCalls(QString("aw_count"), code); = AWPatternFunctions::findFunctionCalls(QString("aw_count"), code);
for (auto function : found) { for (auto function : found) {
QVariantHash metadata = function.toHash(); int count = keys.filter(QRegExp(function.body)).count();
int count = keys.filter(QRegExp(metadata[QString("body")].toString()))
.count();
code.replace(metadata[QString("what")].toString(), code.replace(function.what, QString::number(count));
QString::number(count));
} }
return code; return code;
@ -183,19 +176,14 @@ QString AWPatternFunctions::insertKeyNames(QString code, const QStringList keys)
qCDebug(LOG_AW) << "Looking for key names in code" << code << "using list" qCDebug(LOG_AW) << "Looking for key names in code" << code << "using list"
<< keys; << keys;
QVariantList found QList<AWPatternFunctions::AWFunction> found
= AWPatternFunctions::findFunctionCalls(QString("aw_names"), code); = AWPatternFunctions::findFunctionCalls(QString("aw_names"), code);
for (auto function : found) { for (auto function : found) {
QVariantHash metadata = function.toHash();
QString separator QString separator
= metadata[QString("args")].toStringList().isEmpty() = function.args.isEmpty() ? QString(",") : function.args.at(0);
? QString(",") QStringList required = keys.filter(QRegExp(function.body));
: metadata[QString("args")].toStringList().at(0);
QStringList required
= keys.filter(QRegExp(metadata[QString("body")].toString()));
code.replace(metadata[QString("what")].toString(), code.replace(function.what, required.join(separator));
required.join(separator));
} }
return code; return code;
@ -207,59 +195,36 @@ QString AWPatternFunctions::insertKeys(QString code, const QStringList keys)
qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list" qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list"
<< keys; << keys;
QVariantList found QList<AWPatternFunctions::AWFunction> found
= AWPatternFunctions::findFunctionCalls(QString("aw_keys"), code); = AWPatternFunctions::findFunctionCalls(QString("aw_keys"), code);
for (auto function : found) { for (auto function : found) {
QVariantHash metadata = function.toHash();
QString separator QString separator
= metadata[QString("args")].toStringList().isEmpty() = function.args.isEmpty() ? QString(",") : function.args.at(0);
? QString(",") QStringList required = keys.filter(QRegExp(function.body));
: metadata[QString("args")].toStringList().at(0);
QStringList required
= keys.filter(QRegExp(metadata[QString("body")].toString()));
std::for_each(required.begin(), required.end(), [](QString &value) { std::for_each(required.begin(), required.end(), [](QString &value) {
value = QString("$%1").arg(value); value = QString("$%1").arg(value);
}); });
code.replace(metadata[QString("what")].toString(), code.replace(function.what, required.join(separator));
required.join(separator));
} }
return code; return code;
} }
QStringList AWPatternFunctions::findBars(const QString code,
const QStringList keys)
{
qCDebug(LOG_AW) << "Looking for bars in code" << code << "using list"
<< keys;
QStringList selectedKeys;
for (auto key : keys)
if ((key.startsWith(QString("bar")))
&& (code.contains(QString("$%1").arg(key)))) {
qCInfo(LOG_AW) << "Found bar" << key;
selectedKeys.append(key);
}
if (selectedKeys.isEmpty())
qCWarning(LOG_AW) << "No bars found";
return selectedKeys;
}
QStringList AWPatternFunctions::findKeys(const QString code, QStringList AWPatternFunctions::findKeys(const QString code,
const QStringList keys) const QStringList keys,
const bool isBars)
{ {
qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list" qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list"
<< keys; << keys;
QStringList selectedKeys; QStringList selectedKeys;
for (auto key : keys) for (auto key : keys)
if ((!key.startsWith(QString("bar"))) if ((key.startsWith(QString("bar")) == isBars)
&& (code.contains(QString("$%1").arg(key)))) { && (code.contains(QString("$%1").arg(key)))) {
qCInfo(LOG_AW) << "Found key" << key; qCInfo(LOG_AW) << "Found key" << key << "with bar enabled"
<< isBars;
selectedKeys.append(key); selectedKeys.append(key);
} }
if (selectedKeys.isEmpty()) if (selectedKeys.isEmpty())

View File

@ -27,19 +27,25 @@ class AWKeysAggregator;
namespace AWPatternFunctions namespace AWPatternFunctions
{ {
typedef struct {
QStringList args;
QString body;
QString what;
} AWFunction;
// insert methods // insert methods
QString expandLambdas(QString code, AWKeysAggregator *aggregator, QString expandLambdas(QString code, AWKeysAggregator *aggregator,
const QVariantHash &metadata, const QVariantHash &metadata,
const QStringList &usedKeys); const QStringList &usedKeys);
QString expandTemplates(QString code); QString expandTemplates(QString code);
QVariantList findFunctionCalls(const QString function, const QString code); QList<AWFunction> findFunctionCalls(const QString function, const QString code);
QString insertAllKeys(QString code, const QStringList keys); QString insertAllKeys(QString code, const QStringList keys);
QString insertKeyCount(QString code, const QStringList keys); QString insertKeyCount(QString code, const QStringList keys);
QString insertKeyNames(QString code, const QStringList keys); QString insertKeyNames(QString code, const QStringList keys);
QString insertKeys(QString code, const QStringList keys); QString insertKeys(QString code, const QStringList keys);
// find methods // find methods
QStringList findBars(const QString code, const QStringList keys); QStringList findKeys(const QString code, const QStringList keys,
QStringList findKeys(const QString code, const QStringList keys); const bool isBars);
QStringList findLambdas(const QString code); QStringList findLambdas(const QString code);
}; };

View File

@ -30,7 +30,7 @@ set(TEST_MODULES
abstractformatter datetimeformatter floatformatter noformatter scriptformatter abstractformatter datetimeformatter floatformatter noformatter scriptformatter
extitemaggregator extitemaggregator
batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource
awkeycache awupdatehelper awkeycache awpatternfunctions awupdatehelper
dpplugin) dpplugin)
foreach (TEST_MODULE ${TEST_MODULES}) foreach (TEST_MODULE ${TEST_MODULES})
set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h) set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h)
@ -40,6 +40,10 @@ foreach (TEST_MODULE ${TEST_MODULES})
${PROJECT_TRDPARTY_DIR}/fontdialog/fontdialog.cpp) ${PROJECT_TRDPARTY_DIR}/fontdialog/fontdialog.cpp)
elseif (TEST_MODULE MATCHES "awkeycache") elseif (TEST_MODULE MATCHES "awkeycache")
set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeycache.cpp) set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeycache.cpp)
elseif (TEST_MODULE MATCHES "awpatternfunctions")
set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awformatterhelper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeysaggregator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awpatternfunctions.cpp)
elseif (TEST_MODULE MATCHES "awupdatehelper") elseif (TEST_MODULE MATCHES "awupdatehelper")
set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awupdatehelper.cpp) set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awupdatehelper.cpp)
endif (TEST_MODULE MATCHES "dpplugin") endif (TEST_MODULE MATCHES "dpplugin")

View File

@ -20,8 +20,8 @@
#include <QtTest> #include <QtTest>
#include "awtestlibrary.h"
#include "awkeycache.h" #include "awkeycache.h"
#include "awtestlibrary.h"
void TestAWKeyCache::initTestCase() void TestAWKeyCache::initTestCase()

View File

@ -0,0 +1,112 @@
/***************************************************************************
* This file is part of awesome-widgets *
* *
* awesome-widgets is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* awesome-widgets is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/
#include "testawpatternfunctions.h"
#include <QtTest>
#include "awpatternfunctions.h"
#include "awtestlibrary.h"
void TestAWPatternFunctions::initTestCase()
{
}
void TestAWPatternFunctions::cleanupTestCase()
{
}
void TestAWPatternFunctions::test_findFunctionCalls()
{
QString name = QString("aw_%1").arg(AWTestLibrary::randomString(10));
QString code = AWTestLibrary::randomString(20);
QStringList args = AWTestLibrary::randomStringList(20);
QString function = QString("$%1<%2>{{%3}}")
.arg(name)
.arg(args.join(QChar(',')))
.arg(code);
QString pattern = AWTestLibrary::randomString() + function
+ AWTestLibrary::randomString();
QList<AWPatternFunctions::AWFunction> found
= AWPatternFunctions::findFunctionCalls(name, pattern);
QCOMPARE(found.count(), 1);
QCOMPARE(found.at(0).args, args);
QCOMPARE(found.at(0).body, code);
QCOMPARE(found.at(0).what, function);
}
void TestAWPatternFunctions::test_findKeys()
{
QStringList keys = AWTestLibrary::randomStringList(20);
QStringList bars = AWTestLibrary::randomStringList(20);
std::for_each(bars.begin(), bars.end(),
[](QString &bar) { bar.prepend(QString("bar")); });
QStringList noise = AWTestLibrary::randomStringList(200);
QStringList allKeys = keys + bars + noise;
QString pattern = QString("$%1 $%2")
.arg(keys.join(QString(" $")))
.arg(bars.join(QString(" $")));
keys.sort();
bars.sort();
QStringList foundKeys
= AWPatternFunctions::findKeys(pattern, allKeys, false);
foundKeys.sort();
QStringList foundBars
= AWPatternFunctions::findKeys(pattern, allKeys, true);
foundBars.sort();
QCOMPARE(foundKeys, keys);
QCOMPARE(foundBars, bars);
}
void TestAWPatternFunctions::test_findLambdas()
{
QStringList lambdas = AWTestLibrary::randomStringList(20);
QString pattern = AWTestLibrary::randomString()
+ QString("${{%1}}").arg(lambdas.join(QString("}}${{")))
+ AWTestLibrary::randomString();
QCOMPARE(AWPatternFunctions::findLambdas(pattern), lambdas);
}
void TestAWPatternFunctions::test_expandTemplates()
{
int firstValue = AWTestLibrary::randomInt();
int secondValue = AWTestLibrary::randomInt();
int result = firstValue + secondValue;
QString code
= QString("$template{{%1+%2}}").arg(firstValue).arg(secondValue);
QString prefix = AWTestLibrary::randomString();
QString pattern = prefix + code;
QCOMPARE(AWPatternFunctions::expandTemplates(pattern),
QString("%1%2").arg(prefix).arg(result));
}
QTEST_MAIN(TestAWPatternFunctions);

View File

@ -0,0 +1,43 @@
/***************************************************************************
* This file is part of awesome-widgets *
* *
* awesome-widgets is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* awesome-widgets is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/
#ifndef TESTAWPATTERNFUNCTIONS_H
#define TESTAWPATTERNFUNCTIONS_H
#include <QObject>
class TestAWPatternFunctions : public QObject
{
Q_OBJECT
private slots:
// initialization
void initTestCase();
void cleanupTestCase();
// test
void test_findFunctionCalls();
void test_findKeys();
void test_findLambdas();
void test_expandTemplates();
private:
};
#endif /* TESTAWPATTERNFUNCTIONS_H */