Utils: Add unit tests for Id

Change-Id: Id471072f79ad0cdfbbc0197b437ac16e5d17386b
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Orgad Shaneh
2024-09-21 22:38:58 +03:00
committed by Orgad Shaneh
parent ba54e05e58
commit 8676397303
5 changed files with 92 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ add_subdirectory(filepath)
add_subdirectory(fileutils)
add_subdirectory(fsengine)
add_subdirectory(fuzzymatcher)
add_subdirectory(id)
add_subdirectory(indexedcontainerproxyconstiterator)
add_subdirectory(mathutils)
add_subdirectory(multicursor)

View File

@@ -0,0 +1,4 @@
add_qtc_test(tst_utils_id
DEPENDS Utils
SOURCES tst_id.cpp
)

View File

@@ -0,0 +1,7 @@
import qbs
QtcAutotest {
name: "Id autotest"
Depends { name: "Utils" }
files: "tst_id.cpp"
}

View File

@@ -0,0 +1,79 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QtTest>
#include <utils/id.h>
QT_BEGIN_NAMESPACE
namespace QTest {
template<>
char *toString(const Utils::Id &id)
{
return qstrdup(id.name().constData());
}
} // namespace QTest
QT_END_NAMESPACE
namespace Utils {
class tst_id : public QObject
{
Q_OBJECT
private slots:
void isValid();
void withPrefix();
void withSuffix();
void compare();
void suffixAfter();
};
void tst_id::isValid()
{
QVERIFY(!Id().isValid());
QVERIFY(!Id::fromSetting("").isValid());
QVERIFY(Id::fromSetting("a").isValid());
QVERIFY(Id::generate().isValid());
}
void tst_id::withPrefix()
{
QCOMPARE(Id("bar").withPrefix("foo"), Id("foobar"));
}
void tst_id::withSuffix()
{
Id id("foo");
QCOMPARE(id.withSuffix(42), Id("foo42"));
QCOMPARE(id.withSuffix('l'), Id("fool"));
QCOMPARE(id.withSuffix("lish"), Id("foolish"));
QCOMPARE(id.withSuffix(QStringView(QString("bar"))), Id("foobar"));
}
void tst_id::compare()
{
Id compId("blubb");
Id runId = Id::fromSetting("blubb");
QCOMPARE(compId, runId);
QCOMPARE(qHash(compId), qHash(runId));
QCOMPARE(compId.name(), "blubb");
QCOMPARE(runId, "blubb");
QVERIFY(compId == runId);
QVERIFY(compId == "blubb");
QVERIFY(runId == "blubb");
QVERIFY(Id("bar") < Id("foo"));
QVERIFY(Id("foo") > Id("bar"));
QVERIFY(!(Id("foo") < Id("bar")));
}
void tst_id::suffixAfter()
{
QCOMPARE(Id("foobar").suffixAfter(Id("foo")), "bar");
}
} // Utils
QTEST_GUILESS_MAIN(Utils::tst_id)
#include "tst_id.moc"

View File

@@ -12,6 +12,7 @@ Project {
"fileutils/fileutils.qbs",
"fsengine/fsengine.qbs",
"fuzzymatcher/fuzzymatcher.qbs",
"id/id.qbs",
"indexedcontainerproxyconstiterator/indexedcontainerproxyconstiterator.qbs",
"mathutils/mathutils.qbs",
"multicursor/multicursor.qbs",