QStandardPaths: de-duplicate some unittests code

Shorter lines, easier to read.

Pick-to: 6.5
Change-Id: Ifd83974cce2dd03bf05bcf032da2e459f1d3f798
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Ahmad Samir 2023-04-20 13:31:08 +02:00
parent a7555c3306
commit 18f5dc3746

View File

@ -192,8 +192,8 @@ void tst_qstandardpaths::testDefaultLocations()
setDefaultLocations();
const QString expectedConfHome = QDir::homePath() + QString::fromLatin1("/.config");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation), expectedConfHome);
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation), expectedConfHome);
QCOMPARE(configLoc(), expectedConfHome);
QCOMPARE(genericConfigLoc(), expectedConfHome);
const QStringList confDirs = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation);
QCOMPARE(confDirs.size(), 2);
QVERIFY(confDirs.contains(expectedConfHome));
@ -223,8 +223,8 @@ void tst_qstandardpaths::testCustomLocations()
setCustomLocations();
// test writableLocation()
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation), m_localConfigDir);
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation), m_localConfigDir);
QCOMPARE(configLoc(), m_localConfigDir);
QCOMPARE(genericConfigLoc(), m_localConfigDir);
// test locate()
const QString thisFileName = QString::fromLatin1("aFile");
@ -258,8 +258,8 @@ void tst_qstandardpaths::enableTestMode()
// *Config*Location
const QString configDir = qttestDir + QLatin1String("/config");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation), configDir);
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation), configDir);
QCOMPARE(configLoc(), configDir);
QCOMPARE(genericConfigLoc(), configDir);
const QStringList confDirs = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation);
QCOMPARE(confDirs, QStringList() << configDir << m_globalConfigDir);
// AppConfigLocation should be "GenericConfigLocation/organization-name/app-name"
@ -267,7 +267,7 @@ void tst_qstandardpaths::enableTestMode()
// *Data*Location
const QString dataDir = qttestDir + QLatin1String("/share");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation), dataDir);
QCOMPARE(genericDataLoc(), dataDir);
const QStringList gdDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
QCOMPARE(gdDirs, QStringList() << dataDir << m_globalAppDir);
// AppDataLocation/AppLocalDataLocation should be
@ -277,7 +277,7 @@ void tst_qstandardpaths::enableTestMode()
// *CacheLocation
const QString cacheDir = qttestDir + QLatin1String("/cache");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation), cacheDir);
QCOMPARE(genericCacheLoc(), cacheDir);
const QStringList cacheDirs = QStandardPaths::standardLocations(QStandardPaths::GenericCacheLocation);
QCOMPARE(cacheDirs, QStringList() << cacheDir);
// CacheLocation should be "GenericCacheLocation/organization-name/app-name"
@ -308,13 +308,13 @@ void tst_qstandardpaths::enableTestMode()
// Check this for locations where test programs typically write. Not desktop, download, music etc...
typedef QHash<QStandardPaths::StandardLocation, QString> LocationHash;
LocationHash testLocations;
testLocations.insert(QStandardPaths::AppDataLocation, QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
testLocations.insert(QStandardPaths::AppLocalDataLocation, QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
testLocations.insert(QStandardPaths::GenericDataLocation, QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
testLocations.insert(QStandardPaths::ConfigLocation, QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
testLocations.insert(QStandardPaths::GenericConfigLocation, QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation));
testLocations.insert(QStandardPaths::CacheLocation, QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
testLocations.insert(QStandardPaths::GenericCacheLocation, QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation));
testLocations.insert(QStandardPaths::AppDataLocation, appDataLoc());
testLocations.insert(QStandardPaths::AppLocalDataLocation, appLocalDataLoc());
testLocations.insert(QStandardPaths::GenericDataLocation, genericDataLoc());
testLocations.insert(QStandardPaths::ConfigLocation, configLoc());
testLocations.insert(QStandardPaths::GenericConfigLocation, genericConfigLoc());
testLocations.insert(QStandardPaths::CacheLocation, cacheLoc());
testLocations.insert(QStandardPaths::GenericCacheLocation, genericCacheLoc());
// On Windows, what should "Program Files" become, in test mode?
//testLocations.insert(QStandardPaths::ApplicationsLocation, QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation));
@ -364,18 +364,18 @@ void tst_qstandardpaths::testDataLocation()
// Android is an exception to this case, owing to the fact that
// applications are sandboxed.
#if !defined(Q_OS_ANDROID)
const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation), base + "/tst_qstandardpaths");
const QString base = genericDataLoc();
QCOMPARE(appLocalDataLoc(), base + "/tst_qstandardpaths");
QCoreApplication::instance()->setOrganizationName("Qt");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation), base + "/Qt/tst_qstandardpaths");
QCOMPARE(appLocalDataLoc(), base + "/Qt/tst_qstandardpaths");
QCoreApplication::instance()->setApplicationName("QtTest");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation), base + "/Qt/QtTest");
QCOMPARE(appLocalDataLoc(), base + "/Qt/QtTest");
#endif
#ifdef Q_XDG_PLATFORM
setDefaultLocations();
const QString expectedAppDataDir = QDir::homePath() + QString::fromLatin1("/.local/share/Qt/QtTest");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation), expectedAppDataDir);
QCOMPARE(appLocalDataLoc(), expectedAppDataDir);
const QStringList appDataDirs = QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation);
QCOMPARE(appDataDirs.size(), 3);
QCOMPARE(appDataDirs.at(0), expectedAppDataDir);
@ -389,12 +389,12 @@ void tst_qstandardpaths::testAppConfigLocation()
// On all platforms where applications are not sandboxed,
// AppConfigLocation should be GenericConfigLocation / organization name / app name
#if !defined(Q_OS_ANDROID)
const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation), base + "/tst_qstandardpaths");
const QString base = genericConfigLoc();
QCOMPARE(appConfigLoc(), base + "/tst_qstandardpaths");
QCoreApplication::setOrganizationName("Qt");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation), base + "/Qt/tst_qstandardpaths");
QCOMPARE(appConfigLoc(), base + "/Qt/tst_qstandardpaths");
QCoreApplication::setApplicationName("QtTest");
QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation), base + "/Qt/QtTest");
QCOMPARE(appConfigLoc(), base + "/Qt/QtTest");
#endif
}
@ -804,19 +804,19 @@ void tst_qstandardpaths::testXdgPathCleanup()
const QString relative("./someRelativeDir");
qputenv("XDG_CACHE_HOME", relative.toLatin1());
const QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
const QString cacheDir = cacheLoc();
QCOMPARE_NE(cacheDir, relative);
qputenv("XDG_DATA_HOME", relative.toLatin1());
const QString localDataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
const QString localDataDir = genericDataLoc();
QCOMPARE_NE(localDataDir, relative);
qputenv("XDG_CONFIG_HOME", relative.toLatin1());
const QString localConfig = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
const QString localConfig = configLoc();
QCOMPARE_NE(localConfig, relative);
qputenv("XDG_RUNTIME_DIR", relative.toLatin1());
const QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
const QString runtimeDir = genericDataLoc();
QCOMPARE_NE(runtimeDir, relative);
#endif
}