From 9b8a6da4ff39428ce22dc23e16fc48cc062bc01f Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Mon, 24 Aug 2026 21:05:45 +0200 Subject: fix(signatures): match the guard against on-disk text, newline and all The guard compared the buffer block, with trailing blank lines trimmed, against knownSignatures() values returned verbatim by text(), which carry the trailing newline every editor writes. The two never compared equal, so a signature read back from disk was always treated as unknown: switching appended a second signature instead of replacing, and None removed nothing. Normalise each known entry the same way the block scan does, once in replace(), rather than per comparison. Part of item 152. --- tests/test_signatures.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'tests') diff --git a/tests/test_signatures.cpp b/tests/test_signatures.cpp index 31f9eb4..47b404b 100644 --- a/tests/test_signatures.cpp +++ b/tests/test_signatures.cpp @@ -40,6 +40,7 @@ private slots: void selectingNoneRemovesAKnownSignature(); void aBlockMatchingNoKnownSignatureIsNotRemoved(); void aDelimiterInsideTheQuoteIsNotTheSignature(); + void aSignatureReadBackFromDiskIsReplaced(); private: /// Writes \p files as name -> content into a fresh temporary directory. @@ -273,5 +274,21 @@ void TestSignatures::aDelimiterInsideTheQuoteIsNotTheSignature() QVERIFY(result.endsWith(QStringLiteral("-- \nJane Doe"))); } +void TestSignatures::aSignatureReadBackFromDiskIsReplaced() +{ + // known here is what knownSignatures() produces: text() verbatim, carrying + // the trailing newline every editor writes into a file. The block scan + // treats a trailing blank line as separation rather than text, so a naive + // match compares "Jane Doe" against "Jane Doe\n" and silently fails, and + // switching then APPENDS a second signature instead of replacing the first. + const QStringList known = { QStringLiteral("Jane Doe\n") }; + const QString buffer = QStringLiteral("Hello.\n\n-- \nJane Doe\n"); + + const QString result = Signatures::replace( + buffer, QStringLiteral("Brief"), known, Signatures::Position::End); + + QCOMPARE(result, QStringLiteral("Hello.\n\n-- \nBrief")); +} + QTEST_MAIN(TestSignatures) #include "test_signatures.moc" -- cgit v1.2.3