cmake_parse_arguments: Add additional unit tests

Add additional unit tests for some corner cases in argument splitting.
This commit is contained in:
Matthew Woehlke 2016-09-28 15:20:42 -04:00
parent 41291b20f3
commit 66c70cd9f1
5 changed files with 84 additions and 29 deletions

View File

@ -1,15 +1,5 @@
include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake) include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake)
function(test_multi list)
set(i 0)
foreach(value IN LISTS ${list})
math(EXPR j "${i} + 1")
set(${list}[${i}] "${value}")
TEST(${list}[${i}] "${ARGV${j}}")
set(i ${j})
endforeach()
endfunction()
function(test1) function(test1)
cmake_parse_arguments(PARSE_ARGV 0 cmake_parse_arguments(PARSE_ARGV 0
pref "OPT1;OPT2" "SINGLE1;SINGLE2" "MULTI1;MULTI2") pref "OPT1;OPT2" "SINGLE1;SINGLE2" "MULTI1;MULTI2")
@ -33,7 +23,7 @@ function(test2 arg1)
TEST(pref_OPT2 FALSE) TEST(pref_OPT2 FALSE)
TEST(pref_SINGLE1 "foo;bar") TEST(pref_SINGLE1 "foo;bar")
TEST(pref_SINGLE2 UNDEFINED) TEST(pref_SINGLE2 UNDEFINED)
test_multi(pref_MULTI1 bar "foo;bar") TEST(pref_MULTI1 bar "foo;bar")
TEST(pref_MULTI2 UNDEFINED) TEST(pref_MULTI2 UNDEFINED)
TEST(pref_UNPARSED_ARGUMENTS UNDEFINED) TEST(pref_UNPARSED_ARGUMENTS UNDEFINED)
endfunction() endfunction()
@ -43,6 +33,6 @@ function(test3 arg1)
cmake_parse_arguments(PARSE_ARGV 0 cmake_parse_arguments(PARSE_ARGV 0
pref "" "" "") pref "" "" "")
test_multi(pref_UNPARSED_ARGUMENTS "foo;bar" dog cat) TEST(pref_UNPARSED_ARGUMENTS "foo;bar" dog cat)
endfunction() endfunction()
test3("foo;bar" dog cat) test3("foo;bar" dog cat)

View File

@ -0,0 +1,53 @@
include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake)
function(test1)
cmake_parse_arguments(PARSE_ARGV 0
mpref "" "" "MULTI")
TEST(mpref_MULTI foo "foo\;bar")
cmake_parse_arguments(PARSE_ARGV 1
upref "" "" "MULTI")
TEST(upref_UNPARSED_ARGUMENTS foo "foo\;bar")
endfunction()
test1(MULTI foo "foo\;bar")
function(test2)
cmake_parse_arguments(PARSE_ARGV 0
mpref "" "" "MULTI")
TEST(mpref_MULTI "foo;" "bar;")
cmake_parse_arguments(PARSE_ARGV 1
upref "" "" "MULTI")
TEST(upref_UNPARSED_ARGUMENTS "foo;" "bar;")
endfunction()
test2(MULTI "foo;" "bar;")
function(test3)
cmake_parse_arguments(PARSE_ARGV 0
mpref "" "" "MULTI")
TEST(mpref_MULTI "[foo;]" "bar\\")
cmake_parse_arguments(PARSE_ARGV 1
upref "" "" "MULTI")
TEST(upref_UNPARSED_ARGUMENTS "[foo;]" "bar\\")
endfunction()
test3(MULTI "[foo;]" "bar\\")
function(test4)
cmake_parse_arguments(PARSE_ARGV 0
mpref "" "" "MULTI")
TEST(mpref_MULTI foo "bar;none")
cmake_parse_arguments(PARSE_ARGV 1
upref "" "" "MULTI")
TEST(upref_UNPARSED_ARGUMENTS foo "bar;none")
endfunction()
test4(MULTI foo bar\\ none)

View File

@ -10,3 +10,4 @@ run_cmake(BadArgvN1)
run_cmake(BadArgvN2) run_cmake(BadArgvN2)
run_cmake(BadArgvN3) run_cmake(BadArgvN3)
run_cmake(BadArgvN4) run_cmake(BadArgvN4)
run_cmake(CornerCasesArgvN)

View File

@ -17,4 +17,5 @@ SET (asdf "some value")
TEST(asdf "some value") TEST(asdf "some value")
SET (asdf some list) SET (asdf some list)
TEST(asdf some list)
TEST(asdf "some;list") TEST(asdf "some;list")

View File

@ -1,20 +1,30 @@
macro(TEST variable) function(TEST variable)
SET(expected "${ARGN}") if(ARGC GREATER 2)
if ( "${expected}" STREQUAL "UNDEFINED" ) set(i 0)
if (DEFINED ${variable}) foreach(value IN LISTS ${variable})
message(FATAL_ERROR "'${variable}' shall be undefined but has value '${${variable}}'") math(EXPR j "${i} + 1")
endif() set(${variable}[${i}] "${value}")
elseif( "${expected}" STREQUAL "FALSE" ) TEST(${variable}[${i}] "${ARGV${j}}")
if (NOT ${variable} STREQUAL "FALSE") set(i ${j})
message(FATAL_ERROR "'${variable}' shall be FALSE") endforeach()
endif()
elseif( "${expected}" STREQUAL "TRUE" )
if (NOT ${variable} STREQUAL "TRUE")
message(FATAL_ERROR "'${variable}' shall be TRUE")
endif()
else() else()
if (NOT ${variable} STREQUAL "${expected}") set(expected "${ARGN}")
message(FATAL_ERROR "'${variable}' shall be '${expected}'") 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() endif()
endif() endif()
endmacro() endfunction()