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 <cstdint>
6#include <filesystem>
7#include <random>
8#include <string>
9#include <string_view>
10#include <system_error>
11#include <unordered_map>
12
13#include "cppfig/interface.h"
14#include "cppfig/setting.h"
15#include "cppfig/traits.h"
16#include "cppfig/value.h"
17
18namespace cppfig::testing {
19
49template <typename Schema>
51public:
52 using schema_type = Schema;
53
54 MockConfiguration() { BuildDefaults(); }
55
57 template <IsSetting S>
58 requires(Schema::template has_setting<S>)
59 [[nodiscard]] auto Get() const -> typename S::value_type
60 {
61 using value_type = typename S::value_type;
62 std::string key(S::path);
63
64 auto iter = values_.find(key);
65 if (iter != values_.end()) {
66 auto parsed = ConfigTraits<value_type>::Deserialize(iter->second);
67 if (parsed.has_value()) {
68 return *parsed;
69 }
70 }
71
72 // Return default
73 return S::default_value();
74 }
75
77 template <IsSetting S>
78 requires(Schema::template has_setting<S>)
79 void SetValue(typename S::value_type value)
80 {
81 using value_type = typename S::value_type;
82 std::string key(S::path);
83 values_[key] = ConfigTraits<value_type>::Serialize(value);
84 }
85
87 template <IsSetting S>
88 requires(Schema::template has_setting<S>)
89 auto Set(typename S::value_type value) -> Status
90 {
91 auto validator = GetSettingValidator<S>();
92 auto validation = validator(value);
93 if (!validation) {
94 return InvalidArgumentError(validation.error_message);
95 }
96
97 SetValue<S>(std::move(value));
98 return OkStatus();
99 }
100
102 [[nodiscard]] auto Load() -> Status { return OkStatus(); }
103
105 [[nodiscard]] auto Save() const -> Status { return OkStatus(); }
106
108 void Reset()
109 {
110 values_.clear();
111 BuildDefaults();
112 }
113
120 void SetRawValue(std::string_view path, Value value) { values_[std::string(path)] = std::move(value); }
121
127 void ClearValue(std::string_view path) { values_.erase(std::string(path)); }
128
129private:
130 void BuildDefaults()
131 {
132 Schema::ForEachSetting([this]<typename S>() {
133 using value_type = typename S::value_type;
134 std::string key(S::path);
135 values_[key] = ConfigTraits<value_type>::Serialize(S::default_value());
136 });
137 }
138
139 std::unordered_map<std::string, Value> values_;
140};
141
153// LCOV_EXCL_START — GMock macro internals generate uninstrumented stubs
155public:
156 MOCK_METHOD(Status, Load, (), (override));
157 MOCK_METHOD(Status, Save, (), (const, override));
158 MOCK_METHOD(std::string_view, GetFilePath, (), (const, override));
159 MOCK_METHOD(Status, ValidateAll, (), (const, override));
160 MOCK_METHOD(std::string, GetDiffString, (), (const, override));
161};
162
168public:
174 [[nodiscard]] static auto CreateTempFilePath(std::string_view prefix = "test_config") -> std::string
175 {
176 static std::mt19937_64 generator { std::random_device {}() };
177 std::uniform_int_distribution<std::uint64_t> distribution;
178
179 const auto path = std::filesystem::temp_directory_path()
180 / (std::string(prefix) + "_" + std::to_string(distribution(generator)) + ".conf");
181 return path.string();
182 }
183
185 static void RemoveFile(const std::string& path)
186 {
187 std::error_code error_code;
188 std::filesystem::remove(path, error_code);
189 }
190};
191// LCOV_EXCL_STOP
192
193} // namespace cppfig::testing
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, ValidateAll,(),(const, override))
MOCK_METHOD(std::string_view, GetFilePath,(),(const, override))
Definition mock.h:18
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.