From c96f43b7dd1d41379fe63b4e8b508b2b034520b6 Mon Sep 17 00:00:00 2001
From: Pavel Solodovnikov <hellyeahdominate@gmail.com>
Date: Thu, 22 Jun 2017 11:13:26 +0300
Subject: include_guard: add tests for the feature

---
 Tests/RunCMake/CMakeLists.txt                      |  1 +
 Tests/RunCMake/include_guard/CMakeLists.txt        |  3 +++
 Tests/RunCMake/include_guard/DirectoryScope.cmake  | 19 +++++++++++++++++
 Tests/RunCMake/include_guard/GlobalScope.cmake     | 11 ++++++++++
 .../InvalidArgumentsNumber-result.txt              |  1 +
 .../InvalidArgumentsNumber-stderr.txt              |  5 +++++
 .../include_guard/InvalidArgumentsNumber.cmake     |  1 +
 .../RunCMake/include_guard/InvalidScope-result.txt |  1 +
 .../RunCMake/include_guard/InvalidScope-stderr.txt |  4 ++++
 Tests/RunCMake/include_guard/InvalidScope.cmake    |  1 +
 Tests/RunCMake/include_guard/RunCMakeTest.cmake    |  7 +++++++
 .../RunCMake/include_guard/Scripts/DirScript.cmake | 12 +++++++++++
 .../include_guard/Scripts/GlobScript.cmake         | 12 +++++++++++
 .../RunCMake/include_guard/Scripts/VarScript.cmake | 12 +++++++++++
 Tests/RunCMake/include_guard/VariableScope.cmake   | 24 ++++++++++++++++++++++
 .../include_guard/global_script_dir/CMakeLists.txt |  1 +
 .../include_guard/sub_dir_script1/CMakeLists.txt   |  9 ++++++++
 .../sub_dir_script1/sub_dir_script3/CMakeLists.txt |  1 +
 .../include_guard/sub_dir_script2/CMakeLists.txt   |  1 +
 19 files changed, 126 insertions(+)
 create mode 100644 Tests/RunCMake/include_guard/CMakeLists.txt
 create mode 100644 Tests/RunCMake/include_guard/DirectoryScope.cmake
 create mode 100644 Tests/RunCMake/include_guard/GlobalScope.cmake
 create mode 100644 Tests/RunCMake/include_guard/InvalidArgumentsNumber-result.txt
 create mode 100644 Tests/RunCMake/include_guard/InvalidArgumentsNumber-stderr.txt
 create mode 100644 Tests/RunCMake/include_guard/InvalidArgumentsNumber.cmake
 create mode 100644 Tests/RunCMake/include_guard/InvalidScope-result.txt
 create mode 100644 Tests/RunCMake/include_guard/InvalidScope-stderr.txt
 create mode 100644 Tests/RunCMake/include_guard/InvalidScope.cmake
 create mode 100644 Tests/RunCMake/include_guard/RunCMakeTest.cmake
 create mode 100644 Tests/RunCMake/include_guard/Scripts/DirScript.cmake
 create mode 100644 Tests/RunCMake/include_guard/Scripts/GlobScript.cmake
 create mode 100644 Tests/RunCMake/include_guard/Scripts/VarScript.cmake
 create mode 100644 Tests/RunCMake/include_guard/VariableScope.cmake
 create mode 100644 Tests/RunCMake/include_guard/global_script_dir/CMakeLists.txt
 create mode 100644 Tests/RunCMake/include_guard/sub_dir_script1/CMakeLists.txt
 create mode 100644 Tests/RunCMake/include_guard/sub_dir_script1/sub_dir_script3/CMakeLists.txt
 create mode 100644 Tests/RunCMake/include_guard/sub_dir_script2/CMakeLists.txt

diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt
index 0369fa4..978e553 100644
--- a/Tests/RunCMake/CMakeLists.txt
+++ b/Tests/RunCMake/CMakeLists.txt
@@ -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})
diff --git a/Tests/RunCMake/include_guard/CMakeLists.txt b/Tests/RunCMake/include_guard/CMakeLists.txt
new file mode 100644
index 0000000..d3137f6
--- /dev/null
+++ b/Tests/RunCMake/include_guard/CMakeLists.txt
@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.9)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)
diff --git a/Tests/RunCMake/include_guard/DirectoryScope.cmake b/Tests/RunCMake/include_guard/DirectoryScope.cmake
new file mode 100644
index 0000000..d6c5a3c
--- /dev/null
+++ b/Tests/RunCMake/include_guard/DirectoryScope.cmake
@@ -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()
diff --git a/Tests/RunCMake/include_guard/GlobalScope.cmake b/Tests/RunCMake/include_guard/GlobalScope.cmake
new file mode 100644
index 0000000..02137fa
--- /dev/null
+++ b/Tests/RunCMake/include_guard/GlobalScope.cmake
@@ -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()
diff --git a/Tests/RunCMake/include_guard/InvalidArgumentsNumber-result.txt b/Tests/RunCMake/include_guard/InvalidArgumentsNumber-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/include_guard/InvalidArgumentsNumber-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/include_guard/InvalidArgumentsNumber-stderr.txt b/Tests/RunCMake/include_guard/InvalidArgumentsNumber-stderr.txt
new file mode 100644
index 0000000..cdd33ac
--- /dev/null
+++ b/Tests/RunCMake/include_guard/InvalidArgumentsNumber-stderr.txt
@@ -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\)
diff --git a/Tests/RunCMake/include_guard/InvalidArgumentsNumber.cmake b/Tests/RunCMake/include_guard/InvalidArgumentsNumber.cmake
new file mode 100644
index 0000000..a63a395
--- /dev/null
+++ b/Tests/RunCMake/include_guard/InvalidArgumentsNumber.cmake
@@ -0,0 +1 @@
+include_guard(ARG1 ARG2)
diff --git a/Tests/RunCMake/include_guard/InvalidScope-result.txt b/Tests/RunCMake/include_guard/InvalidScope-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/include_guard/InvalidScope-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/include_guard/InvalidScope-stderr.txt b/Tests/RunCMake/include_guard/InvalidScope-stderr.txt
new file mode 100644
index 0000000..456709d
--- /dev/null
+++ b/Tests/RunCMake/include_guard/InvalidScope-stderr.txt
@@ -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\)
diff --git a/Tests/RunCMake/include_guard/InvalidScope.cmake b/Tests/RunCMake/include_guard/InvalidScope.cmake
new file mode 100644
index 0000000..f69f9fd
--- /dev/null
+++ b/Tests/RunCMake/include_guard/InvalidScope.cmake
@@ -0,0 +1 @@
+include_guard(INVALID)
diff --git a/Tests/RunCMake/include_guard/RunCMakeTest.cmake b/Tests/RunCMake/include_guard/RunCMakeTest.cmake
new file mode 100644
index 0000000..e87bddd
--- /dev/null
+++ b/Tests/RunCMake/include_guard/RunCMakeTest.cmake
@@ -0,0 +1,7 @@
+include(RunCMake)
+
+run_cmake(VariableScope)
+run_cmake(DirectoryScope)
+run_cmake(GlobalScope)
+run_cmake(InvalidScope)
+run_cmake(InvalidArgumentsNumber)
diff --git a/Tests/RunCMake/include_guard/Scripts/DirScript.cmake b/Tests/RunCMake/include_guard/Scripts/DirScript.cmake
new file mode 100644
index 0000000..e61d180
--- /dev/null
+++ b/Tests/RunCMake/include_guard/Scripts/DirScript.cmake
@@ -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()
diff --git a/Tests/RunCMake/include_guard/Scripts/GlobScript.cmake b/Tests/RunCMake/include_guard/Scripts/GlobScript.cmake
new file mode 100644
index 0000000..c26bf40
--- /dev/null
+++ b/Tests/RunCMake/include_guard/Scripts/GlobScript.cmake
@@ -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()
diff --git a/Tests/RunCMake/include_guard/Scripts/VarScript.cmake b/Tests/RunCMake/include_guard/Scripts/VarScript.cmake
new file mode 100644
index 0000000..3080377
--- /dev/null
+++ b/Tests/RunCMake/include_guard/Scripts/VarScript.cmake
@@ -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()
diff --git a/Tests/RunCMake/include_guard/VariableScope.cmake b/Tests/RunCMake/include_guard/VariableScope.cmake
new file mode 100644
index 0000000..7f8477d
--- /dev/null
+++ b/Tests/RunCMake/include_guard/VariableScope.cmake
@@ -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()
diff --git a/Tests/RunCMake/include_guard/global_script_dir/CMakeLists.txt b/Tests/RunCMake/include_guard/global_script_dir/CMakeLists.txt
new file mode 100644
index 0000000..ee7ea2e
--- /dev/null
+++ b/Tests/RunCMake/include_guard/global_script_dir/CMakeLists.txt
@@ -0,0 +1 @@
+include(GlobScript)
diff --git a/Tests/RunCMake/include_guard/sub_dir_script1/CMakeLists.txt b/Tests/RunCMake/include_guard/sub_dir_script1/CMakeLists.txt
new file mode 100644
index 0000000..d3626e5
--- /dev/null
+++ b/Tests/RunCMake/include_guard/sub_dir_script1/CMakeLists.txt
@@ -0,0 +1,9 @@
+function(dir_check)
+  include(DirScript)
+endfunction()
+
+dir_check()
+
+include(DirScript)
+
+add_subdirectory(sub_dir_script3)
diff --git a/Tests/RunCMake/include_guard/sub_dir_script1/sub_dir_script3/CMakeLists.txt b/Tests/RunCMake/include_guard/sub_dir_script1/sub_dir_script3/CMakeLists.txt
new file mode 100644
index 0000000..1c3b1b2
--- /dev/null
+++ b/Tests/RunCMake/include_guard/sub_dir_script1/sub_dir_script3/CMakeLists.txt
@@ -0,0 +1 @@
+include(DirScript)
diff --git a/Tests/RunCMake/include_guard/sub_dir_script2/CMakeLists.txt b/Tests/RunCMake/include_guard/sub_dir_script2/CMakeLists.txt
new file mode 100644
index 0000000..1c3b1b2
--- /dev/null
+++ b/Tests/RunCMake/include_guard/sub_dir_script2/CMakeLists.txt
@@ -0,0 +1 @@
+include(DirScript)
-- 
cgit v0.12