Skip to content

Commit

Permalink
Decouple tests from sources (#549)
Browse files Browse the repository at this point in the history
* Move few test files

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Move more test files

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Move test section to test cmake

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* update imported files

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* fix video game import name

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Run tests on Linux Clang

Signed-off-by: Uilian Ries <uilianries@gmail.com>

---------

Signed-off-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
uilianries committed May 24, 2024
1 parent 60593d9 commit c0f1562
Show file tree
Hide file tree
Showing 48 changed files with 395 additions and 371 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/linux-clang-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ jobs:
-DCMAKE_C_COMPILER=/usr/bin/clang-18 \
-DCMAKE_CXX_COMPILER=/usr/bin/clang++-18 \
-DCODE_COVERAGE:BOOL=ON \
-DBUILD_TESTING:BOOL=ON \
-GNinja
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
run: cmake --build ${{github.workspace}}/build

- name: Test
working-directory: ${{github.workspace}}/build/tests
run: ctest --output-on-failure

- name: Generate code coverage
working-directory: ${{github.workspace}}/build
run: |
ninja faker-ccov-all \
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/linux-gcc-12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ jobs:
run: |
cmake -B ${{github.workspace}}/build \
-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
-DCMAKE_C_COMPILER=gcc-12 \
-DCMAKE_CXX_COMPILER=g++-12 \
-DBUILD_TESTING:BOOL=ON \
-GNinja
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
run: cmake --build ${{github.workspace}}/build

- name: Test
working-directory: ${{github.workspace}}/build
working-directory: ${{github.workspace}}/build/tests
run: ctest --output-on-failure
1 change: 1 addition & 0 deletions .github/workflows/linux-gxx-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ jobs:
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_COMPILER=g++-13 \
-DCMAKE_C_COMPILER=gcc-13 \
-DBUILD_TESTING:BOOL=ON \
-DCMAKE_TOOLCHAIN_FILE=conan/conan_toolchain.cmake && \
cmake --build build
5 changes: 3 additions & 2 deletions .github/workflows/macos-clang-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- name: Install clang
run: brew install llvm@16 && echo 'export PATH="/opt/homebrew/opt/llvm@16/bin:$PATH"' >> /Users/runner/.bash_profile && source /Users/runner/.bash_profile && which clang++

- name: Checkout
uses: actions/checkout@v3
with:
Expand All @@ -27,10 +27,11 @@ jobs:
run: |
cmake -B ${{github.workspace}}/build \
-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
-DBUILD_TESTING:BOOL=ON \
-DCMAKE_CXX_COMPILER=clang++
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- working-directory: build/
- working-directory: build/tests
run: ./faker-cxx-UT
8 changes: 4 additions & 4 deletions .github/workflows/windows-msvc-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
- run: cmake -E make_directory build

- working-directory: build/
run: cmake .. -G "Visual Studio 17 2022"
run: cmake .. -G "Visual Studio 17 2022" -DBUILD_TESTING:BOOL=ON

- working-directory: build/
run: cmake --build .
run: cmake --build . --config ${{env.BUILD_TYPE}}

- working-directory: build/
run: ctest -C Debug --output-on-failure
- working-directory: build/tests
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure
97 changes: 3 additions & 94 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ project(${PROJECT_NAME} CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(BUILD_FAKER_TESTS DEFAULT ON)
option(USE_SYSTEM_DEPENDENCIES "Use fmt and GTest from system" OFF)

if (NOT USE_SYSTEM_DEPENDENCIES)
Expand Down Expand Up @@ -59,48 +58,6 @@ set(FAKER_SOURCES
src/modules/structure/Structure.cpp
)

set(FAKER_UT_SOURCES
src/modules/animal/AnimalTest.cpp
src/modules/book/BookTest.cpp
src/modules/color/ColorTest.cpp
src/modules/commerce/CommerceTest.cpp
src/modules/company/CompanyTest.cpp
src/modules/datatype/DatatypeTest.cpp
src/modules/date/DateTest.cpp
src/modules/finance/FinanceTest.cpp
src/modules/food/FoodTest.cpp
src/modules/internet/InternetTest.cpp
src/modules/location/LocationTest.cpp
src/modules/lorem/LoremTest.cpp
src/modules/number/NumberTest.cpp
src/modules/person/PersonTest.cpp
src/modules/string/StringTest.cpp
src/modules/word/WordTest.cpp
src/common/StringHelperTest.cpp
src/modules/phone/PhoneTest.cpp
src/modules/helper/HelperTest.cpp
src/common/LuhnCheckTest.cpp
src/common/FormatHelperTest.cpp
src/common/mappers/precisionMapper/PrecisionMapperTest.cpp
src/modules/system/SystemTest.cpp
src/modules/database/DatabaseTest.cpp
src/modules/music/MusicTest.cpp
src/modules/movie/MovieTest.cpp
src/modules/git/GitTest.cpp
src/modules/hacker/HackerTest.cpp
src/modules/sport/SportTest.cpp
src/modules/videoGame/VideoGameTest.cpp
src/modules/medicine/MedicineTest.cpp
src/modules/weather/WeatherTest.cpp
src/modules/airline/AirlineTest.cpp
src/modules/image/ImageTest.cpp
src/modules/crypto/CryptoTest.cpp
src/modules/computer/ComputerTest.cpp
src/modules/vehicle/VehicleTest.cpp
src/modules/science/ScienceTest.cpp
src/modules/structure/StructureTest.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})

target_include_directories(
Expand All @@ -115,6 +72,7 @@ else ()
target_compile_options(${LIBRARY_NAME} PRIVATE -Wall -Wextra -Wpedantic -Wconversion -Wformat)
endif ()

include(GNUInstallDirs)
install(TARGETS ${LIBRARY_NAME}
EXPORT ${LIBRARY_NAME}-targets
ARCHIVE DESTINATION lib
Expand Down Expand Up @@ -143,55 +101,6 @@ else ()
target_link_libraries(${LIBRARY_NAME} PRIVATE fmt)
endif ()

if (BUILD_FAKER_TESTS)
enable_testing()

set(target_code_coverage_ALL 1)

include("cmake/cmake-coverage.cmake")

add_code_coverage_all_targets()

add_executable(${LIBRARY_NAME}-UT ${FAKER_UT_SOURCES})
if (MSVC)
target_compile_options(${LIBRARY_NAME}-UT PRIVATE /permissive- /bigobj)
endif()

if (USE_SYSTEM_DEPENDENCIES)
find_package(GTest REQUIRED)
target_link_libraries(${LIBRARY_NAME}-UT PRIVATE GTest::gtest
GTest::gtest_main GTest::gmock GTest::gmock_main faker-cxx)
else ()
add_subdirectory(externals/googletest)

set(GTEST_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
set(GMOCK_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")

target_link_libraries(${LIBRARY_NAME}-UT PRIVATE gtest_main gmock_main
faker-cxx)
endif ()

if (HAS_STD_FORMAT)
target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR})
target_compile_definitions(${LIBRARY_NAME}-UT PRIVATE HAS_STD_FORMAT)
else ()
target_link_libraries(${LIBRARY_NAME}-UT PRIVATE
$<IF:$<TARGET_EXISTS:fmt::fmt>,fmt::fmt,fmt>)
target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR}
${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
endif ()

add_test(
NAME ${LIBRARY_NAME}-UT
COMMAND ${LIBRARY_NAME}-UT
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

target_code_coverage(${LIBRARY_NAME}-UT ALL)
if (BUILD_TESTING)
add_subdirectory(tests)
endif ()
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RUN apt install -y cmake ninja-build g++-13 libstdc++-13-dev

COPY include/ faker-cxx/include/
COPY src/ faker-cxx/src/
COPY tests/ faker-cxx/tests/
COPY externals/ faker-cxx/externals/
COPY cmake/ faker-cxx/cmake/
COPY CMakeLists.txt faker-cxx/CMakeLists.txt
Expand Down
3 changes: 3 additions & 0 deletions scripts/run_tests_linux_gxx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ cd build-linux-gxx || exit 1

cmake .. -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_COMPILER=/usr/bin/g++-13 \
-DBUILD_TESTING:BOOL=ON \
-GNinja

cmake --build .

cd tests/ || exit 1

ctest -C Debug --output-on-failure
102 changes: 102 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
cmake_minimum_required(VERSION 3.22)
project(${CMAKE_PROJECT_NAME}-UT CXX)

include(CTest)
enable_testing()

set(target_code_coverage_ALL 1)

include("${CMAKE_SOURCE_DIR}/cmake/cmake-coverage.cmake")

add_code_coverage_all_targets()

set(FAKER_UT_SOURCES
common/FormatHelperTest.cpp
common/LuhnCheckTest.cpp
common/mappers/precisionMapper/PrecisionMapperTest.cpp
common/StringHelperTest.cpp
modules/airline/AirlineTest.cpp
modules/animal/AnimalTest.cpp
modules/book/BookTest.cpp
modules/color/ColorTest.cpp
modules/commerce/CommerceTest.cpp
modules/company/CompanyTest.cpp
modules/computer/ComputerTest.cpp
modules/crypto/CryptoTest.cpp
modules/database/DatabaseTest.cpp
modules/datatype/DatatypeTest.cpp
modules/date/DateTest.cpp
modules/finance/FinanceTest.cpp
modules/food/FoodTest.cpp
modules/git/GitTest.cpp
modules/hacker/HackerTest.cpp
modules/helper/HelperTest.cpp
modules/image/ImageTest.cpp
modules/internet/InternetTest.cpp
modules/location/LocationTest.cpp
modules/lorem/LoremTest.cpp
modules/medicine/MedicineTest.cpp
modules/movie/MovieTest.cpp
modules/music/MusicTest.cpp
modules/number/NumberTest.cpp
modules/person/PersonTest.cpp
modules/phone/PhoneTest.cpp
modules/science/ScienceTest.cpp
modules/sport/SportTest.cpp
modules/string/StringTest.cpp
modules/structure/StructureTest.cpp
modules/system/SystemTest.cpp
modules/vehicle/VehicleTest.cpp
modules/videoGame/VideoGameTest.cpp
modules/weather/WeatherTest.cpp
modules/word/WordTest.cpp
)

add_executable(${PROJECT_NAME} ${FAKER_UT_SOURCES})
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /permissive- /bigobj)
endif()
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/common
${CMAKE_SOURCE_DIR}/src/modules
)

if (USE_SYSTEM_DEPENDENCIES)
find_package(GTest REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest
GTest::gtest_main GTest::gmock GTest::gmock_main faker-cxx)
else ()
add_subdirectory("${CMAKE_SOURCE_DIR}/externals/googletest"
"${CMAKE_BINARY_DIR}/googletest")

set(GTEST_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
set(GMOCK_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")

target_link_libraries(${PROJECT_NAME} PRIVATE gtest_main gmock_main
faker-cxx)
endif ()

if (HAS_STD_FORMAT)
target_include_directories(
${PROJECT_NAME}
PRIVATE ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR})
target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_STD_FORMAT)
else ()
target_link_libraries(${PROJECT_NAME} PRIVATE
$<IF:$<TARGET_EXISTS:fmt::fmt>,fmt::fmt,fmt>)
target_include_directories(
${PROJECT_NAME}
PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR}
${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
endif ()

add_test(
NAME ${PROJECT_NAME}
COMMAND ${PROJECT_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

target_code_coverage(${PROJECT_NAME} ALL)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "gtest/gtest.h"

#include "src/common/errors/TokenGeneratorNotFoundError.h"
#include "common/errors/TokenGeneratorNotFoundError.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -44,4 +44,4 @@ TEST_F(FormatHelperTest, shouldFormat)
EXPECT_EQ(FormatHelper::format("{}", 1), "1");
EXPECT_EQ(FormatHelper::format("{} {}", "Hello", "World"), "Hello World");
EXPECT_EQ(FormatHelper::format("{0} {1}", "Hello", "World"), "Hello World");
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "PrecisionMapper.h"
#include "common/mappers/precisionMapper/PrecisionMapper.h"

#include "gtest/gtest.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

#include "gtest/gtest.h"

#include "data/AircraftTypes.h"
#include "data/Airlines.h"
#include "data/Airplanes.h"
#include "data/Airports.h"
#include "data/Seat.h"
#include "airline/data/AircraftTypes.h"
#include "airline/data/Airlines.h"
#include "airline/data/Airplanes.h"
#include "airline/data/Airports.h"
#include "airline/data/Seat.h"

using namespace ::testing;
using namespace faker;
Expand Down
Loading

0 comments on commit c0f1562

Please sign in to comment.