Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Tests / ReturnTest / CMakeLists.txt
1 # a simple C only test case
2 cmake_minimum_required (VERSION 2.6)
3 project (ReturnTest)
4
5 function (FAILED testname)
6   message (SEND_ERROR "${testname} failed ${ARGN}")
7 endfunction ()
8
9 function (PASS testname)
10   message ("${testname} passed ${ARGN}")
11 endfunction ()
12
13 # test simple return
14 function (simple)
15   set(simpleResult 1 PARENT_SCOPE)
16   return()
17   set(simpleResult 0 PARENT_SCOPE)
18 endfunction ()
19 simple()
20 if ("${simpleResult}")
21   pass ("simple")
22 else ()
23   failed ("simple got: ${simpleResult}")
24 endif ()
25
26 #test return in an if statement
27 set (simple2IF 1)
28 function (simple2)
29   set(simple2Result 1 PARENT_SCOPE)
30   if (simple2IF)
31     return()
32   endif ()
33   set(simple2Result 0 PARENT_SCOPE)
34 endfunction ()
35 simple2()
36 if ("${simple2Result}")
37   pass ("simple2")
38 else ()
39   failed ("simple2 got: ${simple2Result}")
40 endif ()
41
42 #test return in a foreach loop
43 function (looptest)
44   foreach (iter RANGE 1 5)
45     set (looptestResult "${iter}" PARENT_SCOPE)
46     if ("${iter}" EQUAL 3)
47       return ()
48     endif ()
49   endforeach ()
50 endfunction ()
51 looptest()
52 if ("${looptestResult}" EQUAL 3)
53   pass ("looptest")
54 else ()
55   failed ("looptest got: ${looptestResult}")
56 endif ()
57
58 #test return in a while loop
59 function (whiletest)
60   set (iter "a")
61   while(NOT "${iter}" STREQUAL "aaaaa")
62     set (whiletestResult "${iter}" PARENT_SCOPE)
63     if ("${iter}" STREQUAL "aaa")
64       return ()
65     endif ()
66     set (iter "${iter}a")
67   endwhile()
68 endfunction ()
69 whiletest()
70 if ("${whiletestResult}" STREQUAL "aaa")
71   pass ("whiletest")
72 else ()
73   failed ("whiletest got: ${whiletestResult}")
74 endif ()
75
76 # check subdir return
77 add_subdirectory(subdir)
78 get_directory_property(subdirResult DIRECTORY subdir DEFINITION subdirreturn)
79 if ("${subdirResult}" EQUAL 1)
80   pass ("subdir")
81 else ()
82   failed ("subdir got: ${subdirResult}")
83 endif ()
84
85 # check return from a file
86 include(include_return.cmake)
87 if ("${include_returnResult}" EQUAL 1)
88   pass ("include_return")
89 else ()
90   failed ("include_return got: ${include_returnResult}")
91 endif ()
92
93 # check return from within a macro
94 macro (mymacro)
95   set (foo 1)
96   if (foo)
97     return()
98   endif ()
99 endmacro()
100
101 # test simple return
102 function (simple3)
103   set (bar 0)
104   set(simple3Result 1 PARENT_SCOPE)
105   if (bar)
106   else ()
107     mymacro()
108   endif()
109   set(simple3Result 0 PARENT_SCOPE)
110 endfunction ()
111 simple3()
112 if ("${simple3Result}")
113   pass ("macrotest")
114 else ()
115   failed ("macrotest got: ${simple3Result}")
116 endif ()
117
118
119 # test break command now in a foreach
120 foreach (iter RANGE 1 5)
121   set (break1 "${iter}")
122   if ("${iter}" EQUAL 3)
123     break ()
124   endif ()
125 endforeach ()
126 if ("${break1}" EQUAL 3)
127   pass ("break in foreach")
128 else ()
129   failed ("break in foreach got: ${break1}")
130 endif ()
131
132 # test break in a while loop
133 set (iter "a")
134 while(NOT "${iter}" STREQUAL "aaaaa")
135   if ("${iter}" STREQUAL "aaa")
136     break ()
137   endif ()
138   set (iter "${iter}a")
139 endwhile()
140 if ("${iter}" STREQUAL "aaa")
141   pass ("break in a while")
142 else ()
143   failed ("break in a while got: ${whiletestResult}")
144 endif ()
145
146
147 add_executable (ReturnTest returnTest.c)