Add per-module unit-test dirs for the Release 2 scope

One unit-test/ directory per module/target, mirroring the existing
TetraIF/unit-test/ layout (one QtTest executable per module, guarded by
BUILD_TESTING and the module target, each registered with ctest):

  Lts/Common/CodeGen      codegen_unit_test
  Lts/Common/LtsCoordinate ltscoordinate_unit_test
  Lts/Common/LtsSession   ltssession_unit_test
  Lts/DDS/Core            ddscore_unit_test
  Lts/DDS/QosProfile      ddsqos_unit_test
  Lts/Tools/LogViewer     logviewer_unit_test
  Lts/Tools/SqlBrowser    sqlbrowser_unit_test
  Z/src/NGLLogger         ngllogger_unit_test
  Z/src/WMS_Interface     wms_json_ds_unit_test
  Z/src/nglapplication    nglcoreapplication_unit_test
  Z/src/nglcompression    nglcompression_unit_test
  Z/src/nglsettings       nglsettings_unit_test
  Z/src/ngltranslator     ngltranslator_unit_test

Where a module is built with hidden visibility and has no export macro
(LogViewer, SqlBrowser, ngltranslator, NGLLogger, nglcompression,
nglsettings), its sources are compiled directly into the test
executable, the same pattern as prtcnv_unit_test.

ctest baseline in the container (QT_QPA_PLATFORM=offscreen): 20/23
passed (13 new + 7 pre-existing in-scope tests), 3 Not Run as before
(nglcomp_test, GrantleeParserSmoke, utils_test).

README: ctest baseline table extended to the 23 registered tests and
section 2.4 now documents the per-module test layout. ddsview has no
unit test (GUI tool, XmlHelper needs the DDS dynamic-xtypes runtime).
This commit is contained in:
berrux-dev committed 2026-11-15 01:08:17 +01:00
1 parent a62b729577
commit 09b6df8bc3
40 files changed
+4966 -17

No files matched your search

