Skip to content

Commit 2147b9f

Browse files
committed
feat: cmake with gtest
1 parent 7cc111e commit 2147b9f

12 files changed

+149
-0
lines changed

cmake_with_gtest/.clang-format

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
Language: Cpp
3+
AccessModifierOffset: -3
4+
BasedOnStyle: Google
5+
ColumnLimit: 100
6+
IndentWidth: 4
7+
SortIncludes: false
8+
TabWidth: 4
9+
# IncludeCategories:
10+
# - Regex: '^<.*>'
11+
# Priority: 1
12+
# - Regex: '^<.*\.h>'
13+
# Priority: 2
14+
# - Regex: '^".*\.h"'
15+
# Priority: 3
16+
# - Regex: '^".*"'
17+
# Priority: 4
18+
---
19+
Language: Proto
20+
# Don't format .proto files.
21+
DisableFormat: true

cmake_with_gtest/.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Checks: '-*,modernize-*,google-*,performance-*,portability-*,readability-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,misc-*,mpi-*,-modernize-use-trailing-return-type'
3+
HeaderFilterRegex: '^(?!baiduSDK/).*'
4+
WarningsAsErrors: '*'

cmake_with_gtest/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
build
3+
.cache

cmake_with_gtest/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(cmake_with_gtest
4+
LANGUAGES C CXX
5+
VERSION 0.0.0.1
6+
DESCRIPTION ""
7+
HOMEPAGE_URL ""
8+
)
9+
10+
# GoogleTest requires at least C++14
11+
set(CMAKE_CXX_STANDARD 17)
12+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
13+
14+
set(src_lists)
15+
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src src_lists)
16+
17+
set(exe_name cmake_with_gtest_demo)
18+
add_executable(${exe_name} ${src_lists})
19+
20+
target_include_directories(${exe_name}
21+
PUBLIC
22+
${CMAKE_CURRENT_SOURCE_DIR}/include
23+
PRIVATE
24+
${CMAKE_CURRENT_SOURCE_DIR}/inc_private
25+
)
26+
27+
add_subdirectory(tests)

cmake_with_gtest/cspell.config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "0.2"
2+
ignorePaths: []
3+
dictionaryDefinitions: []
4+
dictionaries: []
5+
words: [
6+
"gtest",
7+
"proto"
8+
]
9+
ignoreWords: []
10+
import: []
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright(c) lisponsored@gmail.com 2024 ****
2+
3+
#ifndef PRIVATE_CLASS_H_
4+
#define PRIVATE_CLASS_H_
5+
6+
namespace cmake_with_gtest {
7+
8+
class PrivateClass {
9+
public:
10+
PrivateClass() = default;
11+
PrivateClass(const PrivateClass&) = default;
12+
PrivateClass& operator=(const PrivateClass&) = default;
13+
PrivateClass(PrivateClass&&) = default;
14+
PrivateClass& operator=(PrivateClass&&) = default;
15+
~PrivateClass() = default;
16+
17+
static int Add(int a, int b);
18+
19+
int Add(int a);
20+
21+
private:
22+
int result_ = 0;
23+
}; // class PrivateClass
24+
25+
} // namespace cmake_with_gtest
26+
27+
#endif // PRIVATE_CLASS_H_

cmake_with_gtest/include/public_class.h

Whitespace-only changes.

cmake_with_gtest/src/main.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
3+
#include "private_class.h"
4+
#include "public_class.h"
5+
6+
int main(int argc, char** argv) {
7+
std::cout << "run:" << argv[0] << std::endl;
8+
const int result = cmake_with_gtest::PrivateClass::Add(1, 2);
9+
std::cout << "add result:" << result << std::endl;
10+
return 0;
11+
}

cmake_with_gtest/src/private_class.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "private_class.h"
2+
3+
namespace cmake_with_gtest {
4+
5+
int PrivateClass::Add(int a, int b) {
6+
return a + b;
7+
}
8+
9+
int PrivateClass::Add(int a) {
10+
result_ += a;
11+
return result_;
12+
}
13+
14+
} // namespace cmake_with_gtest

cmake_with_gtest/src/public_class.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "public_class.h"

cmake_with_gtest/tests/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(tests)
4+
5+
enable_testing()
6+
find_package(GTest)
7+
# include(FetchContent)
8+
# FetchContent_Declare(
9+
# googletest
10+
# URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
11+
# )
12+
add_executable(
13+
hello_test
14+
hello_test.cc
15+
)
16+
target_link_libraries(
17+
hello_test
18+
GTest::gtest_main
19+
)
20+
21+
include(GoogleTest)
22+
gtest_discover_tests(hello_test)

cmake_with_gtest/tests/hello_test.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <gtest/gtest.h>
2+
3+
// Demonstrate some basic assertions.
4+
TEST(HelloTest, BasicAssertions) {
5+
// Expect two strings not to be equal.
6+
EXPECT_STRNE("hello", "world");
7+
// Expect equality.
8+
EXPECT_EQ(7 * 6, 42);
9+
}

0 commit comments

Comments
 (0)