added utils from different project

This commit is contained in:
2024-12-15 22:50:41 +01:00
parent 6db9bd85a5
commit eb0eb436ed
15 changed files with 2636 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.16)
set(DEF_UTESTDIR "uTest")
set(DEF_SRCDIR "src")
set(DEF_INCLUDEDIR "include")
# use just host compilers for this one
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_GENERATOR "Ninja")
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
include(test_core.cmake)
enable_testing()
project(UnitTests)
find_package(doctest REQUIRED PATHS "/mingw64/lib/cmake")
# target_link_libraries(${PROJECT_NAME} PRIVATE doctest::doctest)
addSubDirs_ofRoot("${CMAKE_CURRENT_LIST_DIR}/../components")
+28
View File
@@ -0,0 +1,28 @@
# Unit Testing - Software
In contrast to *test*-directory this module demonstrates how unit tests can also be run on host.
These unit tests aim to test freestanding software modules with more convenient methods and tools
## Setup
make sure you have following packages installed on your host syste
- gnu compiler
- cmake
- generator
- doctest-dev
## Build and test code
* Start some terminal in *uTest*-directory that has access to all needed tools (i.e. on windows a mingw environment or WSL)
Run following commands
```sh
# run cmake configuration
cmake -S . -B build
# run cmake build
cmake --build build
# run tests
./build/<testprog>
# run all tests with
cd build && ctest
```
+43
View File
@@ -0,0 +1,43 @@
function(addSubDirs_ofRoot anchorPath)
message("scanning: ${anchorPath}")
file(GLOB targetModules RELATIVE "${anchorPath}" "${anchorPath}/*")
foreach(module ${targetModules})
message("checking: ${module}")
get_filename_component(moduleName ${module} NAME)
set(modulePath "${anchorPath}/${moduleName}")
if (NOT IS_DIRECTORY "${modulePath}")
# skip files
continue()
endif()
if (${moduleName} MATCHES "^_.*")
# skip ignored component
continue()
endif()
if(EXISTS "${modulePath}/${DEF_UTESTDIR}")
message("Found unit-test: ${moduleName}")
add_subdirectory("${modulePath}/${DEF_UTESTDIR}" ${moduleName})
endif()
endforeach()
endfunction()
macro(unitTestsFrame)
if (CMAKE_TESTING_ENABLED)
# get_filename_component(moduleName "${CMAKE_CURRENT_LIST_DIR}/.." NAME)
file(GLOB_RECURSE targetSources "../${DEF_SRCDIR}/*.cpp" "../${DEF_SRCDIR}/*.c")
add_library(${moduleName} ${targetSources})
target_include_directories(${moduleName} PUBLIC "../${DEF_INCLUDEDIR}")
set(moduleTestName "${moduleName}Test")
file(GLOB_RECURSE testSource "*.cpp")
add_executable(${moduleTestName} ${testSource})
target_link_libraries(${moduleTestName} PRIVATE ${moduleName} doctest::doctest)
target_include_directories(${moduleTestName} PRIVATE "../${DEF_SRCDIR}")
add_test (TEST-${moduleName} ${moduleTestName})
# gtest_discover_tests(${moduleTestName}
# PROPERTIES TIMEOUT 30 LABELS ${moduleName} LABELS "all"
# )
endif()
endmacro()