diff --git a/CMakeLists.txt b/CMakeLists.txt index e22c99cd..dc84ea46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -277,9 +277,11 @@ if(BUILD_TESTING) tests/PythonQtTests.h ) - QT4_WRAP_CPP(PythonQtTests_sources + QT4_WRAP_CPP(_tmp_moc_sources tests/PythonQtTests.h ) + list(APPEND PythonQtTests_sources ${_tmp_moc_sources}) + list(APPEND moc_sources ${_tmp_moc_sources}) if(PythonQt_Wrap_Qtcore) include_directories(generated_cpp${generated_cpp_suffix}) @@ -288,15 +290,73 @@ if(BUILD_TESTING) tests/PythonQtTestCleanup.cpp tests/PythonQtTestCleanup.h ) - QT4_WRAP_CPP(test_sources + QT4_WRAP_CPP(_tmp_moc_sources tests/PythonQtTestCleanup.h ) + list(APPEND test_sources ${_tmp_moc_sources}) + list(APPEND moc_sources ${_tmp_moc_sources}) set_property(SOURCE tests/PythonQtTestMain.cpp APPEND PROPERTY COMPILE_DEFINITIONS "PythonQt_Wrap_Qtcore") endif() + add_custom_target(PythonQtTestsWrapCpp DEPENDS ${moc_sources}) + + set(PythonQtTestApi_cases + testCall + testVariables + testRedirect + testImporter + testQColorDecorators + testQtNamespace + testConnects + testProperties + testDynamicProperties + ) + + set(PythonQtTestSlotCalling_cases + testNoArgSlotCall + testPODSlotCalls + testCPPSlotCalls + testQVariantSlotCalls + testQListSlotCalls + testQMapSlotCalls + testObjectSlotCalls + testMultiArgsSlotCall + testPyObjectSlotCall + testOverloadedCall + #testCppFactory # XXX Fix this. Running this test standalone is failing. + testInheritance + testAutoConversion + testProperties + ) + + set(PythonQtTestSignalHandler_cases + testSignalHandler + testRecursiveSignalHandler + ) + + foreach(suite Api SlotCalling SignalHandler) + set(_compile_definition "${suite}_TEST_SUITE") + set(_suffix "_${suite}") + + add_executable(PythonQtCppTests${_suffix} ${test_sources} ${PythonQtTests_sources}) + target_link_libraries(PythonQtCppTests${_suffix} PythonQt) + set_target_properties(PythonQtCppTests${_suffix} PROPERTIES COMPILE_DEFINITIONS ${_compile_definition}) + add_dependencies(PythonQtCppTests${_suffix} PythonQtTestsWrapCpp) + + # Add one test for each test suite case + foreach(case ${PythonQtTest${suite}_cases}) + add_test( + NAME tests_PythonQtTestMain_${suite}_${case} + COMMAND ${Slicer_LAUNCH_COMMAND} $ tests/PythonQtTestMain ${case} + ) + endforeach() + endforeach() + + # suite: ALL add_executable(PythonQtCppTests ${test_sources} ${PythonQtTests_sources}) target_link_libraries(PythonQtCppTests PythonQt) + add_dependencies(PythonQtCppTests PythonQtTestsWrapCpp) add_test( NAME tests_PythonQtTestMain @@ -353,6 +413,8 @@ if(UNIX) else() set(ENV{PATH} \"\$ENV{PATH};.\") endif() +set(ENV{SUITE} \"\${SUITE}\") +set(ENV{CASE} \"\${CASE}\") execute_process(COMMAND \${TARGET_FILE} RESULT_VARIABLE result) if(NOT result EQUAL 0) message(FATAL_ERROR \"test failed ! [result: \${result}]\") @@ -361,27 +423,52 @@ endif() function(_add_dynamic_loading_test run_testsuite_in_static_initializer) + macro(_add_targets extra_compile_definitions) + + add_library(PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix} SHARED + tests/PythonQtDynamicLoaderSharedLibrary.cpp + ${PythonQtTests_sources} + ) + target_link_libraries(PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix} PythonQt) + add_dependencies(PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix} PythonQtTestsWrapCpp) + + add_executable(PythonQtDynamicLoader_${suffix}${suite_suffix} tests/PythonQtDynamicLoader.cpp) + target_link_libraries(PythonQtDynamicLoader_${suffix}${suite_suffix} ${QT_LIBRARIES}) + + if(run_testsuite_in_static_initializer) + set_property(TARGET + PythonQtDynamicLoaderSharedLibrary_${suffix}${suite_suffix} + PythonQtDynamicLoader_${suffix}${suite_suffix} + PROPERTY COMPILE_DEFINITIONS "RUN_TESTSUITE_IN_STATIC_INITIALIZER" ${extra_compile_definitions} + ) + endif() + endmacro() + set(suffix 0) if(run_testsuite_in_static_initializer) set(suffix 1) endif() - add_library(PythonQtDynamicLoaderSharedLibrary_${suffix} SHARED - tests/PythonQtDynamicLoaderSharedLibrary.cpp - ${PythonQtTests_sources} - ) - target_link_libraries(PythonQtDynamicLoaderSharedLibrary_${suffix} PythonQt) - - add_executable(PythonQtDynamicLoader_${suffix} tests/PythonQtDynamicLoader.cpp) - target_link_libraries(PythonQtDynamicLoader_${suffix} ${QT_LIBRARIES}) + foreach(suite Api SlotCalling SignalHandler) + set(_compile_definition "${suite}_TEST_SUITE") + set(suite_suffix "_${suite}") + + _add_targets("${_compile_definition}") + + # Add one test for each test suite case + foreach(case ${PythonQtTest${suite}_cases}) + add_test( + NAME tests_PythonQtDynamicLoading_RUN_TESTSUITE_IN_STATIC_INITIALIZER_${suffix}_${suite}_${case} + COMMAND ${CMAKE_COMMAND} + -DTARGET_FILE=$ + -DSUITE=${suite} -DCASE=${case} -P ${test_wrapper} + ) + endforeach() + endforeach() - if(run_testsuite_in_static_initializer) - set_target_properties( - PythonQtDynamicLoaderSharedLibrary_${suffix} - PythonQtDynamicLoader_${suffix} - PROPERTIES COMPILE_DEFINITIONS "RUN_TESTSUITE_IN_STATIC_INITIALIZER" - ) - endif() + # suite: ALL + set(suite_suffix "") + _add_targets("") add_test( NAME tests_PythonQtDynamicLoading_RUN_TESTSUITE_IN_STATIC_INITIALIZER_${suffix} diff --git a/tests/PythonQtDynamicLoader.cpp b/tests/PythonQtDynamicLoader.cpp index 40f4fe7b..d82a628a 100644 --- a/tests/PythonQtDynamicLoader.cpp +++ b/tests/PythonQtDynamicLoader.cpp @@ -32,11 +32,13 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } + typedef int (*FUNC_RETURNS_INT_TYPE)(); + FUNC_RETURNS_INT_TYPE func = 0; + // // Resolve and invoke 'this_function_returns_42' function. // - typedef int (*FUNC_RETURNS_INT_TYPE)(); - FUNC_RETURNS_INT_TYPE func = (FUNC_RETURNS_INT_TYPE) library.resolve("this_function_returns_42"); + func = (FUNC_RETURNS_INT_TYPE) library.resolve("this_function_returns_42"); if (!func) { std::cerr << "Failed to resolve symbol 'this_function_returns_42'" << std::endl; @@ -56,16 +58,14 @@ int main(int argc, char* argv[]) // // Resolve and invoke 'run_pythonqt_tests' function. // - typedef int (*FUNC_ARGC_ARGV_RETURNS_INT_TYPE)(int argc, char* argv[]); - FUNC_ARGC_ARGV_RETURNS_INT_TYPE func2 = - (FUNC_ARGC_ARGV_RETURNS_INT_TYPE) library.resolve("run_pythonqt_tests"); - if (!func2) + func = (FUNC_RETURNS_INT_TYPE) library.resolve("run_pythonqt_tests"); + if (!func) { std::cerr << "Failed to resolve symbol 'run_pythonqt_tests'" << std::endl; return EXIT_FAILURE; } - result = func2(argc, argv); + result = func(); expected = 0; if (result != expected) { diff --git a/tests/PythonQtDynamicLoaderSharedLibrary.cpp b/tests/PythonQtDynamicLoaderSharedLibrary.cpp index 6caa1253..87a7771e 100644 --- a/tests/PythonQtDynamicLoaderSharedLibrary.cpp +++ b/tests/PythonQtDynamicLoaderSharedLibrary.cpp @@ -3,6 +3,8 @@ #include "PythonQt.h" #include "PythonQtTests.h" +#include + #ifdef Q_OS_WIN #define MY_EXPORT __declspec(dllexport) #else @@ -17,18 +19,53 @@ extern "C" return 42; } - int _run_pythonqt_tests(int argc, char* argv[]) + int _run_pythonqt_tests() { - std::cout << " running test suite" << std::endl; + bool all = qgetenv("SUITE").isNull(); + bool api_suite = !all && strcmp(qgetenv("SUITE"), "Api") == 0; + bool slotCalling_suite = !all && strcmp(qgetenv("SUITE"), "SlotCalling") == 0; + bool signalHandler_suite = !all && strcmp(qgetenv("SUITE"), "SignalHandler") == 0; + + int argc = 0; + char* argv[2]; + if (all) + { + argv[0] = new char[strlen("ALL") + 1]; + strcpy(argv[0], "ALL"); + ++argc; + } + else + { + argv[0] = new char[qgetenv("SUITE").size() + 1]; + strcpy(argv[0], qgetenv("SUITE")); + ++argc; + Q_ASSERT(!qgetenv("CASE").isNull()); + argv[1] = new char[qgetenv("CASE").size() + 1]; + strcpy(argv[1], qgetenv("CASE")); + ++argc; + } + + std::cout << " running test suite '" << argv[0] << "'"; + if (!all) { std::cout << " - case '" << argv[1] << "'"; } + std::cout << std::endl; // Copied from PythonQtTestMain.cpp int failCount = 0; - PythonQtTestApi api; - failCount += QTest::qExec(&api, argc, argv); - PythonQtTestSignalHandler signalHandler; - failCount += QTest::qExec(&signalHandler, argc, argv); - PythonQtTestSlotCalling slotCalling; - failCount += QTest::qExec(&slotCalling, argc, argv); + if (all || api_suite) + { + PythonQtTestApi api; + failCount += QTest::qExec(&api, argc, argv); + } + if (all || signalHandler_suite) + { + PythonQtTestSignalHandler signalHandler; + failCount += QTest::qExec(&signalHandler, argc, argv); + } + if (all || slotCalling_suite) + { + PythonQtTestSlotCalling slotCalling; + failCount += QTest::qExec(&slotCalling, argc, argv); + } PythonQt::cleanup(); @@ -36,18 +73,18 @@ extern "C" Py_Finalize(); } + for(int argpos = 0; argpos < argc; ++argpos) { delete[] argv[argpos]; } + return failCount; } - int MY_EXPORT run_pythonqt_tests(int argc, char* argv[]) + int MY_EXPORT run_pythonqt_tests() { std::cout << "run_pythonqt_tests:" << std::endl; #ifndef RUN_TESTSUITE_IN_STATIC_INITIALIZER - return _run_pythonqt_tests(argc, argv); + return _run_pythonqt_tests(); #else - Q_UNUSED(argc); - Q_UNUSED(argv); std::cout << " no test suite" << std::endl; return 0; #endif @@ -63,9 +100,7 @@ struct StaticInitializer PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut); #ifdef RUN_TESTSUITE_IN_STATIC_INITIALIZER - int argc = 1; - char * argv [] = {"RunTestSuiteInStaticInitializer"}; - _run_pythonqt_tests(argc, argv); + _run_pythonqt_tests(); #else std::cout << " no test suite" << std::endl; #endif diff --git a/tests/PythonQtTestMain.cpp b/tests/PythonQtTestMain.cpp index d6737cd6..411af70e 100644 --- a/tests/PythonQtTestMain.cpp +++ b/tests/PythonQtTestMain.cpp @@ -45,6 +45,12 @@ #include +#if !defined(Api_TEST_SUITE) && \ + !defined(SignalHandler_TEST_SUITE) && \ + !defined(SlotCalling_TEST_SUITE) +# define ALL_TEST_SUITE +#endif + int main(int argc, char *argv[]) { QApplication qapp(argc, argv); @@ -52,12 +58,18 @@ int main(int argc, char *argv[]) PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut); int failCount = 0; +#if defined(ALL_TEST_SUITE) || defined(Api_TEST_SUITE) PythonQtTestApi api; failCount += QTest::qExec(&api, argc, argv); +#endif +#if defined(ALL_TEST_SUITE) || defined(SignalHandler_TEST_SUITE) PythonQtTestSignalHandler signalHandler; failCount += QTest::qExec(&signalHandler, argc, argv); +#endif +#if defined(ALL_TEST_SUITE) || defined(SlotCalling_TEST_SUITE) PythonQtTestSlotCalling slotCalling; failCount += QTest::qExec(&slotCalling, argc, argv); +#endif PythonQt::cleanup();