Added tests and made compiling the example optional.
This commit is contained in:
parent
3a7a7b428b
commit
e07a6ab6cb
|
@ -1,8 +1,11 @@
|
|||
cmake_minimum_required (VERSION 3.1)
|
||||
project(xdgcfg
|
||||
VERSION 0.3.0
|
||||
LANGUAGES CXX
|
||||
)
|
||||
VERSION 0.3.1
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
set(WITH_EXAMPLES "NO" CACHE STRING "WITH_EXAMPLES defaults to \"NO\"")
|
||||
set(WITH_TESTS "NO" CACHE STRING "WITH_TESTS defaults to \"NO\"")
|
||||
|
||||
include(GNUInstallDirs)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
@ -23,8 +26,17 @@ include_directories(${LIBCONFIG_INCLUDE_DIRS})
|
|||
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
|
||||
link_directories(${LIBCONFIG_LIBRARY_DIRS})
|
||||
|
||||
add_executable(example src/example.cpp)
|
||||
target_link_libraries(example
|
||||
set(COMMON_LIBRARIES
|
||||
${LIBXDG_BASEDIR_LIBRARIES} ${LIBCONFIG_LIBRARIES} stdc++fs)
|
||||
|
||||
if (WITH_EXAMPLES)
|
||||
add_executable(example src/example.cpp)
|
||||
target_link_libraries(example
|
||||
${COMMON_LIBRARIES})
|
||||
endif()
|
||||
|
||||
if(WITH_TESTS)
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
||||
install(FILES src/xdgcfg.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
|
|
24
tests/CMakeLists.txt
Normal file
24
tests/CMakeLists.txt
Normal file
|
@ -0,0 +1,24 @@
|
|||
include(CTest)
|
||||
file(GLOB sources_tests test_*.cpp)
|
||||
|
||||
find_package(Catch2)
|
||||
if(Catch2_FOUND) # Catch 2.x
|
||||
include(Catch)
|
||||
add_executable(all_tests main.cpp ${sources_tests})
|
||||
target_link_libraries(all_tests Catch2::Catch2 ${COMMON_LIBRARIES})
|
||||
target_include_directories(all_tests PRIVATE "/usr/include/catch2")
|
||||
catch_discover_tests(all_tests EXTRA_ARGS "${EXTRA_TEST_ARGS}")
|
||||
else() # Catch 1.x
|
||||
if(EXISTS "/usr/include/catch.hpp")
|
||||
message(STATUS "Catch 1.x found.")
|
||||
foreach(src ${sources_tests})
|
||||
get_filename_component(bin ${src} NAME_WE)
|
||||
add_executable(${bin} main.cpp ${src})
|
||||
target_link_libraries(${bin} ${COMMON_LIBRARIES})
|
||||
add_test(${bin} ${bin} "${EXTRA_TEST_ARGS}")
|
||||
endforeach()
|
||||
else()
|
||||
message(FATAL_ERROR
|
||||
"Neither Catch 2.x nor Catch 1.x could be found.")
|
||||
endif()
|
||||
endif()
|
5
tests/main.cpp
Normal file
5
tests/main.cpp
Normal file
|
@ -0,0 +1,5 @@
|
|||
/* This file is part of xdgcfg. */
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
|
||||
#include <catch.hpp>
|
56
tests/test_write_read.cpp
Normal file
56
tests/test_write_read.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
/* This file is part of xdgcfg. */
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <catch.hpp>
|
||||
#include "../src/xdgcfg.hpp"
|
||||
|
||||
using std::string;
|
||||
|
||||
SCENARIO ("A config file can be written and then read.")
|
||||
{
|
||||
bool exception = false;
|
||||
|
||||
WHEN ("Writing config")
|
||||
{
|
||||
xdgcfg config("test.cfg", "xdgcfg");
|
||||
libconfig::Config &cfg = config.get_cfg();
|
||||
libconfig::Setting &root = cfg.getRoot();
|
||||
|
||||
try
|
||||
{
|
||||
root.add("Hello", libconfig::Setting::TypeString) = "World! 🙂";
|
||||
config.write();
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
exception = true;
|
||||
}
|
||||
|
||||
THEN ("No exception is thrown")
|
||||
{
|
||||
REQUIRE_FALSE(exception);
|
||||
}
|
||||
|
||||
WHEN ("Reading config")
|
||||
{
|
||||
string value;
|
||||
|
||||
try
|
||||
{
|
||||
value = root["Hello"].c_str();
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
exception = true;
|
||||
}
|
||||
|
||||
THEN ("No exception is thrown")
|
||||
AND_THEN ("Value is correct")
|
||||
{
|
||||
REQUIRE_FALSE(exception);
|
||||
REQUIRE(value == "World! 🙂");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user