# -*- conding: utf-8; cmake-tab-width: 4; indent-tabs-mode: nil; -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

if( ENABLE_SWIG_PYTHON AND PYTHON_EXECUTABLE )
    function(mock_server_test name command)
        if( WIN32 )
            set(TPATH ${NDS_LIBRARY_DIR})
        else( WIN32 )
            set(TPATH "$ENV{PATH}")
        endif( WIN32 )
        add_test(
            NAME ${name}
            COMMAND
                ${PROG_ENV} "PYTHONPATH=${TPATH}"
                "${PYTHON_EXECUTABLE}" "${PROG_NDS_TESTER}" ${NDS_TESTER_FLAGS}
                --nds1-mock-server
                "${CMAKE_CURRENT_SOURCE_DIR}/${command}" )
        set_tests_properties( ${name}
            PROPERTIES
                SKIP_RETURN_CODE 77 )
        set( NDS_CUSTOM_MEMCHECK_IGNORE
            ${NDS_CUSTOM_MEMCHECK_IGNORE} ${name}
            CACHE INTERNAL "" )
    endfunction(mock_server_test)
    function(replay_server_test name command json)
        Message("" "replay_test name=${name} command=${command} json=${json} argn=${ARGN}")
        if( WIN32 )
            set(TPATH ${NDS_LIBRARY_DIR})
        else( WIN32 )
            set(TPATH "$ENV{PATH}")
        endif( WIN32 )
        if ( REPLAY_BLOB_CACHE_DIR )
            set_nds_protocols(protocols ${name})
            foreach ( proto ${protocols} )
                set(test_name "${name}${proto}_")
                add_test(
                    NAME ${test_name}
                    COMMAND
                        ${PROG_ENV} "PYTHONPATH=${TPATH}" "REPLAY_BLOB_REMOTE_CACHE=${REPLAY_BLOB_CACHE_DIR}"
                        "${PYTHON_EXECUTABLE}" "${PROG_NDS_TESTER}" ${NDS_TESTER_FLAGS}
                        --replay-server
                        --replay-json-filename ${json}
                        --replay-protocol ${proto}
                        "${CMAKE_CURRENT_SOURCE_DIR}/${command}" ${ARGN} )
                set_tests_properties( ${test_name}
                    PROPERTIES
                        SKIP_RETURN_CODE 77 )
                set( NDS_CUSTOM_MEMCHECK_IGNORE
                    ${NDS_CUSTOM_MEMCHECK_IGNORE} ${test_name}
                    CACHE INTERNAL "" )
            endforeach ( proto )
        endif ( REPLAY_BLOB_CACHE_DIR )
    endfunction(replay_server_test)

    function(add_python_test target)
        list(GET ARGN 0 cmd )
        list(REMOVE_AT ARGN 0)
        if( WIN32 )
            set(TPATH ${NDS_LIBRARY_DIR})
        else( WIN32 )
            set(TPATH "$ENV{PATH}")
        endif( WIN32 )
        add_test( ${target} 
            ${PROG_ENV} "PYTHONPATH=${TPATH}"
            "${PYTHON_EXECUTABLE}" "${PROG_NDS_TESTER}" ${NDS_TESTER_FLAGS}
            "${CMAKE_CURRENT_SOURCE_DIR}/${cmd}" ${ARGN})
        set_tests_properties( ${target}
            PROPERTIES
                SKIP_RETURN_CODE 77 )
    endfunction(add_python_test)
    #-----------------------------------------------------------------------
    #-----------------------------------------------------------------------

    if (WIN32)
        set( NDS_LIBRARY_DIR "" )
        set( NDS_LIBRARY_DIR $ENV{PATH} )
        set( GENERATOR "Debug" )
        list( INSERT NDS_LIBRARY_DIR 0 "${CMAKE_BINARY_DIR}/bin/${GENERATOR}" )
        set( TMP_NDS_LIBRARY_DIR "" )
        foreach( TMP ${NDS_LIBRARY_DIR} )
            file( TO_NATIVE_PATH ${TMP} TMP )
            string( REPLACE "\\" "\\\\" TMP ${TMP} )
            #list( APPEND TMP_NDS_LIBRARY_DIR ${TMP} )
            string( LENGTH "${TMP_NDS_LIBRARY_DIR}" L )
            if( $L LESS 1 )
                set( TMP_NDS_LIBRARY_DIR ${TMP} )
            else( $L LESS 1 )
                set( TMP_NDS_LIBRARY_DIR "${TMP_NDS_LIBRARY_DIR}\\\\;${TMP}" )
            endif( $L LESS 1 )
        endforeach( TMP )
        #join( ${TMP_NDS_LIBRARY_DIR} ";" NDS_LIBRARY_DIR )
        set( NDS_LIBRARY_DIR ${TMP_NDS_LIBRARY_DIR} )
        string( REPLACE "\\" "\\\\" NDS_LIBRARY_DIR ${NDS_LIBRARY_DIR} )
        #get_target_property( NDS_LIBRARY_DIR nds2JNI LIBRARY_OUTPUT_DIRECTORY )
        message( STATUS "NDS_LIBRARY_DIR: ${NDS_LIBRARY_DIR}" )
    else (WIN32)
        get_target_property( NDS_LIBRARY_DIR nds2 LIBRARY_OUTPUT_DIRECTORY )
    endif(WIN32)

    #====================================================================
    # These tests require a minimum of Python
    #====================================================================
    # Final variable substitution before use
    #--------------------------------------------------------------------
    string(CONFIGURE "${NDS_TESTER_FLAGS}" NDS_TESTER_FLAGS )
    #--------------------------------------------------------------------
    add_python_test(_python_is-trend-type_ test_is_trend_type.py)
    #--------------------------------------------------------------------
    mock_server_test( _python_mockup_ test_mockup.py )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_fetch_
        test_fetch.py
        lho-prod.json )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_find-channels-synthetic_
        test_nds2_find_channels_synthetic.py
        nds2-find-channels-synthetic.json )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_availability_
        test_nds2_availability.py
        nds2-availability.json )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds1_get-epochs_
        test_get_epochs.py
        nds1-get-epoch.json -proto-1 )
    replay_server_test(
        _python_nds2_get-epochs_
        test_get_epochs.py
        nds2-get-epoch.json -proto-2 )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_dissimilar-gaps_
        test_nds2_dissimilar_gaps.py
        nds2-dissimilar_gaps.json )
    #--------------------------------------------------------------------
    replay_server_test(
            _python_nds2_ticket-169_
            test_ticket_169.py
            nds2-ticket-169.json )
    #--------------------------------------------------------------------
    replay_server_test(
            _python_nds2_ticket-242_
            test_ticket_242.py
            nds2-ticket-242.json )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_ticket-246_
        test_ticket_246.py
        nds2-ticket-246.json )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds1_iterate-with-gaps_
        test_iterate_with_gaps.py
        nds1-iterate_with_leading_gap.json
        -proto-1 )
    replay_server_test(
            _python_nds2_iterate-with-gaps_
            test_iterate_with_gaps.py
            nds2-iterate_with_leading_gap.json
            -proto-2 )
    replay_server_test(
            _python_nds2_iterate-no-data_
            test_nds2_iterate_no_data.py
            nds2-iterate_with_no_data.json
    )
    #--------------------------------------------------------------------
    replay_server_test(
            _python_nds2_get-last-message_
            test_nds2_get_last_message.py
            nds2-get-last-message.json )
    #--------------------------------------------------------------------
    replay_server_test(
            _python_nds1_count-channels_
            test_nds_count_channels.py
            nds1-count-channels-synthetic.json
            -proto-1 )
    replay_server_test(
            _python_nds2_count-channels_
            test_nds_count_channels.py
            nds2-count-channels-synthetic.json
            -proto-2 )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds1_timeout_
        test_timeout.py
        nds1-timeout.json
        -proto-1)
    replay_server_test(
        _python_nds2_timeout_
        test_timeout.py
        nds2-timeout.json
        -proto-2)
    #--------------------------------------------------------------------
    replay_server_test(
            _python_nds2_ticket-288_
            test_ticket_288.py
            nds2-ticket-288.json)
    #--------------------------------------------------------------------
    replay_server_test(
            _python_nds2_ticket-289_
            test_ticket_289.py
            nds2-ticket-289.json)
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_iterate-all-data_
        test_nds_iterate_all_data.py
        nds2-iterate-all-data-synthetic.json
        -proto-2
    )
    replay_server_test(
        _python_nds2_iterate-all-data-nogaphandler_
        test_nds_iterate_all_data.py
        nds2-iterate-all-data-synthetic.json
        -proto-2 -no-gap
    )
    replay_server_test(
        _python_nds1_iterate-all-data_
        test_nds_iterate_all_data.py
        nds1-iterate-all-data-synthetic.json
        -proto-1
    )
    replay_server_test(
        _python_nds1_iterate-all-data-nogaphandler_
        test_nds_iterate_all_data.py
        nds1-iterate-all-data-synthetic.json
        -proto-1 -no-gap
    )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds1_iterate-with-synth-gaps_
        test_nds_iterate_with_synth_gaps.py
        nds1-iterate-with-gaps-synthetic.json
        -proto-1
    )
    replay_server_test(
        _python_nds1_iterate-with-synth-gaps-nogaphandler_
        test_nds_iterate_with_synth_gaps.py
        nds1-iterate-with-gaps-synthetic.json
        -proto-1 -no-gap
    )
    replay_server_test(
        _python_nds1_iterate-with-synth-gaps-defaultgaphandler_
        test_nds_iterate_with_synth_gaps.py
        nds1-iterate-with-gaps-synthetic.json
        -proto-1 -default-gap-handling
    )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_iterate-live-data-bounded_
        test_nds_iterate_live_data_bounded.py
        nds2-iterate-live-data-bounded-synthetic.json
        -proto-2
    )
    replay_server_test(
        _python_nds1_iterate-live-data-bounded_
        test_nds_iterate_live_data_bounded.py
        nds1-iterate-live-data-bounded-synthetic.json
        -proto-1
    )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds2_iterate-live-data_
        test_nds_iterate_live_data.py
        nds2-iterate-live-data-synthetic.json
        -proto-2
    )
    replay_server_test(
        _python_nds1_iterate-live-data_
        test_nds_iterate_live_data.py
        nds1-iterate-live-data-synthetic.json
        -proto-1
    )
    #--------------------------------------------------------------------
    replay_server_test(
        _python_nds1_get_parameters_
        test_nds_get_parameters.py
        nds1-get-parameters.json
        -proto-1
    )
    replay_server_test(
        _python_nds2_get_parameters_
        test_nds_get_parameters.py
        nds2-get-parameters.json
        -proto-2
    )
    #--------------------------------------------------------------------
endif( ENABLE_SWIG_PYTHON AND PYTHON_EXECUTABLE )
