diff --git a/sources/awesomewidgets/awjsonformatter.cpp b/sources/awesomewidgets/awjsonformatter.cpp index 2b3fa38..b0dd19e 100644 --- a/sources/awesomewidgets/awjsonformatter.cpp +++ b/sources/awesomewidgets/awjsonformatter.cpp @@ -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; diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index 07e9e9b..3f7ca1d 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -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 diff --git a/sources/test/testjsonformatter.cpp b/sources/test/testjsonformatter.cpp new file mode 100644 index 0000000..c5b9c0d --- /dev/null +++ b/sources/test/testjsonformatter.cpp @@ -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 + +#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); diff --git a/sources/test/testjsonformatter.h b/sources/test/testjsonformatter.h new file mode 100644 index 0000000..12ec8a6 --- /dev/null +++ b/sources/test/testjsonformatter.h @@ -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 +#include + + +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 */ diff --git a/sources/test/testscriptformatter.h b/sources/test/testscriptformatter.h index ed81823..6b23e71 100644 --- a/sources/test/testscriptformatter.h +++ b/sources/test/testscriptformatter.h @@ -47,4 +47,4 @@ private: }; -#endif /* TESTNOFORMATTER_H */ +#endif /* TESTSCRIPTFORMATTER_H */