diff options
Diffstat (limited to 'lib/configuration')
-rw-r--r-- | lib/configuration/CMakeLists.txt | 24 | ||||
-rw-r--r-- | lib/configuration/configuration.cpp | 10 | ||||
-rw-r--r-- | lib/configuration/configuration.h | 4 | ||||
-rw-r--r-- | lib/configuration/meson.build | 22 | ||||
-rw-r--r-- | lib/configuration/test/main.cpp | 39 | ||||
-rw-r--r-- | lib/configuration/test/qt.cpp | 71 |
6 files changed, 104 insertions, 66 deletions
diff --git a/lib/configuration/CMakeLists.txt b/lib/configuration/CMakeLists.txt new file mode 100644 index 0000000..6484a70 --- /dev/null +++ b/lib/configuration/CMakeLists.txt @@ -0,0 +1,24 @@ +add_library(configuration STATIC configuration.cpp qt_specialization.cpp) +target_link_libraries(configuration PUBLIC Qt5::Widgets) +target_include_directories(configuration INTERFACE .) + +add_executable(conf_parser test/main.cpp) +target_compile_definitions(conf_parser PRIVATE NO_QT_SPEC) +target_sanitize(conf_parser) +target_link_libraries(conf_parser PRIVATE configuration Catch2::Catch2) + +add_executable(conf_parser_qt test/qt.cpp) +target_link_libraries(conf_parser_qt PRIVATE configuration Catch2::Catch2) + +add_test(NAME conf_parser COMMAND conf_parser + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test") +add_test(NAME conf_parser_qt COMMAND conf_parser_qt -platform offscreen + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test") +set_tests_properties(conf_parser PROPERTIES ENVIRONMENT "CONFIGFILE=${CMAKE_CURRENT_SOURCE_DIR}/test/defaultrc.ini") +set_tests_properties(conf_parser_qt PROPERTIES ENVIRONMENT "CONFIGFILE=${CMAKE_CURRENT_SOURCE_DIR}/test/defaultrc.ini") + +if(${CMAKE_CXX_COMPILER_ID} MATCHES Clang) + add_executable(conf_fuzzer configuration.cpp) + target_compile_definitions(conf_fuzzer PRIVATE FUZZER NO_QT_SPEC) + target_sanitize_fuzzer(conf_fuzzer) +endif() diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index 66617ed..d11b342 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -10,19 +10,18 @@ #include <algorithm> #include <fstream> #include <iostream> -#include <sstream> #include <stdexcept> #ifndef NO_QT_SPEC #include <QStandardPaths> #endif -static Configuration *s_conf = nullptr; +static const Configuration *s_conf = nullptr; Configuration::Configuration() : use_global(true) { - if(!s_conf) { + if(s_conf == nullptr) { throw std::runtime_error("Trying to use default Configuration, but none has been set!"); } } @@ -139,8 +138,7 @@ void Configuration::read(std::basic_istream<char> &input) void Configuration::setValue(const std::string &key, const std::string &value) { if(use_global) { - s_conf->setValue(key, value); - return; + throw std::runtime_error("Global configuration is read-only!"); } if(this->count(key) == 0) { @@ -167,7 +165,7 @@ bool Configuration::make_global() return true; } -Configuration *Configuration::instance() +const Configuration *Configuration::instance() { return s_conf; } diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index af55f62..a80d8e5 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -29,7 +29,7 @@ #define param_typestate(X) #endif -typedef std::variant<std::string, int, bool> conf_value_t; +using conf_value_t = std::variant<std::string, int, bool>; template <typename T> concept concept_value_t = std::is_arithmetic<T>::value || std::is_same<T, bool>::value || std::is_constructible<T, std::string>::value; @@ -131,7 +131,7 @@ public: bool make_global(); private: - static Configuration *instance(); + static const Configuration *instance(); const std::string m_homePath; const bool use_global = false; diff --git a/lib/configuration/meson.build b/lib/configuration/meson.build deleted file mode 100644 index 5e3e4b2..0000000 --- a/lib/configuration/meson.build +++ /dev/null @@ -1,22 +0,0 @@ -dep_configuration = declare_dependency( - include_directories: include_directories('.'), - link_with: static_library('configuration', ['configuration.cpp', 'qt_specialization.cpp'], dependencies: dep_qt5) -) - -test('conf parser', executable('configuration-parser', - sources: [ 'test/main.cpp' ], - dependencies: [ dep_qt5, dep_catch, dep_configuration ] - ), - env: 'CONFIGFILE='+meson.current_source_dir()/'test/defaultrc.ini', - args: [ '-platform', 'offscreen' ], - workdir: meson.current_source_dir()/'test' -) - -if meson.get_compiler('cpp').has_multi_arguments('-g', '-fsanitize=fuzzer') -executable('configuration-fuzzer', - sources: 'configuration.cpp', - cpp_args: [ '-g', '-fsanitize=fuzzer', '-DNO_QT_SPEC', '-DFUZZER' ], - link_args: [ '-fsanitize=fuzzer' ] -# args: [ '-seed=1', '-max_total_time=24', meson.current_source_dir()/'test/corpus' ] -) -endif diff --git a/lib/configuration/test/main.cpp b/lib/configuration/test/main.cpp index d83f7af..9c83f8b 100644 --- a/lib/configuration/test/main.cpp +++ b/lib/configuration/test/main.cpp @@ -1,9 +1,7 @@ -#define CATCH_CONFIG_RUNNER - // clazy:excludeall=non-pod-global-static +#define CATCH_CONFIG_MAIN #include "configuration.h" -#include <QApplication> #include <catch2/catch.hpp> SCENARIO("Configuration") @@ -32,7 +30,7 @@ SCENARIO("Configuration") WHEN("reading default values") { REQUIRE(conf.value<std::string>("name")); - REQUIRE(conf.value<std::string>("name").value() == std::string()); + REQUIRE(conf.value<std::string>("name").value().empty()); REQUIRE(conf.value<int>("number")); REQUIRE(conf.value<int>("number").value() == 0); REQUIRE(conf.value<bool>("toggle")); @@ -43,7 +41,7 @@ SCENARIO("Configuration") REQUIRE(!conf.value<std::string>("nullopt")); REQUIRE(conf.value<std::string>("main/name")); - REQUIRE(conf.value<std::string>("main/name").value() == std::string()); + REQUIRE(conf.value<std::string>("main/name").value().empty()); REQUIRE(conf.value<int>("main/number")); REQUIRE(conf.value<int>("main/number").value() == 0); REQUIRE(conf.value<bool>("main/toggle")); @@ -101,30 +99,6 @@ SCENARIO("Configuration") REQUIRE(conf.value<std::string>("toggle").value() == "true"); REQUIRE(conf.value<std::string>("main/toggle").value() == "false"); } - - THEN("Qt cast specialization") - { - REQUIRE(conf.value<QString>("name").value() == "Top level"); - REQUIRE(conf.value<QString>("number").value() == "12"); - REQUIRE(conf.value<QString>("toggle").value() == "true"); - REQUIRE(conf.value<QString>("main/toggle").value() == "false"); - REQUIRE(!conf.value<QString>("nullopt")); - - REQUIRE(conf.value<QStringList>("list").value() == QStringList({ "one", "two", "three", "for four" })); - REQUIRE(!conf.value<QStringList>("nullopt")); - } - - THEN("Qt shortcut") - { - REQUIRE(conf.value<std::string>("qt/shortcut") == "Ctrl+Q"); - QAction action; - REQUIRE(conf.shortcut<QAction>(action, "qt/shortcut").shortcut().toString() == "Ctrl+Q"); - REQUIRE(conf.shortcut<QAction>(action, "qt/nil").shortcut().toString() == "Ctrl+Q"); - - QKeySequence sequence; - REQUIRE(conf.shortcut<QKeySequence>(sequence, "qt/shortcut").toString() == "Ctrl+Q"); - REQUIRE(conf.shortcut<QKeySequence>(sequence, "qt/nil").toString() == "Ctrl+Q"); - } } } @@ -167,10 +141,3 @@ SCENARIO("Configuration") } } } - -int main(int argc, char **argv) -{ - QApplication app(argc, argv); - int result = Catch::Session().run(argc, argv); - return result; -} diff --git a/lib/configuration/test/qt.cpp b/lib/configuration/test/qt.cpp new file mode 100644 index 0000000..44114df --- /dev/null +++ b/lib/configuration/test/qt.cpp @@ -0,0 +1,71 @@ +// clazy:excludeall=non-pod-global-static +#define CATCH_CONFIG_RUNNER + +#include "configuration.h" +#include <QApplication> +#include <catch2/catch.hpp> + +SCENARIO("Configuration") +{ + GIVEN("a Configuration object with some initial values") + { + Configuration conf{ + { "name", std::string() }, + { "over", std::string() }, + // this entry is not in the conf file + { "other", std::string("not in cfg") }, + // commented out entry in the conf file + { "comment", std::string("123.456") }, + { "number", 0 }, + { "toggle", false }, + + { "main/name", std::string() }, + { "main/number", 0 }, + { "main/toggle", true }, + + { "extra/name", std::string() }, + { "extra/number", 0 }, + { "extra/toggle", false }, + }; + + WHEN("reading default values") + { + } + + WHEN("reading configuration file") + { + conf.read_file(std::getenv("CONFIGFILE")); + + THEN("Qt cast specialization") + { + REQUIRE(conf.value<QString>("name").value() == "Top level"); + REQUIRE(conf.value<QString>("number").value() == "12"); + REQUIRE(conf.value<QString>("toggle").value() == "true"); + REQUIRE(conf.value<QString>("main/toggle").value() == "false"); + REQUIRE(!conf.value<QString>("nullopt")); + + REQUIRE(conf.value<QStringList>("list").value() == QStringList({ "one", "two", "three", "for four" })); + REQUIRE(!conf.value<QStringList>("nullopt")); + } + + THEN("Qt shortcut") + { + REQUIRE(conf.value<std::string>("qt/shortcut") == "Ctrl+Q"); + QAction action; + REQUIRE(conf.shortcut<QAction>(action, "qt/shortcut").shortcut().toString() == "Ctrl+Q"); + REQUIRE(conf.shortcut<QAction>(action, "qt/nil").shortcut().toString() == "Ctrl+Q"); + + QKeySequence sequence; + REQUIRE(conf.shortcut<QKeySequence>(sequence, "qt/shortcut").toString() == "Ctrl+Q"); + REQUIRE(conf.shortcut<QKeySequence>(sequence, "qt/nil").toString() == "Ctrl+Q"); + } + } + } +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + int result = Catch::Session().run(argc, argv); + return result; +} |