Skip to content

Commit

Permalink
feat: testing folder spec
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Apr 2, 2024
1 parent 2328219 commit 5313b64
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(example_project VERSION 1.0)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Include the FetchContent module
Expand Down Expand Up @@ -32,3 +32,9 @@ install(DIRECTORY include/ DESTINATION include)

# Link the external library targets to your project
target_link_libraries(example_project PRIVATE logtard)

# Enable testing with CMake
enable_testing()

# Include the directory where the test code is located
add_subdirectory(tests)
5 changes: 4 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
project_name: example_project
version: 1.0
cpp_standard: 11
cpp_standard: 17
output_type: executable # Can be 'library' or 'executable'
# library_type: static # Can be 'static' or 'shared'
include_dirs:
- include
source_patterns:
- src/*.cpp
install: true # Optional
testing: # Optional
enable_testing: true
dir: tests
external_projects:
- name: logtard
git_repo: https://github.com/araujo88/logtard.git
Expand Down
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type Config struct {
Install bool `yaml:"install"`
ExternalProjects []ExternalProject `yaml:"external_projects"`
OutputType string `yaml:"output_type"`
Testing *Testing `yaml:"testing"`
}

type Testing struct {
EnableTesting bool `yaml:"enable_testing"`
Dir string `yaml:"dir"`
}

// ExternalProject represents an external project to be added to the CMakeLists
Expand Down Expand Up @@ -118,5 +124,16 @@ func generateCMakeLists(config Config) string {
}
}

if config.Testing != nil {
if config.Testing.EnableTesting {
sb.WriteString("\n# Enable testing with CMake")
sb.WriteString("\nenable_testing()\n")
}
if config.Testing.Dir != "" {
sb.WriteString("\n# Include the directory where the test code is located")
sb.WriteString("\nadd_subdirectory(" + config.Testing.Dir + ")\n")
}
}

return sb.String()
}
36 changes: 36 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.14)
project(example_test)

# Set the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Include FetchContent module for GoogleTest
include(FetchContent)

# Force the use of the shared CRT on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Optionally set the MSVC runtime library to DLL
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

# Declare GoogleTest as fetch content
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
)

# Make GoogleTest available for use
FetchContent_MakeAvailable(googletest)

# Create your test executable
add_executable(example_test example_test.cpp) # Replace example_test.cpp with your actual test file(s)
target_link_libraries(example_test gtest_main) # Link against gtest_main for GoogleTest's main function

# If your project or tests require threading, link against Threads::Threads
find_package(Threads REQUIRED)
target_link_libraries(example_test Threads::Threads)

# Add the tests to CMake's testing framework
include(GoogleTest)
gtest_discover_tests(example_test)
13 changes: 13 additions & 0 deletions tests/example_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <gtest/gtest.h>

int add(int a, int b) {
return a + b;
}

TEST(AdditionTest, HandlesPositiveInput) {
EXPECT_EQ(3, add(1, 2));
}

TEST(AdditionTest, HandlesNegativeInput) {
EXPECT_EQ(-1, add(-1, 0));
}

0 comments on commit 5313b64

Please sign in to comment.