[utilities/basket] src/tests: Rewrite testHtmlToText() to be generic and data-driven

Pino Toscano <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 48897fb9e804b1472bd0554c64f9effed728b1c2 by Pino Toscano.
Committed on 25/07/2026 at 21:50.
Pushed by pino into branch 'master'.

Rewrite testHtmlToText() to be generic and data-driven

Rewrite ToolsTest::testHtmlToText() to be more generic and data-driven,
so that:
- the input is no more a specific set of 5 items, but any html file in
  the data directory; this will make it easier to add new cases in the
  future, requiring no code changes
- it is clearer to see what is the input data in case of failure
- all the cases will be tried, rather than stopping at the first that
  fails
- if "foo.html" exists as input, then "foo.txt" is assumed to exist as
  reference output, rather than silently skipping it

There is no behaviour change to the functionality of the actual test.

M  +17   -8    src/tests/toolstest.cpp

https://invent.kde.org/utilities/basket/-/commit/48897fb9e804b1472bd0554c64f9effed728b1c2

diff --git a/src/tests/toolstest.cpp b/src/tests/toolstest.cpp
index 416d532e..0535c968 100644
--- a/src/tests/toolstest.cpp
+++ b/src/tests/toolstest.cpp
@@ -12,6 +12,7 @@ class ToolsTest : public QObject
 {
     Q_OBJECT
 private Q_SLOTS:
+    void testHtmlToText_data();
     void testHtmlToText();
 
 private:
@@ -20,19 +21,27 @@ private:
 
 QTEST_MAIN(ToolsTest)
 
+void ToolsTest::testHtmlToText_data()
+{
+    QTest::addColumn<QString>("filename");
+
+    const QDir datadir(QFINDTESTDATA("htmltotext/"));
+    for (const QString &entry : datadir.entryList({QStringLiteral("*.html")}, QDir::Files, QDir::Name)) {
+        QTest::newRow(qPrintable(entry)) << entry;
+    }
+}
+
 void ToolsTest::testHtmlToText()
 {
     // Test the function on files from htmltotext/
 
-    for (int i = 1; i <= 5; i++) {
-        QString html, text;
-        QString basename = QFINDTESTDATA("htmltotext/");
-        QVERIFY2(QDir(basename).exists(), "Test data file not found");
-        basename += QString::number(i);
+    QFETCH(QString, filename);
 
-        if (readAll(basename + QStringLiteral(".html"), html) && readAll(basename + QStringLiteral(".txt"), text))
-            QCOMPARE(Tools::htmlToText(html), text);
-    }
+    const QString basename = QFINDTESTDATA("htmltotext/");
+    QString html, text;
+    QVERIFY(readAll(basename + filename, html));
+    QVERIFY(readAll(basename + filename.chopped(4) + QStringLiteral("txt"), text));
+    QCOMPARE(Tools::htmlToText(html), text);
 }
 
 bool ToolsTest::readAll(QString fileName, QString &text)
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.