d42700a313
To facilitate that, sprinkle a bunch of QStringLiteral() around string literals and QLatin1Char() around char literals. In some places, I used QLatin1String() instead of QStringLiteral() when the expression is used as an argument to a function that has a QLatin1String overload (this is advised by the Qt docs). I also replaced empty strings like QString x = ""; QString x { "" }; funcWithQStringArg(""); funcWithQStringArg(QString("")); by the QString() default constructor, which yields an empty string more efficiently: QString x; funcWithQStringArg(QString()); QString::fromLocal8Bit() was used whenever strings were read from C libraries (e.g. PAM). For SDDM's own configuration files, I used QString::fromUtf8(), under the assumption that most text editors today default to UTF-8. In some places, I also used the chance to optimize single-char string literals to char literals, e.g. str << list.join(","); //before str << list.join(QLatin1Char(',')); //after Testing done: It compiles and the ConfigurationTest passes. I don't have a test setup for actually running a compiled SDDM, so if someone could check that I didn't break anything, that would be highly appreciated. Closes: #469
101 lines
3.4 KiB
C++
101 lines
3.4 KiB
C++
/*
|
|
* Configuration parser tests
|
|
* Copyright (C) 2014 Martin Bříza <mbriza@redhat.com>
|
|
*
|
|
* This program 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 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
*
|
|
*/
|
|
|
|
#ifndef CONFIGURATIONTEST_H
|
|
#define CONFIGURATIONTEST_H
|
|
|
|
#include <QObject>
|
|
#include <QStringList>
|
|
|
|
#include "ConfigReader.h"
|
|
|
|
#define CONF_FILE QStringLiteral("test.conf")
|
|
#define CONF_FILE_COPY QStringLiteral("test_copy.conf")
|
|
|
|
#define TEST_STRING_1_PLAIN "Test Variable Initial String"
|
|
#define TEST_STRING_1 QStringLiteral(TEST_STRING_1_PLAIN)
|
|
#define TEST_INT_1 12345
|
|
#define TEST_STRINGLIST_1 {QStringLiteral("String1"), QStringLiteral("String2")}
|
|
#define TEST_BOOL_1 true
|
|
|
|
Config (TestConfig, CONF_FILE,
|
|
enum CustomType {
|
|
FOO,
|
|
BAR,
|
|
BAZ
|
|
};
|
|
Entry( String, QString, _S(TEST_STRING_1_PLAIN), _S("Test String Description"));
|
|
Entry( Int, int, TEST_INT_1, _S("Test Integer Description"));
|
|
Entry(StringList, QStringList, QStringList(TEST_STRINGLIST_1), _S("Test StringList Description"));
|
|
Entry( Boolean, bool, TEST_BOOL_1, _S("Test Boolean Description"));
|
|
Entry( Custom, CustomType, FOO, _S("Custom type imitating NumState"));
|
|
Section(Section,
|
|
Entry( String, QString, _S(TEST_STRING_1_PLAIN), _S("Test String Description"));
|
|
Entry( Int, int, TEST_INT_1, _S("Test Integer Description"));
|
|
Entry(StringList, QStringList, QStringList(TEST_STRINGLIST_1), _S("Test StringList Description"));
|
|
Entry( Boolean, bool, TEST_BOOL_1, _S("Test Boolean Description"));
|
|
);
|
|
);
|
|
|
|
inline QTextStream& operator>>(QTextStream &str, TestConfig::CustomType &state) {
|
|
QString text = str.readLine().trimmed();
|
|
if (text.compare(QLatin1String("foo"), Qt::CaseInsensitive) == 0)
|
|
state = TestConfig::FOO;
|
|
else if (text.compare(QLatin1String("bar"), Qt::CaseInsensitive) == 0)
|
|
state = TestConfig::BAR;
|
|
else
|
|
state = TestConfig::BAZ;
|
|
return str;
|
|
}
|
|
|
|
inline QTextStream& operator<<(QTextStream &str, const TestConfig::CustomType &state) {
|
|
if (state == TestConfig::FOO)
|
|
str << "foo";
|
|
else if (state == TestConfig::BAR)
|
|
str << "bar";
|
|
else
|
|
str << "baz";
|
|
return str;
|
|
}
|
|
|
|
class ConfigurationTest : public QObject
|
|
{
|
|
Q_OBJECT
|
|
private slots:
|
|
void initTestCase();
|
|
void cleanupTestCase();
|
|
|
|
void init();
|
|
void cleanup();
|
|
|
|
void Basic();
|
|
void Sections();
|
|
void Unused();
|
|
void LineChanges();
|
|
void CustomEnum();
|
|
void RightOnInit();
|
|
void FileChanged();
|
|
|
|
private:
|
|
TestConfig *config;
|
|
};
|
|
|
|
#endif // CONFIGURATIONTEST_H
|