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
8 changes: 1 addition & 7 deletions Source/Mockolate/Setup/PropertySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,20 @@ protected override TResult InvokeGetter<TResult>(MockBehavior behavior, Func<TRe
}
}

bool foundCallback = false;
foreach (Callback<Func<int, T, T>> _ in _returnCallbacks)
{
Callback<Func<int, T, T>> returnCallback =
_returnCallbacks[_currentReturnCallbackIndex % _returnCallbacks.Count];
if (returnCallback.Invoke(ref _currentReturnCallbackIndex, (invocationCount, @delegate)
=> @delegate(invocationCount, _value), out T? newValue))
{
foundCallback = true;
_value = newValue;
_isInitialized = true;
break;
}
}

if (!foundCallback && _returnCallbacks.Count > 0 && !_isInitialized)
if (!_isInitialized)
{
_value = defaultValueGenerator() is T value ? value : default!;
_isInitialized = true;
Expand Down Expand Up @@ -262,10 +260,6 @@ protected override void InitializeValue(object? value)
{
_value = typedValue;
}
else
{
_value = default!;
}
}

/// <inheritdoc cref="PropertySetup.GetSkipBaseClass()" />
Expand Down
36 changes: 30 additions & 6 deletions Tests/Mockolate.Tests/MockMethods/SetupMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ public async Task HasReturnCalls_ShouldDefaultToFalse()
await That(result).IsFalse();
}

[Fact]
public async Task MethodSetupResult_TriggerCallbacks_Null_ShouldTriggerCallbacksWithNullArray()
{
IInteractiveMethodSetup methodSetup = Mock.Create<IInteractiveMethodSetup>();
MethodSetupResult sut = new(methodSetup, MockBehavior.Default);

sut.TriggerCallbacks(null);

await That(methodSetup.VerifyMock.Invoked.TriggerCallbacks(
// ReSharper disable once MergeIntoPattern
It.Satisfies<object?[]>(arr => arr.Length == 1 && arr[0] is null))).Once();
}

[Fact]
public async Task MultipleOnlyOnceCallbacks_ShouldExecuteInOrder()
{
Expand Down Expand Up @@ -539,14 +552,25 @@ public async Task ToString_ShouldWork()
}

[Fact]
public async Task TriggerCallbacks_ArrayLengthDoesNotMatch_ShouldNotThrow()
public async Task TriggerCallbacks_WhenParameterCountDoesNotMatch_ShouldNotInvokeParameterCallbacks()
{
void Act()
{
MyMethodSetup.DoTriggerCallbacks([null, null,], [null,]);
}
IParameter<int> parameter = It.IsAny<int>().Monitor(out IParameterMonitor<int> monitor);
MyMethodSetup.DoTriggerCallbacks([
new NamedParameter("foo", (IParameter)parameter),
], [4, 5,]);

await That(monitor.Values).IsEmpty();
}

[Fact]
public async Task TriggerCallbacks_WhenParameterCountMatches_ShouldInvokeParameterCallbacks()
{
IParameter<int> parameter = It.IsAny<int>().Monitor(out IParameterMonitor<int> monitor);
MyMethodSetup.DoTriggerCallbacks([
new NamedParameter("foo", (IParameter)parameter),
], [4,]);

await That(Act).DoesNotThrow();
await That(monitor.Values).IsEqualTo([4,]);
}

[Fact]
Expand Down
Loading