
macro(write_target_test OUTPUT_FILENAME TEST_NAME TEST_TARGET TEST_ROOT TEST_EXPECTED_RESULTS TEST_ABS_TOLERANCE TEST_REL_TOLERANCE TEST_ENV TEST_MERGE_OUTPUT)
    set(TEST_CMD \$<TARGET_FILE:${TEST_TARGET}>)
    _write_test_to_file(${OUTPUT_FILENAME} ${TEST_NAME} ${TEST_CMD} ${TEST_ROOT} ${TEST_EXPECTED_RESULTS} ${TEST_ABS_TOLERANCE} ${TEST_REL_TOLERANCE} ${TEST_ENV} ${TEST_MERGE_OUTPUT} ${ARGN})
endmacro()

macro(write_python_test OUTPUT_FILENAME TEST_NAME TEST_TARGET TEST_ROOT TEST_EXPECTED_RESULTS TEST_ABS_TOLERANCE TEST_REL_TOLERANCE TEST_ENV TEST_MERGE_OUTPUT)
    set(TEST_CMD python|${TEST_ROOT}/${TEST_TARGET})
    _write_test_to_file(${OUTPUT_FILENAME} py${TEST_NAME} ${TEST_CMD} ${TEST_ROOT} ${TEST_EXPECTED_RESULTS} ${TEST_ABS_TOLERANCE} ${TEST_REL_TOLERANCE} ${TEST_ENV} ${TEST_MERGE_OUTPUT} ${ARGN})
endmacro()

macro(write_mp_target_test OUTPUT_FILENAME TEST_NAME TEST_TARGET TEST_NP TEST_ROOT TEST_EXPECTED_RESULTS TEST_ABS_TOLERANCE TEST_REL_TOLERANCE TEST_ENV TEST_MERGE_OUTPUT)
    set(TEST_CMD \${MPIEXEC_EXECUTABLE}|-n|${TEST_NP}|\$<TARGET_FILE:${TEST_TARGET}>)
    _write_test_to_file(${OUTPUT_FILENAME} ${TEST_NAME} ${TEST_CMD} ${TEST_ROOT} ${TEST_EXPECTED_RESULTS} ${TEST_ABS_TOLERANCE} ${TEST_REL_TOLERANCE} ${TEST_ENV} ${TEST_MERGE_OUTPUT} ${ARGN})
endmacro()

macro(write_mp_python_test OUTPUT_FILENAME TEST_NAME TEST_TARGET TEST_NP TEST_ROOT TEST_EXPECTED_RESULTS TEST_ABS_TOLERANCE TEST_REL_TOLERANCE TEST_ENV TEST_MERGE_OUTPUT)
    set(TEST_CMD \${MPIEXEC_EXECUTABLE}|-n|${TEST_NP}|python|${TEST_ROOT}/${TEST_TARGET})
    _write_test_to_file(${OUTPUT_FILENAME} py${TEST_NAME} ${TEST_CMD} ${TEST_ROOT} ${TEST_EXPECTED_RESULTS} ${TEST_ABS_TOLERANCE} ${TEST_REL_TOLERANCE} ${TEST_ENV} ${TEST_MERGE_OUTPUT} ${ARGN})
endmacro()

function(_write_test_to_file OUTPUT_FILENAME TEST_NAME TEST_CMD TEST_ROOT TEST_EXPECTED_RESULTS TEST_ABS_TOLERANCE TEST_REL_TOLERANCE TEST_ENV TEST_MERGE_OUTPUT)
    foreach(_arg ${ARGN})
        string(REPLACE "|" ";" _args_list ${_arg})
        set(_extended_args)
        foreach(_arg_in ${_args_list})
            if (EXISTS "${TEST_ROOT}/${_arg_in}")
                list(APPEND _extended_args "${TEST_ROOT}/${_arg_in}")
            else ()
                list(APPEND _extended_args ${_arg_in})
            endif ()
        endforeach()
        string(REPLACE ";" "|" _extended_args "${_extended_args}")
        set(TEST_CMD "${TEST_CMD}|${_extended_args}")
    endforeach()
    if (TEST_EXPECTED_RESULTS AND NOT TEST_EXPECTED_RESULTS STREQUAL "NOTFOUND")
        set(EXPECTED_OUTPUT_ARGUMENT -DEXPECTED_OUTPUT=${TEST_ROOT}/${TEST_EXPECTED_RESULTS})
    endif ()

    set(_TMP_OUTPUT "
# Create output directory
file(MAKE_DIRECTORY \${CMAKE_BINARY_DIR}/test_runs/${TEST_NAME})

add_test(NAME ${TEST_NAME}
   COMMAND ${CMAKE_COMMAND}
   -DTEST_NAME=${TEST_NAME}
   -DNDIFF_EXECUTABLE=${NDIFF_EXECUTABLE}
   -DOCMERGE_EXECUTABLE=${OCMERGE_EXECUTABLE}
   -DTEST_CMD=${TEST_CMD}
   -DTEST_ABS_TOLERANCE=${TEST_ABS_TOLERANCE}
   -DTEST_REL_TOLERANCE=${TEST_REL_TOLERANCE}
   -DTEST_MERGE_OUTPUT=${TEST_MERGE_OUTPUT}
   ${EXPECTED_OUTPUT_ARGUMENT}
   -DTEST_OUTPUT=\${CMAKE_BINARY_DIR}/test_runs/${TEST_NAME}
   -P ${TESTS_BASE_DIR}/run_test.cmake)
   
      set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT \"${TEST_ENV}\")


")
    file(APPEND ${OUTPUT_FILENAME} ${_TMP_OUTPUT})
endfunction()
