[pim/libkleo/gpg4win/gpd-5.2] autotests: Add helper to mask formatted dates and times in tests

Ingo Klöcker <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit bca45c8fa8a6ae47e77cb1cb1daeb658e985322f by Ingo Klöcker, on behalf of Ingo Klöcker.
Committed on 30/07/2026 at 09:36.
Pushed by kloecker into branch 'gpg4win/gpd-5.2'.

Add helper to mask formatted dates and times in tests

Dates and times can be formatted differently on different platforms.
Since we don't want to test the formatting of dates and times we mask
them in the results that we compare to the expected results.

GnuPG-bug-id: 7786
(cherry picked from commit 699d691647e7dee47cf73d9feb54240182ef7f71)

M  +30   -8    autotests/formattingtest.cpp

https://invent.kde.org/pim/libkleo/-/commit/bca45c8fa8a6ae47e77cb1cb1daeb658e985322f

diff --git a/autotests/formattingtest.cpp b/autotests/formattingtest.cpp
index 1a285f29..ea50fcb1 100644
--- a/autotests/formattingtest.cpp
+++ b/autotests/formattingtest.cpp
@@ -195,12 +195,36 @@ public:
         qputenv("TZ", "UTC");
     }
 
+    QString maskDateAndTime(QString text) const
+    {
+        static const QString datePattern = u"[0-9]{1,4}[-/][0-9]{1,2}[-/][0-9]{1,4}"_s;
+        // we use \W (any non-word character) instead of \s for matching trailing AM/PM because \s doesn't match \u202F (Narrow No-Break Space)
+        static const QString timePattern = u"[0-9]{1,2}:[0-9]{2}(?::[0-9]{2})?(?:\\W[AP]M)?"_s;
+        static const QRegularExpression dateTimeRegExp{datePattern + u' ' + timePattern};
+        static const QRegularExpression dateRegExp{datePattern};
+        static const QRegularExpression timeRegExp{timePattern};
+        return text.replace(dateTimeRegExp, u"DATETIME"_s).replace(dateRegExp, u"DATE"_s).replace(timeRegExp, u"TIME"_s);
+    }
+
 private Q_SLOTS:
     void initTestCase()
     {
         GpgME::initializeLibrary();
     }
 
+    void test_maskDateAndTime()
+    {
+        QCOMPARE(maskDateAndTime(u"07/05/26 05:07"_s), u"DATETIME"_s);
+        QCOMPARE(maskDateAndTime(u"7/5/26"_s), u"DATE"_s);
+        QCOMPARE(maskDateAndTime(u"07/05/2026"_s), u"DATE"_s);
+        QCOMPARE(maskDateAndTime(u"2026-05-07"_s), u"DATE"_s);
+        QCOMPARE(maskDateAndTime(u"5:07"_s), u"TIME"_s);
+        QCOMPARE(maskDateAndTime(u"05:07"_s), u"TIME"_s);
+        QCOMPARE(maskDateAndTime(u"05:07:08"_s), u"TIME"_s);
+        QCOMPARE(maskDateAndTime(u"05:07 AM"_s), u"TIME"_s);
+        QCOMPARE(maskDateAndTime(u"05:07:08 PM"_s), u"TIME"_s);
+    }
+
     void test_prettyID_data()
     {
         QTest::addColumn<QByteArray>("id");
@@ -289,10 +313,9 @@ private Q_SLOTS:
         QVERIFY(!verificationResult.error());
         QCOMPARE(verificationResult.numSignatures(), 1);
 
-        const QString resultWithoutTimestamp = Formatting::prettySignature(verificationResult.signature(0), u"[email protected]"_s)
-                                                   .replace(QRegularExpression{u"on .* with"_s}, u"on TIMESTAMP with"_s);
-        QCOMPARE(resultWithoutTimestamp,
-                 u"Signature created on TIMESTAMP with certificate: "
+        const QString result = Formatting::prettySignature(verificationResult.signature(0), u"[email protected]"_s);
+        QCOMPARE(maskDateAndTime(result),
+                 u"Signature created on DATETIME with certificate: "
                  "<a href=\"key:1DE1960C29F97E6762C4EA341820DAAC045579921E0F30567354CCC69FD42A1D\">"
                  "Curve 448 &lt;[email protected]&gt; (1DE1 960C 29F9 7E67)"
                  "</a><br/>"
@@ -318,10 +341,9 @@ private Q_SLOTS:
         QVERIFY(!verificationResult.error());
         QCOMPARE(verificationResult.numSignatures(), 1);
 
-        const QString resultWithoutTimestamp = Formatting::prettySignature(verificationResult.signature(0), u"[email protected]"_s)
-                                                   .replace(QRegularExpression{u"on .* using"_s}, u"on TIMESTAMP using"_s);
-        QCOMPARE(resultWithoutTimestamp,
-                 u"Signature created on TIMESTAMP using an unknown certificate with fingerprint <br/>"
+        const QString result = Formatting::prettySignature(verificationResult.signature(0), u"[email protected]"_s);
+        QCOMPARE(maskDateAndTime(result),
+                 u"Signature created on DATETIME using an unknown certificate with fingerprint <br/>"
                  "<a href='certificate:1DE1960C29F97E6762C4EA341820DAAC045579921E0F30567354CCC69FD42A1D'>1DE19 60C29 F97E6 762C4 EA341 820DA AC045 57992 1E0F3 "
                  "05673</a><br/>"
                  "You can search the certificate on a keyserver or import it from a file."_s);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.