Skip to content

Commit a7f9174

Browse files
committed
Add tests for each test case in Api, SlotCalling and SignalHandler test suites.
1 parent f4d4999 commit a7f9174

File tree

4 files changed

+173
-39
lines changed

4 files changed

+173
-39
lines changed

CMakeLists.txt

Lines changed: 104 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,11 @@ if(BUILD_TESTING)
277277
tests/PythonQtTests.h
278278
)
279279

280-
QT4_WRAP_CPP(PythonQtTests_sources
280+
QT4_WRAP_CPP(_tmp_moc_sources
281281
tests/PythonQtTests.h
282282
)
283+
list(APPEND PythonQtTests_sources ${_tmp_moc_sources})
284+
list(APPEND moc_sources ${_tmp_moc_sources})
283285

284286
if(PythonQt_Wrap_Qtcore)
285287
include_directories(generated_cpp${generated_cpp_suffix})
@@ -288,15 +290,73 @@ if(BUILD_TESTING)
288290
tests/PythonQtTestCleanup.cpp
289291
tests/PythonQtTestCleanup.h
290292
)
291-
QT4_WRAP_CPP(test_sources
293+
QT4_WRAP_CPP(_tmp_moc_sources
292294
tests/PythonQtTestCleanup.h
293295
)
296+
list(APPEND test_sources ${_tmp_moc_sources})
297+
list(APPEND moc_sources ${_tmp_moc_sources})
294298

295299
set_property(SOURCE tests/PythonQtTestMain.cpp APPEND PROPERTY COMPILE_DEFINITIONS "PythonQt_Wrap_Qtcore")
296300
endif()
297301

302+
add_custom_target(PythonQtTestsWrapCpp DEPENDS ${moc_sources})
303+
304+
set(PythonQtTestApi_cases
305+
testCall
306+
testVariables
307+
testRedirect
308+
testImporter
309+
testQColorDecorators
310+
testQtNamespace
311+
testConnects
312+
testProperties
313+
testDynamicProperties
314+
)
315+
316+
set(PythonQtTestSlotCalling_cases
317+
testNoArgSlotCall
318+
testPODSlotCalls
319+
testCPPSlotCalls
320+
testQVariantSlotCalls
321+
testQListSlotCalls
322+
testQMapSlotCalls
323+
testObjectSlotCalls
324+
testMultiArgsSlotCall
325+
testPyObjectSlotCall
326+
testOverloadedCall
327+
#testCppFactory # XXX Fix this. Running this test standalone is failing.
328+
testInheritance
329+
testAutoConversion
330+
testProperties
331+
)
332+
333+
set(PythonQtTestSignalHandler_cases
334+
testSignalHandler
335+
testRecursiveSignalHandler
336+
)
337+
338+
foreach(suite Api SlotCalling SignalHandler)
339+
set(_compile_definition "${suite}_TEST_SUITE")
340+
set(_suffix "_${suite}")
341+
342+
add_executable(PythonQtCppTests${_suffix} ${test_sources} ${PythonQtTests_sources})
343+
target_link_libraries(PythonQtCppTests${_suffix} PythonQt)
344+
set_target_properties(PythonQtCppTests${_suffix} PROPERTIES COMPILE_DEFINITIONS ${_compile_definition})
345+
add_dependencies(PythonQtCppTests${_suffix} PythonQtTestsWrapCpp)
346+
347+
# Add one test for each test suite case
348+
foreach(case ${PythonQtTest${suite}_cases})
349+
add_test(
350+
NAME tests_PythonQtTestMain_${suite}_${case}
351+
COMMAND ${Slicer_LAUNCH_COMMAND} $<TARGET_FILE:PythonQtCppTests_${suite}> tests/PythonQtTestMain ${case}
352+
)
353+
endforeach()
354+
endforeach()
355+
356+
# suite: ALL
298357
add_executable(PythonQtCppTests ${test_sources} ${PythonQtTests_sources})
299358
target_link_libraries(PythonQtCppTests PythonQt)
359+
add_dependencies(PythonQtCppTests PythonQtTestsWrapCpp)
300360

301361
add_test(
302362
NAME tests_PythonQtTestMain
@@ -353,6 +413,8 @@ if(UNIX)
353413
else()
354414
set(ENV{PATH} \"\$ENV{PATH};.\")
355415
endif()
416+
set(ENV{SUITE} \"\${SUITE}\")
417+
set(ENV{CASE} \"\${CASE}\")
356418
execute_process(COMMAND \${TARGET_FILE} RESULT_VARIABLE result)
357419
if(NOT result EQUAL 0)
358420
message(FATAL_ERROR \"test failed ! [result: \${result}]\")
@@ -361,27 +423,52 @@ endif()
361423

362424
function(_add_dynamic_loading_test run_testsuite_in_static_initializer)
363425

426+
macro(_add_targets extra_compile_definitions)
427+
428+
add_library(PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix} SHARED
429+
tests/PythonQtDynamicLoaderSharedLibrary.cpp
430+
${PythonQtTests_sources}
431+
)
432+
target_link_libraries(PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix} PythonQt)
433+
add_dependencies(PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix} PythonQtTestsWrapCpp)
434+
435+
add_executable(PythonQtDynamicLoader_${suffix}${suite_suffix} tests/PythonQtDynamicLoader.cpp)
436+
target_link_libraries(PythonQtDynamicLoader_${suffix}${suite_suffix} ${QT_LIBRARIES})
437+
438+
if(run_testsuite_in_static_initializer)
439+
set_property(TARGET
440+
PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix}
441+
PythonQtDynamicLoader_${suffix}${suite_suffix}
442+
PROPERTY COMPILE_DEFINITIONS "RUN_TESTSUITE_IN_STATIC_INITIALIZER" ${extra_compile_definitions}
443+
)
444+
endif()
445+
endmacro()
446+
364447
set(suffix 0)
365448
if(run_testsuite_in_static_initializer)
366449
set(suffix 1)
367450
endif()
368451

369-
add_library(PythonQtDynamicLoaderSharedLibrary_${suffix} SHARED
370-
tests/PythonQtDynamicLoaderSharedLibrary.cpp
371-
${PythonQtTests_sources}
372-
)
373-
target_link_libraries(PythonQtDynamicLoaderSharedLibrary_${suffix} PythonQt)
374-
375-
add_executable(PythonQtDynamicLoader_${suffix} tests/PythonQtDynamicLoader.cpp)
376-
target_link_libraries(PythonQtDynamicLoader_${suffix} ${QT_LIBRARIES})
452+
foreach(suite Api SlotCalling SignalHandler)
453+
set(_compile_definition "${suite}_TEST_SUITE")
454+
set(suite_suffix "_${suite}")
455+
456+
_add_targets("${_compile_definition}")
457+
458+
# Add one test for each test suite case
459+
foreach(case ${PythonQtTest${suite}_cases})
460+
add_test(
461+
NAME tests_PythonQtDynamicLoading_RUN_TESTSUITE_IN_STATIC_INITIALIZER_${suffix}_${suite}_${case}
462+
COMMAND ${CMAKE_COMMAND}
463+
-DTARGET_FILE=$<TARGET_FILE:PythonQtDynamicLoader_${suffix}${suite_suffix}>
464+
-DSUITE=${suite} -DCASE=${case} -P ${test_wrapper}
465+
)
466+
endforeach()
467+
endforeach()
377468

378-
if(run_testsuite_in_static_initializer)
379-
set_target_properties(
380-
PythonQtDynamicLoaderSharedLibrary_${suffix}
381-
PythonQtDynamicLoader_${suffix}
382-
PROPERTIES COMPILE_DEFINITIONS "RUN_TESTSUITE_IN_STATIC_INITIALIZER"
383-
)
384-
endif()
469+
# suite: ALL
470+
set(suite_suffix "")
471+
_add_targets("")
385472

386473
add_test(
387474
NAME tests_PythonQtDynamicLoading_RUN_TESTSUITE_IN_STATIC_INITIALIZER_${suffix}

tests/PythonQtDynamicLoader.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ int main(int argc, char* argv[])
3232
return EXIT_FAILURE;
3333
}
3434

35+
typedef int (*FUNC_RETURNS_INT_TYPE)();
36+
FUNC_RETURNS_INT_TYPE func = 0;
37+
3538
//
3639
// Resolve and invoke 'this_function_returns_42' function.
3740
//
38-
typedef int (*FUNC_RETURNS_INT_TYPE)();
39-
FUNC_RETURNS_INT_TYPE func = (FUNC_RETURNS_INT_TYPE) library.resolve("this_function_returns_42");
41+
func = (FUNC_RETURNS_INT_TYPE) library.resolve("this_function_returns_42");
4042
if (!func)
4143
{
4244
std::cerr << "Failed to resolve symbol 'this_function_returns_42'" << std::endl;
@@ -56,16 +58,14 @@ int main(int argc, char* argv[])
5658
//
5759
// Resolve and invoke 'run_pythonqt_tests' function.
5860
//
59-
typedef int (*FUNC_ARGC_ARGV_RETURNS_INT_TYPE)(int argc, char* argv[]);
60-
FUNC_ARGC_ARGV_RETURNS_INT_TYPE func2 =
61-
(FUNC_ARGC_ARGV_RETURNS_INT_TYPE) library.resolve("run_pythonqt_tests");
62-
if (!func2)
61+
func = (FUNC_RETURNS_INT_TYPE) library.resolve("run_pythonqt_tests");
62+
if (!func)
6363
{
6464
std::cerr << "Failed to resolve symbol 'run_pythonqt_tests'" << std::endl;
6565
return EXIT_FAILURE;
6666
}
6767

68-
result = func2(argc, argv);
68+
result = func();
6969
expected = 0;
7070
if (result != expected)
7171
{

tests/PythonQtDynamicLoaderSharedLibrary.cpp

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "PythonQt.h"
44
#include "PythonQtTests.h"
55

6+
#include <QtGlobal>
7+
68
#ifdef Q_OS_WIN
79
#define MY_EXPORT __declspec(dllexport)
810
#else
@@ -17,37 +19,72 @@ extern "C"
1719
return 42;
1820
}
1921

20-
int _run_pythonqt_tests(int argc, char* argv[])
22+
int _run_pythonqt_tests()
2123
{
22-
std::cout << " running test suite" << std::endl;
24+
bool all = qgetenv("SUITE").isNull();
25+
bool api_suite = !all && strcmp(qgetenv("SUITE"), "Api") == 0;
26+
bool slotCalling_suite = !all && strcmp(qgetenv("SUITE"), "SlotCalling") == 0;
27+
bool signalHandler_suite = !all && strcmp(qgetenv("SUITE"), "SignalHandler") == 0;
28+
29+
int argc = 0;
30+
char* argv[2];
31+
if (all)
32+
{
33+
argv[0] = new char[strlen("ALL") + 1];
34+
strcpy(argv[0], "ALL");
35+
++argc;
36+
}
37+
else
38+
{
39+
argv[0] = new char[qgetenv("SUITE").size() + 1];
40+
strcpy(argv[0], qgetenv("SUITE"));
41+
++argc;
42+
Q_ASSERT(!qgetenv("CASE").isNull());
43+
argv[1] = new char[qgetenv("CASE").size() + 1];
44+
strcpy(argv[1], qgetenv("CASE"));
45+
++argc;
46+
}
47+
48+
std::cout << " running test suite '" << argv[0] << "'";
49+
if (!all) { std::cout << " - case '" << argv[1] << "'"; }
50+
std::cout << std::endl;
2351

2452
// Copied from PythonQtTestMain.cpp
2553
int failCount = 0;
26-
PythonQtTestApi api;
27-
failCount += QTest::qExec(&api, argc, argv);
28-
PythonQtTestSignalHandler signalHandler;
29-
failCount += QTest::qExec(&signalHandler, argc, argv);
30-
PythonQtTestSlotCalling slotCalling;
31-
failCount += QTest::qExec(&slotCalling, argc, argv);
54+
if (all || api_suite)
55+
{
56+
PythonQtTestApi api;
57+
failCount += QTest::qExec(&api, argc, argv);
58+
}
59+
if (all || signalHandler_suite)
60+
{
61+
PythonQtTestSignalHandler signalHandler;
62+
failCount += QTest::qExec(&signalHandler, argc, argv);
63+
}
64+
if (all || slotCalling_suite)
65+
{
66+
PythonQtTestSlotCalling slotCalling;
67+
failCount += QTest::qExec(&slotCalling, argc, argv);
68+
}
3269

3370
PythonQt::cleanup();
3471

3572
if (Py_IsInitialized()) {
3673
Py_Finalize();
3774
}
3875

76+
for(int argpos = 0; argpos < argc; ++argpos) { delete[] argv[argpos]; }
77+
3978
return failCount;
4079
}
4180

4281

43-
int MY_EXPORT run_pythonqt_tests(int argc, char* argv[])
82+
int MY_EXPORT run_pythonqt_tests()
4483
{
4584
std::cout << "run_pythonqt_tests:" << std::endl;
4685
#ifndef RUN_TESTSUITE_IN_STATIC_INITIALIZER
47-
return _run_pythonqt_tests(argc, argv);
86+
return _run_pythonqt_tests();
4887
#else
49-
Q_UNUSED(argc);
50-
Q_UNUSED(argv);
5188
std::cout << " no test suite" << std::endl;
5289
return 0;
5390
#endif
@@ -63,9 +100,7 @@ struct StaticInitializer
63100
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
64101

65102
#ifdef RUN_TESTSUITE_IN_STATIC_INITIALIZER
66-
int argc = 1;
67-
char * argv [] = {"RunTestSuiteInStaticInitializer"};
68-
_run_pythonqt_tests(argc, argv);
103+
_run_pythonqt_tests();
69104
#else
70105
std::cout << " no test suite" << std::endl;
71106
#endif

tests/PythonQtTestMain.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,31 @@
4545

4646
#include <QApplication>
4747

48+
#if !defined(Api_TEST_SUITE) && \
49+
!defined(SignalHandler_TEST_SUITE) && \
50+
!defined(SlotCalling_TEST_SUITE)
51+
# define ALL_TEST_SUITE
52+
#endif
53+
4854
int main(int argc, char *argv[])
4955
{
5056
QApplication qapp(argc, argv);
5157

5258
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
5359

5460
int failCount = 0;
61+
#if defined(ALL_TEST_SUITE) || defined(Api_TEST_SUITE)
5562
PythonQtTestApi api;
5663
failCount += QTest::qExec(&api, argc, argv);
64+
#endif
65+
#if defined(ALL_TEST_SUITE) || defined(SignalHandler_TEST_SUITE)
5766
PythonQtTestSignalHandler signalHandler;
5867
failCount += QTest::qExec(&signalHandler, argc, argv);
68+
#endif
69+
#if defined(ALL_TEST_SUITE) || defined(SlotCalling_TEST_SUITE)
5970
PythonQtTestSlotCalling slotCalling;
6071
failCount += QTest::qExec(&slotCalling, argc, argv);
72+
#endif
6173

6274
PythonQt::cleanup();
6375

0 commit comments

Comments
 (0)