From b73fb194097ae50201a1f6e31906c901188b190b Mon Sep 17 00:00:00 2001 From: arcan1s Date: Wed, 25 May 2016 12:35:34 +0300 Subject: [PATCH] add tests for extupgrade --- sources/awesomewidgets/extupgrade.cpp | 2 + sources/libraries.cmake | 2 +- sources/test/CMakeLists.txt | 19 ++-- sources/test/testextscript.cpp | 29 +++-- sources/test/testextscript.h | 3 +- sources/test/testextupgrade.cpp | 148 ++++++++++++++++++++++++++ sources/test/testextupgrade.h | 52 +++++++++ 7 files changed, 236 insertions(+), 19 deletions(-) create mode 100644 sources/test/testextupgrade.cpp create mode 100644 sources/test/testextupgrade.h diff --git a/sources/awesomewidgets/extupgrade.cpp b/sources/awesomewidgets/extupgrade.cpp index ed54fc5..951d4d1 100644 --- a/sources/awesomewidgets/extupgrade.cpp +++ b/sources/awesomewidgets/extupgrade.cpp @@ -225,6 +225,8 @@ void ExtUpgrade::updateValue() .filter(QRegExp(m_filter)) .count(); }(qoutput); + + emit(dataReceived(value)); } diff --git a/sources/libraries.cmake b/sources/libraries.cmake index 9a9a052..0c29760 100644 --- a/sources/libraries.cmake +++ b/sources/libraries.cmake @@ -13,7 +13,7 @@ set(Qt_INCLUDE ) set(Qt_LIBRARIES ${Qt5Core_LIBRARIES} ${Qt5DBus_LIBRARIES} ${Qt5Network_LIBRARIES} - ${Qt5Qml_LIBRARIES} ${Qt5Test_LIBRARIES} ${Qt5Widgets_LIBRARIES} + ${Qt5Qml_LIBRARIES} ${Qt5Widgets_LIBRARIES} ) # kf5 libraries diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index e70c070..27f96e1 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -9,15 +9,18 @@ include_directories( ${PROJECT_TRDPARTY_DIR} ${Qt_INCLUDE} ${Kf5_INCLUDE} - ${Qt5Test_INCLUDE_DIRS} ) # extscript -set (EXTSCRIPT_HEADERS testextscript.h) -set (EXTSCRIPT_SOURCES testextscript.cpp) -# qt5_wrap_cpp(EXTSCRIPT_MOC_SOURCES ${EXTSCRIPT_HEADERS}) -add_executable (${SUBPROJECT}-extscript ${EXTSCRIPT_HEADERS} ${EXTSCRIPT_SOURCES} ${EXTSCRIPT_MOC_SOURCES}) -target_link_libraries (${SUBPROJECT}-extscript ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) -add_test (NAME "ExtScript" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extscript - "-o" "../Testing/output-extscript.log") +set(EXTSCRIPT_HEADERS testextscript.h) +set(EXTSCRIPT_SOURCES testextscript.cpp) +add_executable(${SUBPROJECT}-extscript ${EXTSCRIPT_HEADERS} ${EXTSCRIPT_SOURCES}) +target_link_libraries(${SUBPROJECT}-extscript ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +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 ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +add_test(NAME "ExtUpgrade" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extupgrade) diff --git a/sources/test/testextscript.cpp b/sources/test/testextscript.cpp index ded0da1..e5c8619 100644 --- a/sources/test/testextscript.cpp +++ b/sources/test/testextscript.cpp @@ -25,15 +25,16 @@ void TestExtScript::initTestCase() { + generateRandomString(); + extScript = new ExtScript(nullptr); extScript->setInterval(1); - extScript->setExecutable(QString("hello world")); + extScript->setExecutable(randomString); extScript->setNumber(0); extScript->setRedirect(ExtScript::Redirect::stderr2stdout); extScript->setPrefix(QString("echo")); - QVariantHash value = extScript->run(); - qDebug() << "Init values in first run" << value; + extScript->run(); } @@ -43,14 +44,11 @@ void TestExtScript::cleanupTestCase() } - - - void TestExtScript::test_values() { QCOMPARE(extScript->interval(), 1); QCOMPARE(extScript->number(), 0); - QCOMPARE(extScript->executable(), QString("hello world")); + QCOMPARE(extScript->executable(), randomString); QCOMPARE(extScript->strRedirect(), QString("stderr2stdout")); QCOMPARE(extScript->prefix(), QString("echo")); } @@ -69,7 +67,7 @@ void TestExtScript::test_run() QList arguments = spy.takeFirst(); QCOMPARE( arguments.at(0).toHash()[extScript->tag(QString("custom"))].toString(), - QString("\nhello world")); + QString("\n%1").arg(randomString)); } @@ -85,7 +83,7 @@ void TestExtScript::test_filters() QList arguments = spy.takeFirst(); QCOMPARE( arguments.at(0).toHash()[extScript->tag(QString("custom"))].toString(), - QString("
hello world")); + QString("
%1").arg(randomString)); } @@ -104,4 +102,17 @@ void TestExtScript::test_copy() } +void TestExtScript::generateRandomString() +{ + randomString.clear(); + + int diff = 'Z' - 'A'; + int count = rand() % 100 + 1; + for (int i = 0; i < count; i++) { + char c = 'A' + (rand() % diff); + randomString += QChar(c); + } +} + + QTEST_MAIN(TestExtScript); diff --git a/sources/test/testextscript.h b/sources/test/testextscript.h index 2f5688d..16cbb2d 100644 --- a/sources/test/testextscript.h +++ b/sources/test/testextscript.h @@ -24,7 +24,6 @@ class ExtScript; -class QSignalSpy; class TestExtScript : public QObject { @@ -41,7 +40,9 @@ private slots: void test_copy(); private: + void generateRandomString(); ExtScript *extScript = nullptr; + QString randomString; }; diff --git a/sources/test/testextupgrade.cpp b/sources/test/testextupgrade.cpp new file mode 100644 index 0000000..3e4f44c --- /dev/null +++ b/sources/test/testextupgrade.cpp @@ -0,0 +1,148 @@ +/*************************************************************************** + * 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 "testextupgrade.h" + +#include + +#include "extupgrade.h" + + +void TestExtUpgrade::initTestCase() +{ + generateRandomStrings(); + + extUpgrade = new ExtUpgrade(nullptr); + extUpgrade->setInterval(1); + extUpgrade->setExecutable(cmd); + extUpgrade->setNumber(0); + + extUpgrade->run(); +} + + +void TestExtUpgrade::cleanupTestCase() +{ + delete extUpgrade; +} + + +void TestExtUpgrade::test_values() +{ + QCOMPARE(extUpgrade->interval(), 1); + QCOMPARE(extUpgrade->number(), 0); + QCOMPARE(extUpgrade->executable(), cmd); +} + + +void TestExtUpgrade::test_run() +{ + // init spy + QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &))); + QVariantHash firstValue = extUpgrade->run(); + QCOMPARE(firstValue[extUpgrade->tag(QString("pkgcount"))].toInt(), 0); + + // check values + QVERIFY(spy.wait(5000)); + QList arguments = spy.takeFirst(); + QCOMPARE( + arguments.at(0).toHash()[extUpgrade->tag(QString("pkgcount"))].toInt(), + randomStrings.count()); +} + + +void TestExtUpgrade::test_null() +{ + int null = rand() % randomStrings.count(); + extUpgrade->setNull(null); + QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &))); + extUpgrade->run(); + + // check values + QVERIFY(spy.wait(5000)); + QList arguments = spy.takeFirst(); + QCOMPARE( + arguments.at(0).toHash()[extUpgrade->tag(QString("pkgcount"))].toInt(), + randomStrings.count() - null); +} + + +void TestExtUpgrade::test_filter() +{ + QSet filters; + int count = rand() % randomStrings.count(); + for (int i = 0; i < count; i++) { + int index = rand() % randomStrings.count(); + filters << randomStrings.at(index); + } + + extUpgrade->setFilter( + QString("(^%1$)").arg(filters.toList().join(QString("$|^")))); + // init spy + QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &))); + extUpgrade->run(); + + // check values + QVERIFY(spy.wait(5000)); + QList arguments = spy.takeFirst(); + QCOMPARE( + arguments.at(0).toHash()[extUpgrade->tag(QString("pkgcount"))].toInt(), + filters.count()); +} + + +void TestExtUpgrade::test_copy() +{ + ExtUpgrade *newExtUpgrade = extUpgrade->copy(QString("/dev/null"), 1); + + QCOMPARE(newExtUpgrade->interval(), extUpgrade->interval()); + QCOMPARE(newExtUpgrade->executable(), extUpgrade->executable()); + QCOMPARE(newExtUpgrade->filter(), extUpgrade->filter()); + QCOMPARE(newExtUpgrade->null(), extUpgrade->null()); + QCOMPARE(newExtUpgrade->number(), 1); + + delete newExtUpgrade; +} + + +QString TestExtUpgrade::generateRandomString() const +{ + QString string; + int diff = 'Z' - 'A'; + int count = rand() % 100 + 1; + for (int i = 0; i < count; i++) { + char c = 'A' + (rand() % diff); + string += QChar(c); + } + + return string; +} + + +void TestExtUpgrade::generateRandomStrings() +{ + randomStrings.clear(); + + int count = rand() % 100 + 1; + for (int i = 0; i < count; i++) + randomStrings.append(generateRandomString()); + cmd = QString("echo -e '%1'").arg(randomStrings.join(QString("\n"))); +} + + +QTEST_MAIN(TestExtUpgrade); diff --git a/sources/test/testextupgrade.h b/sources/test/testextupgrade.h new file mode 100644 index 0000000..e20b3a5 --- /dev/null +++ b/sources/test/testextupgrade.h @@ -0,0 +1,52 @@ +/*************************************************************************** + * 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 TESTEXTUPGRADE_H +#define TESTEXTUPGRADE_H + +#include +#include + + +class ExtUpgrade; + +class TestExtUpgrade : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_values(); + void test_run(); + void test_null(); + void test_filter(); + void test_copy(); + +private: + QString generateRandomString() const; + void generateRandomStrings(); + ExtUpgrade *extUpgrade = nullptr; + QString cmd; + QStringList randomStrings; +}; + + +#endif /* TESTEXTUPGRADE_H */