Skip to content

Instantly share code, notes, and snippets.

@McMartin
Last active February 6, 2018 22:23
Show Gist options
  • Save McMartin/818dd8f74d73e4d1908f221940e2949e to your computer and use it in GitHub Desktop.
Save McMartin/818dd8f74d73e4d1908f221940e2949e to your computer and use it in GitHub Desktop.
A CMake test framework heavily inspired by Qt Quick Test (http://doc.qt.io/qt-5/qtquick-qtquicktest.html)
function(get_fizzbuzz in out)
math(EXPR off3 "${in} % 3")
math(EXPR off5 "${in} % 5")
if(NOT off3 AND NOT off5)
set(${out} FizzBuzz PARENT_SCOPE)
elseif(NOT off3)
set(${out} Fizz PARENT_SCOPE)
elseif(NOT off5)
set(${out} Buzz PARENT_SCOPE)
else()
set(${out} ${in} PARENT_SCOPE)
endif()
endfunction()
function(fizzbuzz)
foreach(i RANGE 1 100)
get_fizzbuzz(${i} msg)
message("${msg}")
endforeach(i)
endfunction()
if(CMAKE_SCRIPT_MODE_FILE STREQUAL CMAKE_CURRENT_LIST_FILE)
fizzbuzz()
endif()
macro(CHECK)
cmake_policy(SET CMP0012 NEW)
math(EXPR num_assertions "${num_assertions} + 1")
if(${ARGN})
math(EXPR num_assertions_passed "${num_assertions_passed} + 1")
else()
set(has_current_test_failed TRUE)
math(EXPR num_assertions_failed "${num_assertions_failed} + 1")
endif()
set(num_assertions ${num_assertions} PARENT_SCOPE)
set(num_assertions_passed ${num_assertions_passed} PARENT_SCOPE)
set(num_assertions_failed ${num_assertions_failed} PARENT_SCOPE)
set(has_current_test_failed ${has_current_test_failed} PARENT_SCOPE)
endmacro()
function(eval _eval_code)
set(_eval_tmp_file "${CMAKE_CURRENT_LIST_FILE}.eval")
file(WRITE "${_eval_tmp_file}" "${_eval_code}")
include("${_eval_tmp_file}")
file(REMOVE "${_eval_tmp_file}")
endfunction()
function(main)
message(STATUS "********* Start testing of testrunner.cmake *********")
set(num_tests 0)
set(num_tests_passed 0)
set(num_tests_failed 0)
set(num_tests_skipped 0)
set(num_tests_blacklisted 0)
file(GLOB tst_files "tst_*.cmake")
foreach(tst_file ${tst_files})
get_property(old_commands GLOBAL PROPERTY COMMANDS)
include("${tst_file}")
get_property(new_commands GLOBAL PROPERTY COMMANDS)
foreach(command ${old_commands})
list(REMOVE_ITEM new_commands "${command}")
endforeach()
foreach(command ${new_commands})
if("${command}" MATCHES "^test_")
math(EXPR num_tests "${num_tests} + 1")
eval("cmake_policy(SET CMP0011 NEW)
${command}()
set(num_assertions \${num_assertions} PARENT_SCOPE)
set(num_assertions_passed \${num_assertions_passed} PARENT_SCOPE)
set(num_assertions_failed \${num_assertions_failed} PARENT_SCOPE)
set(has_current_test_failed \${has_current_test_failed} PARENT_SCOPE)
")
if(has_current_test_failed)
set(result "FAILED")
math(EXPR num_tests_failed "${num_tests_failed} + 1")
else()
set(result "PASS ")
math(EXPR num_tests_passed "${num_tests_passed} + 1")
endif()
message(STATUS "${result} : ${command}()")
endif()
endforeach()
endforeach()
message(STATUS "Totals: "
"${num_tests_passed} passed, "
"${num_tests_failed} failed, "
"${num_tests_skipped} skipped, "
"${num_tests_blacklisted} blacklisted"
)
endfunction()
if(CMAKE_SCRIPT_MODE_FILE STREQUAL CMAKE_CURRENT_LIST_FILE)
main()
endif()
$ cmake -P runner.cmake
-- ********* Start testing of testrunner.cmake *********
-- PASS : test_get_fizzbuzz()
-- Totals: 1 passed, 0 failed, 0 skipped, 0 blacklisted
include("${CMAKE_CURRENT_LIST_DIR}/fizzbuzz.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/mcmtest.cmake")
function(test_get_fizzbuzz)
get_fizzbuzz(1 one)
CHECK(one STREQUAL "1" AND TRUE)
get_fizzbuzz(3 three)
CHECK(three STREQUAL "Fizz")
get_fizzbuzz(5 five)
CHECK(five STREQUAL "Buzz")
get_fizzbuzz(15 fifteen)
CHECK(fifteen STREQUAL "FizzBuzz")
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment