Debian Codegraph @

tests/utils/TestRunner.h

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