summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeTests/IfTest.cmake.in
blob: 74b8e32b522073222719971d5fbd66fa52681b03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Prepare variable definitions.
set(VAR_UNDEFINED)
set(VAR_PATH /some/path/to/a/file.txt)
set(FALSE_NAMES OFF NO FALSE N FOO-NOTFOUND IGNORE Off No False Ignore off n no false ignore)
set(TRUE_NAMES ON YES TRUE Y On Yes True on yes true y)
foreach(_arg "" 0 1 2 ${TRUE_NAMES} ${FALSE_NAMES})
  set(VAR_${_arg} "${_arg}")
endforeach()

include("@CMAKE_CURRENT_SOURCE_DIR@/../RegexEscapeString.cmake")
REGEX_ESCAPE_STRING(CTEST_ESCAPED_CMAKE_CURRENT_SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@")

macro(test_vars _old)
  # Variables set to false or not set.
  foreach(_var "" 0 ${FALSE_NAMES} UNDEFINED)
    if(VAR_${_var})
      message(FATAL_ERROR "${_old}if(VAR_${_var}) is true!")
    else()
      message(STATUS "${_old}if(VAR_${_var}) is false")
    endif()

    if(NOT VAR_${_var})
      message(STATUS "${_old}if(NOT VAR_${_var}) is true")
    else()
      message(FATAL_ERROR "${_old}if(NOT VAR_${_var}) is false!")
    endif()
  endforeach()

  # Variables set to true.
  foreach(_var 1 2 ${TRUE_NAMES} PATH)
    if(VAR_${_var})
      message(STATUS "${_old}if(VAR_${_var}) is true")
    else()
      message(FATAL_ERROR "${_old}if(VAR_${_var}) is false!")
    endif()

    if(NOT VAR_${_var})
      message(FATAL_ERROR "${_old}if(NOT VAR_${_var}) is true!")
    else()
      message(STATUS "${_old}if(NOT VAR_${_var}) is false")
    endif()
  endforeach()
endmacro()

#-----------------------------------------------------------------------------
# Test the OLD behavior of CMP0012.
cmake_policy(SET CMP0012 OLD)

# False constants not recognized (still false).
foreach(_false "" ${FALSE_NAMES})
  if("${_false}")
    message(FATAL_ERROR "OLD if(${_false}) is true!")
  else()
    message(STATUS "OLD if(${_false}) is false")
  endif()

  if(NOT "${_false}")
    message(STATUS "OLD if(NOT ${_false}) is true")
  else()
    message(FATAL_ERROR "OLD if(NOT ${_false}) is false!")
  endif()
endforeach()

# True constants not recognized.
foreach(_false ${TRUE_NAMES})
  if(${_false})
    message(FATAL_ERROR "OLD if(${_false}) is true!")
  else()
    message(STATUS "OLD if(${_false}) is false")
  endif()

  if(NOT ${_false})
    message(STATUS "OLD if(NOT ${_false}) is true")
  else()
    message(FATAL_ERROR "OLD if(NOT ${_false}) is false!")
  endif()
endforeach()

# Numbers not recognized properly.
foreach(_num 2 -2 2.0 -2.0 2x -2x)
  if(${_num})
    message(FATAL_ERROR "OLD if(${_num}) is true!")
  else()
    message(STATUS "OLD if(${_num}) is false")
  endif()

  if(NOT ${_num})
    message(FATAL_ERROR "OLD if(NOT ${_num}) is true!")
  else()
    message(STATUS "OLD if(NOT ${_num}) is false")
  endif()
endforeach()

test_vars("OLD ")

#-----------------------------------------------------------------------------

# Test the NEW behavior of CMP0012.
cmake_policy(SET CMP0012 NEW)

# Test false constants.
foreach(_false "" 0 ${FALSE_NAMES})
  if("${_false}")
    message(FATAL_ERROR "if(${_false}) is true!")
  else()
    message(STATUS "if(${_false}) is false")
  endif()

  if(NOT "${_false}")
    message(STATUS "if(NOT ${_false}) is true")
  else()
    message(FATAL_ERROR "if(NOT ${_false}) is false!")
  endif()
endforeach()

# Test true constants.
foreach(_true 1 ${TRUE_NAMES})
  if(${_true})
    message(STATUS "if(${_true}) is true")
  else()
    message(FATAL_ERROR "if(${_true}) is false!")
  endif()

  if(NOT ${_true})
    message(FATAL_ERROR "if(NOT ${_true}) is true!")
  else()
    message(STATUS "if(NOT ${_true}) is false")
  endif()
endforeach()

# Numbers recognized properly.
foreach(_num 2 -2 2.0 -2.0)
  if(${_num})
    message(STATUS "if(${_num}) is true")
  else()
    message(FATAL_ERROR "if(${_num}) is false!")
  endif()

  if(NOT ${_num})
    message(FATAL_ERROR "if(NOT ${_num}) is true!")
  else()
    message(STATUS "if(NOT ${_num}) is false")
  endif()
endforeach()

# Bad numbers not recognized.
foreach(_bad 2x -2x)
  if(${_bad})
    message(FATAL_ERROR "if(${_bad}) is true!")
  else()
    message(STATUS "if(${_bad}) is false")
  endif()

  if(NOT ${_bad})
    message(STATUS "if(NOT ${_bad}) is true")
  else()
    message(FATAL_ERROR "if(NOT ${_bad}) is false!")
  endif()
endforeach()

test_vars("")

set(Invalid-Argument-RESULT 1)
set(Invalid-Argument-STDERR ".*CMake Error at (${CTEST_ESCAPED_CMAKE_CURRENT_SOURCE_DIR}/)?If-Invalid-Argument.cmake:1 \\(if\\):.*Unknown arguments specified.*")

include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake")
check_cmake_test(If
  Invalid-Argument
)