Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Common/LtsCommandLineParser/unit-test/commandlineparser_unit_test.cpp
T
berrux-dev 262fd226f9 Restore in-scope files missing from the merge and scope-aware CMakeLists
The merge from story/release-validation-scope only carried files that
existed at the common base or on that branch. In-scope files added on
story/release-2-cleanup after the base (per-module unit-test dirs, the
Release 2 driver and rpmbuild script) were missing, breaking
ADD_SUBDIRECTORY(unit-test) references.

- Restore the 33 missing in-scope files from story/release-2-cleanup.
- Lts/Common/CMakeLists.txt and LtsUtilsGui/CMakeLists.txt: take the
  story/release-2-cleanup versions (the validation versions still
  reference out-of-scope subdirectories: httq, LtsRestClient,
  LtsWebSocket, ConnectDialog, StyleManager).
2026-11-16 19:04:24 +01:00

174 lines
5.4 KiB
C++

/**
* @RDE_HEADER@
* Unittest
* $Id$
* $HeadURL$
* $Author$
*/
#include <QtTest/QTest>
#include <LtsCommandLineParser/LtsCommandLineParser.h>
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QSettings>
#include <QtCore/QTemporaryDir>
#include <QtCore/QUrl>
class commandlineparser_unit_test : public QObject
{
Q_OBJECT
private Q_SLOTS:
void optionDefaults();
void parseWithOptions();
void tcpConnectionUrl();
void setDomainIdOverridesGenerated();
void domainIdFallbackRange();
void initCleanRun();
void iniFileProvidesDomainId();
void unknownOptionBehaviour();
};
using Lcp = LtsCommandLineParser::LtsCommandLineParser;
void commandlineparser_unit_test::optionDefaults()
{
Lcp parser;
// nothing parsed yet
QVERIFY(!parser.domainIdProvided());
QVERIFY(!parser.exerciseIdProvided());
QVERIFY(!parser.iniFileSet());
QVERIFY(!parser.useTcpConnection());
QVERIFY(!parser.tcpConnectionUrl().isValid());
QCOMPARE(parser.exerciseId(), 0);
QCOMPARE(parser.iniFile(), QString());
}
void commandlineparser_unit_test::parseWithOptions()
{
Lcp parser(Lcp::ProvideCommandLineParameterOptions(
Lcp::DomainId | Lcp::ExerciseId | Lcp::CustomConfigFile | Lcp::TcpConnectionUrl));
const bool ok = parser.parse({
QStringLiteral("prog"),
QStringLiteral("-d"), QStringLiteral("42"),
QStringLiteral("-e"), QStringLiteral("7"),
QStringLiteral("-i"), QStringLiteral("/tmp/lts-cc.conf"),
QStringLiteral("-t"), QStringLiteral("tcpv4_lan://10.0.0.1:7400")
});
QVERIFY(ok);
QVERIFY(parser.domainIdProvided());
QVERIFY(parser.exerciseIdProvided());
QVERIFY(parser.iniFileSet());
QVERIFY(parser.useTcpConnection());
}
void commandlineparser_unit_test::tcpConnectionUrl()
{
// the tcpv4_lan scheme is canonicalized to tcpv4lan
{
Lcp parser(Lcp::ProvideCommandLineParameterOptions(Lcp::TcpConnectionUrl));
QVERIFY(parser.parse({
QStringLiteral("prog"),
QStringLiteral("-t"), QStringLiteral("tcpv4_lan://10.0.0.1:7400")
}));
const QUrl url = parser.tcpConnectionUrl();
QCOMPARE(url.scheme(), QStringLiteral("tcpv4lan"));
QCOMPARE(url.host(), QStringLiteral("10.0.0.1"));
QCOMPARE(url.port(), 7400);
}
// http/file input is rewritten to the tcpv4lan scheme
{
Lcp parser(Lcp::ProvideCommandLineParameterOptions(Lcp::TcpConnectionUrl));
QVERIFY(parser.parse({
QStringLiteral("prog"),
QStringLiteral("-t"), QStringLiteral("http://192.168.0.2:7500")
}));
QCOMPARE(parser.tcpConnectionUrl().scheme(), QStringLiteral("tcpv4lan"));
}
// without the option enabled the URL is never returned
{
Lcp parser(Lcp::ProvideCommandLineParameterOptions(Lcp::DomainId));
const QUrl url = parser.tcpConnectionUrl();
QVERIFY(!url.isValid());
QVERIFY(!parser.useTcpConnection());
}
}
void commandlineparser_unit_test::setDomainIdOverridesGenerated()
{
Lcp parser;
parser.setDomainId(7);
// the function-set id is always returned, never the IP-generated fallback
QCOMPARE(parser.domainId(), 7);
}
void commandlineparser_unit_test::domainIdFallbackRange()
{
// without an explicit id the parser falls back to the id generated from
// the local IPv4 address (or 0 when none is found)
Lcp parser;
const int id = parser.domainId();
QVERIFY2(id >= 0 && id <= 232,
qPrintable(QStringLiteral("domainId() fallback out of range: %1").arg(id)));
}
void commandlineparser_unit_test::initCleanRun()
{
// init() parses the test executable's own argument list (program name
// only under ctest) and succeeds
Lcp parser;
QVERIFY(parser.init(false));
}
void commandlineparser_unit_test::iniFileProvidesDomainId()
{
// init() re-parses the application's own argument list and QCommandLineParser
// resets the option state on every parse, so -i can only reach it through the
// process argv. QtTest's main cannot forward CLI options, so the (non-QtTest)
// probe executable runs init() with a real command line instead.
// The ini file is created under the working directory (rather than the
// default /tmp) so the test is independent of /tmp's state.
QTemporaryDir tmp(QDir::current().absolutePath() + QStringLiteral("/lcp_ut_XXXXXX"));
QVERIFY(tmp.isValid());
const QString iniFile = tmp.path() + QStringLiteral("/lts-cc.conf");
{
QSettings s(iniFile, QSettings::IniFormat);
s.setValue(QStringLiteral("Global/DomainId"), 7);
s.sync();
}
QProcess proc;
proc.start(QStringLiteral(INI_PROBE_EXE), { QStringLiteral("-i"), iniFile });
QVERIFY(proc.waitForFinished(10000));
const QByteArray out = proc.readAllStandardOutput();
// no -d given: the domain id is read from the ini file
QVERIFY2(out.contains(QByteArrayLiteral("DOMAINID=7")),
qPrintable(QString::fromUtf8(out)));
}
void commandlineparser_unit_test::unknownOptionBehaviour()
{
// the version option is registered unless NoVersion is set
Lcp withVersion;
QVERIFY(withVersion.parse({ QStringLiteral("prog"), QStringLiteral("-v") }));
QVERIFY(withVersion.isSet(QStringLiteral("v")));
// with NoVersion, -v is not registered and parsing fails
Lcp strict(Lcp::NoVersion);
QVERIFY(!strict.parse({ QStringLiteral("prog"), QStringLiteral("-v") }));
// the help option is always registered
Lcp parser;
parser.setUnitModuleTestMode(true);
QVERIFY(parser.parse({ QStringLiteral("prog"), QStringLiteral("-h") }));
QVERIFY(parser.isSet(QStringLiteral("h")));
}
QTEST_MAIN(commandlineparser_unit_test)
#include "commandlineparser_unit_test.moc"