Skip to content

Commit

Permalink
Add tests for each test case in Api, SlotCalling and SignalHandler te…
Browse files Browse the repository at this point in the history
…st suites.
  • Loading branch information
jcfr committed Feb 17, 2016
1 parent f4d4999 commit a7f9174
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 39 deletions.
121 changes: 104 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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} $<TARGET_FILE:PythonQtCppTests_${suite}> 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
Expand Down Expand Up @@ -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}]\")
Expand All @@ -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=$<TARGET_FILE:PythonQtDynamicLoader_${suffix}${suite_suffix}>
-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}
Expand Down
14 changes: 7 additions & 7 deletions tests/PythonQtDynamicLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down
65 changes: 50 additions & 15 deletions tests/PythonQtDynamicLoaderSharedLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "PythonQt.h"
#include "PythonQtTests.h"

#include <QtGlobal>

#ifdef Q_OS_WIN
#define MY_EXPORT __declspec(dllexport)
#else
Expand All @@ -17,37 +19,72 @@ 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();

if (Py_IsInitialized()) {
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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/PythonQtTestMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,31 @@

#include <QApplication>

#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);

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();

Expand Down

0 comments on commit a7f9174

Please sign in to comment.