ENH: add testing for return and break commands
This commit is contained in:
parent
0e69d38004
commit
f4b7ba9c42
|
@ -46,6 +46,7 @@ IF(BUILD_TESTING)
|
||||||
ADD_TEST_MACRO(LinkLine LinkLine)
|
ADD_TEST_MACRO(LinkLine LinkLine)
|
||||||
ADD_TEST_MACRO(MacroTest miniMacroTest)
|
ADD_TEST_MACRO(MacroTest miniMacroTest)
|
||||||
ADD_TEST_MACRO(FunctionTest miniFunctionTest)
|
ADD_TEST_MACRO(FunctionTest miniFunctionTest)
|
||||||
|
ADD_TEST_MACRO(ReturnTest ReturnTest)
|
||||||
ADD_TEST_MACRO(Properties Properties)
|
ADD_TEST_MACRO(Properties Properties)
|
||||||
ADD_TEST_MACRO(Assembler HelloAsm)
|
ADD_TEST_MACRO(Assembler HelloAsm)
|
||||||
ADD_TEST_MACRO(SourceGroups SourceGroups)
|
ADD_TEST_MACRO(SourceGroups SourceGroups)
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
# a simple C only test case
|
||||||
|
project (ReturnTest)
|
||||||
|
|
||||||
|
set (CMAKE_C_FLAGS "${CMAKE_ANSI_CFLAGS} ${CMAKE_C_FLAGS}")
|
||||||
|
|
||||||
|
function (FAILED testname)
|
||||||
|
message (SEND_ERROR "${testname} failed ${ARGN}")
|
||||||
|
endfunction (FAILED)
|
||||||
|
|
||||||
|
function (PASS testname)
|
||||||
|
message ("${testname} passed ${ARGN}")
|
||||||
|
endfunction (PASS)
|
||||||
|
|
||||||
|
# test simple return
|
||||||
|
function (simple)
|
||||||
|
set(simpleResult 1 PARENT_SCOPE)
|
||||||
|
return()
|
||||||
|
set(simpleResult 0 PARENT_SCOPE)
|
||||||
|
endfunction (simple)
|
||||||
|
simple()
|
||||||
|
if ("${simpleResult}")
|
||||||
|
pass ("simple")
|
||||||
|
else ("${simpleResult}")
|
||||||
|
failed ("simple got: ${simpleResult}")
|
||||||
|
endif ("${simpleResult}")
|
||||||
|
|
||||||
|
#test return in an if statement
|
||||||
|
set (simple2IF 1)
|
||||||
|
function (simple2)
|
||||||
|
set(simple2Result 1 PARENT_SCOPE)
|
||||||
|
if (simple2IF)
|
||||||
|
return()
|
||||||
|
endif (simple2IF)
|
||||||
|
set(simple2Result 0 PARENT_SCOPE)
|
||||||
|
endfunction (simple2)
|
||||||
|
simple2()
|
||||||
|
if ("${simple2Result}")
|
||||||
|
pass ("simple2")
|
||||||
|
else ("${simple2Result}")
|
||||||
|
failed ("simple2 got: ${simple2Result}")
|
||||||
|
endif ("${simple2Result}")
|
||||||
|
|
||||||
|
#test return in a foreach loop
|
||||||
|
function (looptest)
|
||||||
|
foreach (iter RANGE 1 5)
|
||||||
|
set (looptestResult "${iter}" PARENT_SCOPE)
|
||||||
|
if ("${iter}" EQUAL 3)
|
||||||
|
return ()
|
||||||
|
endif ("${iter}" EQUAL 3)
|
||||||
|
endforeach (iter)
|
||||||
|
endfunction (looptest)
|
||||||
|
looptest()
|
||||||
|
if ("${looptestResult}" EQUAL 3)
|
||||||
|
pass ("looptest")
|
||||||
|
else ("${looptestResult}" EQUAL 3)
|
||||||
|
failed ("looptest got: ${looptestResult}")
|
||||||
|
endif ("${looptestResult}" EQUAL 3)
|
||||||
|
|
||||||
|
#test return in a while loop
|
||||||
|
function (whiletest)
|
||||||
|
set (iter "a")
|
||||||
|
while(NOT "${iter}" STREQUAL "aaaaa")
|
||||||
|
set (whiletestResult "${iter}" PARENT_SCOPE)
|
||||||
|
if ("${iter}" STREQUAL "aaa")
|
||||||
|
return ()
|
||||||
|
endif ("${iter}" STREQUAL "aaa")
|
||||||
|
set (iter "${iter}a")
|
||||||
|
endwhile(NOT "${iter}" STREQUAL "aaaaa")
|
||||||
|
endfunction (whiletest)
|
||||||
|
whiletest()
|
||||||
|
if ("${whiletestResult}" STREQUAL "aaa")
|
||||||
|
pass ("whiletest")
|
||||||
|
else ("${whiletestResult}" STREQUAL "aaa")
|
||||||
|
failed ("whiletest got: ${whiletestResult}")
|
||||||
|
endif ("${whiletestResult}" STREQUAL "aaa")
|
||||||
|
|
||||||
|
# check subdir return
|
||||||
|
add_subdirectory(subdir)
|
||||||
|
get_directory_property(subdirResult DIRECTORY subdir DEFINITION subdirreturn)
|
||||||
|
if ("${subdirResult}" EQUAL 1)
|
||||||
|
pass ("subdir")
|
||||||
|
else ("${subdirResult}" EQUAL 1)
|
||||||
|
failed ("subdir got: ${subdirResult}")
|
||||||
|
endif ("${subdirResult}" EQUAL 1)
|
||||||
|
|
||||||
|
# check return from within a macro
|
||||||
|
macro (mymacro)
|
||||||
|
set (foo 1)
|
||||||
|
if (foo)
|
||||||
|
return()
|
||||||
|
endif (foo)
|
||||||
|
endmacro(mymacro)
|
||||||
|
|
||||||
|
# test simple return
|
||||||
|
function (simple3)
|
||||||
|
set (bar 0)
|
||||||
|
set(simple3Result 1 PARENT_SCOPE)
|
||||||
|
if (bar)
|
||||||
|
else (bar)
|
||||||
|
mymacro()
|
||||||
|
endif(bar)
|
||||||
|
set(simple3Result 0 PARENT_SCOPE)
|
||||||
|
endfunction (simple3)
|
||||||
|
simple3()
|
||||||
|
if ("${simple3Result}")
|
||||||
|
pass ("macrotest")
|
||||||
|
else ("${simple3Result}")
|
||||||
|
failed ("macrotest got: ${simple3Result}")
|
||||||
|
endif ("${simple3Result}")
|
||||||
|
|
||||||
|
|
||||||
|
# test break command now in a foreach
|
||||||
|
foreach (iter RANGE 1 5)
|
||||||
|
set (break1 "${iter}")
|
||||||
|
if ("${iter}" EQUAL 3)
|
||||||
|
break ()
|
||||||
|
endif ("${iter}" EQUAL 3)
|
||||||
|
endforeach (iter)
|
||||||
|
if ("${break1}" EQUAL 3)
|
||||||
|
pass ("break in foreach")
|
||||||
|
else ("${break1}" EQUAL 3)
|
||||||
|
failed ("break in foreach got: ${break1}")
|
||||||
|
endif ("${break1}" EQUAL 3)
|
||||||
|
|
||||||
|
# test break in a while loop
|
||||||
|
set (iter "a")
|
||||||
|
while(NOT "${iter}" STREQUAL "aaaaa")
|
||||||
|
if ("${iter}" STREQUAL "aaa")
|
||||||
|
break ()
|
||||||
|
endif ("${iter}" STREQUAL "aaa")
|
||||||
|
set (iter "${iter}a")
|
||||||
|
endwhile(NOT "${iter}" STREQUAL "aaaaa")
|
||||||
|
if ("${iter}" STREQUAL "aaa")
|
||||||
|
pass ("break in a while")
|
||||||
|
else ("${iter}" STREQUAL "aaa")
|
||||||
|
failed ("break in a whi;e got: ${whiletestResult}")
|
||||||
|
endif ("${iter}" STREQUAL "aaa")
|
||||||
|
|
||||||
|
|
||||||
|
add_executable (ReturnTest returnTest.c)
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
printf("Running command: %s with %d arguments\n", argv[0], argc);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
set (subdirreturn 1)
|
||||||
|
return()
|
||||||
|
set (subdirreturn 0)
|
Loading…
Reference in New Issue