if(UNIX)
    add_executable(aegisub-lua EXCLUDE_FROM_ALL
        automation/tests/aegisub.cpp
    )
    target_link_libraries(aegisub-lua PRIVATE libaegisub luabins luajit "Boost::locale" "ICU::in")
    add_custom_target(test-automation
        COMMAND sh -c "$(luarocks path); ${PROJECT_BINARY_DIR}/aegisub-lua tests/busted.lua -p moon tests/modules"
        VERBATIM
        WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/automation"
    )
    add_dependencies(test-automation aegisub-lua)
else()
    add_custom_target(test-automation)
endif()

find_package(GTest)
if(GTEST_FOUND)
    add_executable(gtest-run EXCLUDE_FROM_ALL
        tests/tests/access.cpp
        tests/tests/audio.cpp
        tests/tests/cajun.cpp
        tests/tests/calltip_provider.cpp
        tests/tests/character_count.cpp
        tests/tests/color.cpp
        tests/tests/dialogue_lexer.cpp
        tests/tests/format.cpp
        tests/tests/fs.cpp
        tests/tests/hotkey.cpp
        tests/tests/iconv.cpp
        tests/tests/ifind.cpp
        tests/tests/karaoke_matcher.cpp
        tests/tests/keyframe.cpp
        tests/tests/line_iterator.cpp
        tests/tests/line_wrap.cpp
        tests/tests/mru.cpp
        tests/tests/option.cpp
        tests/tests/path.cpp
        tests/tests/signals.cpp
        tests/tests/split.cpp
        tests/tests/syntax_highlight.cpp
        tests/tests/thesaurus.cpp
        tests/tests/time.cpp
        tests/tests/type_name.cpp
        tests/tests/util.cpp
        tests/tests/uuencode.cpp
        tests/tests/vfr.cpp
        tests/tests/word_split.cpp
        tests/support/main.cpp
        tests/support/util.cpp
    )
    target_compile_definitions(gtest-run PRIVATE CMAKE_BUILD)
    target_include_directories(gtest-run PRIVATE "${PROJECT_SOURCE_DIR}/tests/support")
    target_link_libraries(gtest-run PRIVATE libaegisub "Boost::filesystem" "GTest::GTest" "Iconv::Iconv")
    if(MSVC)
        set_target_properties(gtest-run PROPERTIES COMPILE_FLAGS "/Yu${PROJECT_SOURCE_DIR}/tests/support/tests_pre.h" COMPILE_FLAGS "/FI${PROJECT_SOURCE_DIR}/tests/support/tests_pre.h")
    else()
        target_compile_options(gtest-run PRIVATE -include "${PROJECT_SOURCE_DIR}/tests/support/tests_pre.h")
    endif()
    if(WIN32)
        add_custom_target(test-aegisub
            COMMAND "${PROJECT_SOURCE_DIR}/tests/setup.bat"
            COMMAND gtest-run
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tests"
        )
    else()
        add_custom_target(test-aegisub
            COMMAND "${PROJECT_SOURCE_DIR}/tests/setup.sh"
            COMMAND gtest-run
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tests"
        )
    endif()
else()
    add_custom_target(test-aegisub)
endif()

add_custom_target(test DEPENDS test-automation test-aegisub)
