3#include <gmock/gmock.h>
7#include <unordered_map>
45template <
typename Schema>
53 template <IsSetting S>
54 requires(Schema::template has_setting<S>)
55 [[nodiscard]]
auto Get()
const ->
typename S::value_type
57 using value_type =
typename S::value_type;
58 std::string key(S::path);
60 auto iter = values_.find(key);
61 if (iter != values_.end()) {
63 if (parsed.has_value()) {
69 return S::default_value();
73 template <IsSetting S>
74 requires(Schema::template has_setting<S>)
77 using value_type =
typename S::value_type;
78 std::string key(S::path);
83 template <IsSetting S>
84 requires(Schema::template has_setting<S>)
87 auto validator = GetSettingValidator<S>();
88 auto validation = validator(value);
93 SetValue<S>(std::move(value));
116 void SetRawValue(std::string_view path,
Value value) { values_[std::string(path)] = std::move(value); }
123 void ClearValue(std::string_view path) { values_.erase(std::string(path)); }
128 Schema::ForEachSetting([
this]<
typename S>() {
129 using value_type =
typename S::value_type;
130 std::string key(S::path);
135 std::unordered_map<std::string, Value> values_;
168 return std::string(
"/tmp/") + std::string(prefix) +
"_" + std::to_string(std::rand()) +
".conf";
172 static void RemoveFile(
const std::string& path) { std::remove(path.c_str()); }
Virtual interface for type-erased configuration access.
Definition interface.h:89
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:23
A self-contained, recursive value type for configuration data.
Definition value.h:26
Test fixture helper for configuration tests.
Definition mock.h:163
static auto CreateTempFilePath(std::string_view prefix="test_config") -> std::string
Creates a temporary file path for testing.
Definition mock.h:166
static void RemoveFile(const std::string &path)
Removes a file if it exists.
Definition mock.h:172
Mock implementation of configuration for testing.
Definition mock.h:46
void SetValue(typename S::value_type value)
Sets the value for a setting type (bypasses validation for testing).
Definition mock.h:75
Schema schema_type
Definition mock.h:48
auto Load() -> Status
Simulates loading (no-op for mock).
Definition mock.h:98
MockConfiguration()
Definition mock.h:50
void Reset()
Resets all values to defaults.
Definition mock.h:104
void ClearValue(std::string_view path)
Clears a value by path for testing scenarios where key is not found.
Definition mock.h:123
auto Save() const -> Status
Simulates saving (no-op for mock).
Definition mock.h:101
void SetRawValue(std::string_view path, Value value)
Sets a raw Value for testing parse failure scenarios.
Definition mock.h:116
auto Get() const -> typename S::value_type
Gets the value for a setting type.
Definition mock.h:55
auto Set(typename S::value_type value) -> Status
Sets the value with validation.
Definition mock.h:85
GMock-compatible mock for IConfigurationProviderVirtual.
Definition mock.h:150
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:52
auto InvalidArgumentError(std::string message) -> Status
Returns an InvalidArgument error status.
Definition status.h:61
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.