diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e34564..62d6853 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..0401ad8 --- /dev/null +++ b/tests/CMakeLists.txt @@ -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() diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..832071a --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,5 @@ +/* This file is part of xdgcfg. */ + +#define CATCH_CONFIG_MAIN + +#include diff --git a/tests/test_write_read.cpp b/tests/test_write_read.cpp new file mode 100644 index 0000000..8b5ce65 --- /dev/null +++ b/tests/test_write_read.cpp @@ -0,0 +1,56 @@ +/* This file is part of xdgcfg. */ + +#include +#include +#include +#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! 🙂"); + } + } + } +}