@@ -170,3 +170,8 @@ endif()
ELSE()
message(STATUS "BUILD_GRANTLEE disabled or Grantlee not found; skipping codegen_grantlee, ngl_codegen, and codegen_gui targets")
ENDIF()
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,20 @@
#
# Unit tests for the CodeGen module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- CodeGen: string helpers, data classes and Grantlee template-context
# --- wrappers (pure logic; the KTextTemplate engine is covered by
# --- GrantleeParserSmoke)
if (TARGET codegen_grantlee)
ADD_EXECUTABLE(codegen_unit_test codegen_unit_test.cpp)
TARGET_LINK_LIBRARIES(codegen_unit_test
codegen_grantlee
Qt6::Core
Qt6::Test
)
ADD_TEST(NAME codegen_unit_test COMMAND $<TARGET_FILE:codegen_unit_test>)
endif()
@@ -0,0 +1,701 @@
//
// Unit tests for the ngl_codegen module (Lts/Common/CodeGen): the pure-logic
// string helpers (StructBuilder statics), the codegen_base data classes and
// the KTextTemplate context wrappers (GrantleeWrappers).
// No template execution is done here; the Grantlee/KTextTemplate engine
// itself is covered by the GrantleeParserSmoke smoke test.
//
#include <CodeGen/GrantleeWrappers.h>
#include <CodeGen/SqlGenerator.h>
#include <CodeGen/StructBuilder.h>
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtTest/QTest>
using namespace CodeGen;
// Minimal concrete DataTypeInfo for the IdlDataWrapper tests (the module's
// own concrete data types are private to StructBuilder.cpp).
class TestDataTypeInfo : public DataTypeInfo
{
public:
TestDataTypeInfo(const QString& idlType, const QString& cppType, bool isPod)
: DataTypeInfo(QStringLiteral("test"), isPod), m_idlType(idlType), m_cppType(cppType)
{
}
QString idlType() const override final
{
return m_idlType;
}
QString ddsType() const override final
{
return m_cppType;
}
QString sqlType() const override final
{
return m_cppType;
}
QString cppType() const override final
{
return m_cppType;
}
private:
QString m_idlType;
QString m_cppType;
};
static MemberInformation makeMember(const QString& table, const QString& column)
{
MemberInformation mi;
mi.ea.table = table;
mi.ea.column = column;
mi.db.table = table;
mi.db.columnName = column;
mi.cpp.memberName = column;
return mi;
}
class codegen_unit_test : public QObject
{
Q_OBJECT
private slots:
void firstCharLowercase();
void camelCaseString();
void camelCaseToUnderline();
void nameWrapperTransforms();
void memberWrapperGetters();
void memberWrapperVariantConversion();
void memberWrapperDdsToJsonElement();
void memberWrapperJsonElementToDds();
void memberWrapperDbToJsonElement();
void memberWrapperJsonElementToDb();
void memberWrapperJsonPrePostFallback();
void memberListWrapperFilters();
void dataTopicWrapper();
void functionTopicWrapper();
void idlWrappers();
void dbStructWrapper();
void codegenBaseDataClasses();
};
void codegen_unit_test::firstCharLowercase()
{
QCOMPARE(StructBuilder::firstCharLowercase(QString()), QString());
QCOMPARE(StructBuilder::firstCharLowercase(QStringLiteral("FooBar")), QStringLiteral("fooBar"));
QCOMPARE(StructBuilder::firstCharLowercase(QStringLiteral("abc")), QStringLiteral("abc"));
}
void codegen_unit_test::camelCaseString()
{
// underscore-separated -> camelCase (default keeps the first character lower case)
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo_bar")), QStringLiteral("fooBar"));
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo_bar"), true), QStringLiteral("FooBar"));
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo")), QStringLiteral("foo"));
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("_foo")), QStringLiteral("Foo"));
// trailing underscore sets the "next is upper" flag but appends nothing
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo_")), QStringLiteral("foo"));
QCOMPARE(StructBuilder::camelCaseString(QString()), QString());
}
void codegen_unit_test::camelCaseToUnderline()
{
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("FooBarBaz")), QStringLiteral("foo_bar_baz"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("foo")), QStringLiteral("foo"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("A")), QStringLiteral("a"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("aB")), QStringLiteral("a_b"));
// digits are kept, an upper case after a digit still splits
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("Foo1Bar")), QStringLiteral("foo1_bar"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QString()), QString());
}
void codegen_unit_test::nameWrapperTransforms()
{
QObject parent;
NameWrapper w(QStringLiteral("foo_bar"), &parent);
QCOMPARE(w.name(), QStringLiteral("foo_bar"));
QCOMPARE(w.nameCc(), QStringLiteral("FooBar"));
QCOMPARE(w.nameFlc(), QStringLiteral("fooBar"));
NameWrapper w2(QStringLiteral("FooBar"), &parent);
QCOMPARE(w2.nameCc(), QStringLiteral("FooBar"));
QCOMPARE(w2.nameFlc(), QStringLiteral("fooBar"));
}
void codegen_unit_test::memberWrapperGetters()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_name"));
mi.ea.type = QStringLiteral("varchar(255)");
mi.db.datatype = QStringLiteral("varchar(255)");
mi.db.columnNameView = QStringLiteral("a_name_v");
mi.db.hasSequence = true;
mi.cpp.structName = QStringLiteral("AStruct");
mi.cpp.defaultValue = QStringLiteral("''");
mi.cpp.type = QStringLiteral("QString");
mi.cpp.ddsType = QStringLiteral("dds::core::string255");
mi.cpp.isOptional = true;
mi.cpp.usedForCppStruct = true;
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.cppMemberName(), QStringLiteral("a_name"));
QCOMPARE(w.cppMemberNameCc(), QStringLiteral("AName"));
QCOMPARE(w.cppStructName(), QStringLiteral("AStruct"));
QCOMPARE(w.cppType(), QStringLiteral("QString"));
QCOMPARE(w.cppDefault(), QStringLiteral("''"));
QCOMPARE(w.dbTableName(), QStringLiteral("tbl_a"));
QCOMPARE(w.dbColumnName(), QStringLiteral("a_name"));
QCOMPARE(w.dbColumnNameView(), QStringLiteral("a_name_v"));
QCOMPARE(w.cppDdsType(), QStringLiteral("dds::core::string255"));
QCOMPARE(w.dbHasSequence(), true);
QCOMPARE(w.isOptional(), true);
QCOMPARE(w.isCppStruct(), true);
QCOMPARE(w.info().db.table, QStringLiteral("tbl_a"));
// view column name and dds type fall back when empty
MemberInformation mi2 = makeMember(QStringLiteral("tbl_b"), QStringLiteral("b_id"));
mi2.cpp.type = QStringLiteral("int");
MemberInformationWrapper w2(mi2, &parent);
QCOMPARE(w2.dbColumnNameView(), QStringLiteral("b_id"));
QCOMPARE(w2.cppDdsType(), QStringLiteral("int"));
}
void codegen_unit_test::memberWrapperVariantConversion()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
mi.cpp.isOptional = false;
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.ddsToQVariant(), QStringLiteral("toQVariant(data.foo)"));
QCOMPARE(w.qVariantToDds(), QStringLiteral("data.foo = fromQVariant<int>(q.value(i++))"));
// optional members are wrapped in dds::core::optional<>
mi.cpp.isOptional = true;
mi.cpp.ddsType = QStringLiteral("MyStruct");
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.qVariantToDds(),
QStringLiteral("data.foo = fromQVariant<dds::core::optional<MyStruct>>(q.value(i++))"));
}
void codegen_unit_test::memberWrapperDdsToJsonElement()
{
QObject parent;
// plain member
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.ddsToJsonElement(), QStringLiteral("obj[QStringLiteral(\"foo\")] = element.foo"));
// optional member goes through has_value()
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.ddsToJsonElement(),
QStringLiteral("if (element.foo.has_value()) obj[QStringLiteral(\"foo\")] = *element.foo"));
// empty member name falls back to the member type
MemberInformation mi3;
mi3.cpp.type = QStringLiteral("int");
MemberInformationWrapper w3(mi3, &parent);
QCOMPARE(w3.ddsToJsonElement(), QStringLiteral("obj[QStringLiteral(\"int\")] = element;"));
}
void codegen_unit_test::memberWrapperJsonElementToDds()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.jsonElementToDds(),
QStringLiteral("const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); element.foo = val"));
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.jsonElementToDds(), QStringLiteral(
"const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); "
"if (!val.isNull()) element.foo = val"));
// empty member name falls back to the member type
MemberInformation mi3;
mi3.cpp.type = QStringLiteral("int");
MemberInformationWrapper w3(mi3, &parent);
QCOMPARE(w3.jsonElementToDds(), QStringLiteral("element = objElement.value(QStringLiteral(\"int\"))"));
}
void codegen_unit_test::memberWrapperDbToJsonElement()
{
QObject parent;
// plain member
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.dbToJsonElement(), QStringLiteral("obj[QStringLiteral(\"foo\")] = element.foo"));
// optional (non-QDateTime) member is only written when it has a value
mi.cpp.type = QStringLiteral("QString");
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.dbToJsonElement(),
QStringLiteral("if (!element.foo.isNull()) obj[QStringLiteral(\"foo\")] = element.foo"));
// optional QDateTime member uses isValid()
mi.cpp.type = QStringLiteral("QDateTime");
MemberInformationWrapper w3(mi, &parent);
QCOMPARE(w3.dbToJsonElement(),
QStringLiteral("if (element.foo.isValid()) obj[QStringLiteral(\"foo\")] = element.foo"));
// empty member name falls back to the member type
MemberInformation mi4;
mi4.cpp.type = QStringLiteral("int");
MemberInformationWrapper w4(mi4, &parent);
QCOMPARE(w4.dbToJsonElement(), QStringLiteral("obj[QStringLiteral(\"int\")] = element;"));
}
void codegen_unit_test::memberWrapperJsonElementToDb()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.jsonElementToDb(),
QStringLiteral("const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); element.foo = val"));
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.jsonElementToDb(), QStringLiteral(
"const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); "
"if (!val.isUndefined()) element.foo = val"));
// empty member name falls back to the member type
MemberInformation mi3;
mi3.cpp.type = QStringLiteral("int");
MemberInformationWrapper w3(mi3, &parent);
QCOMPARE(w3.jsonElementToDb(), QStringLiteral("element = objElement.value(QStringLiteral(\"int\"))"));
}
void codegen_unit_test::memberWrapperJsonPrePostFallback()
{
QObject parent;
// json pre/post strings win over the variant pre/post strings when set
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.toVariantPre = QStringLiteral("T(");
mi.cpp.toVariantPost = QStringLiteral(")");
mi.cpp.fromVariantPre2 = QStringLiteral("V(");
mi.cpp.fromVariantPost = QStringLiteral(")");
mi.cpp.toJsonPre = QStringLiteral("J(");
mi.cpp.toJsonPost = QStringLiteral(")");
mi.cpp.fromJsonPre2 = QStringLiteral("F(");
mi.cpp.fromJsonPost = QStringLiteral(")");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.cppToQvPre(), QStringLiteral("T("));
QCOMPARE(w.cppToQvPost(), QStringLiteral(")"));
QCOMPARE(w.cppFromQvPre1(), QString());
QCOMPARE(w.cppFromQvPre2(), QStringLiteral("V("));
QCOMPARE(w.cppFromQvPost(), QStringLiteral(")"));
QCOMPARE(w.cppToJsonPre(), QStringLiteral("J("));
QCOMPARE(w.cppToJsonPost(), QStringLiteral(")"));
QCOMPARE(w.cppFromJsonPre2(), QStringLiteral("F("));
QCOMPARE(w.cppFromJsonPost(), QStringLiteral(")"));
// without json strings the variant strings are used
MemberInformation mi2 = makeMember(QStringLiteral("tbl_a"), QStringLiteral("bar"));
mi2.cpp.toVariantPre = QStringLiteral("T(");
mi2.cpp.fromVariantPre2 = QStringLiteral("V(");
MemberInformationWrapper w2(mi2, &parent);
QCOMPARE(w2.cppToJsonPre(), QStringLiteral("T("));
QCOMPARE(w2.cppToJsonPost(), QString());
QCOMPARE(w2.cppFromJsonPre2(), QStringLiteral("V("));
QCOMPARE(w2.cppFromJsonPost(), QString());
}
void codegen_unit_test::memberListWrapperFilters()
{
QObject parent;
MemberInformation cppMi = makeMember(QStringLiteral("t"), QStringLiteral("m1"));
cppMi.cpp.usedForCppStruct = true;
MemberInformation jsonMi = makeMember(QStringLiteral("t"), QStringLiteral("m2"));
jsonMi.cpp.usedForJson = true;
MemberInformation fkMi = makeMember(QStringLiteral("t"), QStringLiteral("m3"));
fkMi.db.isForeignKey = true;
MemberInformation selMi = makeMember(QStringLiteral("t"), QStringLiteral("m4"));
selMi.db.generateSelectRemove = true;
MemberInformation viewMi = makeMember(QStringLiteral("t"), QStringLiteral("m5"));
viewMi.db.usedForView = true;
MemberInformation keyMi = makeMember(QStringLiteral("t"), QStringLiteral("m6"));
keyMi.isDdsKey = true;
MemberInformation pkMi = makeMember(QStringLiteral("t"), QStringLiteral("m7"));
pkMi.db.isPrimaryKey = true;
QVector<MemberInformation> members;
members = {cppMi, jsonMi, fkMi, selMi, viewMi, keyMi, pkMi};
MemberInformationListWrapper w(QStringLiteral("members"), members, &parent);
QCOMPARE(w.member().size(), 7);
QCOMPARE(w.cppMember().size(), 1);
QCOMPARE(w.jsonMember().size(), 1);
QCOMPARE(w.fkMember().size(), 1);
QCOMPARE(w.selectRemoveMember().size(), 1);
QCOMPARE(w.viewMember().size(), 1);
MemberInformationWrapper* key = w.ddsKey();
QVERIFY(key != nullptr);
QCOMPARE(key->cppMemberName(), QStringLiteral("m6"));
MemberInformationWrapper* pk = w.primaryKey();
QVERIFY(pk != nullptr);
QCOMPARE(pk->cppMemberName(), QStringLiteral("m7"));
// an empty list has no key and no primary key
MemberInformationListWrapper w2(QStringLiteral("empty"), QVector<MemberInformation>(), &parent);
QCOMPARE(w2.member().size(), 0);
QVERIFY(w2.ddsKey() == nullptr);
QVERIFY(w2.primaryKey() == nullptr);
}
void codegen_unit_test::dataTopicWrapper()
{
QObject parent;
CompleteTopicTypeInformation ctti;
ctti.name = QStringLiteral("Topic");
ctti.serviceName = QStringLiteral("Svc");
DataTopicWrapper w(ctti, &parent);
QCOMPARE(w.name(), QStringLiteral("Topic"));
QCOMPARE(w.serviceName(), QStringLiteral("Svc"));
QCOMPARE(w.type(), QStringLiteral("Topic"));
QCOMPARE(w.typeFq(), QStringLiteral("Svc::Topic"));
QCOMPARE(w.dtsImpl(), true);
QCOMPARE(w.dtcImpl(), true);
QCOMPARE(w.routeToClient(), true);
QCOMPARE(w.routeToServer(), false);
QCOMPARE(w.routeInReplay(), true);
QCOMPARE(w.isAutoSubscribe(), true);
QCOMPARE(w.recordTopic(), false);
QVERIFY(w.recTable().isNull());
QVERIFY(w.key().isNull());
QCOMPARE(w.hasTrColumns(), false);
// an already service-qualified name is not qualified again
ctti.name = QStringLiteral("Svc::Topic");
DataTopicWrapper w2(ctti, &parent);
QCOMPARE(w2.typeFq(), QStringLiteral("Svc::Topic"));
// without a service name the type is returned as-is
CompleteTopicTypeInformation ctti3;
ctti3.name = QStringLiteral("Topic3");
DataTopicWrapper w3(ctti3, &parent);
QCOMPARE(w3.typeFq(), QStringLiteral("Topic3"));
// key column and translatable columns
QSharedPointer<DataTypeInfo> intType(new TestDataTypeInfo(QStringLiteral("int"), QStringLiteral("int"), true));
IdlData keyData;
keyData.name = QStringLiteral("id");
keyData.dataTypeInfo = intType;
w.setKeyColumnData(keyData);
QVERIFY(!w.key().isNull());
QCOMPARE(w.key().value<IdlDataWrapper*>()->name(), QStringLiteral("id"));
IdlData trData;
trData.name = QStringLiteral("tr1");
trData.dataTypeInfo = intType;
QVector<IdlData> trColumns;
trColumns = {trData};
w.setTrColumns(trColumns);
QCOMPARE(w.hasTrColumns(), true);
QCOMPARE(w.trColumns().size(), 1);
}
void codegen_unit_test::functionTopicWrapper()
{
QObject parent;
FunctionTopic ft;
ft.name = QStringLiteral("MyFunc");
ft.requestTopicName = QStringLiteral("ReqT");
ft.replyTopicName = QStringLiteral("RepT");
ft.replyTopicType = QStringLiteral("Core::FunctionReply");
ft.routeInReplay = true;
CompleteTopicTypeInformation req;
req.name = QStringLiteral("ReqType");
CompleteTopicTypeInformation rep;
rep.name = QStringLiteral("RepType");
FunctionTopicWrapper w(ft, req, rep, &parent);
QCOMPARE(w.name(), QStringLiteral("MyFunc"));
QCOMPARE(w.onReply(), QStringLiteral("onRepT"));
QCOMPARE(w.onRequest(), QStringLiteral("onReqT"));
QCOMPARE(w.onError(), QStringLiteral("onMyFuncError"));
QCOMPARE(w.isCoreFt(), true);
QCOMPARE(w.routeInReplay(), true);
QCOMPARE(w.reply(), QStringLiteral("RepT"));
QCOMPARE(w.replyFlc(), QStringLiteral("repT"));
QCOMPARE(w.replyHandler(), QStringLiteral("RepTHandler"));
QCOMPARE(w.replyHandlerFlc(), QStringLiteral("repTHandler"));
QCOMPARE(w.replyType(), QStringLiteral("RepType"));
QCOMPARE(w.replyTypeFro(), QStringLiteral("Core::FunctionReplyObject"));
QCOMPARE(w.request(), QStringLiteral("ReqT"));
QCOMPARE(w.requestFlc(), QStringLiteral("reqT"));
QCOMPARE(w.requestHandler(), QStringLiteral("ReqTHandler"));
QCOMPARE(w.requestHandlerFlc(), QStringLiteral("reqTHandler"));
QCOMPARE(w.requestType(), QStringLiteral("ReqType"));
QCOMPARE(w.replyHandlerHash(), QString());
// a non-core reply type keeps its own type name
ft.replyTopicType = QStringLiteral("SomeReply");
FunctionTopicWrapper w2(ft, req, rep, &parent);
QCOMPARE(w2.isCoreFt(), false);
QCOMPARE(w2.replyTypeFro(), QStringLiteral("RepType"));
}
void codegen_unit_test::idlWrappers()
{
QObject parent;
QSharedPointer<DataTypeInfo> podType(new TestDataTypeInfo(QStringLiteral("int"), QStringLiteral("int"), true));
QSharedPointer<DataTypeInfo> structType(new TestDataTypeInfo(QStringLiteral("Svc::MyStruct"),
QStringLiteral("Svc::MyStruct"), false));
IdlData idl;
idl.name = QStringLiteral("foo");
idl.isKey = true;
idl.isOptional = false;
idl.dataTypeInfo = podType;
IdlDataWrapper w(idl, &parent);
QCOMPARE(w.name(), QStringLiteral("foo"));
QCOMPARE(w.type(), QStringLiteral("int"));
QCOMPARE(w.cppType(), QStringLiteral("int"));
// POD types are passed by value (no const reference)
QCOMPARE(w.cppTypeCr(), QStringLiteral("int"));
QCOMPARE(w.isKey(), QStringLiteral(" \t //@key"));
QCOMPARE(w.isOptional(), QString());
QCOMPARE(w.nameGetter(), QStringLiteral("foo"));
IdlData opt;
opt.name = QStringLiteral("bar");
opt.isOptional = true;
opt.dataTypeInfo = structType;
IdlDataWrapper w2(opt, &parent);
QCOMPARE(w2.cppTypeCr(), QStringLiteral("const Svc::MyStruct &"));
QCOMPARE(w2.isKey(), QString());
QCOMPARE(w2.isOptional(), QStringLiteral(" \t //@Optional"));
// optional members are unwrapped via .get()
QCOMPARE(w2.nameGetter(), QStringLiteral("bar.get()"));
// IdlStructWrapper
CompleteTopicTypeInformation ctti;
ctti.name = QStringLiteral("MyStruct");
ctti.comment = QStringLiteral("struct comment");
ctti.baseValueType = QStringLiteral("BaseType");
IdlData m1;
m1.name = QStringLiteral("a");
m1.dataTypeInfo = podType;
IdlData m2;
m2.name = QStringLiteral("b");
m2.dataTypeInfo = structType;
ctti.members = {m1, m2};
IdlStructWrapper sw(ctti, &parent);
QCOMPARE(sw.name(), QStringLiteral("MyStruct"));
QCOMPARE(sw.comment(), QStringLiteral("struct comment"));
QCOMPARE(sw.baseValueType(), QStringLiteral("BaseType"));
// the default extensibility maps to the same string with and without RTI
QCOMPARE(sw.extensibility(), QStringLiteral("EXTENSIBLE_EXTENSIBILITY"));
QCOMPARE(sw.member().size(), 2);
QCOMPARE(sw.member().value(0).value<IdlDataWrapper*>()->name(), QStringLiteral("a"));
}
void codegen_unit_test::dbStructWrapper()
{
QObject parent;
QSharedPointer<TableInformation> tableA(new TableInformation());
tableA->name = QStringLiteral("tbl_a");
QSharedPointer<TableInformation> tableB(new TableInformation());
tableB->name = QStringLiteral("tbl_b");
CompleteStructInformation csi;
csi.structName = QStringLiteral("MyStruct");
csi.viewName = QStringLiteral("my_view");
csi.tables = {tableA, tableB};
csi.primaryTable = tableA;
MemberInformation pkA = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_id"));
pkA.db.isPrimaryKey = true;
pkA.db.usedForInsert = true;
MemberInformation nameA = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_name"));
nameA.db.usedForInsert = true;
MemberInformation fkB = makeMember(QStringLiteral("tbl_b"), QStringLiteral("b_a_id"));
fkB.db.usedForInsert = true;
fkB.fk = QSharedPointer<ForeignKeyInformation>(new ForeignKeyInformation());
fkB.fk->destination.tableName = QStringLiteral("tbl_a");
csi.member = {pkA, nameA, fkB};
DbNotifcationInformation notif1;
notif1.methodName = QStringLiteral("onInsertA");
DbNotifcationInformation notif2;
notif2.methodName = QStringLiteral("onDeleteA");
csi.dbNotifications = {notif1, notif2};
DbStructWrapper w(csi, &parent);
QCOMPARE(w.name(), QStringLiteral("MyStruct"));
QCOMPARE(w.member().size(), 3);
QCOMPARE(w.deleteWithoutPkAllowed(), false); // two tables
QCOMPARE(w.viewTableName(), QStringLiteral("my_view")); // multi-table -> view name
MemberInformationWrapper* pk = w.primaryKey();
QVERIFY(pk != nullptr);
QCOMPARE(pk->dbColumnName(), QStringLiteral("a_id"));
QCOMPARE(w.dbNotifications().size(), 2);
QCOMPARE(w.dbNotifications().value(0).toString(), QStringLiteral("onInsertA"));
QCOMPARE(w.dbNotifications().value(1).toString(), QStringLiteral("onDeleteA"));
// a single-table struct uses the primary table name as view name
CompleteStructInformation csiSingle;
csiSingle.structName = QStringLiteral("Single");
csiSingle.viewName = QStringLiteral("unused");
csiSingle.tables = {tableA};
csiSingle.primaryTable = tableA;
MemberInformation soloPk = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_id"));
soloPk.db.isPrimaryKey = true;
soloPk.db.hasSequence = true;
soloPk.db.usedForInsert = true;
csiSingle.member = {soloPk};
DbStructWrapper wSingle(csiSingle, &parent);
QCOMPARE(wSingle.deleteWithoutPkAllowed(), true);
QCOMPARE(wSingle.viewTableName(), QStringLiteral("tbl_a"));
// insertStructures groups the members per table (two tables)
QVariantList structures = w.insertStructures();
QCOMPARE(structures.size(), 2);
TableWrapper* twA = structures.value(0).value<TableWrapper*>();
TableWrapper* twB = structures.value(1).value<TableWrapper*>();
QVERIFY(twA != nullptr);
QVERIFY(twB != nullptr);
QCOMPARE(twA->name(), QStringLiteral("tbl_a"));
QCOMPARE(twA->member().size(), 2);
QVERIFY(twA->primaryKey() != nullptr);
QCOMPARE(twA->primaryKey()->dbColumnName(), QStringLiteral("a_id"));
// first of several tables carries the base id
QCOMPARE(twA->baseId()->dbColumnName(), QStringLiteral("a_id"));
// the FK member pointing to tbl_a becomes its serial member
QCOMPARE(twA->serialMember()->dbColumnName(), QStringLiteral("b_a_id"));
QCOMPARE(twB->name(), QStringLiteral("tbl_b"));
QCOMPARE(twB->member().size(), 1);
QVERIFY(twB->primaryKey() == nullptr);
QVERIFY(twB->baseId() == nullptr);
QVERIFY(twB->serialMember() == nullptr);
// insertStructures for a single table with a sequence primary key
QVariantList single = wSingle.insertStructures();
QCOMPARE(single.size(), 1);
TableWrapper* twSolo = single.value(0).value<TableWrapper*>();
QVERIFY(twSolo != nullptr);
QCOMPARE(twSolo->name(), QStringLiteral("tbl_a"));
QVERIFY(twSolo->primaryKey() != nullptr);
// the sequence PK is used as the serial member
QCOMPARE(twSolo->serialMember()->dbColumnName(), QStringLiteral("a_id"));
QVERIFY(twSolo->baseId() == nullptr);
}
void codegen_unit_test::codegenBaseDataClasses()
{
// StructBuilder::ServiceTypeStruct
StructBuilder::ServiceTypeStruct sts;
sts.service = QStringLiteral("Svc");
sts.type = QStringLiteral("Type");
sts.bIsSequence = true;
QCOMPARE(sts.isExternalType(), true);
QCOMPARE(sts.isSequence(), true);
QCOMPARE(sts.fqType(), QStringLiteral("Svc::Type"));
StructBuilder::ServiceTypeStruct local;
local.type = QStringLiteral("Plain");
QCOMPARE(local.isExternalType(), false);
// SqlGenerator::SqlStatements::isEmpty only considers the DROP statements
SqlGenerator::SqlStatements stmts;
QCOMPARE(stmts.isEmpty(), true);
stmts.createTables.append(QStringLiteral("CREATE TABLE t()"));
QCOMPARE(stmts.isEmpty(), true);
stmts.dropTables.append(QStringLiteral("DROP TABLE t"));
QCOMPARE(stmts.isEmpty(), false);
// EnumInformation
EnumInformation ei;
QCOMPARE(ei.isValid(), false);
ei.enumName = QStringLiteral("E");
QCOMPARE(ei.isValid(), true);
// MemberInformation isEmpty / toString (CSV format)
MemberInformation mi;
QCOMPARE(mi.isEmpty(), true);
mi.ea.table = QStringLiteral("t");
mi.ea.column = QStringLiteral("c");
mi.ea.type = QStringLiteral("int");
mi.db.table = QStringLiteral("t");
mi.db.columnName = QStringLiteral("c");
mi.cpp.memberName = QStringLiteral("c");
QCOMPARE(mi.isEmpty(), false);
QCOMPARE(mi.toString(),
QStringLiteral("t;c;int;t;c;;false;false;false;false;;c;false;false;;;;;;"));
// CompleteTopicTypeInformation
CompleteTopicTypeInformation ctti;
QCOMPARE(ctti.isValid(), false); // no name
ctti.name = QStringLiteral("S");
QCOMPARE(ctti.isValid(), false); // still no members
ctti.baseValueType = QStringLiteral("B");
QCOMPARE(ctti.hasBaseValue(), true);
IdlData m;
m.name = QStringLiteral("x");
m.dataTypeInfo = QSharedPointer<DataTypeInfo>(new TestDataTypeInfo(QStringLiteral("int"),
QStringLiteral("int"), true));
ctti.members = {m};
QCOMPARE(ctti.isValid(), true);
// CompleteTableInformation / CompleteStructInformation
CompleteTableInformation cti;
QCOMPARE(cti.isEmpty(), true);
MemberInformation col;
col.ea.column = QStringLiteral("c1");
cti.columns = {col};
QCOMPARE(cti.isEmpty(), false);
CompleteStructInformation csi;
QCOMPARE(csi.isEmpty(), true);
QSharedPointer<TableInformation> ti(new TableInformation());
ti->name = QStringLiteral("t");
csi.primaryTable = ti;
QCOMPARE(csi.isEmpty(), false);
}
QTEST_MAIN(codegen_unit_test)
#include "codegen_unit_test.moc"
@@ -28,3 +28,8 @@ NGL_TARGET_LINK_LIBRARIES(LtsCoordinate
)
ADD_SUBDIRECTORY(UnitTest_Coordinate)
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,20 @@
#
# Unit tests for the LtsCoordinate module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- LtsCoordinate: geo<->UTM conversion, MGRS formatting, distances, angles,
# --- elevation callback and batch LtsCoordVector conversion
if (TARGET LtsCoordinate)
ADD_EXECUTABLE(ltscoordinate_unit_test ltscoordinate_unit_test.cpp)
TARGET_LINK_LIBRARIES(ltscoordinate_unit_test
LtsCoordinate
LtsCoordTrans
Qt6::Core
Qt6::Test
)
ADD_TEST(NAME ltscoordinate_unit_test COMMAND $<TARGET_FILE:ltscoordinate_unit_test>)
endif()
@@ -0,0 +1,314 @@
//
// Unit tests for the LtsCoordinate module (Lts/Common/LtsCoordinate):
// LtsCoordinate (geo<->UTM conversion, MGRS formatting, distances, angles,
// elevation callback, conversion counter) and LtsCoordVector (batch
// geo->UTM conversion with zone caching).
//
// Note: geographic coordinates are radians everywhere (Core::GeoPosition
// and LtsCoordTrans::deg2rad/rad2deg), mirroring the LtsCoordTrans tests.
//
#include <LtsCoordinate/lts_coordinate.h>
#include <LtsCoordinate/lts_coord_vector.h>
#include <LtsCoordTrans/lts_coord_trans.h>
#include <Core/core.hpp>
#include <QtTest/QTest>
#include <QtCore/QPointF>
#include <cmath>
#include <vector>
using namespace LtsCoordTrans;
class ltscoordinate_unit_test : public QObject
{
Q_OBJECT
private slots:
void cleanupTestCase();
void constructorsAndGeoAccessors();
void validityChecks();
void valueIsNullOrNan();
void mgrsFormatting();
void geoUtmRoundtrip();
void fromUtm();
void utmCoordinateDiffZone();
void distanceUtm2D();
void distanceUtm3D();
void angleDegreeUtm2D();
void elevationCallback();
void conversionCounter();
void ltsCoordVector();
};
// The elevation callback is a static (process-wide) of LtsCoordinate —
// reset it so other tests in the suite are not affected.
void ltscoordinate_unit_test::cleanupTestCase()
{
LtsCoordinate::setElevationCallback(nullptr);
}
void ltscoordinate_unit_test::constructorsAndGeoAccessors()
{
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat, 42.5);
QCOMPARE(coord.geoPosition().lon, lon);
QCOMPARE(coord.geoPosition().lat, lat);
QCOMPARE(coord.z(), 42.5);
coord.setZ(1.0);
QCOMPARE(coord.z(), 1.0);
const QPointF pnt = coord.toGeoPointF();
QCOMPARE(pnt.x(), lon);
QCOMPARE(pnt.y(), lat);
const LtsCoordinate fromPnt(QPointF(lon, lat), 7.0);
QCOMPARE(fromPnt.z(), 7.0);
const LtsCoordinate def;
QVERIFY(std::isnan(def.z()));
const LtsCoordinate fromGeo = LtsCoordinate::fromGeoPosition(lon, lat, 3.0);
QCOMPARE(fromGeo.z(), 3.0);
const Core::GeoPosition2D geo2D(lon, lat);
const LtsCoordinate from2D(geo2D);
QCOMPARE(from2D.geoPosition().lon, lon);
QCOMPARE(from2D.geoPosition2D().lat, lat);
}
void ltscoordinate_unit_test::validityChecks()
{
// (0,0) is "null" for validity purposes (qFuzzyIsNull), not a real position
LtsCoordinate def;
QVERIFY(!def.hasGeo());
QVERIFY(!def.hasUtm());
QVERIFY(!def.isValid());
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate valid(lon, lat);
QVERIFY(valid.hasGeo());
QVERIFY(valid.isValid());
QVERIFY(!valid.hasUtm()); // not converted yet
(void)valid.utmCoordinate();
QVERIFY(valid.hasUtm());
QVERIFY(valid.utmCoordinate().zone > 0);
}
void ltscoordinate_unit_test::valueIsNullOrNan()
{
QVERIFY(LtsCoordinate::valueIsNullOrNan(0.0));
QVERIFY(LtsCoordinate::valueIsNullOrNan(std::nan("")));
QVERIFY(!LtsCoordinate::valueIsNullOrNan(1.0));
QVERIFY(!LtsCoordinate::valueIsNullOrNan(-1e-6));
}
// Known MGRS values, mirrored from the LtsCoordTrans test suite through the
// LtsCoordinate facade.
void ltscoordinate_unit_test::mgrsFormatting()
{
const LtsCoordinate coord(8.6264954126 * deg2rad(), 53.1138437183 * deg2rad());
QCOMPARE(coord.mgrs(0), QStringLiteral("32U MD"));
QCOMPARE(coord.mgrs(8), QStringLiteral("32U MD 74999999 84999999"));
// no-space form is the concatenation of the space form for this coordinate
QCOMPARE(coord.mgrs(8, false), QStringLiteral("32UMD7499999984999999"));
const LtsCoordinate coord2(11.3003696521 * deg2rad(), 52.8224535191 * deg2rad());
QCOMPARE(coord2.mgrs(0), QStringLiteral("32U PD"));
QCOMPARE(coord2.mgrs(5), QStringLiteral("32U PD 55000 55000"));
const LtsCoordinate coord3(54.8821607021 * deg2rad(), 24.2631973752 * deg2rad());
QCOMPARE(coord3.mgrs(0), QStringLiteral("40R BM"));
QCOMPARE(coord3.mgrs(5), QStringLiteral("40R BM 85000 85000"));
// Without an elevation callback the z part is NaN
const QString withZ = coord.mgrsWithZ(0);
QVERIFY(withZ.startsWith(QStringLiteral("32U MD z=")));
}
void ltscoordinate_unit_test::geoUtmRoundtrip()
{
const double lonDeg = 8.6264954126;
const double latDeg = 53.1138437183;
LtsCoordinate coord(lonDeg * deg2rad(), latDeg * deg2rad(), 5.0);
const UtmCoordinate utm = coord.utmCoordinate();
QVERIFY(utm.zone > 0);
const QPointF geoBack = utm.geographic();
QVERIFY(std::abs(geoBack.x() * rad2deg() - lonDeg) < 1e-6);
QVERIFY(std::abs(geoBack.y() * rad2deg() - latDeg) < 1e-6);
// toUtmPointF / toUtmPoint3D agree with the cached UTM coordinate
const QPointF utmPnt = coord.toUtmPointF();
QVERIFY(std::abs(utmPnt.x() - utm.x) < 1e-6);
QVERIFY(std::abs(utmPnt.y() - utm.y) < 1e-6);
const LtsUtils::QPoint3D utm3D = coord.toUtmPoint3D();
QCOMPARE(utm3D.z(), 5.0);
}
void ltscoordinate_unit_test::fromUtm()
{
const LtsCoordinate c1 = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 10.0);
QCOMPARE(c1.utmCoordinate().zone, 32);
QVERIFY(std::abs(c1.utmCoordinate().x - 500000.) < 0.01);
QVERIFY(std::abs(c1.utmCoordinate().y - 5000000.) < 0.01);
QCOMPARE(c1.z(), 10.0);
const UtmCoordinate utm(32, 500123., 5000456., true);
const LtsCoordinate c2 = LtsCoordinate::fromUtmCoordinate(utm, 20.0);
QCOMPARE(c2.z(), 20.0);
QVERIFY(std::abs(c2.utmCoordinate().x - 500123.) < 0.01);
QVERIFY(std::abs(c2.utmCoordinate().y - 5000456.) < 0.01);
const UtmCoordinate3D c3 = c1.toUtmCoordinate3D();
QCOMPARE(c3.z(), 10.0);
}
void ltscoordinate_unit_test::utmCoordinateDiffZone()
{
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat);
const UtmCoordinate in31 = coord.utmCoordinateDiffZone(31);
const UtmCoordinate in32 = coord.utmCoordinateDiffZone(32);
QCOMPARE(in31.zone, 31);
QCOMPARE(in32.zone, 32);
QVERIFY(in31.x != in32.x);
// Same zone: diff-zone result matches the default conversion
const UtmCoordinate defaultZone = coord.utmCoordinate();
const UtmCoordinate sameZone = coord.utmCoordinateDiffZone(defaultZone.zone);
QCOMPARE(sameZone.x, defaultZone.x);
QCOMPARE(sameZone.y, defaultZone.y);
}
void ltscoordinate_unit_test::distanceUtm2D()
{
const LtsCoordinate a = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 0.);
const LtsCoordinate b = LtsCoordinate::fromUtm(QPointF(500100., 5000000.), 32, 0.);
QVERIFY(std::abs(a.distanceUtm2D(b) - 100.0) < 1e-3);
QVERIFY(std::abs(a.distanceUtm2D(a)) < 1e-6);
QVERIFY(std::abs(a.distanceUtm2D(b) - b.distanceUtm2D(a)) < 1e-9);
}
void ltscoordinate_unit_test::distanceUtm3D()
{
const LtsCoordinate a = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 0.);
const LtsCoordinate b = LtsCoordinate::fromUtm(QPointF(500100., 5000000.), 32, 40.);
const double expected = std::hypot(100., 40.);
QVERIFY(std::abs(a.distanceUtm3D(b) - expected) < 1e-3);
}
void ltscoordinate_unit_test::angleDegreeUtm2D()
{
const LtsCoordinate a = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 0.);
const LtsCoordinate east = LtsCoordinate::fromUtm(QPointF(500100., 5000000.), 32, 0.);
const LtsCoordinate north = LtsCoordinate::fromUtm(QPointF(500000., 5000100.), 32, 0.);
// Conventions: QLineF angle (0 = +x) shifted by +90 degrees
QVERIFY(std::abs(LtsCoordinate::angleDegreeUtm2D(a, east) - 90.0) < 0.1);
QVERIFY(std::abs(LtsCoordinate::angleDegreeUtm2D(a, north) - 0.0) < 0.1);
}
void ltscoordinate_unit_test::elevationCallback()
{
LtsCoordinate::setElevationCallback([](double, double, double, bool* covered) {
if (covered)
*covered = true;
return 123.45;
});
QVERIFY(LtsCoordinate::hasElevationCallback());
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat);
bool covered = false;
QCOMPARE(coord.elevationAt(0.0, &covered), 123.45);
QVERIFY(covered);
QCOMPARE(LtsCoordinate::elevationAt(coord), 123.45);
coord.setZValueAccordingElevation();
QCOMPARE(coord.z(), 123.45);
QVERIFY(std::abs(coord.heightOverElevation()) < 1e-9);
const LtsCoordinate above(lon, lat, 200.0);
QVERIFY(std::abs(above.heightOverElevation() - (200.0 - 123.45)) < 1e-9);
// replaceZValueByElevationIfValue: NaN z is replaced
LtsCoordinate nanZ(lon, lat);
QVERIFY(std::isnan(nanZ.z()));
nanZ.replaceZValueByElevationIfValue();
QCOMPARE(nanZ.z(), 123.45);
// matching z is replaced, non-matching z is left alone
LtsCoordinate z77(lon, lat, 77.0);
z77.replaceZValueByElevationIfValue(77.0);
QCOMPARE(z77.z(), 123.45);
z77.replaceZValueByElevationIfValue(99.0);
QCOMPARE(z77.z(), 123.45);
// mgrsWithZ now carries the elevation
const QString withZ = coord.mgrsWithZ(0);
QVERIFY(withZ.startsWith(QStringLiteral("32U MD z=123.45")));
}
void ltscoordinate_unit_test::conversionCounter()
{
LtsCoordinate::resetConversionCounter();
QCOMPARE(LtsCoordinate::getConversionCounter(), 0L);
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat);
(void)coord.utmCoordinate();
// The counter is only incremented in LTS_DEBUG builds (the pipeline
// driver defines it: BUILD_RPM is not set)
QVERIFY(LtsCoordinate::getConversionCounter() >= 1L);
}
void ltscoordinate_unit_test::ltsCoordVector()
{
std::vector<Core::GeoPosition> geos;
geos.emplace_back(8.6264954126 * deg2rad(), 53.1138437183 * deg2rad(), 1.0);
geos.emplace_back(11.3003696521 * deg2rad(), 52.8224535191 * deg2rad(), 2.0);
// (14.0 E, 52.0 N) - in UTM zone 32 like the others (toStdUtmCoord(32))
geos.emplace_back(14.0 * deg2rad(), 52.0 * deg2rad(), 3.0);
const LtsCoordVector vec(geos);
QCOMPARE(vec.size(), 3);
QCOMPARE(vec.geoPosition().size(), 3u);
QCOMPARE(vec.geoPosition()[1].lat, geos[1].lat);
const std::vector<UtmCoordinate> utmStd = vec.toStdUtmCoord(32);
QCOMPARE(utmStd.size(), 3u);
for (const auto& u : utmStd)
QCOMPARE(u.zone, 32);
const QVector<UtmCoordinate> utmQv = vec.toQVectorUtmCoord(32);
QCOMPARE(utmQv.size(), 3);
QCOMPARE(utmQv[0].x, utmStd[0].x);
const QVector<QPointF> pts = vec.toQVectorQPointF(32);
QCOMPARE(pts.size(), 3);
for (const QPointF& p : pts)
QVERIFY(std::isfinite(p.x()) && std::isfinite(p.y()));
// Per-zone cache: repeated calls return the same values
const QVector<QPointF> again = vec.toQVectorQPointF(32);
QCOMPARE(again, pts);
}
QTEST_MAIN(ltscoordinate_unit_test)
#include "ltscoordinate_unit_test.moc"
@@ -156,3 +156,8 @@ IF(BUILD_RTI)
)
TARGET_COMPILE_DEFINITIONS(LtsSessionData PUBLIC BUILD_RTI)
ENDIF()
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,19 @@
#
# Unit tests for the LtsSession module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- LtsSession: LtsSessionData (session state singleton - pure data logic)
if (TARGET LtsSession)
ADD_EXECUTABLE(ltssession_unit_test ltssession_unit_test.cpp)
TARGET_LINK_LIBRARIES(ltssession_unit_test
LtsSessionData
Qt6::Core
Qt6::Gui
Qt6::Test
)
ADD_TEST(NAME ltssession_unit_test COMMAND $<TARGET_FILE:ltssession_unit_test>)
endif()
@@ -0,0 +1,321 @@
//
// Unit tests for the LtsSession module: LtsSessionData (the module's pure
// data class - session state singleton, presets, operator rights, visible
// parties and debug flags). Compiled into the LtsSessionData library target.
//
#include <LtsSession/lts_session_data.h>
#include <QtTest/QTest>
#include <QtGui/QPixmap>
#include <QtCore/QSet>
#include <QtCore/QUrl>
using LtsSessionData::PartiesByExercise;
using SessionData = LtsSessionData::LtsSessionData;
class ltssession_unit_test : public QObject
{
Q_OBJECT
private slots:
void singletonIdentity();
void constructorDefaults();
void operatorRightChecks();
void presetsRoundtrip();
void removePresetsKeepsDomainId();
void sessionFieldsRoundtrip();
void loggedOut();
void visibleParties();
void debugFlags();
void clearScope();
private:
// LtsSessionData is a process-wide singleton and clear() only resets the
// preset + operator state. Bring it back to a known baseline before each
// slot so the slots do not depend on execution order.
void resetSessionData();
};
void ltssession_unit_test::resetSessionData()
{
auto* data = SessionData::getInstance();
data->clear();
// fields that clear() intentionally does not reset:
data->setDomainIdPreset(-1);
data->setQosFile(QStringList());
data->setScriptId(0);
data->setWorkstationId(-1);
data->setRequestedAppId(LtsAppDescriptor::App::Login);
data->setTcpConnectionUrl(QUrl());
data->setAsTablet(false);
data->setPlaceScriptScreens(false);
for (const QString& key : data->debugFlags().keys())
data->removeDebugFlag(key);
}
void ltssession_unit_test::singletonIdentity()
{
resetSessionData();
auto* data = SessionData::getInstance();
QVERIFY(data != nullptr);
// singleton: same instance on every call
QCOMPARE(SessionData::getInstance(), data);
}
void ltssession_unit_test::constructorDefaults()
{
resetSessionData();
auto* data = SessionData::getInstance();
// presets
QCOMPARE(data->getDomainIdPreset(), -1);
QCOMPARE(data->getPartitionIdPreset(), -1);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Any);
QVERIFY(data->getOperatorNamePreset().isEmpty());
QVERIFY(data->getPasswordPreset().isEmpty());
QVERIFY(data->getQosFile().isEmpty());
// session state
QCOMPARE(data->getOperatorId(), -1);
QCOMPARE(data->getScriptId(), 0);
QVERIFY(data->getOperatorName().isEmpty());
QVERIFY(data->getOperatorRights().isEmpty());
QVERIFY(!data->getShowTerminalWindow());
QVERIFY(data->getProfilePicture().isNull());
QVERIFY(data->getVisibleParties().isEmpty());
QVERIFY(!data->getTcpConnectionUrl().isValid());
QCOMPARE(data->getRequestedAppId(), LtsAppDescriptor::App::Login);
QCOMPARE(data->getWorkstationId(), -1);
QVERIFY(!data->isTablet());
QVERIFY(!data->getPlaceScriptScreens());
QVERIFY(data->debugFlags().isEmpty());
}
void ltssession_unit_test::operatorRightChecks()
{
resetSessionData();
auto* data = SessionData::getInstance();
// fresh state: only right 0 is granted
QVERIFY(data->hasOperatorRight(0));
QVERIFY(!data->hasOperatorRight(1));
QVERIFY(!data->hasOperatorRight(-1));
// explicit rights
data->setOperatorRights(QSet<int>{1, 2});
QVERIFY(data->hasOperatorRight(1));
QVERIFY(data->hasOperatorRight(2));
QVERIFY(!data->hasOperatorRight(3));
QVERIFY(data->hasOperatorRight(0)); // 0 is always granted
// -1 means "all rights"
data->setOperatorRights(QSet<int>{-1});
QVERIFY(data->hasOperatorRight(42));
QVERIFY(data->hasOperatorRight(12345));
QVERIFY(data->hasOperatorRight(0));
}
void ltssession_unit_test::presetsRoundtrip()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setOperatorPreset(QStringLiteral("operator-name"), QStringLiteral("secret"));
QCOMPARE(data->getOperatorNamePreset(), QStringLiteral("operator-name"));
QCOMPARE(data->getPasswordPreset(), QStringLiteral("secret"));
data->setDomainIdPreset(7);
QCOMPARE(data->getDomainIdPreset(), 7);
data->setPartitionPreset(3, IDL::PartitionType::Exercise);
QCOMPARE(data->getPartitionIdPreset(), 3);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Exercise);
const QStringList expectedQos{QStringLiteral("a.qos"), QStringLiteral("b.qos")};
data->setQosFile(expectedQos);
QCOMPARE(data->getQosFile(), expectedQos);
}
void ltssession_unit_test::removePresetsKeepsDomainId()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setOperatorPreset(QStringLiteral("op"), QStringLiteral("pw"));
data->setDomainIdPreset(7);
data->setPartitionPreset(3, IDL::PartitionType::Global);
data->removePresets();
QCOMPARE(data->getPartitionIdPreset(), -1);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Any);
QVERIFY(data->getOperatorNamePreset().isEmpty());
QVERIFY(data->getPasswordPreset().isEmpty());
// the domain id preset is kept on purpose
QCOMPARE(data->getDomainIdPreset(), 7);
}
void ltssession_unit_test::sessionFieldsRoundtrip()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setScriptId(5);
QCOMPARE(data->getScriptId(), 5);
data->setOperatorId(42);
QCOMPARE(data->getOperatorId(), 42);
data->setOperatorName(QStringLiteral("Max Mustermann"));
QCOMPARE(data->getOperatorName(), QStringLiteral("Max Mustermann"));
data->setShowTerminalWindow(true);
QVERIFY(data->getShowTerminalWindow());
data->setTcpConnectionUrl(QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
QCOMPARE(data->getTcpConnectionUrl(), QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
data->setRequestedAppId(LtsAppDescriptor::App::Situation);
QCOMPARE(data->getRequestedAppId(), LtsAppDescriptor::App::Situation);
data->setWorkstationId(11);
QCOMPARE(data->getWorkstationId(), 11);
data->setPlaceScriptScreens(true);
QVERIFY(data->getPlaceScriptScreens());
// a null pixmap is a valid (unfilled) profile picture in console mode
data->setProfilePicture(QPixmap());
QVERIFY(data->getProfilePicture().isNull());
}
void ltssession_unit_test::loggedOut()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setScriptId(9); // not part of the logged-out state
data->setOperatorId(5);
data->setOperatorName(QStringLiteral("op"));
data->setOperatorRights(QSet<int>{1});
data->setShowTerminalWindow(true);
PartiesByExercise parties;
parties.exerciseId = 1;
parties.visibleParties = QVector<int>{10};
data->setVisibleParties(QVector<PartiesByExercise>{parties});
data->loggedOut();
QCOMPARE(data->getOperatorId(), -1);
QVERIFY(data->getOperatorName().isEmpty());
QVERIFY(data->getOperatorRights().isEmpty());
QVERIFY(!data->getShowTerminalWindow());
QVERIFY(data->getVisibleParties().isEmpty());
QVERIFY(data->getProfilePicture().isNull());
// unrelated fields are left untouched
QCOMPARE(data->getScriptId(), 9);
}
void ltssession_unit_test::visibleParties()
{
resetSessionData();
auto* data = SessionData::getInstance();
PartiesByExercise exercise1;
exercise1.exerciseId = 1;
exercise1.visibleParties = QVector<int>{10, 11};
PartiesByExercise exercise2;
exercise2.exerciseId = 2;
exercise2.visibleParties = QVector<int>{20};
data->setVisibleParties(QVector<PartiesByExercise>{exercise1, exercise2});
QVERIFY(data->isVisibleParty(1, 10));
QVERIFY(data->isVisibleParty(1, 11));
QVERIFY(!data->isVisibleParty(1, 12));
QVERIFY(data->isVisibleParty(2, 20));
QVERIFY(!data->isVisibleParty(2, 10));
QVERIFY(!data->isVisibleParty(99, 10)); // unknown exercise
const QVector<int> expectedParties1{10, 11};
QCOMPARE(data->getVisibleParties(1), expectedParties1);
QCOMPARE(data->getVisibleParties(2), QVector<int>{20});
QVERIFY(data->getVisibleParties(99).isEmpty());
const auto& all = data->getVisibleParties();
QCOMPARE(static_cast<int>(all.size()), 2);
QCOMPARE(all[0].exerciseId, 1);
QCOMPARE(all[1].exerciseId, 2);
}
void ltssession_unit_test::debugFlags()
{
resetSessionData();
auto* data = SessionData::getInstance();
QVERIFY(!data->hasDebugFlag(QStringLiteral("flag")));
QVERIFY(data->debugFlagValue(QStringLiteral("flag")).isNull());
data->setDebugFlagValue(QStringLiteral("flag"), 42);
QVERIFY(data->hasDebugFlag(QStringLiteral("flag")));
QCOMPARE(data->debugFlagValue(QStringLiteral("flag")).toInt(), 42);
// values are overwritten, not accumulated
data->setDebugFlagValue(QStringLiteral("flag"), QStringLiteral("value"));
QCOMPARE(data->debugFlagValue(QStringLiteral("flag")).toString(), QStringLiteral("value"));
QCOMPARE(static_cast<int>(data->debugFlags().size()), 1);
data->setDebugFlagValue(QStringLiteral("other"), 7);
QCOMPARE(static_cast<int>(data->debugFlags().size()), 2);
data->removeDebugFlag(QStringLiteral("flag"));
QVERIFY(!data->hasDebugFlag(QStringLiteral("flag")));
QVERIFY(data->debugFlagValue(QStringLiteral("flag")).isNull());
QCOMPARE(static_cast<int>(data->debugFlags().size()), 1);
data->removeDebugFlag(QStringLiteral("other"));
QVERIFY(data->debugFlags().isEmpty());
// removing an unknown flag is a no-op
data->removeDebugFlag(QStringLiteral("unknown"));
QVERIFY(data->debugFlags().isEmpty());
}
void ltssession_unit_test::clearScope()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setScriptId(9);
data->setWorkstationId(3);
data->setTcpConnectionUrl(QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
data->setPlaceScriptScreens(true);
data->setDebugFlagValue(QStringLiteral("flag"), 1);
data->setOperatorId(5);
data->setOperatorName(QStringLiteral("op"));
data->setOperatorRights(QSet<int>{1});
data->setDomainIdPreset(7);
data->setPartitionPreset(3, IDL::PartitionType::Exercise);
data->clear();
// operator state and presets are gone
QCOMPARE(data->getOperatorId(), -1);
QVERIFY(data->getOperatorName().isEmpty());
QVERIFY(data->getOperatorRights().isEmpty());
QCOMPARE(data->getPartitionIdPreset(), -1);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Any);
QVERIFY(data->getOperatorNamePreset().isEmpty());
QVERIFY(data->getPasswordPreset().isEmpty());
// everything else is preserved by clear()
QCOMPARE(data->getDomainIdPreset(), 7);
QCOMPARE(data->getScriptId(), 9);
QCOMPARE(data->getWorkstationId(), 3);
QCOMPARE(data->getTcpConnectionUrl(), QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
QVERIFY(data->getPlaceScriptScreens());
QVERIFY(data->hasDebugFlag(QStringLiteral("flag")));
}
QTEST_MAIN(ltssession_unit_test)
#include "ltssession_unit_test.moc"