more tests for formatters

This commit is contained in:
Evgenii Alekseev 2016-05-30 01:47:06 +03:00
parent cae9e0d2e3
commit 69c09d9ff8
17 changed files with 313 additions and 69 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -20,7 +20,6 @@
#define TESTABSTRACTEXTITEM_H
#include <QObject>
#include <QVariant>
class ExtUpgrade;

View 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);

View 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 */

View File

@ -20,7 +20,6 @@
#define TESTEXTSCRIPT_H
#include <QObject>
#include <QVariant>
class ExtScript;

View File

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

View File

@ -20,7 +20,6 @@
#define TESTEXTUPGRADE_H
#include <QObject>
#include <QVariant>
class ExtUpgrade;

View File

@ -20,7 +20,6 @@
#define TESTEXTWEATHER_H
#include <QObject>
#include <QVariant>
class ExtWeather;

View File

@ -20,8 +20,8 @@
#include <QtTest>
#include "awtestlibrary.h"
#include "awfloatformatter.h"
#include "awtestlibrary.h"
void TestAWFloatFormatter::initTestCase()

View File

@ -20,7 +20,6 @@
#define TESTFLOATFORMATTER_H
#include <QObject>
#include <QVariant>
class AWFloatFormatter;

View File

@ -20,8 +20,8 @@
#include <QtTest>
#include "awtestlibrary.h"
#include "awnoformatter.h"
#include "awtestlibrary.h"
void TestAWNoFormatter::initTestCase()

View File

@ -20,7 +20,6 @@
#define TESTNOFORMATTER_H
#include <QObject>
#include <QVariant>
class AWNoFormatter;

View 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);

View 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 */