add tests for extupgrade

This commit is contained in:
Evgenii Alekseev 2016-05-25 12:35:34 +03:00
parent c57a92b687
commit b73fb19409
7 changed files with 236 additions and 19 deletions

View File

@ -225,6 +225,8 @@ void ExtUpgrade::updateValue()
.filter(QRegExp(m_filter))
.count();
}(qoutput);
emit(dataReceived(value));
}

View File

@ -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

View File

@ -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)

View File

@ -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<QVariant> 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<QVariant> arguments = spy.takeFirst();
QCOMPARE(
arguments.at(0).toHash()[extScript->tag(QString("custom"))].toString(),
QString("<br>hello world"));
QString("<br>%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);

View File

@ -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;
};

View File

@ -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 <QtTest>
#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<QVariant> 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<QVariant> arguments = spy.takeFirst();
QCOMPARE(
arguments.at(0).toHash()[extUpgrade->tag(QString("pkgcount"))].toInt(),
randomStrings.count() - null);
}
void TestExtUpgrade::test_filter()
{
QSet<QString> 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<QVariant> 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);

View File

@ -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 <QObject>
#include <QVariant>
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 */