summaryrefslogtreecommitdiffstats
path: root/Tests
diff options
context:
space:
mode:
authorCraig Scott <craig.scott@crascit.com>2017-05-14 10:16:27 (GMT)
committerCraig Scott <craig.scott@crascit.com>2017-05-15 21:57:27 (GMT)
commit6edd1806ddbfc4138dc9987d2a9c7c4fed56306b (patch)
treec7de7973de6e0958a98e61d39ef5b37a1a21da93 /Tests
parent836cb52e9aec83f88841cb5b45abb1d32bb02214 (diff)
downloadCMake-6edd1806ddbfc4138dc9987d2a9c7c4fed56306b.zip
CMake-6edd1806ddbfc4138dc9987d2a9c7c4fed56306b.tar.gz
CMake-6edd1806ddbfc4138dc9987d2a9c7c4fed56306b.tar.bz2
GoogleTest: Expand capabilities of gtest_add_tests()
Now has keyword-based arguments (old syntax form is still supported). Discovered tests can have a prefix and/or suffix added to the test names and the list of discovered tests is available to the caller. The working dir can also be set and the dependency on the source files is now optional instead of mandatory.
Diffstat (limited to 'Tests')
-rw-r--r--Tests/CMakeLists.txt1
-rw-r--r--Tests/GoogleTest/CMakeLists.txt10
-rw-r--r--Tests/GoogleTest/Test/CMakeLists.txt82
-rw-r--r--Tests/GoogleTest/Test/main1.cxx30
-rw-r--r--Tests/GoogleTest/Test/main2.cxx1
-rw-r--r--Tests/GoogleTest/Test/main2.h6
-rw-r--r--Tests/GoogleTest/Test/main3.cxx11
-rw-r--r--Tests/GoogleTest/Test/main4.cxx1
-rw-r--r--Tests/GoogleTest/Test/main4.h6
9 files changed, 148 insertions, 0 deletions
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 2f53cfc9..acd014a 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -1391,6 +1391,7 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release
if(CMake_TEST_FindGTest)
add_subdirectory(FindGTest)
+ add_subdirectory(GoogleTest)
endif()
if(CMake_TEST_FindICU)
diff --git a/Tests/GoogleTest/CMakeLists.txt b/Tests/GoogleTest/CMakeLists.txt
new file mode 100644
index 0000000..21f8b8b
--- /dev/null
+++ b/Tests/GoogleTest/CMakeLists.txt
@@ -0,0 +1,10 @@
+add_test(NAME GoogleTest.Test COMMAND
+ ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
+ --build-and-test
+ "${CMake_SOURCE_DIR}/Tests/GoogleTest/Test"
+ "${CMake_BINARY_DIR}/Tests/GoogleTest/Test"
+ ${build_generator_args}
+ --build-project TestGoogleTest
+ --build-options ${build_options}
+ --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
+ )
diff --git a/Tests/GoogleTest/Test/CMakeLists.txt b/Tests/GoogleTest/Test/CMakeLists.txt
new file mode 100644
index 0000000..a1f08d4
--- /dev/null
+++ b/Tests/GoogleTest/Test/CMakeLists.txt
@@ -0,0 +1,82 @@
+cmake_minimum_required(VERSION 3.8)
+project(TestGoogleTest)
+include(CTest)
+
+include(GoogleTest)
+
+find_package(GTest REQUIRED)
+
+add_executable(test_gtest1 main1.cxx)
+target_link_libraries(test_gtest1 GTest::GTest)
+
+# Simple test of defaults
+gtest_add_tests(TARGET test_gtest1
+ TEST_LIST testList
+)
+set(expectedTests
+ GoogleTest.LinksAndRuns
+ GoogleTest.ConditionalFail
+)
+if(NOT testList STREQUAL "${expectedTests}")
+ message(FATAL_ERROR "Expected test list: ${expectedTests}
+Actual test list: ${testList}")
+endif()
+
+
+# Same target, different arguments, so use test prefix and suffix to
+# differentiate from the above test cases
+gtest_add_tests(TARGET test_gtest1
+ TEST_LIST testList
+ TEST_PREFIX "set2."
+ TEST_SUFFIX ".foo"
+ EXTRA_ARGS --forceFail
+)
+
+set(expectedTests
+ set2.GoogleTest.LinksAndRuns.foo
+ set2.GoogleTest.ConditionalFail.foo
+)
+if(NOT testList STREQUAL "${expectedTests}")
+ message(FATAL_ERROR "Expected test list: ${expectedTests}
+Actual test list: ${testList}")
+endif()
+
+set_tests_properties(set2.GoogleTest.ConditionalFail.foo PROPERTIES WILL_FAIL YES)
+
+
+# Search specific sources to get the test list
+add_executable(test_gtest2 main2.cxx)
+target_link_libraries(test_gtest2 GTest::Main)
+gtest_add_tests(TARGET test_gtest2
+ TEST_LIST testList
+ SOURCES main2.h
+)
+set(expectedTests
+ GoogleTest.SomethingElse
+)
+if(NOT testList STREQUAL "${expectedTests}")
+ message(FATAL_ERROR "Expected test list: ${expectedTests}
+Actual test list: ${testList}")
+endif()
+
+
+# Non-keyword form, auto-find sources
+add_executable(test_gtest3 main3.cxx)
+target_link_libraries(test_gtest3 GTest::Main)
+gtest_add_tests(test_gtest3 "" AUTO)
+if(NOT TEST GoogleTest.Foo)
+ message(FATAL_ERROR "Test case GoogleTest.Foo not defined")
+endif()
+if(NOT TEST GoogleTest.Bar)
+ message(FATAL_ERROR "Test case GoogleTest.Bar not defined")
+endif()
+
+
+# Non-keyword form, explicitly specified sources. Allows a non-target to be
+# given for the executable.
+add_executable(test_gtest4 main4.cxx)
+target_link_libraries(test_gtest4 GTest::Main)
+gtest_add_tests($<TARGET_FILE:test_gtest4> "" main4.h)
+if(NOT TEST GoogleTest.NoKeywords)
+ message(FATAL_ERROR "Test case GoogleTest.NoKeywords not defined")
+endif()
diff --git a/Tests/GoogleTest/Test/main1.cxx b/Tests/GoogleTest/Test/main1.cxx
new file mode 100644
index 0000000..03d604b
--- /dev/null
+++ b/Tests/GoogleTest/Test/main1.cxx
@@ -0,0 +1,30 @@
+#include <gtest/gtest.h>
+
+#include <string>
+
+namespace {
+bool shouldFail = false;
+}
+
+TEST(GoogleTest, LinksAndRuns)
+{
+ ASSERT_TRUE(true);
+}
+
+TEST(GoogleTest, ConditionalFail)
+{
+ ASSERT_FALSE(shouldFail);
+}
+
+int main(int argc, char* argv[])
+{
+ ::testing::InitGoogleTest(&argc, argv);
+
+ if (argc > 1) {
+ if (argv[1] != std::string("--forceFail")) {
+ throw "Unexpected argument";
+ }
+ shouldFail = true;
+ }
+ return RUN_ALL_TESTS();
+}
diff --git a/Tests/GoogleTest/Test/main2.cxx b/Tests/GoogleTest/Test/main2.cxx
new file mode 100644
index 0000000..05ffb4a
--- /dev/null
+++ b/Tests/GoogleTest/Test/main2.cxx
@@ -0,0 +1 @@
+#include "main2.h"
diff --git a/Tests/GoogleTest/Test/main2.h b/Tests/GoogleTest/Test/main2.h
new file mode 100644
index 0000000..7243f53
--- /dev/null
+++ b/Tests/GoogleTest/Test/main2.h
@@ -0,0 +1,6 @@
+#include <gtest/gtest.h>
+
+TEST(GoogleTest, SomethingElse)
+{
+ ASSERT_TRUE(true);
+}
diff --git a/Tests/GoogleTest/Test/main3.cxx b/Tests/GoogleTest/Test/main3.cxx
new file mode 100644
index 0000000..98ce13c
--- /dev/null
+++ b/Tests/GoogleTest/Test/main3.cxx
@@ -0,0 +1,11 @@
+#include <gtest/gtest.h>
+
+TEST(GoogleTest, Foo)
+{
+ ASSERT_TRUE(true);
+}
+
+TEST(GoogleTest, Bar)
+{
+ ASSERT_TRUE(true);
+}
diff --git a/Tests/GoogleTest/Test/main4.cxx b/Tests/GoogleTest/Test/main4.cxx
new file mode 100644
index 0000000..8023bc1
--- /dev/null
+++ b/Tests/GoogleTest/Test/main4.cxx
@@ -0,0 +1 @@
+#include "main4.h"
diff --git a/Tests/GoogleTest/Test/main4.h b/Tests/GoogleTest/Test/main4.h
new file mode 100644
index 0000000..19da12a
--- /dev/null
+++ b/Tests/GoogleTest/Test/main4.h
@@ -0,0 +1,6 @@
+#include <gtest/gtest.h>
+
+TEST(GoogleTest, NoKeywords)
+{
+ ASSERT_TRUE(true);
+}