3#include <gmock/gmock.h>
10#include <system_error>
11#include <unordered_map>
49template <
typename Schema>
57 template <IsSetting S>
58 requires(Schema::template has_setting<S>)
59 [[nodiscard]]
auto Get()
const ->
typename S::value_type
61 using value_type =
typename S::value_type;
62 std::string key(S::path);
64 auto iter = values_.find(key);
65 if (iter != values_.end()) {
67 if (parsed.has_value()) {
73 return S::default_value();
77 template <IsSetting S>
78 requires(Schema::template has_setting<S>)
81 using value_type =
typename S::value_type;
82 std::string key(S::path);
87 template <IsSetting S>
88 requires(Schema::template has_setting<S>)
91 auto validator = GetSettingValidator<S>();
92 auto validation = validator(value);
97 SetValue<S>(std::move(value));
120 void SetRawValue(std::string_view path,
Value value) { values_[std::string(path)] = std::move(value); }
127 void ClearValue(std::string_view path) { values_.erase(std::string(path)); }
132 Schema::ForEachSetting([
this]<
typename S>() {
133 using value_type =
typename S::value_type;
134 std::string key(S::path);
139 std::unordered_map<std::string, Value> values_;
176 static std::mt19937_64 generator { std::random_device {}() };
177 std::uniform_int_distribution<std::uint64_t> distribution;
179 const auto path = std::filesystem::temp_directory_path()
180 / (std::string(prefix) +
"_" + std::to_string(distribution(generator)) +
".conf");
181 return path.string();
187 std::error_code error_code;
188 std::filesystem::remove(path, error_code);
Virtual interface for type-erased configuration access.
Definition interface.h:103
virtual auto Save() const -> Status=0
Saves the current configuration to the file.
virtual auto Load() -> Status=0
Loads configuration from the file.
virtual auto GetFilePath() const -> std::string_view=0
Returns the file path.
virtual auto GetDiffString() const -> std::string=0
Gets a string representation of the diff.
virtual auto ValidateAll() const -> Status=0
Validates all current values.
A lightweight status object carrying an error code and message.
Definition status.h:25
A self-contained, recursive value type for configuration data.
Definition value.h:32
Test fixture helper for configuration tests.
Definition mock.h:167
static auto CreateTempFilePath(std::string_view prefix="test_config") -> std::string
Creates a temporary file path for testing.
Definition mock.h:174
static void RemoveFile(const std::string &path)
Removes a file if it exists.
Definition mock.h:185
Mock implementation of configuration for testing.
Definition mock.h:50
void SetValue(typename S::value_type value)
Sets the value for a setting type (bypasses validation for testing).
Definition mock.h:79
Schema schema_type
Definition mock.h:52
auto Load() -> Status
Simulates loading (no-op for mock).
Definition mock.h:102
MockConfiguration()
Definition mock.h:54
void Reset()
Resets all values to defaults.
Definition mock.h:108
void ClearValue(std::string_view path)
Clears a value by path for testing scenarios where key is not found.
Definition mock.h:127
auto Save() const -> Status
Simulates saving (no-op for mock).
Definition mock.h:105
void SetRawValue(std::string_view path, Value value)
Sets a raw Value for testing parse failure scenarios.
Definition mock.h:120
auto Get() const -> typename S::value_type
Gets the value for a setting type.
Definition mock.h:59
auto Set(typename S::value_type value) -> Status
Sets the value with validation.
Definition mock.h:89
GMock-compatible mock for IConfigurationProviderVirtual.
Definition mock.h:154
MOCK_METHOD(Status, Save,(),(const, override))
MOCK_METHOD(std::string, GetDiffString,(),(const, override))
MOCK_METHOD(Status, Load,(),(override))
MOCK_METHOD(Status, ValidateAll,(),(const, override))
MOCK_METHOD(std::string_view, GetFilePath,(),(const, override))
auto OkStatus() -> Status
Returns an OK status.
Definition status.h:54
auto InvalidArgumentError(std::string message) -> Status
Returns an InvalidArgument error status.
Definition status.h:63
static auto Deserialize(const Value &value) -> std::optional< T >=delete
Deserializes a value from a Value node.
static auto Serialize(const T &value) -> Value=delete
Serializes a value to a Value node.