mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-01-10 06:49:01 -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.
41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
#nullable enable
|
|
namespace FileManager;
|
|
|
|
public interface IPersistentDictionary
|
|
{
|
|
bool Exists(string propertyName);
|
|
string? GetString(string propertyName, string? defaultValue = null);
|
|
T? GetNonString<T>(string propertyName, T? defaultValue = default);
|
|
object? GetObject(string propertyName);
|
|
void SetString(string propertyName, string? newValue);
|
|
void SetNonString(string propertyName, object? newValue);
|
|
bool RemoveProperty(string propertyName);
|
|
bool SetWithJsonPath(string jsonPath, string propertyName, string? newValue, bool suppressLogging = false);
|
|
string? GetStringFromJsonPath(string jsonPath);
|
|
|
|
string? GetStringFromJsonPath(string jsonPath, string propertyName)
|
|
=> GetStringFromJsonPath($"{jsonPath}.{propertyName}");
|
|
|
|
static T? UpCast<T>(object obj)
|
|
{
|
|
if (obj.GetType().IsAssignableTo(typeof(T))) return (T)obj;
|
|
if (obj is JObject jObject) return jObject.ToObject<T>();
|
|
if (obj is JValue jValue)
|
|
{
|
|
if (typeof(T).IsAssignableTo(typeof(Enum)))
|
|
{
|
|
return
|
|
Enum.TryParse(typeof(T), jValue.Value<string>(), out var enumVal)
|
|
? (T)enumVal
|
|
: Enum.GetValues(typeof(T)).Cast<T>().First();
|
|
}
|
|
return jValue.Value<T>();
|
|
}
|
|
throw new InvalidCastException($"{obj.GetType()} is not convertible to {typeof(T)}");
|
|
}
|
|
}
|