# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.

cmake_minimum_required(VERSION 4.3)
include("${CMAKE_CURRENT_LIST_DIR}/ParseTestList.cmake")

set(filter)
if(TEST_FILTER)
  set(filter "--gtest_filter=${TEST_FILTER}")
endif()

set(launcher "")
if(NOT TEST_EXECUTOR STREQUAL "")
  list(JOIN TEST_EXECUTOR "]==] [==[" launcher)
  set(launcher "[==[${launcher}]==]")
endif()

set(discovery_extra_args "")
if(NOT TEST_DISCOVERY_EXTRA_ARGS STREQUAL "")
  list(JOIN TEST_DISCOVERY_EXTRA_ARGS "]==] [==[" discovery_extra_args)
  set(discovery_extra_args "[==[${discovery_extra_args}]==]")
endif()

string(SHA256 target_hash "${TEST_EXECUTABLE}")
string(SUBSTRING "${target_hash}" 0 10 target_hash)
set(json_file "${CMAKE_BINARY_DIR}/CMakeFiles/googletest_discovery_${target_hash}.json")
file(REMOVE "${json_file}")

cmake_language(EVAL CODE
  "execute_process(
    COMMAND ${launcher} [==[${TEST_EXECUTABLE}]==]
      --gtest_list_tests
      [==[--gtest_output=json:${json_file}]==]
      ${filter}
      ${discovery_extra_args}
    COMMAND_ERROR_IS_FATAL LAST
    OUTPUT_VARIABLE output
  )"
)

macro(write_test_line)
  # Store the gtest test name before messing with these strings
  set(gtest_name ${current_test_suite}.${current_test_name})

  set(pretty_test_suite ${current_test_suite})
  set(pretty_test_name ${current_test_name})

  # Handle disabled tests
  set(disabled OFF)
  if(pretty_test_suite MATCHES "^DISABLED_" OR pretty_test_name MATCHES "^DISABLED_")
    set(disabled ON)
    string(REGEX REPLACE "^DISABLED_" "" pretty_test_suite "${pretty_test_suite}")
    string(REGEX REPLACE "^DISABLED_" "" pretty_test_name "${pretty_test_name}")
  endif()

  if (NOT current_test_value_param STREQUAL "" AND NOT NO_PRETTY_VALUES)
    # If the test name contains a value parameter name (part of the string after last slash), and this
    # name is an integer which indicates that it's the default name generated by googletest, replace it by
    # the value parameter name provided separately
    if("${pretty_test_name}" MATCHES "^(.*)/[0-9]+$")
      set(pretty_test_name "${CMAKE_MATCH_1}/${current_test_value_param}")
    endif()
  endif()

  if(NOT current_test_type_param STREQUAL "")
    # Parse type param name from suite name
    if(pretty_test_suite MATCHES "^(.+)/(.+)$")
      set(pretty_test_suite "${CMAKE_MATCH_1}")
      set(current_type_param_name "${CMAKE_MATCH_2}")
    else()
      set(current_type_param_name "")
    endif()
    if (NOT NO_PRETTY_TYPES)
      string(APPEND pretty_test_name "<${current_test_type_param}>")
    elseif(NOT current_type_param_name STREQUAL "")
      string(APPEND pretty_test_name "<${current_type_param_name}>")
    endif()
  endif()

  set(testname "${pretty_test_suite}.${pretty_test_name}")

  # unescape []
  if(open_sb)
    string(REPLACE "${open_sb}" "[" testname "${testname}")
  endif()
  if(close_sb)
    string(REPLACE "${close_sb}" "]" testname "${testname}")
  endif()

  set(location "")
  if(NOT current_test_file STREQUAL "" AND NOT current_test_line STREQUAL "")
    set(location "${current_test_file}:${current_test_line}")
  endif()

  message(STATUS "${testname}#${gtest_name}#DISABLED=${disabled}#LOCATION=${location}#")
endmacro()

if(EXISTS "${json_file}")
  parse_tests_from_json("${json_file}" write_test_line)
else()
  # gtest < 1.8.1, and all gtest compiled with GTEST_HAS_FILE_SYSTEM=0, don't
  # recognize the --gtest_output=json option, and issue a warning or error on
  # stdout about it being unrecognized, but still return an exit code 0 for
  # success. All versions report the test list on stdout whether
  # --gtest_output=json is recognized or not.

  # NOTE: Because we are calling a macro, we don't want to pass "output" as
  # an argument because it messes up the contents passed through due to the
  # different escaping, etc. that gets applied. We rely on it picking up the
  # "output" variable we have already set here.
  parse_tests_from_output(write_test_line)
endif()
