add tests for abstract formatter

This commit is contained in:
Evgenii Alekseev 2016-06-01 10:55:51 +03:00
parent 69c09d9ff8
commit 4337379177
16 changed files with 230 additions and 69 deletions

View File

@ -145,20 +145,21 @@ void AWFormatterHelper::editItems()
} }
AWFormatterHelper::FormatterClass AWAbstractFormatter::FormatterClass
AWFormatterHelper::defineFormatterClass(const QString stringType) const AWFormatterHelper::defineFormatterClass(const QString stringType) const
{ {
qCDebug(LOG_AW) << "Define formatter class for" << stringType; qCDebug(LOG_AW) << "Define formatter class for" << stringType;
FormatterClass formatter = FormatterClass::NoFormat; AWAbstractFormatter::FormatterClass formatter
= AWAbstractFormatter::FormatterClass::NoFormat;
if (stringType == QString("DateTime")) if (stringType == QString("DateTime"))
formatter = FormatterClass::DateTime; formatter = AWAbstractFormatter::FormatterClass::DateTime;
else if (stringType == QString("Float")) else if (stringType == QString("Float"))
formatter = FormatterClass::Float; formatter = AWAbstractFormatter::FormatterClass::Float;
else if (stringType == QString("NoFormat")) else if (stringType == QString("NoFormat"))
; ;
else if (stringType == QString("Script")) else if (stringType == QString("Script"))
formatter = FormatterClass::Script; formatter = AWAbstractFormatter::FormatterClass::Script;
else else
qCWarning(LOG_AW) << "Unknown formatter" << stringType; qCWarning(LOG_AW) << "Unknown formatter" << stringType;
@ -187,19 +188,19 @@ void AWFormatterHelper::initFormatters()
switch (metadata.second) { switch (metadata.second) {
case FormatterClass::DateTime: case AWAbstractFormatter::FormatterClass::DateTime:
m_formattersClasses[name] m_formattersClasses[name]
= new AWDateTimeFormatter(this, filePath); = new AWDateTimeFormatter(this, filePath);
break; break;
case FormatterClass::Float: case AWAbstractFormatter::FormatterClass::Float:
m_formattersClasses[name] m_formattersClasses[name]
= new AWFloatFormatter(this, filePath); = new AWFloatFormatter(this, filePath);
break; break;
case FormatterClass::Script: case AWAbstractFormatter::FormatterClass::Script:
m_formattersClasses[name] m_formattersClasses[name]
= new AWScriptFormatter(this, filePath); = new AWScriptFormatter(this, filePath);
break; break;
case FormatterClass::NoFormat: case AWAbstractFormatter::FormatterClass::NoFormat:
m_formattersClasses[name] = new AWNoFormatter(this, filePath); m_formattersClasses[name] = new AWNoFormatter(this, filePath);
break; break;
} }
@ -258,7 +259,7 @@ void AWFormatterHelper::installDirectories()
} }
QPair<QString, AWFormatterHelper::FormatterClass> QPair<QString, AWAbstractFormatter::FormatterClass>
AWFormatterHelper::readMetadata(const QString filePath) const AWFormatterHelper::readMetadata(const QString filePath) const
{ {
qCDebug(LOG_AW) << "Read initial parameters from" << filePath; qCDebug(LOG_AW) << "Read initial parameters from" << filePath;
@ -268,10 +269,10 @@ AWFormatterHelper::readMetadata(const QString filePath) const
QString name = settings.value(QString("Name"), filePath).toString(); QString name = settings.value(QString("Name"), filePath).toString();
QString type QString type
= settings.value(QString("X-AW-Type"), QString("NoFormat")).toString(); = settings.value(QString("X-AW-Type"), QString("NoFormat")).toString();
FormatterClass formatter = defineFormatterClass(type); AWAbstractFormatter::FormatterClass formatter = defineFormatterClass(type);
settings.endGroup(); settings.endGroup();
return QPair<QString, AWFormatterHelper::FormatterClass>(name, formatter); return QPair<QString, AWAbstractFormatter::FormatterClass>(name, formatter);
} }
@ -289,15 +290,16 @@ void AWFormatterHelper::doCreateItem()
} }
qCInfo(LOG_AW) << "Selected type" << select; qCInfo(LOG_AW) << "Selected type" << select;
FormatterClass formatter = defineFormatterClass(select); AWAbstractFormatter::FormatterClass formatter
= defineFormatterClass(select);
switch (formatter) { switch (formatter) {
case FormatterClass::DateTime: case AWAbstractFormatter::FormatterClass::DateTime:
return createItem<AWDateTimeFormatter>(); return createItem<AWDateTimeFormatter>();
case FormatterClass::Float: case AWAbstractFormatter::FormatterClass::Float:
return createItem<AWFloatFormatter>(); return createItem<AWFloatFormatter>();
case FormatterClass::Script: case AWAbstractFormatter::FormatterClass::Script:
return createItem<AWScriptFormatter>(); return createItem<AWScriptFormatter>();
case FormatterClass::NoFormat: case AWAbstractFormatter::FormatterClass::NoFormat:
return createItem<AWNoFormatter>(); return createItem<AWNoFormatter>();
} }
} }

