From 01c13e97ac0bab3ac7785b37a2250bf9c52a03ec Mon Sep 17 00:00:00 2001
From: Evili del Rio i Silvan <evili.del.rio@gmail.com>
Date: Tue, 21 Jun 2016 13:45:50 +0200
Subject: [PATCH] Added correct GMock support and a simple Mock object

---
 ext/gtest/CMakeLists.txt |  2 ++
 tests/CMakeLists.txt     |  7 ++++++-
 tests/silly_test.cpp     | 27 +++++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/ext/gtest/CMakeLists.txt b/ext/gtest/CMakeLists.txt
index ef207be..3b7d782 100644
--- a/ext/gtest/CMakeLists.txt
+++ b/ext/gtest/CMakeLists.txt
@@ -17,10 +17,12 @@ ExternalProject_Add(googletest
 # Specify include dir
 ExternalProject_Get_Property(googletest source_dir)
 set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include PARENT_SCOPE)
+set(GMOCK_INCLUDE_DIRS  ${source_dir}/googlemock/include PARENT_SCOPE)
 
 # Specify MainTest's link libraries
 ExternalProject_Get_Property(googletest binary_dir)
 set(GTEST_LIBS_DIR ${binary_dir}/googlemock/gtest PARENT_SCOPE)
+set(GMOCK_LIBS_DIR ${binary_dir}/googlemock PARENT_SCOPE)
 
 
 
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index d0df51b..06ff10f 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -4,7 +4,11 @@ include_directories(
 )
 
 # Include main source dir
-include_directories (${${PROJECT_NAME_STR}_SOURCE_DIR}/src ${GTEST_INCLUDE_DIRS} )
+include_directories (
+  ${${PROJECT_NAME_STR}_SOURCE_DIR}/src
+  ${GTEST_INCLUDE_DIRS}
+  ${GMOCK_INCLUDE_DIRS}
+)
 
 # Exclude tests from coverage
 set(CTEST_CUSTOM_COVERAGE_EXCLUDE *)
@@ -19,6 +23,7 @@ add_executable(unit_test ${SRCS})
 add_dependencies(unit_test googletest)
 target_link_libraries(unit_test
   ${GTEST_LIBS_DIR}/libgtest.a ${GTEST_LIBS_DIR}/libgtest_main.a
+  ${GMOCK_LIBS_DIR}/libgmock.a ${GMOCK_LIBS_DIR}/libgmock_main.a
   ${CMAKE_THREAD_LIBS_INIT})
 
 # Add tests...
diff --git a/tests/silly_test.cpp b/tests/silly_test.cpp
index 3704f4c..3e4001e 100644
--- a/tests/silly_test.cpp
+++ b/tests/silly_test.cpp
@@ -1,4 +1,22 @@
 #include "gtest/gtest.h"
+#include "gmock/gmock.h"
+
+using ::testing::Return;
+
+class Thing {
+private:
+  int ans;
+public:
+  Thing(): ans(42) {};
+  virtual int lue() {
+    return ans;
+  }
+};
+
+class MockThing : public Thing {
+public:
+  MOCK_METHOD0(lue, int());
+};
 
 // Just a dummy test that it will fail!
 TEST(silly_test, silly_test) {
@@ -6,3 +24,12 @@ TEST(silly_test, silly_test) {
   int b = 1;
   EXPECT_EQ(a, b) << "a ("<<a<<") should be equal to b ("<<b<<")";
 }
+
+TEST(silly_test, silly_mock) {
+  MockThing m;
+  EXPECT_CALL(m, lue()).WillOnce(Return(44));
+
+  int a = m.lue();
+
+  EXPECT_EQ(a, 42) << "The answer to Life, the Universe and Everything should be 42!!!" ;
+}
-- 
GitLab