mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 23:47:20 +00:00
add test case for jsonformatter
Change array indexes from "[num]" to ".num." in path
This commit is contained in:
parent
004a97800c
commit
7565ea2e82
@ -178,8 +178,8 @@ QVariant AWJsonFormatter::getFromMap(const QVariant &value,
|
|||||||
void AWJsonFormatter::initPath()
|
void AWJsonFormatter::initPath()
|
||||||
{
|
{
|
||||||
m_splittedPath.clear();
|
m_splittedPath.clear();
|
||||||
QStringList splittedByDot = m_path.split(QRegExp(QString("(\\.|\\[|\\])")),
|
QStringList splittedByDot
|
||||||
QString::SkipEmptyParts);
|
= m_path.split(QChar('.'), QString::SkipEmptyParts);
|
||||||
|
|
||||||
for (auto &element : splittedByDot) {
|
for (auto &element : splittedByDot) {
|
||||||
bool ok;
|
bool ok;
|
||||||
|
@ -27,7 +27,7 @@ set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${PROJECT_MONITORSO
|
|||||||
# modules
|
# modules
|
||||||
set(TEST_MODULES
|
set(TEST_MODULES
|
||||||
abstractextitem extquotes extscript extupgrade extweather
|
abstractextitem extquotes extscript extupgrade extweather
|
||||||
abstractformatter datetimeformatter floatformatter listformatter noformatter scriptformatter stringformatter
|
abstractformatter datetimeformatter floatformatter jsonformatter listformatter noformatter scriptformatter stringformatter
|
||||||
extitemaggregator
|
extitemaggregator
|
||||||
batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource
|
batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource
|
||||||
awbugreporter awconfighelper awkeycache awkeys awpatternfunctions awtelemetryhandler awupdatehelper
|
awbugreporter awconfighelper awkeycache awkeys awpatternfunctions awtelemetryhandler awupdatehelper
|
||||||
|
97
sources/test/testjsonformatter.cpp
Normal file
97
sources/test/testjsonformatter.cpp
Normal 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);
|
50
sources/test/testjsonformatter.h
Normal file
50
sources/test/testjsonformatter.h
Normal 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 */
|
@ -47,4 +47,4 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif /* TESTNOFORMATTER_H */
|
#endif /* TESTSCRIPTFORMATTER_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user