Skip to content

Commit

Permalink
Fixed CMake testing and doctest
Browse files Browse the repository at this point in the history
Signed-off-by: Vinícius Ferrão <[email protected]>
  • Loading branch information
viniciusferrao committed Sep 18, 2023
1 parent 4afcea4 commit 6c8300f
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 111 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ endif()
include(CTest)

if(BUILD_TESTING)
message(AUTHOR_WARNING "Building Tests. Be sure to check out test/constexpr_tests.cpp for constexpr testing")
add_subdirectory(test)
endif()

Expand Down
16 changes: 16 additions & 0 deletions cmake/CommonLibraries.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file set the required libraries for linking the project

set(COMMON_LIBS
${NEWT_LIBRARY}
fmt::fmt
CLI11::CLI11
Boost::headers
Boost::system
Boost::thread
spdlog::spdlog
gsl::gsl-lite
magic_enum::magic_enum
SimpleIni::SimpleIni
resolv
${STDC++FS}
doctest::doctest)
57 changes: 22 additions & 35 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ add_library(cloysterhpc_object OBJECT ${SOURCE_FILES})
add_library(cloysterhpc_static STATIC $<TARGET_OBJECTS:cloysterhpc_object>)
add_executable(main main.cpp)

include(../cmake/CommonLibraries.cmake)