View File

@ -21,6 +21,8 @@
#include "abstractextitemaggregator.h" #include "abstractextitemaggregator.h"
#include "awabstractformatter.h"
class AWAbstractFormatter; class AWAbstractFormatter;
@ -29,8 +31,6 @@ class AWFormatterHelper : public AbstractExtItemAggregator
Q_OBJECT Q_OBJECT
public: public:
enum class FormatterClass { DateTime, Float, Script, NoFormat };
explicit AWFormatterHelper(QWidget *parent = nullptr); explicit AWFormatterHelper(QWidget *parent = nullptr);
virtual ~AWFormatterHelper(); virtual ~AWFormatterHelper();
QString convert(const QVariant &value, const QString &name) const; QString convert(const QVariant &value, const QString &name) const;
@ -46,12 +46,12 @@ public slots:
private: private:
// methods // methods
AWFormatterHelper::FormatterClass AWAbstractFormatter::FormatterClass
defineFormatterClass(const QString stringType) const; defineFormatterClass(const QString stringType) const;
void initFormatters(); void initFormatters();
void initKeys(); void initKeys();
void installDirectories(); void installDirectories();
QPair<QString, AWFormatterHelper::FormatterClass> QPair<QString, AWAbstractFormatter::FormatterClass>
readMetadata(const QString filePath) const; readMetadata(const QString filePath) const;
// parent methods // parent methods
void doCreateItem(); void doCreateItem();

View File

