Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 121 additions & 59 deletions Source/Mockolate.SourceGenerators/Sources/Sources.IndexerSetups.cs

Large diffs are not rendered by default.

447 changes: 249 additions & 198 deletions Source/Mockolate/Setup/IndexerSetup.cs

Large diffs are not rendered by default.

317 changes: 205 additions & 112 deletions Source/Mockolate/Setup/Interfaces.IndexerSetup.cs

Large diffs are not rendered by default.

70 changes: 47 additions & 23 deletions Source/Mockolate/Setup/Interfaces.PropertySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,64 +40,88 @@ public interface IInteractivePropertySetup : ISetup
}

/// <summary>
/// Interface for setting up a property with fluent syntax.
/// Interface for setting up a property getter with fluent syntax.
/// </summary>
public interface IPropertySetup<T>
public interface IPropertyGetterSetup<T>
{
/// <summary>
/// Flag indicating if the base class implementation should be called, and its return values used as default values.
/// </summary>
/// <remarks>
/// If not specified, use <see cref="MockBehavior.CallBaseClass" />.
/// </remarks>
IPropertySetup<T> CallingBaseClass(bool callBaseClass = true);

/// <summary>
/// Initializes the property with the given <paramref name="value" />.
/// </summary>
IPropertySetup<T> InitializeWith(T value);

/// <summary>
/// Registers a callback to be invoked whenever the property's getter is accessed.
/// </summary>
IPropertySetupCallbackBuilder<T> OnGet(Action callback);
IPropertySetupCallbackBuilder<T> Do(Action callback);

/// <summary>
/// Registers a callback to be invoked whenever the property's getter is accessed.
/// </summary>
/// <remarks>
/// The callback receives the value of the property as single parameter.
/// </remarks>
IPropertySetupCallbackBuilder<T> OnGet(Action<T> callback);
IPropertySetupCallbackBuilder<T> Do(Action<T> callback);

/// <summary>
/// Registers a callback to be invoked whenever the property's getter is accessed.
/// </summary>
/// <remarks>
/// The callback receives an incrementing access counter as first parameter and the value of the property as second parameter.
/// The callback receives an incrementing access counter as first parameter and the value of the property as second
/// parameter.
/// </remarks>
IPropertySetupCallbackBuilder<T> OnGet(Action<int, T> callback);
IPropertySetupCallbackBuilder<T> Do(Action<int, T> callback);
}

/// <summary>
/// Interface for setting up a property setter with fluent syntax.
/// </summary>
public interface IPropertySetterSetup<T>
{
/// <summary>
/// Registers a callback to be invoked whenever the property's value is set.
/// </summary>
IPropertySetupCallbackBuilder<T> OnSet(Action callback);
IPropertySetupCallbackBuilder<T> Do(Action callback);

/// <summary>
/// Registers a callback to be invoked whenever the property's value is set.
/// </summary>
/// <remarks>
/// The callback receives the value the property is set to as single parameter.
/// </remarks>
IPropertySetupCallbackBuilder<T> OnSet(Action<T> callback);
IPropertySetupCallbackBuilder<T> Do(Action<T> callback);

/// <summary>
/// Registers a callback to be invoked whenever the property's value is set.
/// </summary>
/// <remarks>
/// The callback receives an incrementing access counter as first parameter and the value the property is set to as second parameter.
/// The callback receives an incrementing access counter as first parameter and the value the property is set to as
/// second parameter.
/// </remarks>
IPropertySetupCallbackBuilder<T> Do(Action<int, T> callback);
}

