Commit c96f43b7 authored by Pavel Solodovnikov's avatar Pavel Solodovnikov

include_guard: add tests for the feature

parent 80f1221f
......@@ -215,6 +215,7 @@ add_RunCMake_test(get_property)
add_RunCMake_test(if)
add_RunCMake_test(include)
add_RunCMake_test(include_directories)
add_RunCMake_test(include_guard)
add_RunCMake_test(list)
add_RunCMake_test(message)
add_RunCMake_test(project -DCMake_TEST_RESOURCES=${CMake_TEST_RESOURCES})
......
cmake_minimum_required(VERSION 3.9)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Scripts")
# Test include_guard with DIRECTORY scope
# Add subdirectory which includes DirScript three times:
# 1. Include at inner function scope
# 2. At directory scope
# 3. At another subdirectory to check that the guard is checked
# against parent directories
add_subdirectory(sub_dir_script1)
# Add another directory which includes DirScript
add_subdirectory(sub_dir_script2)
# check inclusions count
get_property(dir_count GLOBAL PROPERTY DIR_SCRIPT_COUNT)
if(NOT dir_count EQUAL 2)
message(FATAL_ERROR
"Wrong DIR_SCRIPT_COUNT value: ${dir_count}, expected: 2")
endif()
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Scripts")
# Test GLOBAL include guard
add_subdirectory(global_script_dir)
include(GlobScript)
get_property(glob_count GLOBAL PROPERTY GLOB_SCRIPT_COUNT)
if(NOT glob_count EQUAL 1)
message(FATAL_ERROR
"Wrong GLOB_SCRIPT_COUNT value: ${glob_count}, expected: 1")
endif()
CMake Error at InvalidArgumentsNumber.cmake:1 \(include_guard\):
include_guard given an invalid number of arguments. The command takes at
most 1 argument.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
CMake Error at InvalidScope.cmake:1 \(include_guard\):
include_guard given an invalid scope: INVALID
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
include(RunCMake)
run_cmake(VariableScope)
run_cmake(DirectoryScope)
run_cmake(GlobalScope)
run_cmake(InvalidScope)
run_cmake(InvalidArgumentsNumber)
include_guard(DIRECTORY)
set(prop_name DIR_SCRIPT_COUNT)
get_property(count_is_set GLOBAL PROPERTY ${prop_name} SET)
if(NOT count_is_set)
set_property(GLOBAL PROPERTY ${prop_name} 1)
else()
get_property(count GLOBAL PROPERTY ${prop_name})
math(EXPR count "${count} + 1")
set_property(GLOBAL PROPERTY ${prop_name} ${count})
endif()
include_guard(GLOBAL)
set(prop_name GLOB_SCRIPT_COUNT)
get_property(count_is_set GLOBAL PROPERTY ${prop_name} SET)
if(NOT count_is_set)
set_property(GLOBAL PROPERTY ${prop_name} 1)
else()
get_property(count GLOBAL PROPERTY ${prop_name})
math(EXPR count "${count} + 1")
set_property(GLOBAL PROPERTY ${prop_name} ${count})
endif()
include_guard()
set(prop_name VAR_SCRIPT_COUNT)
get_property(count_is_set GLOBAL PROPERTY ${prop_name} SET)
if(NOT count_is_set)
set_property(GLOBAL PROPERTY ${prop_name} 1)
else()
get_property(count GLOBAL PROPERTY ${prop_name})
math(EXPR count "${count} + 1")
set_property(GLOBAL PROPERTY ${prop_name} ${count})
endif()
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Scripts")
# Test include_guard with VARIABLE scope
function(var_include_func)
# Include twice in the same scope
include(VarScript)
include(VarScript)
get_property(var_count GLOBAL PROPERTY VAR_SCRIPT_COUNT)
if(NOT var_count EQUAL 1)
message(FATAL_ERROR
"Wrong VAR_SCRIPT_COUNT value: ${var_count}, expected: 1")
endif()
endfunction()
var_include_func()
# Check again that include_guard has been reset
include(VarScript)
get_property(var_count GLOBAL PROPERTY VAR_SCRIPT_COUNT)
if(NOT var_count EQUAL 2)
message(FATAL_ERROR
"Wrong VAR_SCRIPT_COUNT value: ${var_count}, expected: 2")
endif()
function(dir_check)
include(DirScript)
endfunction()
dir_check()
include(DirScript)
add_subdirectory(sub_dir_script3)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment