From 69c09d9ff8b121900a4017c8294beddd4e6458e4 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Mon, 30 May 2016 01:47:06 +0300 Subject: [PATCH] more tests for formatters --- sources/awesomewidgets/awscriptformatter.cpp | 7 +- sources/test/CMakeLists.txt | 56 +++------- sources/test/awtestlibrary.cpp | 16 +++ sources/test/awtestlibrary.h | 11 +- sources/test/testabstractextitem.h | 1 - sources/test/testdatetimeformatter.cpp | 68 ++++++++++++ sources/test/testdatetimeformatter.h | 46 ++++++++ sources/test/testextscript.h | 1 - sources/test/testextupgrade.cpp | 11 +- sources/test/testextupgrade.h | 1 - sources/test/testextweather.h | 1 - sources/test/testfloatformatter.cpp | 2 +- sources/test/testfloatformatter.h | 1 - sources/test/testnoformatter.cpp | 2 +- sources/test/testnoformatter.h | 1 - sources/test/testscriptformatter.cpp | 107 +++++++++++++++++++ sources/test/testscriptformatter.h | 50 +++++++++ 17 files changed, 313 insertions(+), 69 deletions(-) create mode 100644 sources/test/testdatetimeformatter.cpp create mode 100644 sources/test/testdatetimeformatter.h create mode 100644 sources/test/testscriptformatter.cpp create mode 100644 sources/test/testscriptformatter.h diff --git a/sources/awesomewidgets/awscriptformatter.cpp b/sources/awesomewidgets/awscriptformatter.cpp index 89c404d..3bc23cd 100644 --- a/sources/awesomewidgets/awscriptformatter.cpp +++ b/sources/awesomewidgets/awscriptformatter.cpp @@ -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 })") diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index 0ed3817..fd94701 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -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) diff --git a/sources/test/awtestlibrary.cpp b/sources/test/awtestlibrary.cpp index f1eb1db..6883972 100644 --- a/sources/test/awtestlibrary.cpp +++ b/sources/test/awtestlibrary.cpp @@ -18,6 +18,8 @@ #include "awtestlibrary.h" +#include + char AWTestLibrary::randomChar() { @@ -59,3 +61,17 @@ QStringList AWTestLibrary::randomStringList(const int max) return output; } + + +QStringList AWTestLibrary::randomSelect(const QStringList available) +{ + QSet 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(); +} diff --git a/sources/test/awtestlibrary.h b/sources/test/awtestlibrary.h index a4d1096..355d8a0 100644 --- a/sources/test/awtestlibrary.h +++ b/sources/test/awtestlibrary.h @@ -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); }; diff --git a/sources/test/testabstractextitem.h b/sources/test/testabstractextitem.h index 4d672ed..805fe5a 100644 --- a/sources/test/testabstractextitem.h +++ b/sources/test/testabstractextitem.h @@ -20,7 +20,6 @@ #define TESTABSTRACTEXTITEM_H #include -#include class ExtUpgrade; diff --git a/sources/test/testdatetimeformatter.cpp b/sources/test/testdatetimeformatter.cpp new file mode 100644 index 0000000..c785338 --- /dev/null +++ b/sources/test/testdatetimeformatter.cpp @@ -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 + +#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); diff --git a/sources/test/testdatetimeformatter.h b/sources/test/testdatetimeformatter.h new file mode 100644 index 0000000..6386458 --- /dev/null +++ b/sources/test/testdatetimeformatter.h @@ -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 + + +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 */ diff --git a/sources/test/testextscript.h b/sources/test/testextscript.h index 01bed73..4c1d195 100644 --- a/sources/test/testextscript.h +++ b/sources/test/testextscript.h @@ -20,7 +20,6 @@ #define TESTEXTSCRIPT_H #include -#include class ExtScript; diff --git a/sources/test/testextupgrade.cpp b/sources/test/testextupgrade.cpp index 9b149a0..64cbc10 100644 --- a/sources/test/testextupgrade.cpp +++ b/sources/test/testextupgrade.cpp @@ -86,15 +86,8 @@ void TestExtUpgrade::test_null() void TestExtUpgrade::test_filter() { - QSet 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(); diff --git a/sources/test/testextupgrade.h b/sources/test/testextupgrade.h index 99f8a0b..7db8bcb 100644 --- a/sources/test/testextupgrade.h +++ b/sources/test/testextupgrade.h @@ -20,7 +20,6 @@ #define TESTEXTUPGRADE_H #include -#include class ExtUpgrade; diff --git a/sources/test/testextweather.h b/sources/test/testextweather.h index 1b1638a..8e28e05 100644 --- a/sources/test/testextweather.h +++ b/sources/test/testextweather.h @@ -20,7 +20,6 @@ #define TESTEXTWEATHER_H #include -#include class ExtWeather; diff --git a/sources/test/testfloatformatter.cpp b/sources/test/testfloatformatter.cpp index 711e6c6..46ef23e 100644 --- a/sources/test/testfloatformatter.cpp +++ b/sources/test/testfloatformatter.cpp @@ -20,8 +20,8 @@ #include -#include "awtestlibrary.h" #include "awfloatformatter.h" +#include "awtestlibrary.h" void TestAWFloatFormatter::initTestCase() diff --git a/sources/test/testfloatformatter.h b/sources/test/testfloatformatter.h index 8ee99f7..77746c9 100644 --- a/sources/test/testfloatformatter.h +++ b/sources/test/testfloatformatter.h @@ -20,7 +20,6 @@ #define TESTFLOATFORMATTER_H #include -#include class AWFloatFormatter; diff --git a/sources/test/testnoformatter.cpp b/sources/test/testnoformatter.cpp index dc5a937..a1ed6ce 100644 --- a/sources/test/testnoformatter.cpp +++ b/sources/test/testnoformatter.cpp @@ -20,8 +20,8 @@ #include -#include "awtestlibrary.h" #include "awnoformatter.h" +#include "awtestlibrary.h" void TestAWNoFormatter::initTestCase() diff --git a/sources/test/testnoformatter.h b/sources/test/testnoformatter.h index 1b43309..b9cf531 100644 --- a/sources/test/testnoformatter.h +++ b/sources/test/testnoformatter.h @@ -20,7 +20,6 @@ #define TESTNOFORMATTER_H #include -#include class AWNoFormatter; diff --git a/sources/test/testscriptformatter.cpp b/sources/test/testscriptformatter.cpp new file mode 100644 index 0000000..d2e0adc --- /dev/null +++ b/sources/test/testscriptformatter.cpp @@ -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 + +#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); diff --git a/sources/test/testscriptformatter.h b/sources/test/testscriptformatter.h new file mode 100644 index 0000000..ed81823 --- /dev/null +++ b/sources/test/testscriptformatter.h @@ -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 + + +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 */