/// <summary>
/// Interface for setting up a property with fluent syntax.
/// </summary>
public interface IPropertySetup<T>
{
/// <summary>
/// Sets up callbacks on the getter.
/// </summary>
IPropertyGetterSetup<T> OnGet { get; }

/// <summary>
/// Sets up callbacks on the setter.
/// </summary>
IPropertySetterSetup<T> OnSet { get; }

/// <summary>
/// Flag indicating if the base class implementation should be called, and its return values used as default values.
/// </summary>
/// <remarks>
/// If not specified, use <see cref="MockBehavior.CallBaseClass" />.
/// </remarks>
IPropertySetupCallbackBuilder<T> OnSet(Action<int, T> callback);
IPropertySetup<T> CallingBaseClass(bool callBaseClass = true);

/// <summary>
/// Initializes the property with the given <paramref name="value" />.
/// </summary>
IPropertySetup<T> InitializeWith(T value);

/// <summary>
/// Registers the <paramref name="returnValue" /> for this property.
Expand Down
37 changes: 23 additions & 14 deletions Source/Mockolate/Setup/PropertySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ protected override TResult InvokeGetter<TResult>(MockBehavior behavior, Func<TRe
/// <summary>
/// Sets up a property.
/// </summary>
public class PropertySetup<T>(string name)
: PropertySetup, IPropertySetupCallbackBuilder<T>, IPropertySetupReturnBuilder<T>
public class PropertySetup<T>(string name) : PropertySetup,
IPropertySetupCallbackBuilder<T>, IPropertySetupReturnBuilder<T>,
IPropertyGetterSetup<T>, IPropertySetterSetup<T>
{
private readonly List<Callback<Action<int, T>>> _getterCallbacks = [];
private readonly List<Callback<Func<int, T, T>>> _returnCallbacks = [];
Expand Down Expand Up @@ -326,53 +327,61 @@ public IPropertySetup<T> InitializeWith(T value)
return this;
}

/// <inheritdoc cref="IPropertySetup{T}.OnGet(Action)" />
public IPropertySetupCallbackBuilder<T> OnGet(Action callback)
/// <inheritdoc cref="IPropertySetup{T}.OnGet" />
public IPropertyGetterSetup<T> OnGet
=> this;

/// <inheritdoc cref="IPropertyGetterSetup{T}.Do(Action)" />
IPropertySetupCallbackBuilder<T> IPropertyGetterSetup<T>.Do(Action callback)
{
Callback<Action<int, T>> item = new((_, _) => callback());
_currentCallback = item;
_getterCallbacks.Add(item);
return this;
}

/// <inheritdoc cref="IPropertySetup{T}.OnGet(Action{T})" />
public IPropertySetupCallbackBuilder<T> OnGet(Action<T> callback)
/// <inheritdoc cref="IPropertyGetterSetup{T}.Do(Action{T})" />
IPropertySetupCallbackBuilder<T> IPropertyGetterSetup<T>.Do(Action<T> callback)
{
Callback<Action<int, T>> item = new((_, v) => callback(v));
_currentCallback = item;
_getterCallbacks.Add(item);
return this;
}

/// <inheritdoc cref="IPropertySetup{T}.OnGet(Action{int, T})" />
public IPropertySetupCallbackBuilder<T> OnGet(Action<int, T> callback)
/// <inheritdoc cref="IPropertyGetterSetup{T}.Do(Action{int, T})" />
IPropertySetupCallbackBuilder<T> IPropertyGetterSetup<T>.Do(Action<int, T> callback)
{
Callback<Action<int, T>> item = new(callback);
_currentCallback = item;
_getterCallbacks.Add(item);
return this;
}

/// <inheritdoc cref="IPropertySetup{T}.OnSet(Action)" />
public IPropertySetupCallbackBuilder<T> OnSet(Action callback)
/// <inheritdoc cref="IPropertySetup{T}.OnSet" />
public IPropertySetterSetup<T> OnSet
=> this;

/// <inheritdoc cref="IPropertySetterSetup{T}.Do(Action)" />
IPropertySetupCallbackBuilder<T> IPropertySetterSetup<T>.Do(Action callback)
{
Callback<Action<int, T>> item = new((_, _) => callback());
_currentCallback = item;
_setterCallbacks.Add(item);
return this;
}

/// <inheritdoc cref="IPropertySetup{T}.OnSet(Action{T})" />
public IPropertySetupCallbackBuilder<T> OnSet(Action<T> callback)
/// <inheritdoc cref="IPropertySetterSetup{T}.Do(Action{T})" />
IPropertySetupCallbackBuilder<T> IPropertySetterSetup<T>.Do(Action<T> callback)
{
Callback<Action<int, T>> item = new((_, newValue) => callback(newValue));
_currentCallback = item;
_setterCallbacks.Add(item);
return this;
}

/// <inheritdoc cref="IPropertySetup{T}.OnSet(Action{int, T})" />
public IPropertySetupCallbackBuilder<T> OnSet(Action<int, T> callback)
/// <inheritdoc cref="IPropertySetterSetup{T}.Do(Action{int, T})" />
IPropertySetupCallbackBuilder<T> IPropertySetterSetup<T>.Do(Action<int, T> callback)
{
Callback<Action<int, T>> item = new(callback);
_currentCallback = item;
Expand Down
Loading
Loading