-
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.
Merge pull request #36 from edaa-org/C++-GoogleTest
Added a C++/CMake/Ninja example project and unit tests for GoogleTest.
- Loading branch information
Showing
5 changed files
with
125 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,35 @@ | ||
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) | ||
|
||
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.[ch]pp) | ||
|
||
add_library(app STATIC ${SRC_FILES}) | ||
target_include_directories(app PUBLIC ${PROJECT_SOURCE_DIR}/src) | ||
|
||
# GoogleTest | ||
add_subdirectory(./lib/googletest) | ||
|
||
######################################## | ||
# Test files | ||
######################################## | ||
file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/*.[ch]pp) | ||
|
||
######################################## | ||
# Unit Tests | ||
######################################## | ||
add_executable(unit_tests ${TEST_SRC_FILES}) | ||
target_link_libraries(unit_tests app gtest_main) | ||
|
||
|
||
enable_testing() | ||
include(GoogleTest) | ||
gtest_discover_tests(unit_tests) |
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(int value = 0) : _value { value } {} | ||
|
||
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,26 @@ | ||
#include "Counter.hpp" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace { | ||
|
||
TEST(Counter, Init) { | ||
Counter c0 { 0 }; | ||
Counter c1 { 1 }; | ||
EXPECT_EQ(0, c0.Value()); | ||
EXPECT_EQ(1, c1.Value()); | ||
} | ||
|
||
TEST(Counter, Increment) { | ||
Counter c { 0 }; | ||
EXPECT_EQ(0, c.Increment()); | ||
EXPECT_EQ(1, c.Value()); | ||
} | ||
|
||
TEST(Counter, Decrement) { | ||
Counter c { 1 }; | ||
EXPECT_EQ(1, c.Decrement()); | ||
EXPECT_EQ(0, c.Value()); | ||
} | ||
|
||
} // namespace |