Przeglądaj źródła

CMakeParseArguments: add a RunCMake test suite

Matthias Maennich 10 lat temu
rodzic
commit
cbbdfc2b61

+ 1 - 0
Tests/RunCMake/CMakeLists.txt

@@ -177,6 +177,7 @@ add_RunCMake_test(build_command)
 add_RunCMake_test(execute_process)
 add_RunCMake_test(export)
 add_RunCMake_test(cmake_minimum_required)
+add_RunCMake_test(cmake_parse_arguments)
 add_RunCMake_test(continue)
 add_RunCMake_test(ctest_build)
 add_RunCMake_test(ctest_configure)

+ 3 - 0
Tests/RunCMake/cmake_parse_arguments/CMakeLists.txt

@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.4)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)

+ 16 - 0
Tests/RunCMake/cmake_parse_arguments/CornerCases.cmake

@@ -0,0 +1,16 @@
+include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake)
+include(CMakeParseArguments)
+
+# example from the documentation
+# OPTIONAL is a keyword and therefore terminates the definition of
+# the multi-value DEFINITION before even a single value has been added
+
+set(options OPTIONAL FAST)
+set(oneValueArgs DESTINATION RENAME)
+set(multiValueArgs TARGETS CONFIGURATIONS)
+cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}"
+                      "${multiValueArgs}"
+                      TARGETS foo DESTINATION OPTIONAL)
+
+TEST(MY_INSTALL_DESTINATION UNDEFINED)
+TEST(MY_INSTALL_OPTIONAL TRUE)

+ 1 - 0
Tests/RunCMake/cmake_parse_arguments/Errors-result.txt

@@ -0,0 +1 @@
+1

+ 17 - 0
Tests/RunCMake/cmake_parse_arguments/Errors-stderr.txt

@@ -0,0 +1,17 @@
+CMake Error at Errors.cmake:3 \(cmake_parse_arguments\):
+  CMAKE_PARSE_ARGUMENTS Function invoked with incorrect arguments for
+  function named: CMAKE_PARSE_ARGUMENTS
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
++
+CMake Error at Errors.cmake:4 \(cmake_parse_arguments\):
+  CMAKE_PARSE_ARGUMENTS Function invoked with incorrect arguments for
+  function named: CMAKE_PARSE_ARGUMENTS
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
++
+CMake Error at Errors.cmake:5 \(cmake_parse_arguments\):
+  CMAKE_PARSE_ARGUMENTS Function invoked with incorrect arguments for
+  function named: CMAKE_PARSE_ARGUMENTS
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 6 - 0
Tests/RunCMake/cmake_parse_arguments/Errors.cmake

@@ -0,0 +1,6 @@
+include(CMakeParseArguments)
+
+cmake_parse_arguments()
+cmake_parse_arguments(prefix OPT)
+cmake_parse_arguments(prefix OPT SINGLE)
+cmake_parse_arguments(prefix OPT SINGLE MULTI) # not an error

+ 69 - 0
Tests/RunCMake/cmake_parse_arguments/Initialization.cmake

@@ -0,0 +1,69 @@
+include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake)
+include(CMakeParseArguments)
+
+# unparsed arguments
+cmake_parse_arguments(pref "" "" "")
+TEST(pref_UNPARSED_ARGUMENTS UNDEFINED)
+
+cmake_parse_arguments(pref "" "" "" FOO)
+TEST(pref_UNPARSED_ARGUMENTS "FOO")
+cmake_parse_arguments(pref "" "" "" FOO BAR)
+TEST(pref_UNPARSED_ARGUMENTS "FOO;BAR")
+cmake_parse_arguments(pref "" "" "")
+TEST(pref_UNPARSED_ARGUMENTS UNDEFINED)
+
+
+# options
+cmake_parse_arguments(pref "OPT1" "" "")
+TEST(pref_OPT1 FALSE)
+
+cmake_parse_arguments(pref "OPT1;OPT2" "" "")
+TEST(pref_OPT1 FALSE)
+TEST(pref_OPT2 FALSE)
+
+cmake_parse_arguments(pref "OPT1" "" "" OPT1)
+TEST(pref_OPT1 TRUE)
+cmake_parse_arguments(pref "OPT1;OPT2" "" "" OPT1 OPT2)
+TEST(pref_OPT1 TRUE)
+TEST(pref_OPT2 TRUE)
+cmake_parse_arguments(pref "OPT1;OPT2" "" "")
+TEST(pref_OPT1 FALSE)
+TEST(pref_OPT2 FALSE)
+
+
+# single arguments
+cmake_parse_arguments(pref "" "SINGLE1" "")
+TEST(pref_SINGLE1 UNDEFINED)
+
+cmake_parse_arguments(pref "" "SINGLE1;SINGLE2" "")
+TEST(pref_SINGLE1 UNDEFINED)
+TEST(pref_SINGLE2 UNDEFINED)
+
+
+cmake_parse_arguments(pref "" "SINGLE1" "" SINGLE1 foo)
+TEST(pref_SINGLE1 foo)
+cmake_parse_arguments(pref "" "SINGLE1;SINGLE2" "" SINGLE1 foo SINGLE2 bar)
+TEST(pref_SINGLE1 foo)
+TEST(pref_SINGLE2 bar)
+cmake_parse_arguments(pref "" "SINGLE1;SINGLE2" "")
+TEST(pref_SINGLE1 UNDEFINED)
+TEST(pref_SINGLE2 UNDEFINED)
+
+
+# multi arguments
+
+cmake_parse_arguments(pref "" "" "MULTI1")
+TEST(pref_MULTI1 UNDEFINED)
+
+cmake_parse_arguments(pref "" "" "MULTI1;MULTI2")
+TEST(pref_MULTI1 UNDEFINED)
+TEST(pref_MULTI2 UNDEFINED)
+
+cmake_parse_arguments(pref "" "" "MULTI1" MULTI1 foo)
+TEST(pref_MULTI1 foo)
+cmake_parse_arguments(pref "" "" "MULTI1;MULTI2" MULTI1 foo bar MULTI2 bar foo)
+TEST(pref_MULTI1 foo bar)
+TEST(pref_MULTI2 bar foo)
+cmake_parse_arguments(pref "" "" "MULTI1;MULTI2")
+TEST(pref_MULTI1 UNDEFINED)
+TEST(pref_MULTI2 UNDEFINED)

