Avoid senseless duplication of code in a testlib selftest

Delegate repeated tasks to functions - DRY !

Change-Id: I8973d2b9cd1f7d0fe94a9ae4aebcff434821e210
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Edward Welbourne 2021-08-16 17:54:55 +02:00
parent a6a3b1e79c
commit e3d5d363f7
1 changed files with 24 additions and 36 deletions

View File

@ -49,64 +49,62 @@ private slots:
void test3();
void multiFail();
private:
void logNames(const char *caller);
void table_data();
};
void tst_Subtest::logNames(const char *caller)
{
auto orNull = [](const char *s) { return s ? s : "(null)"; };
qDebug("%s %s %s", caller, orNull(QTest::currentTestFunction()),
orNull(QTest::currentDataTag()));
}
void tst_Subtest::initTestCase()
{
qDebug() << "initTestCase"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
logNames("initTestCase");
}
void tst_Subtest::cleanupTestCase()
{
qDebug() << "cleanupTestCase"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
logNames("cleanupTestCase");
}
void tst_Subtest::init()
{
qDebug() << "init"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
logNames("init");
}
void tst_Subtest::cleanup()
{
qDebug() << "cleanup"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
logNames("cleanup");
}
void tst_Subtest::test1()
{
qDebug() << "test1"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
logNames("test1");
}
void tst_Subtest::test2_data()
void tst_Subtest::table_data()
{
qDebug() << "test2_data"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
QTest::addColumn<QString>("str");
QTest::newRow("data0") << QString("hello0");
QTest::newRow("data1") << QString("hello1");
QTest::newRow("data2") << QString("hello2");
}
void tst_Subtest::test2_data()
{
logNames("test2_data");
table_data();
qDebug() << "test2_data end";
}
void tst_Subtest::test2()
{
qDebug() << "test2"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
logNames("test2");
static int count = 0;
@ -118,24 +116,14 @@ void tst_Subtest::test2()
void tst_Subtest::test3_data()
{
qDebug() << "test3_data"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
QTest::addColumn<QString>("str");
QTest::newRow("data0") << QString("hello0");
QTest::newRow("data1") << QString("hello1");
QTest::newRow("data2") << QString("hello2");
logNames("test3_data");
table_data();
qDebug() << "test3_data end";
}
void tst_Subtest::test3()
{
qDebug() << "test3"
<< (QTest::currentTestFunction() ? QTest::currentTestFunction() : "(null)")
<< (QTest::currentDataTag() ? QTest::currentDataTag() : "(null)");
logNames("test3");
QFETCH(QString, str);