1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/*
* Copyright (C) 2021 Damir Porobic <damir.porobic@gmx.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* Example taken from https://rodolfotech.blogspot.com/2017/01/qtest-google-mock.html
*/
#ifndef KSNIP_TESTRUNNER_H
#define KSNIP_TESTRUNNER_H
#include <gmock/gmock.h>
class GoogleTestEventListener : public ::testing::EmptyTestEventListener {
void OnTestStart(const ::testing::TestInfo&) override {
}
void OnTestPartResult(const ::testing::TestPartResult& test_part_result) override {
if (test_part_result.failed()) {
QFAIL(
QString("mock objects failed with '%1' at %2:%3")
.arg(QString(test_part_result.summary()))
.arg(test_part_result.file_name())
.arg(test_part_result.line_number())
.toLatin1().constData()
);
}
}
void OnTestEnd(const ::testing::TestInfo&) override {
}
};
#define INIT_GOOGLE_MOCKS(argc, argv) { \
::testing::InitGoogleTest (&(argc), (argv)); \
::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners(); \
delete listeners.Release(listeners.default_result_printer());\
listeners.Append(new GoogleTestEventListener); }
/*
* Taken from QTEST_MAIN macro and added additional INIT_GOOGLE_MOCKS in the calls.
*/
#if defined(QT_WIDGETS_LIB)
#include <QtTest/qtest_widgets.h>
#ifdef QT_KEYPAD_NAVIGATION
# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
#else
# define QTEST_DISABLE_KEYPAD_NAVIGATION
#endif
#define TEST_MAIN(TestObject) \
QT_BEGIN_NAMESPACE \
QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS \
QT_END_NAMESPACE \
int main(int argc, char *argv[]) \
{ \
INIT_GOOGLE_MOCKS (argc, argv); \
\
QApplication app(argc, argv); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
QTEST_DISABLE_KEYPAD_NAVIGATION \
QTEST_ADD_GPU_BLACKLIST_SUPPORT \
TestObject tc; \
QTEST_SET_MAIN_SOURCE_PATH \
return QTest::qExec(&tc, argc, argv); \
}
#elif defined(QT_GUI_LIB)
#include <QtTest/qtest_gui.h>
#define TEST_MAIN(TestObject) \
QT_BEGIN_NAMESPACE \
QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS \
QT_END_NAMESPACE \
int main(int argc, char *argv[]) \
{ \
INIT_GOOGLE_MOCKS (argc, argv); \
\
QGuiApplication app(argc, argv); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
QTEST_ADD_GPU_BLACKLIST_SUPPORT \
TestObject tc; \
QTEST_SET_MAIN_SOURCE_PATH \
return QTest::qExec(&tc, argc, argv); \
}
#else
#define TEST_MAIN(TestObject) \
int main(int argc, char *argv[]) \
{ \
INIT_GOOGLE_MOCKS (argc, argv); \
\
QCoreApplication app(argc, argv); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
TestObject tc; \
QTEST_SET_MAIN_SOURCE_PATH \
return QTest::qExec(&tc, argc, argv); \
}
#endif // QT_GUI_LIB
#endif //KSNIP_TESTRUNNER_H
|