target_link_libraries(
main
PRIVATE
Expand All @@ -19,19 +21,7 @@ target_link_libraries(
target_link_system_libraries(
main
PRIVATE
${NEWT_LIBRARY}
fmt::fmt
CLI11::CLI11
Boost::headers
Boost::system
Boost::thread
spdlog::spdlog
gsl::gsl-lite
magic_enum::magic_enum
SimpleIni::SimpleIni
resolv
${STDC++FS}
doctest::doctest)
${COMMON_LIBS})

target_link_libraries(
cloysterhpc_object
Expand All @@ -42,28 +32,25 @@ target_link_libraries(
target_link_system_libraries(
cloysterhpc_object
PRIVATE
${NEWT_LIBRARY}
fmt::fmt
CLI11::CLI11
Boost::headers
Boost::system
Boost::thread
spdlog::spdlog
gsl::gsl-lite
magic_enum::magic_enum
SimpleIni::SimpleIni
resolv
${STDC++FS}
doctest::doctest)

target_include_directories(cloysterhpc_static PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(cloysterhpc_static ${WARNING_GUARD} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)

target_include_directories(cloysterhpc_object PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(cloysterhpc_object ${WARNING_GUARD} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)

target_include_directories(main PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(main ${WARNING_GUARD} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)
${COMMON_LIBS})

target_include_directories(cloysterhpc_static PRIVATE
"${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(cloysterhpc_static ${WARNING_GUARD} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)

target_include_directories(cloysterhpc_object PRIVATE
"${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(cloysterhpc_object ${WARNING_GUARD} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)

target_include_directories(main PRIVATE
"${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(main ${WARNING_GUARD} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)

# Set target locations to the root folder of the project in /bin and /lib.
# WARNING: This will break multiple target compile, disable it if necessary
Expand Down
57 changes: 57 additions & 0 deletions src/inifile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,71 @@ void inifile::saveFile(const std::filesystem::path& filepath)
ini.SaveFile(filepath.c_str());
}

// BUG: Returning a pointer is not a good idea, it causes ownership issues.
bool inifile::exists(const std::string& section, const std::string& key)
{
const auto* sample = ini.GetValue(section.c_str(), key.c_str());
return sample != nullptr;
}

// BUG: Returning a pointer is not a good idea, it causes ownership issues.
bool inifile::exists(const std::string& section)
{
const auto* sample = ini.GetSection(section.c_str());
return sample != nullptr;
}

#ifdef BUILD_TESTING
#include <doctest/doctest.h>
#else
#define DOCTEST_CONFIG_DISABLE
#include <doctest/doctest.h>
#endif

TEST_SUITE("Load .ini files")
{

TEST_CASE("Get information")
{
inifile ini;
std::filesystem::path path;
path = std::filesystem::current_path() / "sample/inifile.ini";
ini.loadFile(path);
const std::string clusterName
= ini.getValue("information", "cluster_name");
CHECK(clusterName == "cloyster");
}

TEST_CASE("Set value")
{
inifile ini;
std::filesystem::path path;
path = std::filesystem::current_path() / "sample/inifile.ini";
ini.loadFile(path);
const std::string newValue = "modified";
ini.setValue("information", "cluster_name", newValue);
CHECK(ini.getValue("information", "cluster_name") == newValue);
}

TEST_CASE("Delete value")
{
inifile ini;
std::filesystem::path path;
path = std::filesystem::current_path() / "sample/inifile.ini";
ini.loadFile(path);
const bool result = ini.deleteValue("information", "company_name");
CHECK(result);
}

TEST_CASE("Save to a new file")
{
inifile ini;
std::filesystem::path path;
std::filesystem::path newFile;
path = std::filesystem::current_path() / "sample/inifile.ini";
newFile = std::filesystem::current_path() / "sample/newinifile.ini";
ini.loadFile(path);
ini.saveFile(newFile);
CHECK(std::filesystem::exists(newFile));
}
}
52 changes: 34 additions & 18 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,53 @@ add_test(NAME cli.version_matches COMMAND main --version)
set_tests_properties(cli.version_matches PROPERTIES PASS_REGULAR_EXPRESSION "${PROJECT_VERSION}")

file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
message(STATUS "Test files found are: ${TEST_SOURCES}")
list(REMOVE_ITEM TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
message(STATUS "Dedicated test files found are: ${TEST_SOURCES}")

add_executable(${PROJECT_NAME} ${TEST_SOURCES})
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../src/*.cpp)
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../src/main.cpp)
message(STATUS "Source files found are: ${SOURCES}")

add_library(cloysterhpc_test_object OBJECT ${TEST_SOURCES} ${SOURCES})
add_executable(${PROJECT_NAME} main.cpp)

# Enable BUILD_TESTING macro to compile the test shipped on the source files
target_compile_definitions(cloysterhpc_test_object PRIVATE BUILD_TESTING=1)

include(../cmake/CommonLibraries.cmake)

target_link_libraries(
${PROJECT_NAME}
PRIVATE
cloysterhpc_static
cloysterhpc_test_object
cloysterhpc::cloysterhpc_warnings
cloysterhpc::cloysterhpc_options)

target_link_system_libraries(
${PROJECT_NAME}
PRIVATE
${NEWT_LIBRARY}
fmt::fmt
CLI11::CLI11
Boost::headers
Boost::system
Boost::thread
spdlog::spdlog
gsl::gsl-lite
magic_enum::magic_enum
SimpleIni::SimpleIni
resolv
${STDC++FS}
doctest::doctest)
${COMMON_LIBS})

target_include_directories(${PROJECT_NAME} ${WARNING_GUARD} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/../include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/../include>)
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/../include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/../include>)

target_link_libraries(
cloysterhpc_test_object
PRIVATE
cloysterhpc::cloysterhpc_options
cloysterhpc::cloysterhpc_warnings)

target_link_system_libraries(
cloysterhpc_test_object
PRIVATE
${COMMON_LIBS})

target_include_directories(cloysterhpc_test_object PRIVATE
"${CMAKE_BINARY_DIR}/../configured_files/include")
target_include_directories(cloysterhpc_test_object ${WARNING_GUARD} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/../include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/../include>)

set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD})

Expand Down
57 changes: 0 additions & 57 deletions test/inifile.cpp

This file was deleted.

8 changes: 8 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file main.cpp
* @brief This file is a test runner for the CloysterHPC library.
*
* It must include cloyster.h file to set the global variables.
*/
#include <cloysterhpc/cloyster.h>

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

Expand Down

0 comments on commit 6c8300f

Please sign in to comment.