
set(CTEST_OUTPUT_ON_FAILURE TRUE)

file(GLOB_RECURSE test_source *.cpp)
file(GLOB test_specs *.json)

file(COPY ${CMAKE_SOURCE_DIR}/src/examples/server.key ${CMAKE_SOURCE_DIR}/src/examples/server.pem DESTINATION ${CMAKE_BINARY_DIR})
file(COPY ${CMAKE_SOURCE_DIR}/src/examples/server.key ${CMAKE_SOURCE_DIR}/src/examples/server.pem DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
file(COPY ${test_specs} DESTINATION ${CMAKE_BINARY_DIR})
file(COPY ${test_specs} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

include_directories(..)
include_directories(${CMAKE_BINARY_DIR})
include_directories(${JSONCPP_INCLUDE_DIRS})

if(HTTP_CLIENT AND HTTP_SERVER)
        add_definitions(-DHTTP_TESTING)
else()
        list(REMOVE_ITEM test_source "${CMAKE_CURRENT_SOURCE_DIR}/testhttpserver.cpp")
endif()

if(UNIX_DOMAIN_SOCKET_SERVER AND UNIX_DOMAIN_SOCKET_CLIENT)
    add_definitions(-DUNIXDOMAINSOCKET_TESTING)
endif()

if(COMPILE_STUBGEN)
	add_definitions(-DSTUBGEN_TESTING)
endif()

add_executable(unit_testsuite ${test_source})
target_link_libraries(unit_testsuite jsonrpccommon)
target_link_libraries(unit_testsuite jsonrpcserver)
target_link_libraries(unit_testsuite jsonrpcclient)

if(COMPILE_STUBGEN)
        list(APPEND test_source "${CMAKE_BINARY_DIR}/gen/abstractstubserver.h")
        list(APPEND test_source "${CMAKE_BINARY_DIR}/gen/stubclient.h")
        target_link_libraries(unit_testsuite libjsonrpcstub)
        add_dependencies(unit_testsuite common_stubs)
endif()

add_test(client ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite "[client]")
add_test(server ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite "[server]")
add_test(NAME common WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite "[common]")

if(HTTP_CLIENT AND HTTP_SERVER)
        add_test(NAME connector_http WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite "[connector_http]")
endif()

if (UNIX_DOMAIN_SOCKET_CLIENT AND UNIX_DOMAIN_SOCKET_SERVER)
        add_test(NAME connector_unixdomainsocket WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite "[connector_unixdomainsocket]")
endif()

if(COMPILE_STUBGEN)
        add_test(NAME stubgen WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite "[stubgenerator]")
endif()

if(HTTP_CLIENT AND HTTP_SERVER AND COMPILE_STUBGEN)
        add_test(NAME integration WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite "[integration]")
endif()

add_test(NAME all WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit_testsuite)

