mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-01-06 04:47:51 -05:00
- Added `MockLibraryBook` which contains factories for easily creating mock LibraryBooks and Books
- Added mock Configuration
- New `IPersistentDictionary` interface
- New `MockPersistentDictionary` class which uses a `JObject` as its data store
- Added `public static Configuration CreateMockInstance()`
- This method returns a mock Configuration instance **and also sets the `Configuration.Instance` property**
- Throws an exception if not in debug
- Updated all chardonnay controls to use the mocks in design mode. Previously I was using my actual database and settings file, but that approach is fragile and is unfriendly towards anyone else trying to work on it.
36 lines
1.4 KiB
C#
36 lines
1.4 KiB
C#
using FileManager;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
#nullable enable
|
|
namespace LibationFileManager;
|
|
|
|
internal class MockPersistentDictionary : IPersistentDictionary
|
|
{
|
|
private JObject JsonObject { get; } = new();
|
|
|
|
public bool Exists(string propertyName)
|
|
=> JsonObject.ContainsKey(propertyName);
|
|
public string? GetString(string propertyName, string? defaultValue = null)
|
|
=> JsonObject[propertyName]?.Value<string>() ?? defaultValue;
|
|
public T? GetNonString<T>(string propertyName, T? defaultValue = default)
|
|
=> GetObject(propertyName) is object obj ? IPersistentDictionary.UpCast<T>(obj) : defaultValue;
|
|
public object? GetObject(string propertyName)
|
|
=> JsonObject[propertyName]?.Value<object>();
|
|
public void SetString(string propertyName, string? newValue)
|
|
=> JsonObject[propertyName] = newValue;
|
|
public void SetNonString(string propertyName, object? newValue)
|
|
=> JsonObject[propertyName] = newValue is null ? null : JToken.FromObject(newValue);
|
|
public bool RemoveProperty(string propertyName)
|
|
=> JsonObject.Remove(propertyName);
|
|
public string? GetStringFromJsonPath(string jsonPath)
|
|
=> JsonObject.SelectToken(jsonPath)?.Value<string>();
|
|
public bool SetWithJsonPath(string jsonPath, string propertyName, string? newValue, bool suppressLogging = false)
|
|
{
|
|
if (JsonObject.SelectToken(jsonPath) is JToken token && token?[propertyName] is not null)
|
|
{
|
|
token[propertyName] = newValue;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
} |