mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 15:37:23 +00:00
more tests for formatters
This commit is contained in:
parent
cae9e0d2e3
commit
69c09d9ff8
@ -116,6 +116,7 @@ void AWScriptFormatter::setAppendCode(const bool _appendCode)
|
||||
qCDebug(LOG_LIB) << "Set append code" << _appendCode;
|
||||
|
||||
m_appendCode = _appendCode;
|
||||
initProgram();
|
||||
}
|
||||
|
||||
|
||||
@ -124,6 +125,7 @@ void AWScriptFormatter::setCode(const QString _code)
|
||||
qCDebug(LOG_LIB) << "Set code" << _code;
|
||||
|
||||
m_code = _code;
|
||||
initProgram();
|
||||
}
|
||||
|
||||
|
||||
@ -132,6 +134,7 @@ void AWScriptFormatter::setHasReturn(const bool _hasReturn)
|
||||
qCDebug(LOG_LIB) << "Set has return" << _hasReturn;
|
||||
|
||||
m_hasReturn = _hasReturn;
|
||||
initProgram();
|
||||
}
|
||||
|
||||
|
||||
@ -150,9 +153,6 @@ void AWScriptFormatter::readConfiguration()
|
||||
settings.endGroup();
|
||||
|
||||
bumpApi(AWEFAPI);
|
||||
|
||||
// init JS code
|
||||
initProgram();
|
||||
}
|
||||
|
||||
|
||||
@ -204,6 +204,7 @@ void AWScriptFormatter::writeConfiguration() const
|
||||
|
||||
void AWScriptFormatter::initProgram()
|
||||
{
|
||||
// init JS code
|
||||
if (m_appendCode)
|
||||
m_program
|
||||
= QString("(function(value) { %1%2 })")
|
||||
|
@ -17,47 +17,15 @@ set(AWTESTLIBRARY_SOURCES awtestlibrary.cpp)
|
||||
add_library(${SUBPROJECT}-awtest STATIC ${AWTESTLIBRARY_SOURCES} ${AWTESTLIBRARY_HEADERS})
|
||||
target_link_libraries(${SUBPROJECT}-awtest ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES})
|
||||
set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES})
|
||||
## extensions
|
||||
# abstractextitem
|
||||
set(ABSTRACTEXTITEM_HEADERS testabstractextitem.h)
|
||||
set(ABSTRACTEXTITEM_SOURCES testabstractextitem.cpp)
|
||||
add_executable(${SUBPROJECT}-abstractextitem ${ABSTRACTEXTITEM_HEADERS} ${ABSTRACTEXTITEM_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-abstractextitem ${LIBRARY_TEST_SET})
|
||||
add_test(NAME "AbstractExtItem" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-abstractextitem)
|
||||
# extquotes
|
||||
set(EXTQUOTES_HEADERS testextquotes.h)
|
||||
set(EXTQUOTES_SOURCES testextquotes.cpp)
|
||||
add_executable(${SUBPROJECT}-extquotes ${EXTQUOTES_HEADERS} ${EXTQUOTES_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-extquotes ${LIBRARY_TEST_SET})
|
||||
add_test(NAME "ExtQuotes" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extquotes)
|
||||
# extscript
|
||||
set(EXTSCRIPT_HEADERS testextscript.h)
|
||||
set(EXTSCRIPT_SOURCES testextscript.cpp)
|
||||
add_executable(${SUBPROJECT}-extscript ${EXTSCRIPT_HEADERS} ${EXTSCRIPT_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-extscript ${LIBRARY_TEST_SET})
|
||||
add_test(NAME "ExtScript" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extscript)
|
||||
# extupgrade
|
||||
set(EXTUPGRADE_HEADERS testextupgrade.h)
|
||||
set(EXTUPGRADE_SOURCES testextupgrade.cpp)
|
||||
add_executable(${SUBPROJECT}-extupgrade ${EXTUPGRADE_HEADERS} ${EXTUPGRADE_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-extupgrade ${LIBRARY_TEST_SET})
|
||||
add_test(NAME "ExtUpgrade" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extupgrade)
|
||||
# extweather
|
||||
set(EXTWEATHER_HEADERS testextweather.h)
|
||||
set(EXTWEATHER_SOURCES testextweather.cpp)
|
||||
add_executable(${SUBPROJECT}-extweather ${EXTWEATHER_HEADERS} ${EXTWEATHER_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-extweather ${LIBRARY_TEST_SET})
|
||||
add_test(NAME "ExtWeather" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extweather)
|
||||
## formatters
|
||||
# float formatter
|
||||
set(FLOATFORMATTER_HEADERS testfloatformatter.h)
|
||||
set(FLOATFORMATTER_SOURCES testfloatformatter.cpp)
|
||||
add_executable(${SUBPROJECT}-floatformatter ${FLOATFORMATTER_HEADERS} ${FLOATFORMATTER_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-floatformatter ${LIBRARY_TEST_SET})
|
||||
add_test(NAME "Float4Formatter" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-floatformatter)
|
||||
# no formatter
|
||||
set(NOFORMATTER_HEADERS testnoformatter.h)
|
||||
set(NOFORMATTER_SOURCES testnoformatter.cpp)
|
||||
add_executable(${SUBPROJECT}-noformatter ${NOFORMATTER_HEADERS} ${NOFORMATTER_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-noformatter ${LIBRARY_TEST_SET})
|
||||
add_test(NAME "NoFormatter" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-noformatter)
|
||||
|
||||
## modules
|
||||
set(TEST_MODULES
|
||||
abstractextitem extquotes extscript extupgrade extweather
|
||||
datetimeformatter floatformatter noformatter scriptformatter)
|
||||
foreach (TEST_MODULE ${TEST_MODULES})
|
||||
set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h)
|
||||
set(${TEST_MODULE}_SOURCES test${TEST_MODULE}.cpp)
|
||||
add_executable(${SUBPROJECT}-${TEST_MODULE} ${${TEST_MODULE}_HEADERS} ${${TEST_MODULE}_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-${TEST_MODULE} ${LIBRARY_TEST_SET})
|
||||
add_test(NAME ${TEST_MODULE} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-${TEST_MODULE})
|
||||
endforeach (TEST_MODULE)
|
||||
|
@ -18,6 +18,8 @@
|
||||
|
||||
#include "awtestlibrary.h"
|
||||
|
||||
#include <QSet>
|
||||
|
||||
|
||||
char AWTestLibrary::randomChar()
|
||||
{
|
||||
@ -59,3 +61,17 @@ QStringList AWTestLibrary::randomStringList(const int max)
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
QStringList AWTestLibrary::randomSelect(const QStringList available)
|
||||
{
|
||||
QSet<QString> output;
|
||||
|
||||
int count = 1 + randomInt(available.count());
|
||||
for (int i = 0; i < count; i++) {
|
||||
int index = randomInt(available.count());
|
||||
output << available.at(index);
|
||||
}
|
||||
|
||||
return output.toList();
|
||||
}
|
||||
|
@ -24,11 +24,12 @@
|
||||
|
||||
namespace AWTestLibrary
|
||||
{
|
||||
char randomChar();
|
||||
double randomDouble();
|
||||
int randomInt(const int max = 100);
|
||||
QString randomString(const int max = 100);
|
||||
QStringList randomStringList(const int max = 100);
|
||||
char randomChar();
|
||||
double randomDouble();
|
||||
int randomInt(const int max = 100);
|
||||
QString randomString(const int max = 100);
|
||||
QStringList randomStringList(const int max = 100);
|
||||
QStringList randomSelect(const QStringList available);
|
||||
};
|
||||
|
||||
|
||||
|
@ -20,7 +20,6 @@
|
||||
#define TESTABSTRACTEXTITEM_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class ExtUpgrade;
|
||||
|
68
sources/test/testdatetimeformatter.cpp
Normal file
68
sources/test/testdatetimeformatter.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
/***************************************************************************
|
||||
* 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 "testdatetimeformatter.h"
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include "awdatetimeformatter.h"
|
||||
#include "awtestlibrary.h"
|
||||
#include "version.h"
|
||||
|
||||
|
||||
void TestAWDateTimeFormatter::initTestCase()
|
||||
{
|
||||
format = AWTestLibrary::randomSelect(QString(TIME_KEYS).split(QChar(','))).join(QChar(' '));
|
||||
|
||||
formatter = new AWDateTimeFormatter(nullptr);
|
||||
formatter->setFormat(format);
|
||||
}
|
||||
|
||||
|
||||
void TestAWDateTimeFormatter::cleanupTestCase()
|
||||
{
|
||||
delete formatter;
|
||||
}
|
||||
|
||||
|
||||
void TestAWDateTimeFormatter::test_values()
|
||||
{
|
||||
QCOMPARE(formatter->format(), format);
|
||||
}
|
||||
|
||||
|
||||
void TestAWDateTimeFormatter::test_conversion()
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
QCOMPARE(formatter->convert(now), now.toString(format));
|
||||
}
|
||||
|
||||
|
||||
void TestAWDateTimeFormatter::test_copy()
|
||||
{
|
||||
AWDateTimeFormatter *newFormatter
|
||||
= formatter->copy(QString("/dev/null"), 1);
|
||||
|
||||
QCOMPARE(newFormatter->format(), formatter->format());
|
||||
QCOMPARE(newFormatter->number(), 1);
|
||||
|
||||
delete newFormatter;
|
||||
}
|
||||
|
||||
|
||||
QTEST_MAIN(TestAWDateTimeFormatter);
|
46
sources/test/testdatetimeformatter.h
Normal file
46
sources/test/testdatetimeformatter.h
Normal file
@ -0,0 +1,46 @@
|
||||
/***************************************************************************
|
||||
* 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 TESTDATETIMEFORMATTER_H
|
||||
#define TESTDATETIMEFORMATTER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
|
||||
class AWDateTimeFormatter;
|
||||
|
||||
class TestAWDateTimeFormatter : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
// initialization
|
||||
void initTestCase();
|
||||
void cleanupTestCase();
|
||||
// test
|
||||
void test_values();
|
||||
void test_conversion();
|
||||
void test_copy();
|
||||
|
||||
private:
|
||||
AWDateTimeFormatter *formatter = nullptr;
|
||||
QString format;
|
||||
};
|
||||
|
||||
|
||||
#endif /* TESTDATETIMEFORMATTER_H */
|
@ -20,7 +20,6 @@
|
||||
#define TESTEXTSCRIPT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class ExtScript;
|
||||
|
@ -86,15 +86,8 @@ void TestExtUpgrade::test_null()
|
||||
|
||||
void TestExtUpgrade::test_filter()
|
||||
{
|
||||
QSet<QString> filters;
|
||||
int count = AWTestLibrary::randomInt(randomStrings.count());
|
||||
for (int i = 0; i < count; i++) {
|
||||
int index = AWTestLibrary::randomInt(randomStrings.count());
|
||||
filters << randomStrings.at(index);
|
||||
}
|
||||
|
||||
extUpgrade->setFilter(
|
||||
QString("(^%1$)").arg(filters.toList().join(QString("$|^"))));
|
||||
QStringList filters = AWTestLibrary::randomSelect(randomStrings);
|
||||
extUpgrade->setFilter(QString("(^%1$)").arg(filters.join(QString("$|^"))));
|
||||
// init spy
|
||||
QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &)));
|
||||
extUpgrade->run();
|
||||
|
@ -20,7 +20,6 @@
|
||||
#define TESTEXTUPGRADE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class ExtUpgrade;
|
||||
|
@ -20,7 +20,6 @@
|
||||
#define TESTEXTWEATHER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class ExtWeather;
|
||||
|
@ -20,8 +20,8 @@
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include "awtestlibrary.h"
|
||||
#include "awfloatformatter.h"
|
||||
#include "awtestlibrary.h"
|
||||
|
||||
|
||||
void TestAWFloatFormatter::initTestCase()
|
||||
|
@ -20,7 +20,6 @@
|
||||
#define TESTFLOATFORMATTER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class AWFloatFormatter;
|
||||
|
@ -20,8 +20,8 @@
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include "awtestlibrary.h"
|
||||
#include "awnoformatter.h"
|
||||
#include "awtestlibrary.h"
|
||||
|
||||
|
||||
void TestAWNoFormatter::initTestCase()
|
||||
|
@ -20,7 +20,6 @@
|
||||
#define TESTNOFORMATTER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class AWNoFormatter;
|
||||
|
107
sources/test/testscriptformatter.cpp
Normal file
107
sources/test/testscriptformatter.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
/***************************************************************************
|
||||
* 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 "testscriptformatter.h"
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include "awscriptformatter.h"
|
||||
#include "awtestlibrary.h"
|
||||
|
||||
|
||||
void TestAWScriptFormatter::initTestCase()
|
||||
{
|
||||
formatter = new AWScriptFormatter(nullptr);
|
||||
formatter->setCode(fullCode);
|
||||
formatter->setAppendCode(false);
|
||||
formatter->setHasReturn(true);
|
||||
}
|
||||
|
||||
|
||||
void TestAWScriptFormatter::cleanupTestCase()
|
||||
{
|
||||
delete formatter;
|
||||
}
|
||||
|
||||
|
||||
void TestAWScriptFormatter::test_values()
|
||||
{
|
||||
QCOMPARE(formatter->code(), fullCode);
|
||||
QCOMPARE(formatter->appendCode(), false);
|
||||
QCOMPARE(formatter->hasReturn(), true);
|
||||
|
||||
QCOMPARE(formatter->program(), fullCode);
|
||||
}
|
||||
|
||||
|
||||
void TestAWScriptFormatter::test_conversion()
|
||||
{
|
||||
QString string = AWTestLibrary::randomString();
|
||||
QCOMPARE(formatter->convert(string), string);
|
||||
}
|
||||
|
||||
|
||||
void TestAWScriptFormatter::test_appendCode()
|
||||
{
|
||||
// set
|
||||
formatter->setAppendCode(true);
|
||||
QCOMPARE(formatter->appendCode(), true);
|
||||
formatter->setCode(codeWithReturn);
|
||||
|
||||
// test
|
||||
QCOMPARE(formatter->program(), fullCode);
|
||||
test_conversion();
|
||||
|
||||
// reset
|
||||
formatter->setAppendCode(false);
|
||||
}
|
||||
|
||||
|
||||
void TestAWScriptFormatter::test_hasReturn()
|
||||
{
|
||||
// set
|
||||
formatter->setHasReturn(false);
|
||||
QCOMPARE(formatter->hasReturn(), false);
|
||||
formatter->setCode(code);
|
||||
|
||||
// test 1
|
||||
QEXPECT_FAIL("", "Should fail because appendCode set to false", Continue);
|
||||
QCOMPARE(formatter->program(), fullCode);
|
||||
|
||||
// test 2
|
||||
formatter->setAppendCode(true);
|
||||
QCOMPARE(formatter->program(), fullCode);
|
||||
test_conversion();
|
||||
}
|
||||
|
||||
|
||||
void TestAWScriptFormatter::test_copy()
|
||||
{
|
||||
AWScriptFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1);
|
||||
|
||||
QCOMPARE(newFormatter->appendCode(), formatter->appendCode());
|
||||
QCOMPARE(newFormatter->code(), formatter->code());
|
||||
QCOMPARE(newFormatter->hasReturn(), formatter->hasReturn());
|
||||
QCOMPARE(newFormatter->program(), formatter->program());
|
||||
QCOMPARE(newFormatter->number(), 1);
|
||||
|
||||
delete newFormatter;
|
||||
}
|
||||
|
||||
|
||||
QTEST_MAIN(TestAWScriptFormatter);
|
50
sources/test/testscriptformatter.h
Normal file
50
sources/test/testscriptformatter.h
Normal file
@ -0,0 +1,50 @@
|
||||
/***************************************************************************
|
||||
* 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 TESTSCRIPTFORMATTER_H
|
||||
#define TESTSCRIPTFORMATTER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
|
||||
class AWScriptFormatter;
|
||||
|
||||
class TestAWScriptFormatter : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
// initialization
|
||||
void initTestCase();
|
||||
void cleanupTestCase();
|
||||
// test
|
||||
void test_values();
|
||||
void test_conversion();
|
||||
void test_appendCode();
|
||||
void test_hasReturn();
|
||||
void test_copy();
|
||||
|
||||
private:
|
||||
AWScriptFormatter *formatter = nullptr;
|
||||
QString code = QString("output = value");
|
||||
QString codeWithReturn = QString("%1; return output;").arg(code);
|
||||
QString fullCode = QString("(function(value) { %1 })").arg(codeWithReturn);
|
||||
};
|
||||
|
||||
|
||||
#endif /* TESTNOFORMATTER_H */
|
Loading…
Reference in New Issue
Block a user