+ 25 - 0
Tests/RunCMake/cmake_parse_arguments/Mix.cmake

@@ -0,0 +1,25 @@
+include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake)
+include(CMakeParseArguments)
+
+# specify two keywords for each category and set the first keyword of each
+# within ARGN
+cmake_parse_arguments(pref "OPT1;OPT2" "SINGLE1;SINGLE2" "MULTI1;MULTI2"
+                        OPT1 SINGLE1 foo MULTI1 bar foo bar)
+TEST(pref_OPT1 TRUE)
+TEST(pref_OPT2 FALSE)
+TEST(pref_SINGLE1 foo)
+TEST(pref_SINGLE2 UNDEFINED)
+TEST(pref_MULTI1 bar foo bar)
+TEST(pref_MULTI2 UNDEFINED)
+TEST(pref_UNPARSED_ARGUMENTS UNDEFINED)
+
+# same as above but reversed ARGN
+cmake_parse_arguments(pref "OPT1;OPT2" "SINGLE1;SINGLE2" "MULTI1;MULTI2"
+                        MULTI1 bar foo bar SINGLE1 foo OPT1)
+TEST(pref_OPT1 TRUE)
+TEST(pref_OPT2 FALSE)
+TEST(pref_SINGLE1 foo)
+TEST(pref_SINGLE2 UNDEFINED)
+TEST(pref_MULTI1 bar foo bar)
+TEST(pref_MULTI2 UNDEFINED)
+TEST(pref_UNPARSED_ARGUMENTS UNDEFINED)

+ 7 - 0
Tests/RunCMake/cmake_parse_arguments/RunCMakeTest.cmake

@@ -0,0 +1,7 @@
+include(RunCMake)
+
+run_cmake(Utils)
+run_cmake(Initialization)
+run_cmake(Mix)
+run_cmake(CornerCases)
+run_cmake(Errors)

+ 20 - 0
Tests/RunCMake/cmake_parse_arguments/Utils.cmake

@@ -0,0 +1,20 @@
+include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake)
+
+# test the TEST macro itself
+
+TEST(asdf UNDEFINED)
+
+SET (asdf FALSE)
+TEST(asdf FALSE)
+
+SET (asdf TRUE)
+TEST(asdf TRUE)
+
+SET (asdf TRUE)
+TEST(asdf TRUE)
+
+SET (asdf "some value")
+TEST(asdf "some value")
+
+SET (asdf some list)
+TEST(asdf "some;list")

+ 20 - 0
Tests/RunCMake/cmake_parse_arguments/test_utils.cmake

@@ -0,0 +1,20 @@
+macro(TEST variable)
+  SET(expected "${ARGN}")
+  if ( "${expected}" STREQUAL "UNDEFINED" )
+    if (DEFINED ${variable})
+      message(FATAL_ERROR "'${variable}' shall be undefined but has value '${${variable}}'")
+    endif()
+  elseif( "${expected}" STREQUAL "FALSE" )
+    if (NOT ${variable} STREQUAL "FALSE")
+      message(FATAL_ERROR "'${variable}' shall be FALSE")
+    endif()
+  elseif( "${expected}" STREQUAL "TRUE" )
+    if (NOT ${variable} STREQUAL "TRUE")
+      message(FATAL_ERROR "'${variable}' shall be TRUE")
+    endif()
+  else()
+    if (NOT ${variable} STREQUAL "${expected}")
+      message(FATAL_ERROR "'${variable}' shall be '${expected}'")
+    endif()
+  endif()
+endmacro()