Debian Codegraph @

cmake/tribits/core/utils/UnitTestHelpers.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10 include(CMakeParseArguments)
11 include(GlobalSet)
12
13 # Set policy here instead of including TribitCTestPolicis.cmake since we want
14 # this to be a standalone module
15 cmake_policy(SET CMP0007 NEW) # Don't ignore empty list items
16
17
18 # @MACRO: unittest_initialize_vars()
19 #
20 # Call to initialize the unit test variables before running unit tests.
21 #
22 # Usage::
23 #
24 # unittest_initialize_vars()
25 #
26 macro(unittest_initialize_vars)
27 # Assume that all unit tests will pass by default
28 global_set(UNITTEST_OVERALL_PASS TRUE)
29 global_set(UNITTEST_OVERALL_NUMPASSED 0)
30 global_set(UNITTEST_OVERALL_NUMRUN 0)
31 endmacro()
32
33
34 # @FUNCTION: unittest_compare_const()
35 #
36 # Perform a single unit test equality check and update overall test statistics
37 #
38 # Usage::
39 #
40 # unittest_compare_const(<varName> <expectedValue>)
41 #
42 # If ``${<varName>} == <expectedValue>``, then the check passes, otherwise it
43 # fails. This prints the variable name and values and shows the test result.
44 #
45 # This updates the global variables ``UNITTEST_OVERALL_NUMRUN``,
46 # ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used
47 # by the unit test harness system to assess overall pass/fail.
48 #
49 function(unittest_compare_const VAR_NAME CONST_VAL)
50
51 math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 )
52 global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN})
53
54 message(
55 "\nCheck:\n"
56 " ${VAR_NAME} =\n"
57 " [${${VAR_NAME}}]\n"
58 " EQUALS:\n"
59 " [${CONST_VAL}]"
60 )
61
62 if ("${${VAR_NAME}}" STREQUAL "${CONST_VAL}")
63 message(" [PASSED]\n")
64 math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 )
65 global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED})
66 else()
67 message(" [FAILED]\n")
68 global_set(UNITTEST_OVERALL_PASS FALSE)
69 message(WARNING "Stack trace for failed unit test")
70 endif()
71
72 endfunction()
73
74
75 # @FUNCTION: unittest_compare_list_ele_const()
76 #
77 # Perform a single unit test equality check for a single list element
78 #
79 # Usage::
80 #
81 # unittest_compare_list_ele_const(<listName> <idx> <expectedConstValue>)
82 #
83 # If ``${<listName>[<idx>]} == <expectedValue>``, then the check passes, otherwise it
84 # fails. This prints the variable name and values and shows the test result.
85 #
86 # This updates the global variables ``UNITTEST_OVERALL_NUMRUN``,
87 # ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used
88 # by the unit test harness system to assess overall pass/fail.
89 #
90 function(unittest_compare_list_ele_const listName idx expectedConstValue)
91
92 math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 )
93 global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN})
94
95 list(GET "${listName}" ${idx} listEleIdx)
96 set(listNameAndIdx "${listName}[${idx}]")
97
98 message(
99 "\nCheck:\n"
100 " ${listNameAndIdx} =\n"
101 " [${listEleIdx}]\n"
102 " EQUALS:\n"
103 " [${expectedConstValue}]"
104 )
105
106 if ("${listEleIdx}" STREQUAL "${expectedConstValue}")
107 message(" [PASSED]\n")
108 math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 )
109 global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED})
110 else()
111 message(" [FAILED]\n")
112 global_set(UNITTEST_OVERALL_PASS FALSE)
113 message(WARNING "Stack trace for failed unit test")
114 endif()
115
116 endfunction()
117
118
119 # @FUNCTION: unittest_string_block_compare()
120 #
121 # Compare two string blocks (with multiple newlines '\n') line-by-line and
122 # print the first line that fails the comparison.
123 #
124 # Usage::
125 #
126 # unittest_string_block_compare(
127 # <stringVar> "<stringExpected>"
128 # )
129 #
130 # This updates the global variables ``UNITTEST_OVERALL_NUMRUN``,
131 # ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used
132 # by the unit test harness system to assess overall pass/fail.
133 #
134 function(unittest_string_block_compare stringVar stringExpected)
135
136 message("\nCheck: ${stringVar} equals expected string:")
137
138 math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 )
139 global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN})
140
141 string(REPLACE "\n" ";" stringList "${${stringVar}}")
142 string(REPLACE "\n" ";" stringExpectedList "${stringExpected}")
143
144 list(LENGTH stringList stringLen)
145 list(LENGTH stringExpectedList stringExpectedLen)
146
147 # minLen = min(stringLen, stringExpectedLen)
148 set(minLen ${stringLen})
149 if (stringExpectedLen LESS minLen)
150 set(minLen ${stringExpectedLen})
151 endif()
152
153 set(allMatched TRUE)
154 set(idx 0)
155 while (idx LESS minLen)
156 list(GET stringList ${idx} stringEle)
157 list(GET stringExpectedList ${idx} stringExpectedEle)
158 if (NOT stringEle STREQUAL stringExpectedEle)
159 message(
160 " Error: Line ${idx} of ${stringVar}:\n"
161 " '${stringEle}'\n"
162 " !=\n"
163 " '${stringExpectedEle}'\n"
164 " [FAILED]\n"
165 )
166 global_set(UNITTEST_OVERALL_PASS FALSE)
167 message(WARNING "Stack trace for failed unit test")
168 set(allMatched FALSED)
169 break()
170 endif()
171 math(EXPR idx "${idx}+1")
172 endwhile()
173
174 if (NOT allMatched)
175 # The error handling was already handled above
176 elseif (NOT stringLen EQUAL stringExpectedLen)
177 # The lines of the strings matched but one of the strings had more lines
178 # than the other
179 if (stringLen GREATER stringExpectedLen)
180 list(GET stringList ${stringExpectedLen} nextLine)
181 message(
182 " Error: ${stringVar} has ${stringLen} lines where expected string has"
183 " only ${stringExpectedLen} lines and the next extra line is:\n"
184 " '${nextLine}'\n"
185 " [FAILED]\n"
186 )
187 elseif (stringExpectedLen GREATER stringLen)
188 list(GET stringExpectedList ${stringLen} nextLine)
189 message(
190 " Error: Expected string has ${stringExpectedLen} lines where ${stringVar} has"
191 "only ${stringLen} lines and the next extra line is:\n"
192 " '${nextLine}'\n"
193 " [FAILED]\n"
194 )
195 endif()
196 global_set(UNITTEST_OVERALL_PASS FALSE)
197 message(WARNING "Stack trace for failed unit test")
198 else()
199 # The strings matched exactly!
200 message(" [PASSED]\n")
201 math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 )
202 global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED})
203 endif()
204
205 endfunction()
206
207
208 # @FUNCTION: unittest_string_regex()
209 #
210 # Perform a series of regexes on a given string and update overall test
211 # statistics.
212 #
213 # Usage::
214 #
215 # unittest_string_regex(
216 # "<inputString>"
217 # REGEX_STRINGS "<str0>" "<str1>" ...
218 # )
219 #
220 # If the ``<inputString>`` matches all of the of the regexs ``"<str0>"``,
221 # ``"<str1>"``, ..., then the test passes. Otherwise it fails.
222 #
223 # This updates the global variables ``UNITTEST_OVERALL_NUMRUN``,
224 # ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used
225 # by the unit test harness system to assess overall pass/fail.
226 #
227 function(unittest_string_regex INPUT_STRING)
228
229 cmake_parse_arguments(
230 #prefix
231 PARSE
232 #options
233 ""
234 #one_value_keywords
235 ""
236 #multi_value_keywords
237 "REGEX_STRINGS"
238 ${ARGN}
239 )
240
241 tribits_check_for_unparsed_arguments()
242
243 foreach(REGEX ${PARSE_REGEX_STRINGS})
244
245 math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 )
246 global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN})
247
248 string(REGEX MATCH "${REGEX}" REGEX_MATCH_RESULT "${INPUT_STRING}")
249
250 message(" Searching string:")
251 message(" '${INPUT_STRING}'")
252 if (REGEX_MATCH_RESULT)
253 message(" for REGEX {${REGEX}}: [PASSED]\n")
254 math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 )
255 global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED})
256 else()
257 message(" for REGEX {${REGEX}}: [FAILED]\n")
258 global_set(UNITTEST_OVERALL_PASS FALSE)
259 message(WARNING "Stack trace for failed unit test")
260 endif()
261
262 endforeach()
263
264 endfunction()
265
266
267 # @FUNCTION: unittest_string_var_regex()
268 #
269 # Perform a series of regexes on a given string variable and update overall
270 # test statistics.
271 #
272 # Usage::
273 #
274 # unittest_string_var_regex(
275 # <inputStringVar>
276 # REGEX_STRINGS "<str0>" "<str1>" ...
277 # )
278 #
279 # If the ``"${<inputStringVar>}"`` matches all of the of the regexs
280 # ``"<str0>"``, ``"<str1>"``, ..., then the test passes. Otherwise it fails.
281 #
282 # This updates the global variables ``UNITTEST_OVERALL_NUMRUN``,
283 # ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used
284 # by the unit test harness system to assess overall pass/fail.
285 #
286 function(unittest_string_var_regex inputStringVar)
287
288 cmake_parse_arguments(PARSE_ARGV 1
289 PARSE "" "" # prefix, options, one_value_keywords
290 "REGEX_STRINGS" #multi_value_keywords
291 )
292 tribits_check_for_unparsed_arguments(PARSE)
293
294 foreach(REGEX ${PARSE_REGEX_STRINGS})
295
296 math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 )
297 global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN})
298
299 string(REGEX MATCH "${REGEX}" REGEX_MATCH_RESULT "${${inputStringVar}}")
300
301 message("Searching string variable value '${inputStringVar}':")
302 message(" '${${inputStringVar}}'")
303 if (REGEX_MATCH_RESULT)
304 message(" for REGEX {${REGEX}}: [PASSED]\n")
305 math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 )
306 global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED})
307 else()
308 message(" for REGEX {${REGEX}}: [FAILED]\n")
309 global_set(UNITTEST_OVERALL_PASS FALSE)
310 message(WARNING "Stack trace for failed unit test")
311 endif()
312
313 endforeach()
314
315 endfunction()
316
317
318 # @FUNCTION: unittest_has_substr_const()
319 #
320 # Check that a given string var contains the given substring and update
321 # overall test statistics
322 #
323 # Usage::
324 #
325 # unittest_has_substr_const(<varName> <substr>)
326 #
327 # If ``${<varName>}`` contains the substring ``<substr>``, then the check
328 # passes, otherwise it fails. This prints the variable name and values and
329 # shows the test result.
330 #
331 # This updates the global variables ``UNITTEST_OVERALL_NUMRUN``,
332 # ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used
333 # by the unit test harness system to assess overall pass/fail.
334 #
335 function(unittest_has_substr_const VAR_NAME SUBSTR_VAL)
336
337 math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 )
338 global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN})
339
340 message(
341 "\nCheck:\n"
342 " ${VAR_NAME} =\n"
343 " [${${VAR_NAME}}]\n"
344 " Contains:\n"
345 " [${SUBSTR_VAL}]"
346 )
347
348 string(FIND "${${VAR_NAME}}" "${SUBSTR_VAL}" SUBSTR_START_IDX)
349 #print_var(SUBSTR_START_IDX)
350
351 if (${SUBSTR_START_IDX} GREATER -1)
352 message(" [PASSED]\n")
353 math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 )
354 global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED})
355 else()
356 message(" [FAILED]\n")
357 global_set(UNITTEST_OVERALL_PASS FALSE)
358 message(WARNING "Stack trace for failed unit test")
359 endif()
360
361 endfunction()
362
363
364 # @FUNCTION: unittest_not_has_substr_const()
365 #
366 # Check that a given string var does **NOT** contains the given substring and
367 # update overall test statistics
368 #
369 # Usage::
370 #
371 # unittest_not_has_substr_const(<varName> <substr>)
372 #
373 # If ``${<varName>}`` contains the substring ``<substr>``, then the check
374 # failed, otherwise it passes. This prints the variable name and values and
375 # shows the test result.
376 #
377 # This updates the global variables ``UNITTEST_OVERALL_NUMRUN``,
378 # ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used
379 # by the unit test harness system to assess overall pass/fail.
380 #
381 function(unittest_not_has_substr_const VAR_NAME SUBSTR_VAL)
382
383 math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 )
384 global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN})
385
386 message(
387 "\nCheck:\n"
388 " ${VAR_NAME} =\n"
389 " [${${VAR_NAME}}]\n"
390 " Does NOT contain:\n"
391 " [${SUBSTR_VAL}]"
392 )
393
394 string(FIND "${${VAR_NAME}}" "${SUBSTR_VAL}" SUBSTR_START_IDX)
395 #print_var(SUBSTR_START_IDX)
396
397 if (${SUBSTR_START_IDX} GREATER -1)
398 message(" [FAILED]\n")
399 global_set(UNITTEST_OVERALL_PASS FALSE)
400 message(WARNING "Stack trace for failed unit test")
401 else()
402 message(" [PASSED]\n")
403 math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 )
404 global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED})
405 endif()
406
407 endfunction()
408
409
410 # @FUNCTION: unittest_file_regex()
411 #
412 # Perform a series regexes of given strings and update overall test statistics.
413 #
414 # Usage::
415 #
416 # unittest_file_regex(
417 # <inputFileName>
418 # REGEX_STRINGS "<str1>" "<str2>" ...
419 # )
420 #
421 # The contents of ``<inputFileName>`` are read into a string and then passed
422 # to `unittest_string_regex()`_ to assess pass/fail.
423 #
424 function(unittest_file_regex INPUT_FILE)
425 message("\nRegexing for strings in the file '${INPUT_FILE}':\n")
426 file(READ "${INPUT_FILE}" INPUT_FILE_STRING)
427 unittest_string_regex("${INPUT_FILE_STRING}" ${ARGN})
428 endfunction()
429
430
431 # @FUNCTION: unittest_final_result()
432 #
433 # Print final statistics from all tests and assert final pass/fail
434 #
435 # Usage::
436 #
437 # unittest_final_result(<expectedNumPassed>)
438 #
439 # If ``${UNITTEST_OVERALL_PASS}==TRUE`` and ``${UNITTEST_OVERALL_NUMPASSED} ==
440 # <expectedNumPassed>``, then the overall test program is determined to have
441 # passed and string::
442 #
443 # "Final UnitTests Result: PASSED"
444 #
445 # is printed. Otherwise, the overall test program is determined to have
446 # failed, the string::
447 #
448 # "Final UnitTests Result: FAILED"
449 #
450 # is printed, and ``message(SEND_ERROR "FAIL")`` is called.
451 #
452 # The reason that we require passing in the expected number of passed tests is
453 # as an extra precaution to make sure that important unit tests are not left
454 # out. CMake is a very loosely typed language and it pays to be a little
455 # paranoid.
456 #
457 function(unittest_final_result EXPECTED_NUMPASSED)
458 message("\nFinal UnitTests Result: num_run = ${UNITTEST_OVERALL_NUMRUN}\n")
459 if (UNITTEST_OVERALL_PASS)
460 if (UNITTEST_OVERALL_NUMPASSED EQUAL ${EXPECTED_NUMPASSED})
461 message("Final UnitTests Result: PASSED"
462 " (num_passed = ${UNITTEST_OVERALL_NUMPASSED})")
463 else()
464 message("\nError: num_passed = ${UNITTEST_OVERALL_NUMPASSED}"
465 " != num_expected = ${EXPECTED_NUMPASSED}")
466 message("\nFinal UnitTests Result: FAILED\n")
467 message(SEND_ERROR "FAIL")
468 endif()
469 else()
470 message("\nFinal UnitTests Result: FAILED\n")
471 endif()
472 endfunction()
473