# Adding D unittests
#
#  Copyright (c) 2010 Jens Mueller <jens.k.mueller@gmx.de>
#
# All rights reserved.
#
# See LICENSE for details.
#

macro(_add_unittests _TEST_TARGET _SOURCES _INCLUDE_DIRECTORIES _LINK_LIBRARIES)
    set(_TEST_FLAGS "${CMAKE_D_FLAGS_DEBUG_INIT}")
    separate_arguments(_TEST_FLAGS)
    
    set(_TEST_MAIN "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/unittest.d")
    if(NOT EXISTS ${_TEST_MAIN})
        file(WRITE ${_TEST_MAIN} "// Generated by UseDUnittest.cmake\nint main() { return 0; }")
    endif()

    #if(_INCLUDE_DIRECTORIES)
        set(_TEST_INCLUDE_DIRECTORIES )
        foreach(_INCLUDE_DIRECTORY ${_INCLUDE_DIRECTORIES})
            if(_INCLUDE_DIRECTORY) # since commented out if doesn't work for some reason
                list(APPEND _TEST_INCLUDE_DIRECTORIES "${CMAKE_INCLUDE_FLAG_D}${_INCLUDE_DIRECTORY}")
            endif()
        endforeach()
    #endif()

    #if(_LINK_LIBRARIES)
        set(_TEST_LINK_LIBRARIES )
        foreach(_LINK_LIBRARY ${_LINK_LIBRARIES})
            if(_LINK_LIBRARY) # since commented out if doesn't work for some reason
                list(APPEND _TEST_LINK_LIBRARIES "${CMAKE_LINK_LIBRARY_FLAG}${_LINK_LIBRARY}")
            endif()
        endforeach()
    #endif()

    set(_TEST_SOURCES )
    foreach(_SOURCE ${_SOURCES})
        list(APPEND _TEST_LINK_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/${_SOURCE}")
    endforeach()

    if(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
        set(_TEST_LIBRARY_PATH "${CMAKE_LIBRARY_PATH_FLAG}${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
    endif()

    add_test(NAME build_${_TEST_TARGET}
        COMMAND ${CMAKE_D_COMPILER} ${_TEST_FLAGS} ${_TEST_INCLUDE_DIRECTORIES} ${_TEST_LIBRARY_PATH} ${_TEST_LINK_LIBRARIES} -of${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_TEST_TARGET} ${_TEST_SOURCES} -unittest ${_TEST_MAIN})

    add_test(NAME ${_TEST_TARGET}
        COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_TEST_TARGET})
    set_tests_properties(${_TEST_TARGET} PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
endmacro()

macro(add_unittests_for_target _TARGET)
    set(_TEST_TARGET "test_${_TARGET}")
    
    get_target_property(_TARGET_TEST_SOURCES ${_TARGET} SOURCES)
    if(_TARGET_TEST_SOURCES)    
        get_target_property(_TARGET_INCLUDE_DIRECTORIES ${_TARGET} INCLUDE_DIRECTORIES)
        get_target_property(_TARGET_LINK_LIBRARIES ${_TARGET} LINK_LIBRARIES)

        _add_unittests(${_TEST_TARGET} "${_TARGET_TEST_SOURCES}" "${_TARGET_INCLUDE_DIRECTORIES}" "${_TARGET_LINK_LIBRARIES}")
    else()
        message(SEND_ERROR "cannot add unittests for target ${TARGET }with no source files")
    endif()
endmacro()

macro(add_unittests _SOURCE)
    set(_TEST_TARGET "test_${_SOURCE}")

    get_directory_property(_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
    get_directory_property(_LINK_LIBRARIES LINK_LIBRARIES)
    
    _add_unittests(${_TEST_TARGET} "${_SOURCE}" "${_INCLUDE_DIRECTORIES}" "${_LINK_LIBRARIES}")
endmacro()
