Browse Source

Redesign Version class

PR #17484.
adaptive-webui-19844
Chocobo1 2 years ago committed by GitHub
parent
commit
33e6ca6778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/base/search/searchpluginmanager.cpp
  2. 2
      src/base/search/searchpluginmanager.h
  3. 11
      src/base/utils/foreignapps.cpp
  4. 2
      src/base/utils/foreignapps.h
  5. 152
      src/base/utils/version.h
  6. 28
      src/gui/programupdater.cpp
  7. 4
      src/gui/utils.cpp
  8. 2
      src/webui/webapplication.h
  9. 145
      test/testutilsversion.cpp

4
src/base/search/searchpluginmanager.cpp

@ -528,7 +528,7 @@ void SearchPluginManager::parseVersionInfo(const QByteArray &info)
if (list.size() != 2) continue; if (list.size() != 2) continue;
const auto pluginName = QString::fromUtf8(list.first().trimmed()); const auto pluginName = QString::fromUtf8(list.first().trimmed());
const PluginVersion version = PluginVersion::tryParse(list.last().trimmed(), {}); const auto version = PluginVersion::fromString(QString::fromLatin1(list.last().trimmed()));
if (!version.isValid()) continue; if (!version.isValid()) continue;
@ -577,7 +577,7 @@ PluginVersion SearchPluginManager::getPluginVersion(const Path &filePath)
if (!line.startsWith(u"#VERSION:", Qt::CaseInsensitive)) continue; if (!line.startsWith(u"#VERSION:", Qt::CaseInsensitive)) continue;
const QString versionStr = line.mid(9); const QString versionStr = line.mid(9);
const PluginVersion version = PluginVersion::tryParse(versionStr, {}); const auto version = PluginVersion::fromString(versionStr);
if (version.isValid()) if (version.isValid())
return version; return version;

2
src/base/search/searchpluginmanager.h

@ -36,7 +36,7 @@
#include "base/path.h" #include "base/path.h"
#include "base/utils/version.h" #include "base/utils/version.h"
using PluginVersion = Utils::Version<short, 2>; using PluginVersion = Utils::Version<2>;
Q_DECLARE_METATYPE(PluginVersion) Q_DECLARE_METATYPE(PluginVersion)
namespace Net namespace Net

11
src/base/utils/foreignapps.cpp

@ -72,16 +72,11 @@ namespace
// So trim off unrelated characters // So trim off unrelated characters
const auto versionStr = QString::fromLocal8Bit(outputSplit[1]); const auto versionStr = QString::fromLocal8Bit(outputSplit[1]);
const int idx = versionStr.indexOf(QRegularExpression(u"[^\\.\\d]"_qs)); const int idx = versionStr.indexOf(QRegularExpression(u"[^\\.\\d]"_qs));
const auto version = PythonInfo::Version::fromString(versionStr.left(idx));
try if (!version.isValid())
{
info = {exeName, versionStr.left(idx)};
}
catch (const RuntimeError &)
{
return false; return false;
}
info = {exeName, version};
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python detected, executable name: '%1', version: %2") LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python detected, executable name: '%1', version: %2")
.arg(info.executableName, info.version.toString()), Log::INFO); .arg(info.executableName, info.version.toString()), Log::INFO);
return true; return true;

2
src/base/utils/foreignapps.h

