File: AddCatchTest.cmake

package info (click to toggle)
sopt 4.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,632 kB
  • sloc: cpp: 13,011; xml: 182; makefile: 6
file content (105 lines) | stat: -rw-r--r-- 3,354 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# First finds or downloads catch

# Function to create a common main
function(common_catch_main)
  if(TARGET common_catch_main_object)
    return()
  endif()
  file(WRITE "${CMAKE_BINARY_DIR}/common_catch_main.cc"
    "#define CATCH_CONFIG_MAIN\n"
    "#include \"catch2/catch_test_macros.hpp\"\n"
  )
  add_library(common_catch_main_object OBJECT "${CMAKE_BINARY_DIR}/common_catch_main.cc")
  if(Catch2_FOUND)
    target_link_libraries(common_catch_main_object PRIVATE Catch2::Catch2)
  endif()
endfunction()

# A function to create a test, once a an executable exists
function(add_catch_test_with_seed testname testexec seed)
  cmake_parse_arguments(catch "NOCATCHLABEL" "WORKING_DIRECTORY" "LABELS;ARGUMENTS" ${ARGN})

  unset(EXTRA_ARGS)
  if(catch_WORKING_DIRECTORY)
    set(EXTRA_ARGS WORKING_DIRECTORY ${catch_WORKING_DIRECTORY})
  endif()
  set(arguments ${catch_ARGUMENTS})
  if(NOT "${seed}" STREQUAL "")
    list(APPEND arguments --rng-seed ${seed})
  else()
    list(APPEND arguments --rng-seed time)
  endif()

  if(CATCH_JUNIT)
    add_test(NAME ${testname}
      COMMAND ${testexec}
          ${arguments}
          -r junit
          -o ${PROJECT_BINARY_DIR}/Testing/${testname}.xml
    )
  else()
    add_test(NAME ${testname} COMMAND ${testexec} ${arguments} ${EXTRA_ARGS})
  endif()

  if(NOT catch_NOCATCHLABEL)
    list(APPEND catch_LABELS catch)
  endif()
  set_tests_properties(${testname} PROPERTIES LABELS "${catch_LABELS}")
endfunction()

# Then adds a function to create a test
function(add_catch_test testname)
  cmake_parse_arguments(catch
    "NOMAIN;NOTEST;NOCATCHLABEL"
    "SEED;WORKING_DIRECTORY;COMMON_MAIN;PRECOMMAND"
    "LIBRARIES;DEPENDS;INCLUDES;LABELS;ARGUMENTS"
    ${ARGN}
  )

  # Source deduce from testname if possible
  unset(source)
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${testname}.cc")
    set(source ${testname}.cc)
  elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${testname}.cpp")
    set(source ${testname}.cpp)
  elseif("${catch_UNPARSED_ARGUMENTS}" STREQUAL "")
    message(FATAL_ERROR "No source given or found for ${testname}")
  endif()

  # By default, uses a common main function for all, compiled once
  # We create here
  if(catch_NOMAIN)
    add_executable(test_${testname} ${source} ${catch_UNPARSED_ARGUMENTS})
  elseif(catch_COMMON_MAIN)
    add_executable(test_${testname}
      ${source} $<TARGET_OBJECTS:${catch_COMMON_MAIN}> ${catch_UNPARSED_ARGUMENTS})
  else()
    common_catch_main()
    add_executable(test_${testname}
      ${source} $<TARGET_OBJECTS:common_catch_main_object> ${catch_UNPARSED_ARGUMENTS})
  endif()

  if(catch_LIBRARIES)
    target_link_libraries(test_${testname} ${catch_LIBRARIES} Catch2::Catch2)
  endif()
  if(TARGET lookup_dependencies)
    add_dependencies(test_${testname} lookup_dependencies)
  endif()

  if(catch_NOCATCHLABEL)
    set(catch_NOCATCHLABEL "NOCATCHLABEL")
  else()
    unset(catch_NOCATCHLABEL)
  endif()
  set(test_command test_${testname})
  if(catch_PRECOMMAND)
    set(test_command "${catch_PRECOMMAND} ${test_command}")
  endif()
  if(NOT catch_NOTEST)
    add_catch_test_with_seed(
      test_${testname} "./test_${testname}" "${catch_SEED}" ${catch_UNPARSED_ARGUMENTS}
      ${catch_NOCATCHLABEL} WORKING_DIRECTORY ${catch_WORKING_DIRECTORY}
      LABELS ${catch_LABELS} ARGUMENTS ${catch_ARGUMENTS}
    )
  endif()
endfunction()