@ -46,20 +46,58 @@ void AWAbstractFormatter::copyDefaults(AbstractExtItem *_other) const
QString AWAbstractFormatter::uniq() const QString AWAbstractFormatter::uniq() const
{ {
return QString("%1(%2)").arg(name()).arg(m_type); return QString("%1(%2)").arg(name()).arg(strType());
} }
QString AWAbstractFormatter::type() const QString AWAbstractFormatter::strType() const
{
QString value;
switch (m_type) {
case FormatterClass::DateTime:
value = QString("DateTime");
break;
case FormatterClass::Float:
value = QString("Float");
break;
case FormatterClass::Script:
value = QString("Script");
break;
case FormatterClass::NoFormat:
value = QString("NoFormat");
break;
}
return value;
}
AWAbstractFormatter::FormatterClass AWAbstractFormatter::type() const
{ {
return m_type; return m_type;
} }
void AWAbstractFormatter::setType(const QString _type) void AWAbstractFormatter::setStrType(const QString _type)
{ {
qCDebug(LOG_LIB) << "Type" << _type; qCDebug(LOG_LIB) << "Type" << _type;
if (_type == QString("DateTime"))
m_type = FormatterClass::DateTime;
else if (_type == QString("Float"))
m_type = FormatterClass::Float;
else if (_type == QString("Script"))
m_type = FormatterClass::Script;
else
m_type = FormatterClass::NoFormat;
}
void AWAbstractFormatter::setType(
const AWAbstractFormatter::FormatterClass _type)
{
qCDebug(LOG_LIB) << "Type" << static_cast<int>(_type);
m_type = _type; m_type = _type;
} }
@ -71,7 +109,7 @@ void AWAbstractFormatter::readConfiguration()
QSettings settings(fileName(), QSettings::IniFormat); QSettings settings(fileName(), QSettings::IniFormat);
settings.beginGroup(QString("Desktop Entry")); settings.beginGroup(QString("Desktop Entry"));
setType(settings.value(QString("X-AW-Type"), m_type).toString()); setStrType(settings.value(QString("X-AW-Type"), strType()).toString());
settings.endGroup(); settings.endGroup();
} }
@ -84,7 +122,7 @@ void AWAbstractFormatter::writeConfiguration() const
qCInfo(LOG_LIB) << "Configuration file" << settings.fileName(); qCInfo(LOG_LIB) << "Configuration file" << settings.fileName();
settings.beginGroup(QString("Desktop Entry")); settings.beginGroup(QString("Desktop Entry"));
settings.setValue(QString("X-AW-Type"), m_type); settings.setValue(QString("X-AW-Type"), strType());
settings.endGroup(); settings.endGroup();
settings.sync(); settings.sync();

View File

@ -24,9 +24,12 @@
class AWAbstractFormatter : public AbstractExtItem class AWAbstractFormatter : public AbstractExtItem
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString type READ type WRITE setType) Q_PROPERTY(FormatterClass type READ type WRITE setType)
Q_PROPERTY(QString strType READ strType WRITE setStrType)
public: public:
enum class FormatterClass { DateTime, Float, Script, NoFormat };
explicit AWAbstractFormatter(QWidget *parent, explicit AWAbstractFormatter(QWidget *parent,
const QString filePath = QString()); const QString filePath = QString());
virtual ~AWAbstractFormatter(); virtual ~AWAbstractFormatter();
@ -34,8 +37,10 @@ public:
void copyDefaults(AbstractExtItem *_other) const; void copyDefaults(AbstractExtItem *_other) const;
QString uniq() const; QString uniq() const;
// properties // properties
QString type() const; QString strType() const;
void setType(const QString _type = QString("NoFormat")); FormatterClass type() const;
void setStrType(const QString type);
void setType(const FormatterClass _type = FormatterClass::NoFormat);
public slots: public slots:
virtual void readConfiguration(); virtual void readConfiguration();
@ -44,7 +49,7 @@ public slots:
private: private:
// properties // properties
QString m_type = QString("NoFormat"); FormatterClass m_type = FormatterClass::NoFormat;
}; };

View File

@ -115,7 +115,7 @@ int AWDateTimeFormatter::showConfiguration(const QVariant args)
return ret; return ret;
setName(ui->lineEdit_name->text()); setName(ui->lineEdit_name->text());
setComment(ui->lineEdit_comment->text()); setComment(ui->lineEdit_comment->text());
setType(ui->label_typeValue->text()); setStrType(ui->label_typeValue->text());
setFormat(ui->lineEdit_format->text()); setFormat(ui->lineEdit_format->text());
writeConfiguration(); writeConfiguration();

View File

@ -212,7 +212,7 @@ int AWFloatFormatter::showConfiguration(const QVariant args)
return ret; return ret;
setName(ui->lineEdit_name->text()); setName(ui->lineEdit_name->text());
setComment(ui->lineEdit_comment->text()); setComment(ui->lineEdit_comment->text());
setType(ui->label_typeValue->text()); setStrType(ui->label_typeValue->text());
setFormat(ui->comboBox_format->currentText().at(0).toLatin1()); setFormat(ui->comboBox_format->currentText().at(0).toLatin1());
setPrecision(ui->spinBox_precision->value()); setPrecision(ui->spinBox_precision->value());
setCount(ui->spinBox_width->value()); setCount(ui->spinBox_width->value());