@ -37,7 +37,7 @@ namespace Utils::ForeignApps
{ {
struct PythonInfo struct PythonInfo
{ {
using Version = Utils::Version<quint8, 3, 1>; using Version = Utils::Version<3, 1>;
bool isValid() const; bool isValid() const;
bool isSupportedVersion() const; bool isSupportedVersion() const;

152
src/base/utils/version.h

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Mike Tzou (Chocobo1)
* Copyright (C) 2016 Eugene Shalygin * Copyright (C) 2016 Eugene Shalygin
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -31,84 +32,78 @@
#include <array> #include <array>
#include <type_traits> #include <type_traits>
#include <QDebug> #include <QList>
#include <QString> #include <QString>
#include <QStringView>
#include "base/exceptions.h"
#include "base/global.h"
#include "base/interfaces/istringable.h" #include "base/interfaces/istringable.h"
namespace Utils namespace Utils
{ {
template <typename T, std::size_t N, std::size_t Mandatory = N> // This class provides a default implementation of `isValid()` that should work for most cases
// It is ultimately up to the user to decide whether the version numbers are useful/meaningful
template <int N, int Mandatory = N>
class Version final : public IStringable class Version final : public IStringable
{ {
static_assert((N > 0), "The number of version components may not be smaller than 1"); static_assert((N > 0), "The number of version components may not be smaller than 1");
static_assert((Mandatory > 0), "The number of mandatory components may not be smaller than 1");
static_assert((N >= Mandatory), static_assert((N >= Mandatory),
"The number of mandatory components may not be larger than the total number of components"); "The number of mandatory components may not be larger than the total number of components");
public: public:
typedef T ComponentType; using ThisType = Version<N, Mandatory>;
typedef Version<T, N, Mandatory> ThisType;
constexpr Version() = default; constexpr Version() = default;
template <typename ... Other template <typename ... Ts
, typename std::enable_if_t<std::conjunction_v<std::is_constructible<T, Other>...>, int> = 0> , typename std::enable_if_t<std::conjunction_v<std::is_convertible<Ts, int>...>, int> = 0>
constexpr Version(Other ... components) constexpr Version(Ts ... params)
: m_components {{static_cast<T>(components) ...}} : m_components {{params ...}}
{ {
static_assert((sizeof...(Other) <= N), "Too many parameters provided"); static_assert((sizeof...(Ts) <= N), "Too many parameters provided");
static_assert((sizeof...(Other) >= Mandatory), "Not enough parameters provided"); static_assert((sizeof...(Ts) >= Mandatory), "Not enough parameters provided");
} }
/** constexpr bool isValid() const
* @brief Creates version from string in format "x.y.z"
*
* @param version Version string in format "x.y.z"
* @throws RuntimeError if parsing fails
*/
Version(const QString &version)
: m_components {parseList(version.split(u'.'))}
{
}
/**
* @brief Creates version from byte array in format "x.y.z"
*
* @param version Version string in format "x.y.z"
* @throws RuntimeError if parsing fails
*/
Version(const QByteArray &version)
: m_components {parseList(version.split('.'))}
{ {
bool hasValid = false;
for (const int i : m_components)
{
if (i < 0)
return false;
if (i > 0)
hasValid = true;
}
return hasValid;
} }
constexpr ComponentType majorNumber() const constexpr int majorNumber() const
{ {
static_assert(N >= 1, "The number of version components is too small");
return m_components[0]; return m_components[0];
} }
constexpr ComponentType minorNumber() const constexpr int minorNumber() const
{ {
static_assert(N >= 2, "The number of version components is too small"); static_assert((N >= 2), "The number of version components is too small");
return m_components[1]; return m_components[1];
} }
constexpr ComponentType revisionNumber() const constexpr int revisionNumber() const
{ {
static_assert(N >= 3, "The number of version components is too small"); static_assert((N >= 3), "The number of version components is too small");
return m_components[2]; return m_components[2];
} }
constexpr ComponentType patchNumber() const constexpr int patchNumber() const
{ {
static_assert(N >= 4, "The number of version components is too small"); static_assert((N >= 4), "The number of version components is too small");
return m_components[3]; return m_components[3];
} }
constexpr ComponentType operator[](const std::size_t i) const constexpr int operator[](const int i) const
{ {
return m_components.at(i); return m_components.at(i);
} }
@ -116,24 +111,19 @@ namespace Utils
QString toString() const override QString toString() const override
{ {
// find the last one non-zero component // find the last one non-zero component
std::size_t lastSignificantIndex = N - 1; int lastSignificantIndex = N - 1;
while ((lastSignificantIndex > 0) && ((*this)[lastSignificantIndex] == 0)) while ((lastSignificantIndex > 0) && (m_components[lastSignificantIndex] == 0))
--lastSignificantIndex; --lastSignificantIndex;
if (lastSignificantIndex + 1 < Mandatory) // lastSignificantIndex >= 0 if ((lastSignificantIndex + 1) < Mandatory) // lastSignificantIndex >= 0
lastSignificantIndex = Mandatory - 1; // and Mandatory >= 1 lastSignificantIndex = Mandatory - 1; // and Mandatory >= 1
QString res = QString::number((*this)[0]); QString res = QString::number(m_components[0]);
for (std::size_t i = 1; i <= lastSignificantIndex; ++i) for (int i = 1; i <= lastSignificantIndex; ++i)
res += (u'.' + QString::number((*this)[i])); res += (u'.' + QString::number(m_components[i]));
return res; return res;
} }
constexpr bool isValid() const
{
return (*this != ThisType {});
}
// TODO: remove manually defined operators and use compiler generated `operator<=>()` in C++20 // TODO: remove manually defined operators and use compiler generated `operator<=>()` in C++20
friend bool operator==(const ThisType &left, const ThisType &right) friend bool operator==(const ThisType &left, const ThisType &right)
{ {
@ -145,66 +135,50 @@ namespace Utils
return (left.m_components < right.m_components); return (left.m_components < right.m_components);
} }
template <typename StringClassWithSplitMethod> static Version fromString(const QStringView string, const Version &defaultVersion = {})
static Version tryParse(const StringClassWithSplitMethod &s, const Version &defaultVersion)
{ {
try const QList<QStringView> stringParts = string.split(u'.');
{ const int count = stringParts.size();
return Version(s);
}
catch (const RuntimeError &error)
{
qDebug() << "Error parsing version:" << error.message();
return defaultVersion;
}
}
private:
using ComponentsArray = std::array<T, N>;
template <typename StringList> if ((count > N) || (count < Mandatory))
static ComponentsArray parseList(const StringList &versionParts) return defaultVersion;
{
if ((static_cast<std::size_t>(versionParts.size()) > N)
|| (static_cast<std::size_t>(versionParts.size()) < Mandatory))
{
throw RuntimeError(u"Incorrect number of version components"_qs);
}
bool ok = false; Version version;
ComponentsArray res {{}}; for (int i = 0; i < count; ++i)
for (std::size_t i = 0; i < static_cast<std::size_t>(versionParts.size()); ++i)
{ {
res[i] = static_cast<T>(versionParts[static_cast<typename StringList::size_type>(i)].toInt(&ok)); bool ok = false;
version.m_components[i] = stringParts[i].toInt(&ok);
if (!ok) if (!ok)
throw RuntimeError(u"Can not parse version component"_qs); return defaultVersion;
} }
return res;
return version;
} }
ComponentsArray m_components {{}}; private:
std::array<int, N> m_components {{}};
}; };
template <typename T, std::size_t N, std::size_t Mandatory> template <int N, int Mandatory>
constexpr bool operator!=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right) constexpr bool operator!=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{ {
return !(left == right); return !(left == right);
} }
template <typename T, std::size_t N, std::size_t Mandatory> template <int N, int Mandatory>
constexpr bool operator>(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right) constexpr bool operator>(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{ {
return (right < left); return (right < left);
} }
template <typename T, std::size_t N, std::size_t Mandatory> template <int N, int Mandatory>
constexpr bool operator<=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right) constexpr bool operator<=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{ {
return !(left > right); return !(left > right);
} }
template <typename T, std::size_t N, std::size_t Mandatory> template <int N, int Mandatory>
constexpr bool operator>=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right) constexpr bool operator>=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{ {
return !(left < right); return !(left < right);
} }

28
src/gui/programupdater.cpp

@ -52,25 +52,21 @@ namespace
{ {
bool isVersionMoreRecent(const QString &remoteVersion) bool isVersionMoreRecent(const QString &remoteVersion)
{ {
using Version = Utils::Version<int, 4, 3>; using Version = Utils::Version<4, 3>;
try const auto newVersion = Version::fromString(remoteVersion);
{ if (!newVersion.isValid())
const Version newVersion {remoteVersion};
const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
if (newVersion == currentVersion)
{
const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
QRegularExpression(u"(alpha|beta|rc)"_qs));
if (isDevVersion)
return true;
}
return (newVersion > currentVersion);
}
catch (const RuntimeError &)
{
return false; return false;
const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
if (newVersion == currentVersion)
{
const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
QRegularExpression(u"(alpha|beta|rc)"_qs));
if (isDevVersion)
return true;
} }
return (newVersion > currentVersion);
} }
} }

4
src/gui/utils.cpp

@ -193,8 +193,8 @@ void Utils::Gui::openFolderSelect(const Path &path)
proc.start(u"nautilus"_qs, {u"--version"_qs}); proc.start(u"nautilus"_qs, {u"--version"_qs});
proc.waitForFinished(); proc.waitForFinished();
const auto nautilusVerStr = QString::fromLocal8Bit(proc.readLine()).remove(QRegularExpression(u"[^0-9.]"_qs)); const auto nautilusVerStr = QString::fromLocal8Bit(proc.readLine()).remove(QRegularExpression(u"[^0-9.]"_qs));
using NautilusVersion = Utils::Version<int, 3>; using NautilusVersion = Utils::Version<3>;
if (NautilusVersion::tryParse(nautilusVerStr, {1, 0, 0}) > NautilusVersion {3, 28, 0}) if (NautilusVersion::fromString(nautilusVerStr, {1, 0, 0}) > NautilusVersion(3, 28, 0))
proc.startDetached(u"nautilus"_qs, {(Fs::isDir(path) ? path.parentPath() : path).toString()}); proc.startDetached(u"nautilus"_qs, {(Fs::isDir(path) ? path.parentPath() : path).toString()});
else else
proc.startDetached(u"nautilus"_qs, {u"--no-desktop"_qs, (Fs::isDir(path) ? path.parentPath() : path).toString()}); proc.startDetached(u"nautilus"_qs, {u"--no-desktop"_qs, (Fs::isDir(path) ? path.parentPath() : path).toString()});

2
src/webui/webapplication.h

@ -49,7 +49,7 @@
#include "base/utils/version.h" #include "base/utils/version.h"
#include "api/isessionmanager.h" #include "api/isessionmanager.h"
inline const Utils::Version<int, 3, 2> API_VERSION {2, 8, 14}; inline const Utils::Version<3, 2> API_VERSION {2, 8, 14};
class APIController; class APIController;
class AuthController; class AuthController;

145
test/testutilsversion.cpp

@ -42,96 +42,131 @@ public:
private slots: private slots:
void testConstructors() const void testConstructors() const
{ {
using TwoDigits = Utils::Version<unsigned char, 2, 1>; // should not compile:
TwoDigits(); // Utils::Version<-1>();
// Utils::Version<0>();
// Utils::Version<2, 3>();
// Utils::Version<2, -1>();
// Utils::Version<2, 0>();
using TwoDigits = Utils::Version<2, 1>;
TwoDigits(0); TwoDigits(0);
TwoDigits(50);
TwoDigits(0, 1); TwoDigits(0, 1);
using ThreeDigits = Utils::Version<int, 3>; using ThreeDigits = Utils::Version<3, 3>;
// should not compile: // should not compile:
// ThreeDigits(1); // ThreeDigits(1);
// ThreeDigits(1, 2); // ThreeDigits(1, 2);
// ThreeDigits(1.0, 2, 3);
// ThreeDigits(1, 2, 3, 4); // ThreeDigits(1, 2, 3, 4);
QCOMPARE(ThreeDigits(u"1.2.3"_qs), ThreeDigits(1, 2, 3)); ThreeDigits(1, 2, 3);
QCOMPARE(ThreeDigits(QByteArrayLiteral("1.2.3")), ThreeDigits(1, 2, 3)); }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0)) void testIsValid() const
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u""_qs)); {
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"1"_qs)); using ThreeDigits = Utils::Version<3>;
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"1.0"_qs)); QCOMPARE(ThreeDigits().isValid(), false);
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"1.0.1.1"_qs)); QCOMPARE(ThreeDigits(0, 0, 0).isValid(), false);
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"random_string"_qs)); QCOMPARE(ThreeDigits(0, 0, -1).isValid(), false);
QCOMPARE(ThreeDigits(0, 0, 1).isValid(), true);
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("1"))); QCOMPARE(ThreeDigits(0, 1, 0).isValid(), true);
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("1.0"))); QCOMPARE(ThreeDigits(1, 0, 0).isValid(), true);
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("1.0.1.1"))); QCOMPARE(ThreeDigits(10, 11, 12).isValid(), true);
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("random_string")));
#endif
} }
void testVersionComponents() const void testVersionComponents() const
{ {
const Utils::Version<int, 1> version1 {1}; const Utils::Version<1> version1 {1};
QCOMPARE(version1[0], 1); QCOMPARE(version1[0], 1);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version1[1]);
#endif
QCOMPARE(version1.majorNumber(), 1); QCOMPARE(version1.majorNumber(), 1);
// should not compile: // should not compile:
// version1.minorNumber(); // version1.minorNumber();
// version1.revisionNumber(); // version1.revisionNumber();
// version1.patchNumber(); // version1.patchNumber();
const Utils::Version<2, 1> version2 {2};
QCOMPARE(version2[0], 2);
QCOMPARE(version2[1], 0);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version1[1]); QVERIFY_THROWS_EXCEPTION(std::out_of_range, version2[2]);
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version1[2]);
#endif #endif
QCOMPARE(version2.majorNumber(), 2);
QCOMPARE(version2.minorNumber(), 0);
// should not compile:
// version2.revisionNumber();
// version2.patchNumber();
const Utils::Version<int, 4> version2 {10, 11, 12, 13}; const Utils::Version<3, 2> version3 {3, 2};
QCOMPARE(version2[0], 10); QCOMPARE(version3[0], 3);
QCOMPARE(version2[1], 11); QCOMPARE(version3[1], 2);
QCOMPARE(version2[2], 12); QCOMPARE(version3[2], 0);
QCOMPARE(version2[3], 13); #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QCOMPARE(version2.majorNumber(), 10); QVERIFY_THROWS_EXCEPTION(std::out_of_range, version3[3]);
QCOMPARE(version2.minorNumber(), 11); #endif
QCOMPARE(version2.revisionNumber(), 12); QCOMPARE(version3.majorNumber(), 3);
QCOMPARE(version2.patchNumber(), 13); QCOMPARE(version3.minorNumber(), 2);
QCOMPARE(version3.revisionNumber(), 0);
// should not compile:
// version3.patchNumber();
const Utils::Version<4> version4 {10, 11, 12, 13};
QCOMPARE(version4[0], 10);
QCOMPARE(version4[1], 11);
QCOMPARE(version4[2], 12);
QCOMPARE(version4[3], 13);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version4[4]);
#endif
QCOMPARE(version4.majorNumber(), 10);
QCOMPARE(version4.minorNumber(), 11);
QCOMPARE(version4.revisionNumber(), 12);
QCOMPARE(version4.patchNumber(), 13);
} }
void testToString() const void testToString() const
{ {
using OneMandatory = Utils::Version<int, 2, 1>; using OneMandatory = Utils::Version<2, 1>;
QCOMPARE(OneMandatory(u"10"_qs).toString(), u"10"_qs); QCOMPARE(OneMandatory(10).toString(), u"10"_qs);
QCOMPARE(OneMandatory(2).toString(), u"2"_qs);
QCOMPARE(OneMandatory(2, 0).toString(), u"2"_qs);
QCOMPARE(OneMandatory(2, 2).toString(), u"2.2"_qs);
using FourDigits = Utils::Version<int, 4>; using FourDigits = Utils::Version<4>;
QCOMPARE(FourDigits().toString(), u"0.0.0.0"_qs);
QCOMPARE(FourDigits(10, 11, 12, 13).toString(), u"10.11.12.13"_qs); QCOMPARE(FourDigits(10, 11, 12, 13).toString(), u"10.11.12.13"_qs);
} }
void testIsValid() const void testFromString() const
{
using ThreeDigits = Utils::Version<int, 3>;
QCOMPARE(ThreeDigits().isValid(), false);
QCOMPARE(ThreeDigits(10, 11, 12).isValid(), true);
}
void testTryParse() const
{ {
using OneMandatory = Utils::Version<int, 2, 1>; using OneMandatory = Utils::Version<2, 1>;
const OneMandatory default1 {10, 11}; const OneMandatory default1 {10, 11};
QCOMPARE(OneMandatory::tryParse(u"1"_qs, default1), OneMandatory(1)); QCOMPARE(OneMandatory::fromString(u"1"_qs, default1), OneMandatory(1));
QCOMPARE(OneMandatory::tryParse(u"1.2"_qs, default1), OneMandatory(1, 2)); QCOMPARE(OneMandatory::fromString(u"1.2"_qs, default1), OneMandatory(1, 2));
QCOMPARE(OneMandatory::tryParse(u"1,2"_qs, default1), default1); QCOMPARE(OneMandatory::fromString(u"100.2000"_qs, default1), OneMandatory(100, 2000));
QCOMPARE(OneMandatory::fromString(u"1,2"_qs), OneMandatory());
QCOMPARE(OneMandatory::tryParse(u""_qs, default1), default1); QCOMPARE(OneMandatory::fromString(u"1,2"_qs, default1), default1);
QCOMPARE(OneMandatory::tryParse(u"random_string"_qs, default1), default1); QCOMPARE(OneMandatory::fromString(u"1.2a"_qs), OneMandatory());
QCOMPARE(OneMandatory::fromString(u"1.2.a"_qs), OneMandatory());
using FourDigits = Utils::Version<int, 4>; QCOMPARE(OneMandatory::fromString(u""_qs), OneMandatory());
const FourDigits default4 {10, 11, 12, 13}; QCOMPARE(OneMandatory::fromString(u""_qs, default1), default1);
QCOMPARE(FourDigits::tryParse(u"1"_qs, default4), default4); QCOMPARE(OneMandatory::fromString(u"random_string"_qs), OneMandatory());
QCOMPARE(FourDigits::tryParse(u"1.2.3.4"_qs, default4), FourDigits(1, 2, 3, 4)); QCOMPARE(OneMandatory::fromString(u"random_string"_qs, default1), default1);
QCOMPARE(FourDigits::tryParse(u"1,2.3.4"_qs, default4), default4);
using FourDigits = Utils::Version<4, 3>;
const FourDigits default2 {10, 11, 12, 13};
QCOMPARE(FourDigits::fromString(u"1"_qs, default2), default2);
QCOMPARE(FourDigits::fromString(u"1.2"_qs), FourDigits());
QCOMPARE(FourDigits::fromString(u"1.2.3"_qs), FourDigits(1, 2, 3));
QCOMPARE(FourDigits::fromString(u"1.2.3.0"_qs), FourDigits(1, 2, 3));
QCOMPARE(FourDigits::fromString(u"1.2.3.4"_qs), FourDigits(1, 2, 3, 4));
} }
void testComparisons() const void testComparisons() const
{ {
using ThreeDigits = Utils::Version<int, 3>; using ThreeDigits = Utils::Version<3>;
QVERIFY(ThreeDigits() == ThreeDigits()); QVERIFY(ThreeDigits() == ThreeDigits());
QVERIFY(!(ThreeDigits() != ThreeDigits())); QVERIFY(!(ThreeDigits() != ThreeDigits()));

Loading…
Cancel
Save