NerfUS

Dependencies:   mbed mbed-rtos HardwareInterface EthernetInterface WebSocketClient

Files at this revision

API Documentation at this revision

Comitter:
Maxime Dupuis
Date:
Sat Feb 04 22:48:36 2017 -0500
Parent:
2:6e467e4978f9
Child:
4:8b9f91de6cf1
Commit message:
Integrate GoogleTest and GoogleMock

How to build and run the tests:

- mkdir build
- cd build
- cmake ..
- make
- ./run_all_tests

Changed in this revision

.hgignore Show annotated file Show diff for this revision Revisions of this file
CMakeLists.txt Show annotated file Show diff for this revision Revisions of this file
CMakeLists.txt.in Show annotated file Show diff for this revision Revisions of this file
test/GoogleMockTest.cpp Show annotated file Show diff for this revision Revisions of this file
test/GoogleTestTest.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sat Feb 04 22:48:36 2017 -0500
@@ -0,0 +1,1 @@
+build/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeLists.txt	Sat Feb 04 22:48:36 2017 -0500
@@ -0,0 +1,43 @@
+cmake_minimum_required(VERSION 3.5)
+project(NerfUS)
+
+# Download and unpack googletest at configure time
+configure_file(CMakeLists.txt.in
+               googletest-download/CMakeLists.txt)
+execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
+execute_process(COMMAND ${CMAKE_COMMAND} --build .
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
+ 
+# Prevent GoogleTest from overriding our compiler/linker options
+# when building with Visual Studio
+set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
+ 
+# Add googletest directly to our build. This adds
+# the following targets: gtest, gtest_main, gmock
+# and gmock_main
+add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
+                 ${CMAKE_BINARY_DIR}/googletest-build)
+ 
+# The gtest/gmock targets carry header search path
+# dependencies automatically when using CMake 2.8.11 or
+# later. Otherwise we have to add them here ourselves.
+if (CMAKE_VERSION VERSION_LESS 2.8.11)
+  include_directories("${gtest_SOURCE_DIR}/include"
+                      "${gmock_SOURCE_DIR}/include")
+endif()
+ 
+# Now simply link your own targets against gtest, gmock,
+# etc. as appropriate
+include_directories(include)
+include_directories("${gtest_SOURCE_DIR}/include")
+
+# Where Google Test's libraries can be found.
+#link_directories("./googletest-build/googlemock/gtest/libgtest.a")
+
+file(GLOB SOURCES "source/*.cpp")
+file(GLOB TEST_SOURCES "test/*.cpp")
+
+add_executable(run_unit_tests ${SOURCES} ${TEST_SOURCES})
+
+target_link_libraries(run_unit_tests gtest gmock gmock_main)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeLists.txt.in	Sat Feb 04 22:48:36 2017 -0500
@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 2.8.2)
+ 
+project(googletest-download NONE)
+ 
+include(ExternalProject)
+ExternalProject_Add(googletest
+  GIT_REPOSITORY    https://github.com/google/googletest.git
+  GIT_TAG           master
+  SOURCE_DIR        "${CMAKE_BINARY_DIR}/googletest-src"
+  BINARY_DIR        "${CMAKE_BINARY_DIR}/googletest-build"
+  CONFIGURE_COMMAND ""
+  BUILD_COMMAND     ""
+  INSTALL_COMMAND   ""
+  TEST_COMMAND      ""
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/GoogleMockTest.cpp	Sat Feb 04 22:48:36 2017 -0500
@@ -0,0 +1,38 @@
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+class Adder
+{
+    public:
+        virtual ~Adder() {}
+        virtual int add(int a, int b)
+        {
+            return a + b;
+        }
+};
+
+class MockAdder : public Adder
+{
+    public:
+        MOCK_METHOD2(add, int(int a, int b));
+};
+
+TEST(GoogleMockTest, ExpectCalled)
+{
+    MockAdder adder;
+
+    EXPECT_CALL(adder, add(1, 2));
+
+    adder.add(1, 2);
+}
+
+TEST(GoogleMockTest, DefineReturnedValue)
+{
+    using ::testing::Return;
+    MockAdder adder;
+
+    EXPECT_CALL(adder, add(1, 2))
+        .WillOnce(Return(42));
+
+    ASSERT_EQ(42, adder.add(1, 2));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/GoogleTestTest.cpp	Sat Feb 04 22:48:36 2017 -0500
@@ -0,0 +1,47 @@
+#include "gtest/gtest.h"
+
+#include <string>
+
+TEST(GoogleTestTest, BasicAssertion)
+{
+    ASSERT_TRUE(true);
+    ASSERT_FALSE(false);
+}
+
+TEST(GoogleTestTest, BinaryComparison)
+{
+    ASSERT_EQ(1, 1);
+    ASSERT_NE(1, 2);
+    ASSERT_LT(1, 2);
+    ASSERT_LE(1, 2);
+    ASSERT_GT(3, 2);
+    ASSERT_GE(3, 2);
+}
+
+TEST(GoogleTestTest, CStringComparison)
+{
+    ASSERT_STREQ("alice", "alice");
+    ASSERT_STRNE("alice", "Alice");
+    ASSERT_STRCASEEQ("alice", "Alice");
+    ASSERT_STRCASENE("alice", "bob");
+}
+
+TEST(GoogleTestTest, StringComparison)
+{
+    ASSERT_EQ(std::string("alice"), std::string("alice"));
+    ASSERT_EQ(std::string("alice"), "alice");
+    ASSERT_EQ("alice", std::string("alice"));
+}
+
+TEST(GoogleTestTest, Exceptions)
+{
+    ASSERT_THROW(throw std::exception(), std::exception);
+    ASSERT_NO_THROW(true);
+}
+
+TEST(GoogleTestTest, FloatingPointsComparison)
+{
+    ASSERT_FLOAT_EQ(1.5f, 1.500000000000000000000000001f);
+    ASSERT_DOUBLE_EQ(1.5, 1.500000000000000000000000001);
+    ASSERT_NEAR(1.5, 1.51, 0.02);
+}