View File

@ -79,7 +79,7 @@ int AWNoFormatter::showConfiguration(const QVariant args)
return ret; return ret;
setName(ui->lineEdit_name->text()); setName(ui->lineEdit_name->text());
setComment(ui->lineEdit_comment->text()); setComment(ui->lineEdit_comment->text());
setType(ui->label_typeValue->text()); setStrType(ui->label_typeValue->text());
writeConfiguration(); writeConfiguration();
return ret; return ret;

View File

@ -174,7 +174,7 @@ int AWScriptFormatter::showConfiguration(const QVariant args)
return ret; return ret;
setName(ui->lineEdit_name->text()); setName(ui->lineEdit_name->text());
setComment(ui->lineEdit_comment->text()); setComment(ui->lineEdit_comment->text());
setType(ui->label_typeValue->text()); setStrType(ui->label_typeValue->text());
setAppendCode(ui->checkBox_appendCode->checkState() == Qt::Checked); setAppendCode(ui->checkBox_appendCode->checkState() == Qt::Checked);
setHasReturn(ui->checkBox_hasReturn->checkState() == Qt::Checked); setHasReturn(ui->checkBox_hasReturn->checkState() == Qt::Checked);
setCode(ui->textEdit_code->toPlainText()); setCode(ui->textEdit_code->toPlainText());

View File

