# a simple C only test case
cmake_minimum_required (VERSION 2.6)
project (ReturnTest)

function (FAILED testname)
  message (SEND_ERROR "${testname} failed ${ARGN}")
endfunction ()

function (PASS testname)
  message ("${testname} passed ${ARGN}")
endfunction ()

# test simple return
function (simple)
  set(simpleResult 1 PARENT_SCOPE)
  return()
  set(simpleResult 0 PARENT_SCOPE)
endfunction ()
simple()
if ("${simpleResult}")
  pass ("simple")
else ()
  failed ("simple got: ${simpleResult}")
endif ()

#test return in an if statement
set (simple2IF 1)
function (simple2)
  set(simple2Result 1 PARENT_SCOPE)
  if (simple2IF)
    return()
  endif ()
  set(simple2Result 0 PARENT_SCOPE)
endfunction ()
simple2()
if ("${simple2Result}")
  pass ("simple2")
else ()
  failed ("simple2 got: ${simple2Result}")
endif ()

#test return in a foreach loop
function (looptest)
  foreach (iter RANGE 1 5)
    set (looptestResult "${iter}" PARENT_SCOPE)
    if ("${iter}" EQUAL 3)
      return ()
    endif ()
  endforeach ()
endfunction ()
looptest()
if ("${looptestResult}" EQUAL 3)
  pass ("looptest")
else ()
  failed ("looptest got: ${looptestResult}")
endif ()

#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 ()
    set (iter "${iter}a")
  endwhile()
endfunction ()
whiletest()
if ("${whiletestResult}" STREQUAL "aaa")
  pass ("whiletest")
else ()
  failed ("whiletest got: ${whiletestResult}")
endif ()

# check subdir return
add_subdirectory(subdir)
get_directory_property(subdirResult DIRECTORY subdir DEFINITION subdirreturn)
if ("${subdirResult}" EQUAL 1)
  pass ("subdir")
else ()
  failed ("subdir got: ${subdirResult}")
endif ()

# check return from a file
include(include_return.cmake)
if ("${include_returnResult}" EQUAL 1)
  pass ("include_return")
else ()
  failed ("include_return got: ${include_returnResult}")
endif ()

# check return from within a macro
macro (mymacro)
  set (foo 1)
  if (foo)
    return()
  endif ()
endmacro()

# test simple return
function (simple3)
  set (bar 0)
  set(simple3Result 1 PARENT_SCOPE)
  if (bar)
  else ()
    mymacro()
  endif()
  set(simple3Result 0 PARENT_SCOPE)
endfunction ()
simple3()
if ("${simple3Result}")
  pass ("macrotest")
else ()
  failed ("macrotest got: ${simple3Result}")
endif ()


# test break command now in a foreach
foreach (iter RANGE 1 5)
  set (break1 "${iter}")
  if ("${iter}" EQUAL 3)
    break ()
  endif ()
endforeach ()
if ("${break1}" EQUAL 3)
  pass ("break in foreach")
else ()
  failed ("break in foreach got: ${break1}")
endif ()

# test break in a while loop
set (iter "a")
while(NOT "${iter}" STREQUAL "aaaaa")
  if ("${iter}" STREQUAL "aaa")
    break ()
  endif ()
  set (iter "${iter}a")
endwhile()
if ("${iter}" STREQUAL "aaa")
  pass ("break in a while")
else ()
  failed ("break in a while got: ${whiletestResult}")
endif ()


add_executable (ReturnTest returnTest.c)
