File indexing completed on 2024-05-05 04:55:40

0001 /**
0002  * \file testutils.cpp
0003  * Utility functions for tests.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 07 Oct 2012
0008  *
0009  * Copyright (C) 2012-2013  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "testutils.h"
0028 #include <QTest>
0029 #include <QRegularExpression>
0030 #include <cstdio>
0031 
0032 /**
0033  * Run the tests of a test suite.
0034  *
0035  * Besides the standard QtTest options, this test runner also allows to select
0036  * the testcase with "-testcase" and list the testcases with "-testcases".
0037  * This function will exit the application for certain \a args parsed by QtTest.
0038  *
0039  * @param testSuite an object containing QtTest test cases as children
0040  * @param args command line arguments from QCoreApplication::arguments()
0041  *
0042  * @return 0 if OK, the number of failed tests if failed.
0043  */
0044 int TestUtils::runTestSuite(const QObject& testSuite, QStringList& args)
0045 {
0046   bool listTestCases = false;
0047   QRegularExpression testCaseRe;
0048   for (int i = 1; i < args.size(); ++i) {
0049     if (args.at(i) == QLatin1String("-help")) {
0050       std::printf(" -testcases : Returns a list of current testcases\n"
0051                   " -testcase re      : Run only testcases matching regular "
0052                   "expression\n");
0053     } else if (args.at(i) == QLatin1String("-testcases")) {
0054       listTestCases = true;
0055       args.removeAt(i);
0056       break;
0057     } else if (args.at(i) == QLatin1String("-testcase") && i + 1 < args.size()) {
0058       testCaseRe.setPattern(args.at(i + 1));
0059       args.removeAt(i + 1);
0060       args.removeAt(i);
0061     }
0062   }
0063 
0064   int testsFailed = 0;
0065   int testCasesPassed = 0;
0066   int testCasesFailed = 0;
0067 
0068   foreach (QObject* tc, testSuite.children()) {
0069     QString tcName(QString::fromLatin1(tc->metaObject()->className()));
0070     if (listTestCases) {
0071       std::printf("%s\n", qPrintable(tcName));
0072     } else if (testCaseRe.pattern().isEmpty() || testCaseRe.match(tcName).hasMatch()) {
0073       int rc = QTest::qExec(tc, args);
0074       testsFailed += rc;
0075       if (rc == 0) {
0076         ++testCasesPassed;
0077       } else {
0078         ++testCasesFailed;
0079       }
0080     }
0081   }
0082 
0083   std::printf("Test cases: %d passed, %d failed\n",
0084               testCasesPassed, testCasesFailed);
0085   return testsFailed;
0086 }