@ -168,13 +168,13 @@ void ExtScript::setStrRedirect(const QString _redirect)
qCDebug(LOG_LIB) << "Redirect" << _redirect; qCDebug(LOG_LIB) << "Redirect" << _redirect;
if (_redirect == QString("stdout2sdterr")) if (_redirect == QString("stdout2sdterr"))
m_redirect = Redirect::stdout2stderr; setRedirect(Redirect::stdout2stderr);
else if (_redirect == QString("stderr2sdtout")) else if (_redirect == QString("stderr2sdtout"))
m_redirect = Redirect::stderr2stdout; setRedirect(Redirect::stderr2stdout);
else if (_redirect == QString("swap")) else if (_redirect == QString("swap"))
m_redirect = Redirect::swap; setRedirect(Redirect::swap);
else else
m_redirect = Redirect::nothing; setRedirect(Redirect::nothing);
} }
@ -317,7 +317,7 @@ int ExtScript::showConfiguration(const QVariant args)
setExecutable(ui->lineEdit_command->text()); setExecutable(ui->lineEdit_command->text());
setPrefix(ui->lineEdit_prefix->text()); setPrefix(ui->lineEdit_prefix->text());
setActive(ui->checkBox_active->checkState() == Qt::Checked); setActive(ui->checkBox_active->checkState() == Qt::Checked);
setStrRedirect(ui->comboBox_redirect->currentText()); setRedirect(static_cast<Redirect>(ui->comboBox_redirect->currentIndex()));
setInterval(ui->spinBox_interval->value()); setInterval(ui->spinBox_interval->value());
// filters // filters
updateFilter(QString("color"), updateFilter(QString("color"),

View File

@ -37,7 +37,7 @@ class ExtScript : public AbstractExtItem
Q_PROPERTY(Redirect redirect READ redirect WRITE setRedirect) Q_PROPERTY(Redirect redirect READ redirect WRITE setRedirect)
public: public:
enum class Redirect { stdout2stderr, nothing, stderr2stdout, swap }; enum class Redirect { stdout2stderr = 0, nothing = 1, stderr2stdout = 2, swap = 3 };
explicit ExtScript(QWidget *parent, const QString filePath = QString()); explicit ExtScript(QWidget *parent, const QString filePath = QString());
virtual ~ExtScript(); virtual ~ExtScript();

View File

@ -43,8 +43,6 @@ GraphicalItem::GraphicalItem(QWidget *parent, const QString filePath)
ui->setupUi(this); ui->setupUi(this);
translate(); translate();
initScene();
connect(ui->checkBox_custom, SIGNAL(stateChanged(int)), this, connect(ui->checkBox_custom, SIGNAL(stateChanged(int)), this,
SLOT(changeValue(int))); SLOT(changeValue(int)));
connect(ui->comboBox_type, SIGNAL(currentIndexChanged(int)), this, connect(ui->comboBox_type, SIGNAL(currentIndexChanged(int)), this,
@ -141,6 +139,31 @@ QString GraphicalItem::image(const QVariant &value)
} }
void GraphicalItem::initScene()
{
// cleanup
delete m_helper;
delete m_scene;
// init scene
m_scene = new QGraphicsScene();
m_scene->setBackgroundBrush(QBrush(Qt::NoBrush));
// init view
m_view = new QGraphicsView(m_scene);
m_view->setStyleSheet(QString("background: transparent"));
m_view->setContentsMargins(0, 0, 0, 0);
m_view->setFrameShape(QFrame::NoFrame);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->resize(m_width + 5, m_height + 5);
// init helper
m_helper = new GraphicalItemHelper(this, m_scene);
m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height,
m_count);
}
QString GraphicalItem::bar() const QString GraphicalItem::bar() const
{ {
return m_bar; return m_bar;
@ -433,6 +456,7 @@ void GraphicalItem::readConfiguration()
settings.endGroup(); settings.endGroup();
bumpApi(AWGIAPI); bumpApi(AWGIAPI);
initScene();
} }
@ -489,8 +513,8 @@ int GraphicalItem::showConfiguration(const QVariant args)
setMinValue(ui->doubleSpinBox_min->value()); setMinValue(ui->doubleSpinBox_min->value());
setActiveColor(ui->lineEdit_activeColor->text()); setActiveColor(ui->lineEdit_activeColor->text());
setInactiveColor(ui->lineEdit_inactiveColor->text()); setInactiveColor(ui->lineEdit_inactiveColor->text());
setStrType(ui->comboBox_type->currentText()); setType(static_cast<Type>(ui->comboBox_type->currentIndex()));
setStrDirection(ui->comboBox_direction->currentText()); setDirection(static_cast<Direction>(ui->comboBox_direction->currentIndex()));
setItemHeight(ui->spinBox_height->value()); setItemHeight(ui->spinBox_height->value());
setItemWidth(ui->spinBox_width->value()); setItemWidth(ui->spinBox_width->value());
@ -590,27 +614,6 @@ void GraphicalItem::changeValue(const int state)
} }
void GraphicalItem::initScene()
{
// init scene
m_scene = new QGraphicsScene();
m_scene->setBackgroundBrush(QBrush(Qt::NoBrush));
// init view
m_view = new QGraphicsView(m_scene);
m_view->setStyleSheet(QString("background: transparent"));
m_view->setContentsMargins(0, 0, 0, 0);
m_view->setFrameShape(QFrame::NoFrame);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->resize(m_width + 5, m_height + 5);
// init helper
m_helper = new GraphicalItemHelper(this, m_scene);
m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height,
m_count);
}
void GraphicalItem::translate() void GraphicalItem::translate()
{ {
ui->label_name->setText(i18n("Name")); ui->label_name->setText(i18n("Name"));

View File

@ -50,12 +50,13 @@ class GraphicalItem : public AbstractExtItem
public: public:
enum class Direction { LeftToRight = 0, RightToLeft = 1 }; enum class Direction { LeftToRight = 0, RightToLeft = 1 };
enum class Type { Horizontal, Vertical, Circle, Graph, Bars }; enum class Type { Horizontal = 0, Vertical = 1, Circle = 2, Graph = 3, Bars = 4 };
explicit GraphicalItem(QWidget *parent, const QString filePath = QString()); explicit GraphicalItem(QWidget *parent, const QString filePath = QString());
virtual ~GraphicalItem(); virtual ~GraphicalItem();
GraphicalItem *copy(const QString _fileName, const int _number); GraphicalItem *copy(const QString _fileName, const int _number);
QString image(const QVariant &value); QString image(const QVariant &value);
void initScene();
// get methods // get methods
QString bar() const; QString bar() const;
QString activeColor() const; QString activeColor() const;
@ -105,7 +106,6 @@ private:
QGraphicsScene *m_scene = nullptr; QGraphicsScene *m_scene = nullptr;
QGraphicsView *m_view = nullptr; QGraphicsView *m_view = nullptr;
Ui::GraphicalItem *ui = nullptr; Ui::GraphicalItem *ui = nullptr;
void initScene();
void translate(); void translate();
// properties // properties
QString m_bar = QString("cpu"); QString m_bar = QString("cpu");

View File

@ -21,7 +21,7 @@ set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Q
## modules ## modules
set(TEST_MODULES set(TEST_MODULES
abstractextitem extquotes extscript extupgrade extweather abstractextitem extquotes extscript extupgrade extweather
datetimeformatter floatformatter noformatter scriptformatter) abstractformatter datetimeformatter floatformatter noformatter scriptformatter)
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)
set(${TEST_MODULE}_SOURCES test${TEST_MODULE}.cpp) set(${TEST_MODULE}_SOURCES test${TEST_MODULE}.cpp)

View File

@ -0,0 +1,67 @@
/***************************************************************************
* 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 "testabstractformatter.h"
#include <QtTest>
#include "awnoformatter.h"
#include "awtestlibrary.h"
void TestAbstractFormatter::initTestCase()
{
formatter = new AWNoFormatter(nullptr);
}
void TestAbstractFormatter::cleanupTestCase()
{
delete formatter;
}
void TestAbstractFormatter::test_values()
{
}
void TestAbstractFormatter::test_type()
{
QString type = AWTestLibrary::randomString();
QEXPECT_FAIL("", "Will fail because of invalid format", Continue);
formatter->setStrType(type);
QCOMPARE(formatter->strType(), type);
formatter->setStrType(QString("NoFormat"));
QCOMPARE(formatter->strType(), QString("NoFormat"));
}
void TestAbstractFormatter::test_copy()
{
AWNoFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1);
QCOMPARE(newFormatter->type(), formatter->type());
QCOMPARE(newFormatter->name(), formatter->name());
delete newFormatter;
}
QTEST_MAIN(TestAbstractFormatter);

View File

@ -0,0 +1,45 @@
/***************************************************************************
* 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 TESTABSTRACTFORMATTER_H
#define TESTABSTRACTFORMATTER_H
#include <QObject>
class AWNoFormatter;
class TestAbstractFormatter : public QObject
{
Q_OBJECT
private slots:
// initialization
void initTestCase();
void cleanupTestCase();
// test
void test_values();
void test_type();
void test_copy();
private:
AWNoFormatter *formatter = nullptr;
};
#endif /* TESTABSTRACTFORMATTER_H */

View File

@ -27,7 +27,8 @@
void TestAWDateTimeFormatter::initTestCase() void TestAWDateTimeFormatter::initTestCase()
{ {
format = AWTestLibrary::randomSelect(QString(TIME_KEYS).split(QChar(','))).join(QChar(' ')); format = AWTestLibrary::randomSelect(QString(TIME_KEYS).split(QChar(',')))
.join(QChar(' '));
formatter = new AWDateTimeFormatter(nullptr); formatter = new AWDateTimeFormatter(nullptr);
formatter->setFormat(format); formatter->setFormat(format);