cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
Loading...
Searching...
No Matches
mock.h
Go to the documentation of this file.
1#pragma once
2
3#include <gmock/gmock.h>
4
5#include <string>
6#include <string_view>
7#include <unordered_map>
8
9#include "cppfig/interface.h"
10#include "cppfig/setting.h"
11#include "cppfig/traits.h"
12#include "cppfig/value.h"
13
14namespace cppfig::testing {
15
45template <typename Schema>
47public:
48 using schema_type = Schema;
49
50 MockConfiguration() { BuildDefaults(); }
51
53 template <IsSetting S>
54 requires(Schema::template has_setting<S>)
55 [[nodiscard]] auto Get() const -> typename S::value_type
56 {
57 using value_type = typename S::value_type;
58 std::string key(S::path);
59
60 auto iter = values_.find(key);
61 if (iter != values_.end()) {
62 auto parsed = ConfigTraits<value_type>::Deserialize(iter->second);
63 if (parsed.has_value()) {
64 return *parsed;
65 }
66 }
67
68 // Return default
69 return S::default_value();
70 }
71
73 template <IsSetting S>
74 requires(Schema::template has_setting<S>)
75 void SetValue(typename S::value_type value)
76 {
77 using value_type = typename S::value_type;
78 std::string key(S::path);
79 values_[key] = ConfigTraits<value_type>::Serialize(value);
80 }
81
83 template <IsSetting S>
84 requires(Schema::template has_setting<S>)
85 auto Set(typename S::value_type value) -> Status
86 {
87 auto validator = GetSettingValidator<S>();
88 auto validation = validator(value);
89 if (!validation) {
90 return InvalidArgumentError(validation.error_message);
91 }
92
93 SetValue<S>(std::move(value));
94 return OkStatus();
95 }
96
98 [[nodiscard]] auto Load() -> Status { return OkStatus(); }
99
101 [[nodiscard]] auto Save() const -> Status { return OkStatus(); }
102
104 void Reset()
105 {
106 values_.clear();
107 BuildDefaults();
108 }
109
116 void SetRawValue(std::string_view path, Value value) { values_[std::string(path)] = std::move(value); }
117
123 void ClearValue(std::string_view path) { values_.erase(std::string(path)); }
124
125private:
126 void BuildDefaults()
127 {
128 Schema::ForEachSetting([this]<typename S>() {
129 using value_type = typename S::value_type;
130 std::string key(S::path);
131 values_[key] = ConfigTraits<value_type>::Serialize(S::default_value());
132 });
133 }
134
135 std::unordered_map<std::string, Value> values_;
136};
137
149// LCOV_EXCL_START — GMock macro internals generate uninstrumented stubs
151public:
152 MOCK_METHOD(Status, Load, (), (override));
153 MOCK_METHOD(Status, Save, (), (const, override));
154 MOCK_METHOD(std::string_view, GetFilePath, (), (const, override));
155 MOCK_METHOD(Status, ValidateAll, (), (const, override));
156 MOCK_METHOD(std::string, GetDiffString, (), (const, override));
157};
158
164public:
166 [[nodiscard]] static auto CreateTempFilePath(std::string_view prefix = "test_config") -> std::string
167 {
168 return std::string("/tmp/") + std::string(prefix) + "_" + std::to_string(std::rand()) + ".conf";
169 }
170
172 static void RemoveFile(const std::string& path) { std::remove(path.c_str()); }
173};
174// LCOV_EXCL_STOP
175
176} // namespace cppfig::testing
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, ValidateAll,(),(const, override))
MOCK_METHOD(std::string_view, GetFilePath,(),(const, override))
Definition mock.h:14
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.