diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index 45ac3e0..6a8f7f7 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -29,6 +29,7 @@ #include "awlistformatter.h" #include "awnoformatter.h" #include "awscriptformatter.h" +#include "awstringformatter.h" AWFormatterHelper::AWFormatterHelper(QWidget *parent) @@ -157,12 +158,14 @@ AWFormatterHelper::defineFormatterClass(const QString stringType) const formatter = AWAbstractFormatter::FormatterClass::DateTime; else if (stringType == QString("Float")) formatter = AWAbstractFormatter::FormatterClass::Float; + else if (stringType == QString("List")) + formatter = AWAbstractFormatter::FormatterClass::List; else if (stringType == QString("NoFormat")) ; else if (stringType == QString("Script")) formatter = AWAbstractFormatter::FormatterClass::Script; - else if (stringType == QString("List")) - formatter = AWAbstractFormatter::FormatterClass::List; + else if (stringType == QString("String")) + formatter = AWAbstractFormatter::FormatterClass::String; else qCWarning(LOG_AW) << "Unknown formatter" << stringType; @@ -199,16 +202,20 @@ void AWFormatterHelper::initFormatters() m_formattersClasses[name] = new AWFloatFormatter(this, filePath); break; + case AWAbstractFormatter::FormatterClass::List: + m_formattersClasses[name] = new AWListFormatter(this, filePath); + break; case AWAbstractFormatter::FormatterClass::Script: m_formattersClasses[name] = new AWScriptFormatter(this, filePath); break; + case AWAbstractFormatter::FormatterClass::String: + m_formattersClasses[name] + = new AWStringFormatter(this, filePath); + break; case AWAbstractFormatter::FormatterClass::NoFormat: m_formattersClasses[name] = new AWNoFormatter(this, filePath); break; - case AWAbstractFormatter::FormatterClass::List: - m_formattersClasses[name] = new AWListFormatter(this, filePath); - break; } } } @@ -286,8 +293,8 @@ void AWFormatterHelper::doCreateItem() { QStringList selection = QStringList() << QString("NoFormat") << QString("DateTime") - << QString("Float") << QString("Script") - << QString("List"); + << QString("Float") << QString("List") + << QString("Script") << QString("String"); bool ok; QString select = QInputDialog::getItem( this, i18n("Select type"), i18n("Type:"), selection, 0, false, &ok); @@ -304,12 +311,14 @@ void AWFormatterHelper::doCreateItem() return createItem(); case AWAbstractFormatter::FormatterClass::Float: return createItem(); - case AWAbstractFormatter::FormatterClass::Script: - return createItem(); - case AWAbstractFormatter::FormatterClass::NoFormat: - return createItem(); case AWAbstractFormatter::FormatterClass::List: return createItem(); + case AWAbstractFormatter::FormatterClass::Script: + return createItem(); + case AWAbstractFormatter::FormatterClass::String: + return createItem(); + case AWAbstractFormatter::FormatterClass::NoFormat: + return createItem(); } } diff --git a/sources/awesomewidgets/awabstractformatter.cpp b/sources/awesomewidgets/awabstractformatter.cpp index 013a3f7..af9e55e 100644 --- a/sources/awesomewidgets/awabstractformatter.cpp +++ b/sources/awesomewidgets/awabstractformatter.cpp @@ -60,15 +60,18 @@ QString AWAbstractFormatter::strType() const case FormatterClass::Float: value = QString("Float"); break; + case FormatterClass::List: + value = QString("List"); + break; case FormatterClass::Script: value = QString("Script"); break; + case FormatterClass::String: + value = QString("String"); + break; case FormatterClass::NoFormat: value = QString("NoFormat"); break; - case FormatterClass::List: - value = QString("List"); - break; } return value; @@ -89,10 +92,12 @@ void AWAbstractFormatter::setStrType(const QString _type) m_type = FormatterClass::DateTime; else if (_type == QString("Float")) m_type = FormatterClass::Float; - else if (_type == QString("Script")) - m_type = FormatterClass::Script; else if (_type == QString("List")) m_type = FormatterClass::List; + else if (_type == QString("Script")) + m_type = FormatterClass::Script; + else if (_type == QString("String")) + m_type = FormatterClass::String; else m_type = FormatterClass::NoFormat; } diff --git a/sources/awesomewidgets/awabstractformatter.h b/sources/awesomewidgets/awabstractformatter.h index c62c34c..883314d 100644 --- a/sources/awesomewidgets/awabstractformatter.h +++ b/sources/awesomewidgets/awabstractformatter.h @@ -28,7 +28,14 @@ class AWAbstractFormatter : public AbstractExtItem Q_PROPERTY(QString strType READ strType WRITE setStrType) public: - enum class FormatterClass { DateTime, Float, Script, NoFormat, List }; + enum class FormatterClass { + DateTime, + Float, + List, + Script, + String, + NoFormat + }; explicit AWAbstractFormatter(QWidget *parent, const QString filePath = QString()); diff --git a/sources/awesomewidgets/awfloatformatter.cpp b/sources/awesomewidgets/awfloatformatter.cpp index c936e86..b27625c 100644 --- a/sources/awesomewidgets/awfloatformatter.cpp +++ b/sources/awesomewidgets/awfloatformatter.cpp @@ -52,8 +52,13 @@ QString AWFloatFormatter::convert(const QVariant &_value) const { qCDebug(LOG_LIB) << "Convert value" << _value; - return QString("%1").arg(_value.toDouble() * multiplier() + summand(), - count(), format(), precision(), fillChar()); + QString output + = QString("%1").arg(_value.toDouble() * multiplier() + summand(), + count(), format(), precision(), fillChar()); + if (forceWidth()) + output = output.left(count()); + + return output; } @@ -68,6 +73,7 @@ AWFloatFormatter *AWFloatFormatter::copy(const QString _fileName, item->setCount(count()); item->setFormat(format()); item->setFillChar(fillChar()); + item->setForceWidth(forceWidth()); item->setMultiplier(multiplier()); item->setNumber(_number); item->setPrecision(precision()); @@ -89,6 +95,12 @@ QChar AWFloatFormatter::fillChar() const } +bool AWFloatFormatter::forceWidth() const +{ + return m_forceWidth; +} + + char AWFloatFormatter::format() const { return m_format; @@ -129,6 +141,14 @@ void AWFloatFormatter::setFillChar(const QChar _fillChar) } +void AWFloatFormatter::setForceWidth(const bool _forceWidth) +{ + qCDebug(LOG_LIB) << "Set force strip" << _forceWidth; + + m_forceWidth = _forceWidth; +} + + void AWFloatFormatter::setFormat(char _format) { qCDebug(LOG_LIB) << "Set format" << _format; @@ -177,6 +197,8 @@ void AWFloatFormatter::readConfiguration() setCount(settings.value(QString("X-AW-Width"), count()).toInt()); setFillChar( settings.value(QString("X-AW-FillChar"), fillChar()).toString().at(0)); + setForceWidth( + settings.value(QString("X-AW-ForceWidth"), forceWidth()).toBool()); setFormat(settings.value(QString("X-AW-Format"), QString(format())) .toString() .at(0) @@ -204,6 +226,8 @@ int AWFloatFormatter::showConfiguration(const QVariant args) ui->spinBox_precision->setValue(precision()); ui->spinBox_width->setValue(count()); ui->lineEdit_fill->setText(QString(fillChar())); + ui->checkBox_forceWidth->setCheckState(forceWidth() ? Qt::Checked + : Qt::Unchecked); ui->doubleSpinBox_multiplier->setValue(multiplier()); ui->doubleSpinBox_summand->setValue(summand()); @@ -217,6 +241,7 @@ int AWFloatFormatter::showConfiguration(const QVariant args) setPrecision(ui->spinBox_precision->value()); setCount(ui->spinBox_width->value()); setFillChar(ui->lineEdit_fill->text().at(0)); + setForceWidth(ui->checkBox_forceWidth->checkState() == Qt::Checked); setMultiplier(ui->doubleSpinBox_multiplier->value()); setSummand(ui->doubleSpinBox_summand->value()); @@ -236,6 +261,7 @@ void AWFloatFormatter::writeConfiguration() const settings.setValue(QString("X-AW-Width"), count()); settings.setValue(QString("X-AW-FillChar"), fillChar()); settings.setValue(QString("X-AW-Format"), format()); + settings.setValue(QString("X-AW-ForceWidth"), forceWidth()); settings.setValue(QString("X-AW-Multiplier"), multiplier()); settings.setValue(QString("X-AW-Precision"), precision()); settings.setValue(QString("X-AW-Summand"), summand()); @@ -254,6 +280,7 @@ void AWFloatFormatter::translate() ui->label_precision->setText(i18n("Precision")); ui->label_width->setText(i18n("Width")); ui->label_fill->setText(i18n("Fill char")); + ui->checkBox_forceWidth->setText(i18n("Force width")); ui->label_multiplier->setText(i18n("Multiplier")); ui->label_summand->setText(i18n("Summand")); } diff --git a/sources/awesomewidgets/awfloatformatter.h b/sources/awesomewidgets/awfloatformatter.h index 7fcb6ef..7a687c7 100644 --- a/sources/awesomewidgets/awfloatformatter.h +++ b/sources/awesomewidgets/awfloatformatter.h @@ -31,6 +31,7 @@ class AWFloatFormatter : public AWAbstractFormatter Q_OBJECT Q_PROPERTY(int count READ count WRITE setCount) Q_PROPERTY(QChar fillChar READ fillChar WRITE setFillChar) + Q_PROPERTY(bool forceWidth READ forceWidth WRITE setForceWidth) Q_PROPERTY(char format READ format WRITE setFormat) Q_PROPERTY(double multiplier READ multiplier WRITE setMultiplier) Q_PROPERTY(int precision READ precision WRITE setPrecision) @@ -45,12 +46,14 @@ public: // properties int count() const; QChar fillChar() const; + bool forceWidth() const; char format() const; double multiplier() const; int precision() const; double summand() const; void setCount(const int _count); void setFillChar(const QChar _fillChar); + void setForceWidth(const bool _forceWidth); void setFormat(char _format); void setMultiplier(const double _multiplier); void setPrecision(const int _precision); @@ -67,6 +70,7 @@ private: // properties int m_count = 0; QChar m_fillChar = QChar(); + bool m_forceWidth = false; char m_format = 'f'; double m_multiplier = 1.0; int m_precision = -1; diff --git a/sources/awesomewidgets/awfloatformatter.ui b/sources/awesomewidgets/awfloatformatter.ui index 8d6a1e6..e4b7e68 100644 --- a/sources/awesomewidgets/awfloatformatter.ui +++ b/sources/awesomewidgets/awfloatformatter.ui @@ -7,7 +7,7 @@ 0 0 420 - 315 + 362 @@ -199,6 +199,36 @@ + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + Force width + + + + + diff --git a/sources/awesomewidgets/awstringformatter.cpp b/sources/awesomewidgets/awstringformatter.cpp new file mode 100644 index 0000000..acff42c --- /dev/null +++ b/sources/awesomewidgets/awstringformatter.cpp @@ -0,0 +1,192 @@ +/*************************************************************************** + * 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 "awstringformatter.h" +#include "ui_awstringformatter.h" + +#include + +#include +#include + +#include "awdebug.h" + + +AWStringFormatter::AWStringFormatter(QWidget *parent, const QString filePath) + : AWAbstractFormatter(parent, filePath) + , ui(new Ui::AWStringFormatter) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + if (!filePath.isEmpty()) + readConfiguration(); + ui->setupUi(this); + translate(); +} + + +AWStringFormatter::~AWStringFormatter() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + delete ui; +} + + +QString AWStringFormatter::convert(const QVariant &_value) const +{ + qCDebug(LOG_LIB) << "Convert value" << _value; + + QString output = QString("%1").arg(_value.toString(), count(), fillChar()); + if (forceWidth()) + output = output.left(count()); + + return output; +} + + +AWStringFormatter *AWStringFormatter::copy(const QString _fileName, + const int _number) +{ + qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number; + + AWStringFormatter *item + = new AWStringFormatter(static_cast(parent()), _fileName); + AWAbstractFormatter::copyDefaults(item); + item->setCount(count()); + item->setFillChar(fillChar()); + item->setForceWidth(forceWidth()); + item->setNumber(_number); + + return item; +} + + +int AWStringFormatter::count() const +{ + return m_count; +} + + +QChar AWStringFormatter::fillChar() const +{ + return m_fillChar; +} + + +bool AWStringFormatter::forceWidth() const +{ + return m_forceWidth; +} + + +void AWStringFormatter::setCount(const int _count) +{ + qCDebug(LOG_LIB) << "Set width" << _count; + + m_count = _count; +} + + +void AWStringFormatter::setFillChar(const QChar _fillChar) +{ + qCDebug(LOG_LIB) << "Set char" << _fillChar; + + m_fillChar = _fillChar; +} + + +void AWStringFormatter::setForceWidth(const bool _forceWidth) +{ + qCDebug(LOG_LIB) << "Set force strip" << _forceWidth; + + m_forceWidth = _forceWidth; +} + + +void AWStringFormatter::readConfiguration() +{ + AWAbstractFormatter::readConfiguration(); + + QSettings settings(fileName(), QSettings::IniFormat); + + settings.beginGroup(QString("Desktop Entry")); + setCount(settings.value(QString("X-AW-Width"), count()).toInt()); + setFillChar( + settings.value(QString("X-AW-FillChar"), fillChar()).toString().at(0)); + setForceWidth( + settings.value(QString("X-AW-ForceWidth"), forceWidth()).toBool()); + settings.endGroup(); + + bumpApi(AWEFAPI); +} + + +int AWStringFormatter::showConfiguration(const QVariant args) +{ + Q_UNUSED(args) + + ui->lineEdit_name->setText(name()); + ui->lineEdit_comment->setText(comment()); + ui->label_typeValue->setText(QString("String")); + ui->spinBox_width->setValue(count()); + ui->lineEdit_fill->setText(QString(fillChar())); + ui->checkBox_forceWidth->setCheckState(forceWidth() ? Qt::Checked + : Qt::Unchecked); + + int ret = exec(); + if (ret != 1) + return ret; + setName(ui->lineEdit_name->text()); + setComment(ui->lineEdit_comment->text()); + setStrType(ui->label_typeValue->text()); + setCount(ui->spinBox_width->value()); + setFillChar(ui->lineEdit_fill->text().at(0)); + setForceWidth(ui->checkBox_forceWidth->checkState() == Qt::Checked); + + writeConfiguration(); + return ret; +} + + +void AWStringFormatter::writeConfiguration() const +{ + AWAbstractFormatter::writeConfiguration(); + + QSettings settings(writtableConfig(), QSettings::IniFormat); + qCInfo(LOG_LIB) << "Configuration file" << settings.fileName(); + + settings.beginGroup(QString("Desktop Entry")); + settings.setValue(QString("X-AW-Width"), count()); + settings.setValue(QString("X-AW-FillChar"), fillChar()); + settings.setValue(QString("X-AW-ForceWidth"), forceWidth()); + settings.endGroup(); + + settings.sync(); +} + + +void AWStringFormatter::translate() +{ + ui->label_name->setText(i18n("Name")); + ui->label_comment->setText(i18n("Comment")); + ui->label_type->setText(i18n("Type")); + ui->label_width->setText(i18n("Width")); + ui->label_fill->setText(i18n("Fill char")); + ui->checkBox_forceWidth->setText(i18n("Force width")); +} diff --git a/sources/awesomewidgets/awstringformatter.h b/sources/awesomewidgets/awstringformatter.h new file mode 100644 index 0000000..aff823a --- /dev/null +++ b/sources/awesomewidgets/awstringformatter.h @@ -0,0 +1,65 @@ +/*************************************************************************** + * 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 AWSTRINGFORMATTER_H +#define AWSTRINGFORMATTER_H + +#include "awabstractformatter.h" + + +namespace Ui +{ +class AWStringFormatter; +} + +class AWStringFormatter : public AWAbstractFormatter +{ + Q_OBJECT + Q_PROPERTY(int count READ count WRITE setCount) + Q_PROPERTY(QChar fillChar READ fillChar WRITE setFillChar) + Q_PROPERTY(bool forceWidth READ forceWidth WRITE setForceWidth) + +public: + explicit AWStringFormatter(QWidget *parent, + const QString filePath = QString()); + virtual ~AWStringFormatter(); + QString convert(const QVariant &_value) const; + AWStringFormatter *copy(const QString _fileName, const int _number); + // properties + int count() const; + QChar fillChar() const; + bool forceWidth() const; + void setCount(const int _count); + void setFillChar(const QChar _fillChar); + void setForceWidth(const bool _forceWidth); + +public slots: + void readConfiguration(); + int showConfiguration(const QVariant args = QVariant()); + void writeConfiguration() const; + +private: + Ui::AWStringFormatter *ui = nullptr; + void translate(); + // properties + int m_count = 0; + QChar m_fillChar = QChar(); + bool m_forceWidth = false; +}; + + +#endif /* AWSTRINGFORMATTER_H */ diff --git a/sources/awesomewidgets/awstringformatter.ui b/sources/awesomewidgets/awstringformatter.ui new file mode 100644 index 0000000..3ae5420 --- /dev/null +++ b/sources/awesomewidgets/awstringformatter.ui @@ -0,0 +1,225 @@ + + + AWStringFormatter + + + + 0 + 0 + 420 + 225 + + + + Configuration + + + + + + + + + 0 + 0 + + + + Name + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + 0 + 0 + + + + Comment + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + Type + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + + + + Width + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + -10000 + + + 10000 + + + + + + + + + + + + 0 + 0 + + + + Fill char + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 1 + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + Force width + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + AWStringFormatter + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + AWStringFormatter + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index ed1dd2c..c2821a5 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -27,7 +27,7 @@ set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${PROJECT_MONITORSO ## modules set(TEST_MODULES abstractextitem extquotes extscript extupgrade extweather - abstractformatter datetimeformatter floatformatter listformatter noformatter scriptformatter + abstractformatter datetimeformatter floatformatter listformatter noformatter scriptformatter stringformatter extitemaggregator batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource awconfighelper awkeycache awkeys awpatternfunctions awupdatehelper diff --git a/sources/test/testfloatformatter.cpp b/sources/test/testfloatformatter.cpp index 46ef23e..dbe75c7 100644 --- a/sources/test/testfloatformatter.cpp +++ b/sources/test/testfloatformatter.cpp @@ -49,8 +49,7 @@ void TestAWFloatFormatter::test_count() QCOMPARE(formatter->count(), count); // test - double value = AWTestLibrary::randomDouble(); - QString output = formatter->convert(value); + QString output = formatter->convert(AWTestLibrary::randomDouble()); QCOMPARE(output.count(), count); // reset @@ -67,8 +66,7 @@ void TestAWFloatFormatter::test_fillChar() formatter->setCount(101); // test - int value = AWTestLibrary::randomInt(); - QString output = formatter->convert(value); + QString output = formatter->convert(AWTestLibrary::randomInt()); QVERIFY(output.startsWith(QChar(c))); // reset @@ -77,6 +75,24 @@ void TestAWFloatFormatter::test_fillChar() } +void TestAWFloatFormatter::test_forceWidth() +{ + // assign + int count = AWTestLibrary::randomInt(6); + formatter->setForceWidth(true); + formatter->setCount(count); + QCOMPARE(formatter->forceWidth(), true); + + // test + QString output = formatter->convert(AWTestLibrary::randomDouble()); + QCOMPARE(output.count(), count); + + // reset + formatter->setForceWidth(false); + formatter->setCount(0); +} + + void TestAWFloatFormatter::test_format() { // assign @@ -87,8 +103,7 @@ void TestAWFloatFormatter::test_format() QCOMPARE(formatter->format(), 'e'); // test - double value = AWTestLibrary::randomDouble(); - QString output = formatter->convert(value); + QString output = formatter->convert(AWTestLibrary::randomDouble()); QVERIFY(output.contains('e')); // reset @@ -104,8 +119,7 @@ void TestAWFloatFormatter::test_precision() QCOMPARE(formatter->precision(), precision); // test - double value = AWTestLibrary::randomDouble(); - QString output = formatter->convert(value); + QString output = formatter->convert(AWTestLibrary::randomDouble()); output.remove(QString("0.")); QCOMPARE(output.count(), precision); @@ -157,6 +171,7 @@ void TestAWFloatFormatter::test_copy() QCOMPARE(newFormatter->count(), formatter->count()); QCOMPARE(newFormatter->fillChar(), formatter->fillChar()); + QCOMPARE(newFormatter->forceWidth(), formatter->forceWidth()); QCOMPARE(newFormatter->format(), formatter->format()); QCOMPARE(newFormatter->multiplier(), formatter->multiplier()); QCOMPARE(newFormatter->precision(), formatter->precision()); @@ -171,6 +186,7 @@ void TestAWFloatFormatter::doRandom() { formatter->setCount(AWTestLibrary::randomInt()); formatter->setFillChar(QChar(AWTestLibrary::randomChar())); + formatter->setForceWidth(AWTestLibrary::randomInt() % 2); formatter->setFormat(AWTestLibrary::randomChar()); formatter->setMultiplier(AWTestLibrary::randomDouble()); formatter->setPrecision(AWTestLibrary::randomInt()); diff --git a/sources/test/testfloatformatter.h b/sources/test/testfloatformatter.h index 77746c9..bdb0e0d 100644 --- a/sources/test/testfloatformatter.h +++ b/sources/test/testfloatformatter.h @@ -36,6 +36,7 @@ private slots: void test_values(); void test_count(); void test_fillChar(); + void test_forceWidth(); void test_format(); void test_precision(); void test_multiplier(); diff --git a/sources/test/teststringformatter.cpp b/sources/test/teststringformatter.cpp new file mode 100644 index 0000000..a7f98b6 --- /dev/null +++ b/sources/test/teststringformatter.cpp @@ -0,0 +1,118 @@ +/*************************************************************************** + * 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 "teststringformatter.h" + +#include + +#include "awstringformatter.h" +#include "awtestlibrary.h" + + +void TestAWStringFormatter::initTestCase() +{ + formatter = new AWStringFormatter(nullptr); +} + + +void TestAWStringFormatter::cleanupTestCase() +{ + delete formatter; +} + + +void TestAWStringFormatter::test_values() +{ +} + + +void TestAWStringFormatter::test_count() +{ + // assign + int count = 10 + AWTestLibrary::randomInt(); + formatter->setCount(count); + QCOMPARE(formatter->count(), count); + + // test + QString output = formatter->convert(AWTestLibrary::randomString()); + QCOMPARE(output.count(), count); + + // reset + formatter->setCount(0); +} + + +void TestAWStringFormatter::test_fillChar() +{ + // assign + char c = AWTestLibrary::randomChar(); + formatter->setFillChar(QChar(c)); + QCOMPARE(formatter->fillChar(), QChar(c)); + formatter->setCount(101); + + // test + QString output = formatter->convert(AWTestLibrary::randomString()); + QVERIFY(output.startsWith(QChar(c))); + + // reset + formatter->setFillChar(QChar()); + formatter->setCount(0); +} + + +void TestAWStringFormatter::test_forceWidth() +{ + // assign + int count = AWTestLibrary::randomInt(); + formatter->setForceWidth(true); + formatter->setCount(count); + QCOMPARE(formatter->forceWidth(), true); + + // test + QString output = formatter->convert(AWTestLibrary::randomString()); + QCOMPARE(output.count(), count); + + // reset + formatter->setForceWidth(false); + formatter->setCount(0); +} + + +void TestAWStringFormatter::test_copy() +{ + doRandom(); + AWStringFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1); + + QCOMPARE(newFormatter->count(), formatter->count()); + QCOMPARE(newFormatter->fillChar(), formatter->fillChar()); + QCOMPARE(newFormatter->forceWidth(), formatter->forceWidth()); + QCOMPARE(newFormatter->number(), 1); + + delete newFormatter; +} + + +void TestAWStringFormatter::doRandom() +{ + formatter->setCount(AWTestLibrary::randomInt()); + formatter->setFillChar(QChar(AWTestLibrary::randomChar())); + formatter->setForceWidth(AWTestLibrary::randomInt() % 2); +} + + +QTEST_MAIN(TestAWStringFormatter); diff --git a/sources/test/teststringformatter.h b/sources/test/teststringformatter.h new file mode 100644 index 0000000..6ebe95d --- /dev/null +++ b/sources/test/teststringformatter.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * 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 TESTSTRINGFORMATTER_H +#define TESTSTRINGFORMATTER_H + +#include + + +class AWStringFormatter; + +class TestAWStringFormatter : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_values(); + void test_count(); + void test_fillChar(); + void test_forceWidth(); + void test_copy(); + +private: + void doRandom(); + AWStringFormatter *formatter = nullptr; +}; + + +#endif /* TESTSTRINGFORMATTER_H */