From e3fab29fedb07e6e602ab994ccabf3f8aadbd137 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Thu, 13 Jun 2024 07:37:48 +0900 Subject: [PATCH] Add core_configuration::error_handling --- .../configuration_json_helper.hpp | 55 ++++++-- .../core_configuration/core_configuration.hpp | 32 +++-- .../details/global_configuration.hpp | 5 +- .../details/machine_specific.hpp | 18 ++- .../core_configuration/details/profile.hpp | 27 ++-- .../details/profile/device.hpp | 130 +++++++----------- src/share/core_configuration/types.hpp | 12 ++ .../json/to_json_example.json | 33 ----- .../src/core_configuration_test.hpp | 91 ++++++------ .../core_configuration/src/device_test.hpp | 65 ++++----- .../core_configuration/src/errors_test.hpp | 10 +- .../src/global_configuration_test.hpp | 9 +- .../src/machine_specific_test.hpp | 9 +- 13 files changed, 256 insertions(+), 240 deletions(-) create mode 100644 src/share/core_configuration/types.hpp diff --git a/src/share/core_configuration/configuration_json_helper.hpp b/src/share/core_configuration/configuration_json_helper.hpp index 91b32aaee..4ff6c3542 100644 --- a/src/share/core_configuration/configuration_json_helper.hpp +++ b/src/share/core_configuration/configuration_json_helper.hpp @@ -1,5 +1,7 @@ #pragma once +#include "logger.hpp" +#include "types.hpp" #include #include @@ -11,7 +13,8 @@ class base_t { public: virtual ~base_t() = default; virtual const std::string& get_key(void) const = 0; - virtual void update_value(const nlohmann::json& json) = 0; + virtual void update_value(const nlohmann::json& json, + error_handling error_handling) = 0; virtual void update_json(nlohmann::json& json) const = 0; }; @@ -35,9 +38,27 @@ class value_t final : public base_t { return value_; } - void update_value(const nlohmann::json& json) override { - if (auto v = pqrs::json::find(json, key_)) { - value_ = *v; + void update_value(const nlohmann::json& json, + error_handling error_handling) override { + pqrs::json::requires_object(json, "json"); + + auto it = json.find(key_); + if (it == std::end(json)) { + return; + } + + try { + if constexpr (std::is_same::value) { + pqrs::json::requires_boolean(*it, "`" + key_ + "`"); + } else if constexpr (std::is_same::value) { + pqrs::json::requires_number(*it, "`" + key_ + "`"); + } + + value_ = it->template get(); + } catch (...) { + if (error_handling == error_handling::strict) { + throw; + } } } @@ -68,9 +89,11 @@ class object_t final : public base_t { return key_; } - void update_value(const nlohmann::json& json) override { + void update_value(const nlohmann::json& json, + error_handling error_handling) override { if (auto v = pqrs::json::find_object(json, key_)) { - value_ = std::make_shared(v->value()); + value_ = std::make_shared(v->value(), + error_handling); } } @@ -101,16 +124,21 @@ class array_t final : public base_t { return key_; } - void update_value(const nlohmann::json& json) override { - if (!json.contains(key_)) { + void update_value(const nlohmann::json& json, + error_handling error_handling) override { + pqrs::json::requires_object(json, "json"); + + auto it = json.find(key_); + if (it == std::end(json)) { return; } - pqrs::json::requires_array(json[key_], "`" + key_ + "`"); + pqrs::json::requires_array(*it, "`" + key_ + "`"); - for (const auto& j : json[key_]) { + for (const auto& j : *it) { try { - value_.push_back(std::make_shared(j)); + value_.push_back(std::make_shared(j, + error_handling)); } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key_, e.what())); } @@ -164,9 +192,10 @@ class helper_values final { value)); } - void update_value(const nlohmann::json& json) { + void update_value(const nlohmann::json& json, + error_handling error_handling) { for (const auto& v : values_) { - v->update_value(json); + v->update_value(json, error_handling); } } diff --git a/src/share/core_configuration/core_configuration.hpp b/src/share/core_configuration/core_configuration.hpp index 1a2c3ce30..c4343ad33 100644 --- a/src/share/core_configuration/core_configuration.hpp +++ b/src/share/core_configuration/core_configuration.hpp @@ -32,15 +32,20 @@ class core_configuration final { public: core_configuration(const core_configuration&) = delete; - core_configuration(void) : core_configuration("", 0) { + core_configuration(error_handling error_handling) + : core_configuration("", 0, error_handling) { } core_configuration(const std::string& file_path, - uid_t expected_file_owner) + uid_t expected_file_owner, + error_handling error_handling) : json_(nlohmann::json::object()), + error_handling_(error_handling), loaded_(false), - global_configuration_(std::make_shared(nlohmann::json::object())), - machine_specific_(std::make_shared(nlohmann::json::object())) { + global_configuration_(std::make_shared(nlohmann::json::object(), + error_handling)), + machine_specific_(std::make_shared(nlohmann::json::object(), + error_handling)) { helper_values_.push_back_object("global", global_configuration_); helper_values_.push_back_object("machine_specific", @@ -69,7 +74,8 @@ class core_configuration final { try { json_ = json_utility::parse_jsonc(input); - helper_values_.update_value(json_); + helper_values_.update_value(json_, + error_handling); loaded_ = true; @@ -85,9 +91,10 @@ class core_configuration final { // Fallbacks if (profiles_.empty()) { profiles_.push_back(std::make_shared(nlohmann::json({ - {"name", "Default profile"}, - {"selected", true}, - }))); + {"name", "Default profile"}, + {"selected", true}, + }), + error_handling_)); } } @@ -139,12 +146,14 @@ class core_configuration final { void push_back_profile(void) { profiles_.push_back(std::make_shared(nlohmann::json({ - {"name", "New profile"}, - }))); + {"name", "New profile"}, + }), + error_handling_)); } void duplicate_profile(const details::profile& profile) { - profiles_.push_back(std::make_shared(nlohmann::json(profile))); + profiles_.push_back(std::make_shared(nlohmann::json(profile), + error_handling_)); auto& p = *(profiles_.back()); p.set_name(p.get_name() + " (copy)"); @@ -247,6 +256,7 @@ class core_configuration final { } nlohmann::json json_; + error_handling error_handling_; bool loaded_; gsl::not_null> global_configuration_; diff --git a/src/share/core_configuration/details/global_configuration.hpp b/src/share/core_configuration/details/global_configuration.hpp index c9aafdcbd..f1435f390 100644 --- a/src/share/core_configuration/details/global_configuration.hpp +++ b/src/share/core_configuration/details/global_configuration.hpp @@ -10,7 +10,8 @@ class global_configuration final { public: global_configuration(const global_configuration&) = delete; - global_configuration(const nlohmann::json& json) + global_configuration(const nlohmann::json& json, + error_handling error_handling) : json_(json) { helper_values_.push_back_value("check_for_updates_on_startup", check_for_updates_on_startup_, @@ -33,7 +34,7 @@ class global_configuration final { pqrs::json::requires_object(json, "json"); - helper_values_.update_value(json); + helper_values_.update_value(json, error_handling); } nlohmann::json to_json(void) const { diff --git a/src/share/core_configuration/details/machine_specific.hpp b/src/share/core_configuration/details/machine_specific.hpp index 8d2a0cafd..76d7563ae 100644 --- a/src/share/core_configuration/details/machine_specific.hpp +++ b/src/share/core_configuration/details/machine_specific.hpp @@ -13,7 +13,8 @@ class machine_specific final { public: entry(const entry&) = delete; - entry(const nlohmann::json& json) + entry(const nlohmann::json& json, + error_handling error_handling) : json_(json) { helper_values_.push_back_value("enable_multitouch_extension", enable_multitouch_extension_, @@ -21,7 +22,7 @@ class machine_specific final { pqrs::json::requires_object(json, "json"); - helper_values_.update_value(json); + helper_values_.update_value(json, error_handling); } nlohmann::json to_json(void) const { @@ -47,13 +48,16 @@ class machine_specific final { machine_specific(const machine_specific&) = delete; - machine_specific(const nlohmann::json& json) - : json_(json) { + machine_specific(const nlohmann::json& json, + error_handling error_handling) + : json_(json), + error_handling_(error_handling) { pqrs::json::requires_object(json, "json"); for (const auto& [key, value] : json.items()) { if (value.is_object()) { - entries_[karabiner_machine_identifier(key)] = std::make_shared(value); + entries_[karabiner_machine_identifier(key)] = std::make_shared(value, + error_handling_); } } } @@ -75,7 +79,8 @@ class machine_specific final { entry& get_entry(const karabiner_machine_identifier& identifier = constants::get_karabiner_machine_identifier()) { if (!entries_.contains(identifier)) { - entries_[identifier] = std::make_shared(nlohmann::json::object()); + entries_[identifier] = std::make_shared(nlohmann::json::object(), + error_handling_); } return *(entries_[identifier]); @@ -83,6 +88,7 @@ class machine_specific final { private: nlohmann::json json_; + error_handling error_handling_; std::unordered_map> entries_; }; diff --git a/src/share/core_configuration/details/profile.hpp b/src/share/core_configuration/details/profile.hpp index 15c52ec70..afc312232 100644 --- a/src/share/core_configuration/details/profile.hpp +++ b/src/share/core_configuration/details/profile.hpp @@ -15,8 +15,10 @@ class profile final { public: profile(const profile&) = delete; - profile(const nlohmann::json& json) + profile(const nlohmann::json& json, + error_handling error_handling) : json_(json), + error_handling_(error_handling), selected_(false) { helper_values_.push_back_array("devices", devices_); @@ -33,7 +35,7 @@ class profile final { pqrs::json::requires_object(json, "json"); - helper_values_.update_value(json); + helper_values_.update_value(json, error_handling); for (const auto& [key, value] : json.items()) { if (key == "name") { @@ -268,8 +270,9 @@ class profile final { } details::device d(nlohmann::json({ - {"identifiers", identifiers}, - })); + {"identifiers", identifiers}, + }), + error_handling_); return d.get_ignore(); } @@ -293,8 +296,9 @@ class profile final { } details::device d(nlohmann::json({ - {"identifiers", identifiers}, - })); + {"identifiers", identifiers}, + }), + error_handling_); return d.get_manipulate_caps_lock_led(); } @@ -318,8 +322,9 @@ class profile final { } details::device d(nlohmann::json({ - {"identifiers", identifiers}, - })); + {"identifiers", identifiers}, + }), + error_handling_); return d.get_treat_as_built_in_keyboard(); } @@ -976,11 +981,13 @@ class profile final { } devices_.push_back(std::make_shared(nlohmann::json({ - {"identifiers", identifiers}, - }))); + {"identifiers", identifiers}, + }), + error_handling_)); } nlohmann::json json_; + error_handling error_handling_; std::string name_; bool selected_; details::parameters parameters_; diff --git a/src/share/core_configuration/details/profile/device.hpp b/src/share/core_configuration/details/profile/device.hpp index 484b1322e..e6934513c 100644 --- a/src/share/core_configuration/details/profile/device.hpp +++ b/src/share/core_configuration/details/profile/device.hpp @@ -1,5 +1,6 @@ #pragma once +#include "../../configuration_json_helper.hpp" #include "exprtk_utility.hpp" #include "simple_modifications.hpp" #include @@ -71,22 +72,47 @@ if (abs(cos(radian)) <= abs(sin(radian))) { device(const device&) = delete; - device(const nlohmann::json& json) : json_(json), - ignore_(false), - manipulate_caps_lock_led_(false), - treat_as_built_in_keyboard_(false), - disable_built_in_keyboard_if_exists_(false), - mouse_flip_x_(false), - mouse_flip_y_(false), - mouse_flip_vertical_wheel_(false), - mouse_flip_horizontal_wheel_(false), - mouse_swap_xy_(false), - mouse_swap_wheels_(false), - mouse_discard_x_(false), - mouse_discard_y_(false), - mouse_discard_vertical_wheel_(false), - mouse_discard_horizontal_wheel_(false), - game_pad_swap_sticks_(false) { + device(const nlohmann::json& json, + error_handling error_handling) + : json_(json), + ignore_(false), + manipulate_caps_lock_led_(false), + treat_as_built_in_keyboard_(false), + disable_built_in_keyboard_if_exists_(false) { + helper_values_.push_back_value("mouse_flip_x", + mouse_flip_x_, + false); + helper_values_.push_back_value("mouse_flip_y", + mouse_flip_y_, + false); + helper_values_.push_back_value("mouse_flip_vertical_wheel", + mouse_flip_vertical_wheel_, + false); + helper_values_.push_back_value("mouse_flip_horizontal_wheel", + mouse_flip_horizontal_wheel_, + false); + helper_values_.push_back_value("mouse_swap_xy", + mouse_swap_xy_, + false); + helper_values_.push_back_value("mouse_swap_wheels", + mouse_swap_wheels_, + false); + helper_values_.push_back_value("mouse_discard_x", + mouse_discard_x_, + false); + helper_values_.push_back_value("mouse_discard_y", + mouse_discard_y_, + false); + helper_values_.push_back_value("mouse_discard_vertical_wheel", + mouse_discard_vertical_wheel_, + false); + helper_values_.push_back_value("mouse_discard_horizontal_wheel", + mouse_discard_horizontal_wheel_, + false); + helper_values_.push_back_value("game_pad_swap_sticks", + game_pad_swap_sticks_, + false); + auto ignore_configured = false; auto manipulate_caps_lock_led_configured = false; @@ -102,6 +128,8 @@ if (abs(cos(radian)) <= abs(sin(radian))) { pqrs::json::requires_object(json, "json"); + helper_values_.update_value(json, error_handling); + for (const auto& [key, value] : json.items()) { if (key == "identifiers") { try { @@ -132,61 +160,6 @@ if (abs(cos(radian)) <= abs(sin(radian))) { disable_built_in_keyboard_if_exists_ = value.get(); - } else if (key == "mouse_flip_x") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_flip_x_ = value.get(); - - } else if (key == "mouse_flip_y") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_flip_y_ = value.get(); - - } else if (key == "mouse_flip_vertical_wheel") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_flip_vertical_wheel_ = value.get(); - - } else if (key == "mouse_flip_horizontal_wheel") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_flip_horizontal_wheel_ = value.get(); - - } else if (key == "mouse_swap_xy") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_swap_xy_ = value.get(); - - } else if (key == "mouse_swap_wheels") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_swap_wheels_ = value.get(); - - } else if (key == "mouse_discard_x") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_discard_x_ = value.get(); - - } else if (key == "mouse_discard_y") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_discard_y_ = value.get(); - - } else if (key == "mouse_discard_vertical_wheel") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_discard_vertical_wheel_ = value.get(); - - } else if (key == "mouse_discard_horizontal_wheel") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - mouse_discard_horizontal_wheel_ = value.get(); - - } else if (key == "game_pad_swap_sticks") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - game_pad_swap_sticks_ = value.get(); - } else if (key == "game_pad_xy_stick_continued_movement_absolute_magnitude_threshold") { pqrs::json::requires_number(value, "`" + key + "`"); @@ -291,22 +264,14 @@ if (abs(cos(radian)) <= abs(sin(radian))) { nlohmann::json to_json(void) const { auto j = json_; + + helper_values_.update_json(j); + j["identifiers"] = identifiers_; j["ignore"] = ignore_; j["manipulate_caps_lock_led"] = manipulate_caps_lock_led_; j["treat_as_built_in_keyboard"] = treat_as_built_in_keyboard_; j["disable_built_in_keyboard_if_exists"] = disable_built_in_keyboard_if_exists_; - j["mouse_flip_x"] = mouse_flip_x_; - j["mouse_flip_y"] = mouse_flip_y_; - j["mouse_flip_vertical_wheel"] = mouse_flip_vertical_wheel_; - j["mouse_flip_horizontal_wheel"] = mouse_flip_horizontal_wheel_; - j["mouse_swap_xy"] = mouse_swap_xy_; - j["mouse_swap_wheels"] = mouse_swap_wheels_; - j["mouse_discard_x"] = mouse_discard_x_; - j["mouse_discard_y"] = mouse_discard_y_; - j["mouse_discard_vertical_wheel"] = mouse_discard_vertical_wheel_; - j["mouse_discard_horizontal_wheel"] = mouse_discard_horizontal_wheel_; - j["game_pad_swap_sticks"] = game_pad_swap_sticks_; #define OPTIONAL_SETTING(name) \ { \ @@ -684,6 +649,7 @@ if (abs(cos(radian)) <= abs(sin(radian))) { std::optional game_pad_stick_horizontal_wheel_formula_; simple_modifications simple_modifications_; simple_modifications fn_function_keys_; + configuration_json_helper::helper_values helper_values_; }; inline void to_json(nlohmann::json& json, const device& device) { diff --git a/src/share/core_configuration/types.hpp b/src/share/core_configuration/types.hpp new file mode 100644 index 000000000..181a0e286 --- /dev/null +++ b/src/share/core_configuration/types.hpp @@ -0,0 +1,12 @@ +#pragma once + +namespace krbn { +namespace core_configuration { + +enum class error_handling { + loose, // ignore errors + strict, // throw exception +}; + +} +} // namespace krbn diff --git a/tests/src/core_configuration/json/to_json_example.json b/tests/src/core_configuration/json/to_json_example.json index afe536371..57f7eab80 100644 --- a/tests/src/core_configuration/json/to_json_example.json +++ b/tests/src/core_configuration/json/to_json_example.json @@ -128,7 +128,6 @@ ] } ], - "game_pad_swap_sticks": false, "identifiers": { "is_game_pad": false, "is_keyboard": true, @@ -138,16 +137,6 @@ }, "ignore": false, "manipulate_caps_lock_led": true, - "mouse_discard_horizontal_wheel": false, - "mouse_discard_vertical_wheel": false, - "mouse_discard_x": false, - "mouse_discard_y": false, - "mouse_flip_horizontal_wheel": false, - "mouse_flip_vertical_wheel": false, - "mouse_flip_x": false, - "mouse_flip_y": false, - "mouse_swap_wheels": false, - "mouse_swap_xy": false, "simple_modifications": [ { "from": { @@ -165,7 +154,6 @@ { "disable_built_in_keyboard_if_exists": true, "fn_function_keys": [], - "game_pad_swap_sticks": false, "identifiers": { "is_game_pad": false, "is_keyboard": true, @@ -175,16 +163,6 @@ }, "ignore": true, "manipulate_caps_lock_led": true, - "mouse_discard_horizontal_wheel": false, - "mouse_discard_vertical_wheel": false, - "mouse_discard_x": false, - "mouse_discard_y": false, - "mouse_flip_horizontal_wheel": false, - "mouse_flip_vertical_wheel": false, - "mouse_flip_x": false, - "mouse_flip_y": false, - "mouse_swap_wheels": false, - "mouse_swap_xy": false, "simple_modifications": [], "treat_as_built_in_keyboard": false }, @@ -212,7 +190,6 @@ ] } ], - "game_pad_swap_sticks": false, "identifiers": { "is_game_pad": false, "is_keyboard": true, @@ -222,16 +199,6 @@ }, "ignore": false, "manipulate_caps_lock_led": true, - "mouse_discard_horizontal_wheel": false, - "mouse_discard_vertical_wheel": false, - "mouse_discard_x": false, - "mouse_discard_y": false, - "mouse_flip_horizontal_wheel": false, - "mouse_flip_vertical_wheel": false, - "mouse_flip_x": false, - "mouse_flip_y": false, - "mouse_swap_wheels": false, - "mouse_swap_xy": false, "simple_modifications": [ { "from": { diff --git a/tests/src/core_configuration/src/core_configuration_test.hpp b/tests/src/core_configuration/src/core_configuration_test.hpp index d944567f0..25ecb7888 100644 --- a/tests/src/core_configuration/src/core_configuration_test.hpp +++ b/tests/src/core_configuration/src/core_configuration_test.hpp @@ -68,7 +68,9 @@ void run_core_configuration_test(void) { using namespace std::literals; "valid"_test = [] { - krbn::core_configuration::core_configuration configuration("json/example.jsonc", geteuid()); + krbn::core_configuration::core_configuration configuration("json/example.jsonc", + geteuid(), + krbn::core_configuration::error_handling::strict); { std::vector> expected; @@ -173,13 +175,15 @@ void run_core_configuration_test(void) { { std::ifstream input("json/to_json_example.json"); auto expected = krbn::json_utility::parse_jsonc(input); - expect(configuration.to_json() == expected) << "json/to_json_example.json is not match"; + expect(configuration.to_json() == expected) << "configuration.to_json() == expected"; } }; "not found"_test = [] { { - krbn::core_configuration::core_configuration configuration("json/not_found.json", geteuid()); + krbn::core_configuration::core_configuration configuration("json/not_found.json", + geteuid(), + krbn::core_configuration::error_handling::strict); expect(configuration.get_selected_profile().get_name() == "Default profile"); expect(configuration.is_loaded() == false); } @@ -187,7 +191,9 @@ void run_core_configuration_test(void) { "broken.json"_test = [] { { - krbn::core_configuration::core_configuration configuration("json/broken.json", geteuid()); + krbn::core_configuration::core_configuration configuration("json/broken.json", + geteuid(), + krbn::core_configuration::error_handling::strict); expect(configuration.get_selected_profile().get_simple_modifications().get_pairs().empty()); expect(configuration.is_loaded() == false); @@ -210,7 +216,9 @@ void run_core_configuration_test(void) { } } { - krbn::core_configuration::core_configuration configuration("/bin/ls", geteuid()); + krbn::core_configuration::core_configuration configuration("/bin/ls", + geteuid(), + krbn::core_configuration::error_handling::strict); expect(configuration.get_selected_profile().get_simple_modifications().get_pairs().empty()); expect(configuration.is_loaded() == false); @@ -218,7 +226,9 @@ void run_core_configuration_test(void) { }; "invalid_key_code_name.json"_test = [] { - krbn::core_configuration::core_configuration configuration("json/invalid_key_code_name.json", geteuid()); + krbn::core_configuration::core_configuration configuration("json/invalid_key_code_name.json", + geteuid(), + krbn::core_configuration::error_handling::strict); std::vector> expected; @@ -235,7 +245,8 @@ void run_core_configuration_test(void) { "global_configuration.to_json"_test = [] { { auto json = nlohmann::json::object(); - krbn::core_configuration::details::global_configuration global_configuration(json); + krbn::core_configuration::details::global_configuration global_configuration(json, + krbn::core_configuration::error_handling::strict); nlohmann::json expected({}); expect(global_configuration.to_json() == expected); @@ -246,7 +257,8 @@ void run_core_configuration_test(void) { nlohmann::json json{ {"dummy", {{"keep_me", true}}}, }; - krbn::core_configuration::details::global_configuration global_configuration(json); + krbn::core_configuration::details::global_configuration global_configuration(json, + krbn::core_configuration::error_handling::strict); global_configuration.set_check_for_updates_on_startup(false); global_configuration.set_show_in_menu_bar(false); global_configuration.set_show_profile_name_in_menu_bar(true); @@ -266,7 +278,9 @@ void run_core_configuration_test(void) { "machine_specific.to_json"_test = [] { { - krbn::core_configuration::core_configuration configuration("json/machine_specific.jsonc", geteuid()); + krbn::core_configuration::core_configuration configuration("json/machine_specific.jsonc", + geteuid(), + krbn::core_configuration::error_handling::strict); std::ifstream input("json/machine_specific.jsonc"); auto expected = krbn::json_utility::parse_jsonc(input); @@ -282,7 +296,9 @@ void run_core_configuration_test(void) { // from emtpy json { - krbn::core_configuration::core_configuration configuration("", geteuid()); + krbn::core_configuration::core_configuration configuration("", + geteuid(), + krbn::core_configuration::error_handling::strict); auto json = configuration.to_json(); expect(!json.contains("machine_specific")); @@ -315,7 +331,8 @@ void run_core_configuration_test(void) { // empty json { auto json = nlohmann::json::object(); - krbn::core_configuration::details::profile profile(json); + krbn::core_configuration::details::profile profile(json, + krbn::core_configuration::error_handling::strict); expect(profile.get_name() == std::string("")); expect(profile.get_selected() == false); expect(profile.get_simple_modifications().get_pairs().size() == 0); @@ -451,7 +468,8 @@ void run_core_configuration_test(void) { }, }}, }); - krbn::core_configuration::details::profile profile(json); + krbn::core_configuration::details::profile profile(json, + krbn::core_configuration::error_handling::strict); expect(profile.get_name() == std::string("profile 1")); expect(profile.get_selected() == true); { @@ -645,7 +663,8 @@ void run_core_configuration_test(void) { }, }}, }); - krbn::core_configuration::details::profile profile(json); + krbn::core_configuration::details::profile profile(json, + krbn::core_configuration::error_handling::strict); { std::vector> expected; @@ -660,7 +679,8 @@ void run_core_configuration_test(void) { "profile.to_json"_test = [] { { auto json = nlohmann::json::object(); - krbn::core_configuration::details::profile profile(json); + krbn::core_configuration::details::profile empty_profile(json, + krbn::core_configuration::error_handling::strict); nlohmann::json expected({ {"complex_modifications", nlohmann::json::object({ {"rules", nlohmann::json::array()}, @@ -681,7 +701,7 @@ void run_core_configuration_test(void) { {"fn_function_keys", get_default_fn_function_keys_json()}, {"virtual_hid_keyboard", get_default_virtual_hid_keyboard_json()}, }); - expect(profile.to_json() == expected); + expect(empty_profile.to_json() == expected) << "empty_profile.to_json() == expected"; } { nlohmann::json json({ @@ -710,22 +730,12 @@ void run_core_configuration_test(void) { {"ignore", true}, {"disable_built_in_keyboard_if_exists", true}, {"manipulate_caps_lock_led", false}, - {"mouse_discard_horizontal_wheel", false}, - {"mouse_discard_vertical_wheel", false}, - {"mouse_discard_x", false}, - {"mouse_discard_y", false}, - {"mouse_flip_horizontal_wheel", false}, - {"mouse_flip_vertical_wheel", false}, - {"mouse_flip_x", false}, - {"mouse_flip_y", false}, - {"mouse_swap_xy", false}, - {"mouse_swap_wheels", false}, - {"game_pad_swap_sticks", false}, {"treat_as_built_in_keyboard", false}, }, }}, }); - krbn::core_configuration::details::profile profile(json); + krbn::core_configuration::details::profile profile(json, + krbn::core_configuration::error_handling::strict); profile.set_name("profile 1"); profile.set_selected(true); @@ -895,18 +905,7 @@ void run_core_configuration_test(void) { {"ignore", true}, {"disable_built_in_keyboard_if_exists", true}, {"fn_function_keys", nlohmann::json::array()}, - {"game_pad_swap_sticks", false}, {"manipulate_caps_lock_led", false}, - {"mouse_discard_horizontal_wheel", false}, - {"mouse_discard_vertical_wheel", false}, - {"mouse_discard_x", false}, - {"mouse_discard_y", false}, - {"mouse_flip_horizontal_wheel", false}, - {"mouse_flip_vertical_wheel", false}, - {"mouse_flip_x", false}, - {"mouse_flip_y", false}, - {"mouse_swap_wheels", false}, - {"mouse_swap_xy", false}, {"simple_modifications", nlohmann::json::array()}, {"treat_as_built_in_keyboard", false}, }, @@ -1530,26 +1529,34 @@ void run_core_configuration_test(void) { "complex_modifications.minmax_parameter_value"_test = [] { { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test1.json", geteuid()); + krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test1.json", + geteuid(), + krbn::core_configuration::error_handling::strict); auto actual = configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("basic.simultaneous_threshold_milliseconds"); expect(actual->first == 101); expect(actual->second == 401); } { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test2.json", geteuid()); + krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test2.json", + geteuid(), + krbn::core_configuration::error_handling::strict); auto actual = configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("basic.simultaneous_threshold_milliseconds"); expect(actual->first == 102); expect(actual->second == 402); } { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test3.json", geteuid()); + krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test3.json", + geteuid(), + krbn::core_configuration::error_handling::strict); auto actual = configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("basic.simultaneous_threshold_milliseconds"); expect(actual->first == 103); expect(actual->second == 403); } { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test1.json", geteuid()); + krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test1.json", + geteuid(), + krbn::core_configuration::error_handling::strict); expect(!configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("unknown")); } }; diff --git a/tests/src/core_configuration/src/device_test.hpp b/tests/src/core_configuration/src/device_test.hpp index f4db3479c..2b19e879f 100644 --- a/tests/src/core_configuration/src/device_test.hpp +++ b/tests/src/core_configuration/src/device_test.hpp @@ -114,7 +114,8 @@ void run_device_test(void) { // empty json { auto json = nlohmann::json::object(); - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_identifiers().get_vendor_id() == pqrs::hid::vendor_id::value_t(0)); expect(device.get_identifiers().get_product_id() == pqrs::hid::product_id::value_t(0)); expect(device.get_identifiers().get_device_address() == ""); @@ -163,7 +164,8 @@ void run_device_test(void) { ")", })}, }); - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_identifiers().get_vendor_id() == pqrs::hid::vendor_id::value_t(1234)); expect(device.get_identifiers().get_product_id() == pqrs::hid::product_id::value_t(5678)); expect(device.get_identifiers().get_device_address() == "ec-ba-73-21-e6-f5"); @@ -196,12 +198,14 @@ void run_device_test(void) { }}, }); { - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_ignore() == true); } { json["ignore"] = false; - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_ignore() == false); } } @@ -217,12 +221,14 @@ void run_device_test(void) { }}, }); { - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_ignore() == true); } { json["ignore"] = false; - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_ignore() == false); } } @@ -238,12 +244,14 @@ void run_device_test(void) { }}, }); { - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_manipulate_caps_lock_led() == true); } { json["manipulate_caps_lock_led"] = false; - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_manipulate_caps_lock_led() == false); } } @@ -262,7 +270,8 @@ void run_device_test(void) { { // disable_built_in_keyboard_if_exists will be false when treat_as_built_in_keyboard is true. - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); expect(device.get_treat_as_built_in_keyboard() == true); expect(device.get_disable_built_in_keyboard_if_exists() == false); @@ -292,11 +301,11 @@ void run_device_test(void) { "device.to_json"_test = [] { { auto json = nlohmann::json::object(); - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device empty_device(json, + krbn::core_configuration::error_handling::strict); nlohmann::json expected({ {"disable_built_in_keyboard_if_exists", false}, {"fn_function_keys", nlohmann::json::array()}, - {"game_pad_swap_sticks", false}, {"identifiers", { { "vendor_id", @@ -321,24 +330,12 @@ void run_device_test(void) { }}, {"ignore", false}, {"manipulate_caps_lock_led", false}, - {"mouse_discard_horizontal_wheel", false}, - {"mouse_discard_vertical_wheel", false}, - {"mouse_discard_x", false}, - {"mouse_discard_y", false}, - {"mouse_flip_horizontal_wheel", false}, - {"mouse_flip_vertical_wheel", false}, - {"mouse_flip_x", false}, - {"mouse_flip_y", false}, - {"mouse_swap_wheels", false}, - {"mouse_swap_xy", false}, {"simple_modifications", nlohmann::json::array()}, {"treat_as_built_in_keyboard", false}, }); - expect(device.to_json() == expected); - - nlohmann::json actual = device.to_json(); - expect(actual == expected); + expect(empty_device.to_json() == expected) << "empty_device.to_json() == expected"; } + { nlohmann::json json({ {"dummy", {{"keep_me", true}}}, @@ -355,11 +352,16 @@ void run_device_test(void) { {"ignore", true}, {"manipulate_caps_lock_led", true}, {"treat_as_built_in_keyboard", true}, + {"mouse_discard_horizontal_wheel", true}, {"mouse_discard_vertical_wheel", true}, + {"mouse_discard_x", true}, {"mouse_discard_y", true}, {"mouse_flip_horizontal_wheel", true}, + {"mouse_flip_vertical_wheel", true}, {"mouse_flip_x", true}, + {"mouse_flip_y", true}, {"mouse_swap_wheels", true}, + {"mouse_swap_xy", true}, {"game_pad_swap_sticks", true}, {"game_pad_xy_stick_continued_movement_absolute_magnitude_threshold", 0.5}, {"game_pad_xy_stick_continued_movement_interval_milliseconds", 10}, @@ -375,7 +377,8 @@ void run_device_test(void) { {"game_pad_stick_vertical_wheel_formula", "sgn(sin(radian))"}, {"game_pad_stick_horizontal_wheel_formula", "sgn(cos(radian))"}, }); - krbn::core_configuration::details::device device(json); + krbn::core_configuration::details::device device(json, + krbn::core_configuration::error_handling::strict); nlohmann::json expected({ {"disable_built_in_keyboard_if_exists", false}, {"dummy", {{"keep_me", true}}}, @@ -422,16 +425,16 @@ void run_device_test(void) { {"game_pad_stick_vertical_wheel_formula", "sgn(sin(radian))"}, {"game_pad_stick_horizontal_wheel_formula", "sgn(cos(radian))"}, {"manipulate_caps_lock_led", true}, - {"mouse_discard_horizontal_wheel", false}, + {"mouse_discard_horizontal_wheel", true}, {"mouse_discard_vertical_wheel", true}, - {"mouse_discard_x", false}, + {"mouse_discard_x", true}, {"mouse_discard_y", true}, {"mouse_flip_horizontal_wheel", true}, - {"mouse_flip_vertical_wheel", false}, + {"mouse_flip_vertical_wheel", true}, {"mouse_flip_x", true}, - {"mouse_flip_y", false}, + {"mouse_flip_y", true}, {"mouse_swap_wheels", true}, - {"mouse_swap_xy", false}, + {"mouse_swap_xy", true}, {"simple_modifications", nlohmann::json::array()}, {"treat_as_built_in_keyboard", true}, }); diff --git a/tests/src/core_configuration/src/errors_test.hpp b/tests/src/core_configuration/src/errors_test.hpp index 8e88eec49..df32ba233 100644 --- a/tests/src/core_configuration/src/errors_test.hpp +++ b/tests/src/core_configuration/src/errors_test.hpp @@ -11,11 +11,13 @@ void handle_json(const nlohmann::json& json) { if (c == "complex_modifications") { krbn::core_configuration::details::complex_modifications(json.at("input")); } else if (c == "devices") { - krbn::core_configuration::details::device(json.at("input")); + krbn::core_configuration::details::device(json.at("input"), + krbn::core_configuration::error_handling::strict); } else if (c == "parameters") { json.at("input").get(); } else if (c == "profile") { - krbn::core_configuration::details::profile(json.at("input")); + krbn::core_configuration::details::profile(json.at("input"), + krbn::core_configuration::error_handling::strict); } else if (c == "simple_modifications") { krbn::core_configuration::details::simple_modifications m; m.update(json.at("input")); @@ -42,8 +44,8 @@ void run_errors_test(void) { expect(false) << "The expected exception is not thrown"; } catch (pqrs::json::unmarshal_error& ex) { expect(std::string_view(e.at("error").get()) == ex.what()); - } catch (...) { - expect(false) << "An unexpected exception is thrown"; + } catch (std::exception& ex) { + expect(false) << "An unexpected exception is thrown [" << ex.what() << "] " << e.at("input"); } } } diff --git a/tests/src/core_configuration/src/global_configuration_test.hpp b/tests/src/core_configuration/src/global_configuration_test.hpp index 77e4e1e4d..729f84d89 100644 --- a/tests/src/core_configuration/src/global_configuration_test.hpp +++ b/tests/src/core_configuration/src/global_configuration_test.hpp @@ -9,7 +9,8 @@ void run_global_configuration_test(void) { // empty json { auto json = nlohmann::json::object(); - krbn::core_configuration::details::global_configuration global_configuration(json); + krbn::core_configuration::details::global_configuration global_configuration(json, + krbn::core_configuration::error_handling::strict); expect(global_configuration.get_check_for_updates_on_startup() == true); expect(global_configuration.get_show_in_menu_bar() == true); expect(global_configuration.get_show_profile_name_in_menu_bar() == false); @@ -28,7 +29,8 @@ void run_global_configuration_test(void) { {"ask_for_confirmation_before_quitting", false}, {"unsafe_ui", true}, }; - krbn::core_configuration::details::global_configuration global_configuration(json); + krbn::core_configuration::details::global_configuration global_configuration(json, + krbn::core_configuration::error_handling::strict); expect(global_configuration.get_check_for_updates_on_startup() == false); expect(global_configuration.get_show_in_menu_bar() == false); expect(global_configuration.get_show_profile_name_in_menu_bar() == true); @@ -59,7 +61,8 @@ void run_global_configuration_test(void) { {"enable_notification_window", nlohmann::json::object()}, {"unsafe_ui", false}, }; - krbn::core_configuration::details::global_configuration global_configuration(json); + krbn::core_configuration::details::global_configuration global_configuration(json, + krbn::core_configuration::error_handling::loose); expect(global_configuration.get_check_for_updates_on_startup() == true); expect(global_configuration.get_show_in_menu_bar() == true); expect(global_configuration.get_show_profile_name_in_menu_bar() == false); diff --git a/tests/src/core_configuration/src/machine_specific_test.hpp b/tests/src/core_configuration/src/machine_specific_test.hpp index 5cfb093be..926ad114c 100644 --- a/tests/src/core_configuration/src/machine_specific_test.hpp +++ b/tests/src/core_configuration/src/machine_specific_test.hpp @@ -13,7 +13,8 @@ void run_machine_specific_test(void) { // empty json { auto json = nlohmann::json::object(); - krbn::core_configuration::details::machine_specific machine_specific(json); + krbn::core_configuration::details::machine_specific machine_specific(json, + krbn::core_configuration::error_handling::strict); nlohmann::json j(machine_specific); expect(nlohmann::json::object() == j); @@ -31,7 +32,8 @@ void run_machine_specific_test(void) { {"enable_multitouch_extension", true}, })}, }); - krbn::core_configuration::details::machine_specific machine_specific(json); + krbn::core_configuration::details::machine_specific machine_specific(json, + krbn::core_configuration::error_handling::strict); expect(true == machine_specific.get_entry(id1).get_enable_multitouch_extension()); // new entry expect(false == machine_specific.get_entry(id2).get_enable_multitouch_extension()); @@ -68,7 +70,8 @@ void run_machine_specific_test(void) { {"enable_multitouch_extension", 42}, })}, }); - krbn::core_configuration::details::machine_specific machine_specific(json); + krbn::core_configuration::details::machine_specific machine_specific(json, + krbn::core_configuration::error_handling::loose); expect(false == machine_specific.get_entry(id1).get_enable_multitouch_extension()); } };