-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an C++ example project and unit tests for GoogleTest (gtest).
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
|
||
# Project configuration | ||
set(PROJECT_NAME Unittesting) | ||
project(${PROJECT_NAME}) | ||
# * PROJECT_SOURCE_DIR - Top level source directory for the project | ||
# * PROJECT_BINARY_DIR - Full path to build directory for project | ||
|
||
# Compiler flags | ||
set(CMAKE_CXX_FLAGS "-g -Wall") | ||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
include_directories(${PROJECT_SOURCE_DIR}/src) | ||
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp) | ||
|
||
# GoogleTest | ||
add_subdirectory(./lib/googletest) | ||
enable_testing() | ||
# | ||
# Include the gtest library. gtest_SOURCE_DIR is available due to | ||
# 'project(gtest)' above. | ||
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) | ||
|
||
######################################## | ||
# Test files | ||
######################################## | ||
file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/*.cpp) | ||
|
||
######################################## | ||
# Unit Tests | ||
######################################## | ||
add_executable(runUnitTests ${TEST_SRC_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "Counter.hpp" | ||
|
||
int Counter::Value() { | ||
return _value; | ||
} | ||
|
||
int Counter::Increment() { | ||
return _value++; | ||
} | ||
|
||
int Counter::Decrement() { | ||
return _value--; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef COUNTER_H_ | ||
#define COUNTER_H_ | ||
|
||
class Counter { | ||
private: | ||
int _value; | ||
|
||
public: | ||
Counter() : counter_(0) {} | ||
|
||
int Value(); | ||
int Increment(); | ||
int Decrement(); | ||
}; | ||
|
||
#endif // COUNTER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "../src/Counter.hpp" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace { | ||
TEST(Counter, Increment) { | ||
Counter c; | ||
|
||
EXPECT_EQ(0, c.Value()); | ||
EXPECT_EQ(0, c.Increment()); | ||
EXPECT_EQ(1, c.Value()); | ||
|
||
EXPECT_EQ(1, c.Decrement()); | ||
EXPECT_EQ(0, c.Value()); | ||
} | ||
|
||
} // namespace |