add test case for jsonformatter

Change array indexes from "[num]" to ".num." in path
This commit is contained in:
Evgenii Alekseev 2016-11-03 00:23:10 +03:00
parent 004a97800c
commit 7565ea2e82
5 changed files with 151 additions and 4 deletions

View File

@ -178,8 +178,8 @@ QVariant AWJsonFormatter::getFromMap(const QVariant &value,
void AWJsonFormatter::initPath()
{
m_splittedPath.clear();
QStringList splittedByDot = m_path.split(QRegExp(QString("(\\.|\\[|\\])")),
QString::SkipEmptyParts);
QStringList splittedByDot
= m_path.split(QChar('.'), QString::SkipEmptyParts);
for (auto &element : splittedByDot) {
bool ok;

View File

@ -27,7 +27,7 @@ set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${PROJECT_MONITORSO
# modules
set(TEST_MODULES
abstractextitem extquotes extscript extupgrade extweather
abstractformatter datetimeformatter floatformatter listformatter noformatter scriptformatter stringformatter
abstractformatter datetimeformatter floatformatter jsonformatter listformatter noformatter scriptformatter stringformatter
extitemaggregator
batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource
awbugreporter awconfighelper awkeycache awkeys awpatternfunctions awtelemetryhandler awupdatehelper

View File

@ -0,0 +1,97 @@
/***************************************************************************
* 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 "testjsonformatter.h"
#include <QtTest>
#include "awjsonformatter.h"
#include "awtestlibrary.h"
void TestAWJsonFormatter::initTestCase()
{
formatter = new AWJsonFormatter(nullptr);
generate();
formatter->setPath(path);
}
void TestAWJsonFormatter::cleanupTestCase()
{
delete formatter;
}
void TestAWJsonFormatter::test_values()
{
QCOMPARE(formatter->path(), path);
}
void TestAWJsonFormatter::test_conversion()
{
QCOMPARE(formatter->convert(json), value);
}
void TestAWJsonFormatter::test_copy()
{
AWJsonFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1);
QCOMPARE(newFormatter->path(), formatter->path());
QCOMPARE(newFormatter->number(), 1);
delete newFormatter;
}
void TestAWJsonFormatter::generate()
{
value = AWTestLibrary::randomString();
QVariantMap first;
QString firstKey = AWTestLibrary::randomString();
first[firstKey] = value;
int listCount = AWTestLibrary::randomInt(5) + 1;
int validCount = AWTestLibrary::randomInt(listCount);
QVariantList second;
for (int i = 0; i < listCount; i++) {
if (i == validCount) {
second.append(first);
} else {
QString key = AWTestLibrary::randomString();
QString val = AWTestLibrary::randomString();
QVariantMap dict;
dict[key] = val;
second.append(dict);
}
}
QString thirdKey = AWTestLibrary::randomString();
QVariantMap output;
output[thirdKey] = second;
json = output;
path = QString("%1.%2.%3").arg(thirdKey).arg(validCount).arg(firstKey);
}
QTEST_MAIN(TestAWJsonFormatter);

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 TESTJSONFORMATTER_H
#define TESTJSONFORMATTER_H
#include <QObject>
#include <QVariant>
class AWJsonFormatter;
class TestAWJsonFormatter : public QObject
{
Q_OBJECT
private slots:
// initialization
void initTestCase();
void cleanupTestCase();
// test
void test_values();
void test_conversion();
void test_copy();
private:
void generate();
AWJsonFormatter *formatter = nullptr;
QVariant json;
QString path;
QString value;
};
#endif /* TESTJSONFORMATTER_Hl */

View File

@ -47,4 +47,4 @@ private:
};
#endif /* TESTNOFORMATTER_H */
#endif /* TESTSCRIPTFORMATTER_H */