include_guard: add tests for the feature

This commit is contained in:
Pavel Solodovnikov 2017-06-22 11:13:26 +03:00
parent 80f1221f50
commit c96f43b7dd
19 changed files with 126 additions and 0 deletions

View File

@ -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})

View File

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.9)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View File

@ -0,0 +1,19 @@
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()

View File

@ -0,0 +1,11 @@
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()

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,5 @@
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\)

View File

@ -0,0 +1 @@
include_guard(ARG1 ARG2)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
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\)

View File

@ -0,0 +1 @@
include_guard(INVALID)

View File

@ -0,0 +1,7 @@
include(RunCMake)
run_cmake(VariableScope)
run_cmake(DirectoryScope)
run_cmake(GlobalScope)
run_cmake(InvalidScope)
run_cmake(InvalidArgumentsNumber)

View File

@ -0,0 +1,12 @@
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()

View File

@ -0,0 +1,12 @@
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()

View File

@ -0,0 +1,12 @@
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()

View File

@ -0,0 +1,24 @@
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()

View File

@ -0,0 +1 @@
include(GlobScript)

View File

@ -0,0 +1,9 @@
function(dir_check)
include(DirScript)
endfunction()
dir_check()
include(DirScript)
add_subdirectory(sub_dir_script3)

View File

@ -0,0 +1 @@
include(DirScript)

View File

@ -0,0 +1 @@
include(DirScript)