Skip to content

Commit 6547d91

Browse files
committed
Proper unit tests for the application
1 parent 1b6a724 commit 6547d91

10 files changed

+798
-278
lines changed

CMakeLists.txt

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ project(copyq)
22

33
OPTION(WITH_WEBKIT "WebKit support" ON)
44
OPTION(WITH_QT5 "Qt5 support" OFF)
5+
OPTION(WITH_TESTS "Run test cases from command line" OFF)
56

67
if (WITH_QT5)
78
cmake_minimum_required(VERSION 2.8.8)
@@ -34,7 +35,6 @@ else()
3435
endif(WITH_WEBKIT)
3536
set(QT_USE_QTWEBKIT ${HAS_WEBKIT})
3637

37-
include(${QT_USE_FILE})
3838
set(USE_QXT TRUE)
3939
endif()
4040

@@ -144,6 +144,26 @@ endif(USE_QXT)
144144
# Be more strict while compiling debugging version
145145
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
146146
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wextra -Wall -pedantic")
147+
set(WITH_TESTS ON)
148+
endif()
149+
150+
# Compile with tests?
151+
if (WITH_TESTS)
152+
message(STATUS "Building with tests.")
153+
add_definitions( -DHAS_TESTS )
154+
155+
file(GLOB copyq_SOURCES ${copyq_SOURCES}
156+
src/tests/*.cpp
157+
)
158+
file(GLOB copyq_MOCABLE ${copyq_MOCABLE}
159+
src/tests/*.h
160+
)
161+
162+
if (WITH_QT5)
163+
set(copyq_Qt5_Modules ${copyq_Qt5_Modules} Test)
164+
else()
165+
set(QT_USE_QTTEST TRUE)
166+
endif()
147167
endif()
148168

149169
set(copyq_LIBRARIES ${copyq_LIBRARIES} ${X11_LIBRARIES} ${X11_Xfixes_LIB})
@@ -187,6 +207,8 @@ add_executable(copyq ${copyq_SOURCES}
187207

188208
if (WITH_QT5)
189209
qt5_use_modules(copyq ${copyq_Qt5_Modules})
210+
else()
211+
include(${QT_USE_FILE})
190212
endif()
191213

192214
target_link_libraries(copyq ${QT_LIBRARIES} ${copyq_LIBRARIES})

src/app.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <QLibraryInfo>
2525
#include <QLocale>
2626
#include <QTranslator>
27+
#ifdef HAS_TESTS
28+
# include <QProcessEnvironment>
29+
#endif
2730

2831
#ifdef Q_OS_UNIX
2932
# include <QSocketNotifier>
@@ -54,8 +57,17 @@ App::App(int &argc, char **argv)
5457
, m_exitCode(0)
5558
, m_closed(false)
5659
{
57-
QCoreApplication::setOrganizationName("copyq");
58-
QCoreApplication::setApplicationName("copyq");
60+
#ifdef HAS_TESTS
61+
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
62+
if ( env.value("COPYQ_TESTING") == "1" ) {
63+
QCoreApplication::setOrganizationName("copyq-test");
64+
QCoreApplication::setApplicationName("copyq-test");
65+
} else
66+
#endif
67+
{
68+
QCoreApplication::setOrganizationName("copyq");
69+
QCoreApplication::setApplicationName("copyq");
70+
}
5971

6072
const QString locale = QLocale::system().name();
6173
QTranslator *translator = new QTranslator(this);

src/clipboardmonitor.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "client_server.h"
2323
#include "clipboarditem.h"
24-
#include "clipboardserver.h"
2524

2625
#include <QMimeData>
2726
#include <QTimer>
@@ -165,7 +164,10 @@ ClipboardMonitor::ClipboardMonitor(int &argc, char **argv)
165164
this, SLOT(readyRead()), Qt::DirectConnection );
166165
connect( m_socket, SIGNAL(disconnected()),
167166
this, SLOT(quit()) );
168-
m_socket->connectToServer( ClipboardServer::monitorServerName() );
167+
168+
QStringList args = QCoreApplication::instance()->arguments();
169+
Q_ASSERT(args.size() == 3);
170+
m_socket->connectToServer(args.at(2));
169171
if ( !m_socket->waitForConnected(2000) ) {
170172
log( tr("Cannot connect to server!"), LogError );
171173
exit(1);

src/clipboardserver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void ClipboardServer::startMonitoring()
196196
connect( m_monitor, SIGNAL(readyReadStandardError()),
197197
this, SLOT(monitorStandardError()) );
198198
m_monitor->start( QApplication::arguments().at(0),
199-
QStringList() << "monitor",
199+
QStringList("monitor") << monitorServerName(),
200200
QProcess::ReadOnly );
201201
if ( !m_monitor->waitForStarted(2000) ) {
202202
log( tr("Cannot start clipboard monitor!"), LogError );

src/main.cpp

+37-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
#include <windows.h>
3434
#endif
3535

36+
#ifdef HAS_TESTS
37+
# include "tests/tests.h"
38+
# include <QTest>
39+
40+
namespace tests {
41+
42+
QTEST_MAIN(Tests)
43+
44+
}
45+
#endif // HAS_TESTS
46+
3647
Q_DECLARE_METATYPE(QByteArray*)
3748

3849
namespace {
@@ -86,6 +97,13 @@ int startClient(int argc, char *argv[])
8697
return app.exec();
8798
}
8899

100+
#ifdef HAS_TESTS
101+
int startTests(int argc, char *argv[])
102+
{
103+
return tests::main(argc, argv);
104+
}
105+
#endif
106+
89107
bool needsHelp(const char *arg)
90108
{
91109
return strcmp("-h",arg) == 0 ||
@@ -100,11 +118,19 @@ bool needsVersion(const char *arg)
100118
strcmp("version",arg) == 0;
101119
}
102120

121+
#ifdef HAS_TESTS
122+
bool needsTests(const char *arg)
123+
{
124+
return strcmp("--tests", arg) == 0 ||
125+
strcmp("tests", arg) == 0;
126+
}
127+
#endif
128+
103129
} // namespace
104130

105131
int main(int argc, char *argv[])
106132
{
107-
// print version
133+
// print version, help or run tests
108134
if (argc == 2 || argc == 3) {
109135
const char *arg = argv[1];
110136
if ( argc == 2 && needsVersion(arg) ) {
@@ -113,20 +139,25 @@ int main(int argc, char *argv[])
113139
} else if ( needsHelp(arg) ) {
114140
evaluate("help", argc == 3 ? argv[2] : NULL);
115141
return 0;
142+
#ifdef HAS_TESTS
143+
} else if ( needsTests(arg) ) {
144+
// Skip the "tests" argument and pass the rest to tests.
145+
return startTests(argc - 1, argv + 1);
146+
#endif
116147
}
117148
}
118149

119150
if (argc == 1) {
120151
// if server hasn't been run yet and no argument were specified
121-
// then run this as server
152+
// then run this process as server
122153
return startServer(argc, argv);
123-
} else if (argc == 2 && strcmp(argv[1], "monitor") == 0) {
124-
// if argument specified and server is running
125-
// then run this as client
154+
} else if (argc == 3 && strcmp(argv[1], "monitor") == 0) {
155+
// if first argument is monitor (second is monitor server name/ID)
156+
// then run this process as monitor
126157
return startMonitor(argc, argv);
127158
} else {
128159
// if argument specified and server is running
129-
// then run this as client
160+
// then run this process as client
130161
return startClient(argc, argv);
131162
}
132163
}

src/scriptable.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ QList<CommandHelp> commandHelp()
190190
Scriptable::tr("\nPrint help for COMMAND or all commands."))
191191
.addArg("[" + Scriptable::tr("COMMAND") + "]")
192192
<< CommandHelp("version, -v, --version",
193-
Scriptable::tr("\nPrint version of program and libraries."));
193+
Scriptable::tr("\nPrint version of program and libraries."))
194+
#ifdef HAS_TESTS
195+
<< CommandHelp("tests, --tests",
196+
Scriptable::tr("Run tests."))
197+
#endif
198+
;
194199
}
195200

196201
QString helpHead()

src/src.pro

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ SOURCES += \
8181
QT += core gui xml network script
8282
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
8383

84+
debug {
85+
DEFINES += HAS_TESTS
86+
QT += testlib
87+
SOURCES += tests/tests.cpp
88+
HEADERS += tests/tests.h
89+
}
90+
8491
WITH_WEBKIT {
8592
SOURCES += itemweb.cpp
8693
HEADERS += include/itemweb.h

0 commit comments

Comments
 (0)