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
134 changes: 129 additions & 5 deletions Tests/Mockolate.Internal.Tests/Setup/IndexerSetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task ExecuteGetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
indexerSetup.OnGet.Do(() => { callCount++; });
IndexerGetterAccess<int> access = new(1);

string result = indexerSetup.DoGetResult(access, "foo");
indexerSetup.DoGetResult(access, "foo");

await That(callCount).IsEqualTo(1);
}
Expand Down Expand Up @@ -233,11 +233,24 @@ public async Task ExecuteGetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
indexerSetup.OnGet.Do(() => { callCount++; });
IndexerGetterAccess<int, int> access = new(1, 2);

string result = indexerSetup.DoGetResult(access, "foo");
indexerSetup.DoGetResult(access, "foo");

await That(callCount).IsEqualTo(1);
}

[Fact]
public async Task ExecuteSetterCallback_WhenAssignedValueDoesNotCastToTValue_ShouldNotExecute()
{
int callCount = 0;
MyIndexerSetup<int, int> indexerSetup = new();
indexerSetup.OnSet.Do(() => { callCount++; });
IndexerSetterAccess<int, int, string> access = new(1, 2, "bar");

indexerSetup.DoSetResult(access, 2L);

await That(callCount).IsEqualTo(0);
}

[Fact]
public async Task ExecuteSetterCallback_WhenGenericTypeDoesNotMatch_ShouldNotExecute()
{
Expand Down Expand Up @@ -316,6 +329,33 @@ public async Task GetResult_WithBaseValue_StoresComputedValueForLaterLookup()
await That(stored).IsEqualTo("base");
}

[Fact]
public async Task GetResult_WithFuncGenerator_AndInitialization_ShouldUseInitializationValue()
{
IndexerSetup<string, int, int> setup = new(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
(IParameterMatch<int>)It.IsAny<int>(),
(IParameterMatch<int>)It.IsAny<int>());
setup.InitializeWith((p1, p2) => $"{p1}-{p2}");
IndexerValueStorage<string> storage = new();
IndexerGetterAccess<int, int> access1 = new(7, 9)
{
Storage = storage,
};

string result = setup.GetResult<string>(access1, MockBehavior.Default, () => "fallback");

IndexerGetterAccess<int, int> access2 = new(7, 9)
{
Storage = storage,
};
bool found = access2.TryFindStoredValue(out string stored);

await That(result).IsEqualTo("7-9");
await That(found).IsTrue();
await That(stored).IsEqualTo("7-9");
}

private sealed class MyIndexerSetup<T1, T2>()
: IndexerSetup<string, T1, T2>(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
Expand Down Expand Up @@ -392,11 +432,24 @@ public async Task ExecuteGetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
indexerSetup.OnGet.Do(() => { callCount++; });
IndexerGetterAccess<int, int, int> access = new(1, 2, 3);

string result = indexerSetup.DoGetResult(access, "foo");
indexerSetup.DoGetResult(access, "foo");

await That(callCount).IsEqualTo(1);
}

[Fact]
public async Task ExecuteSetterCallback_WhenAssignedValueDoesNotCastToTValue_ShouldNotExecute()
{
int callCount = 0;
MyIndexerSetup<int, int, int> indexerSetup = new();
indexerSetup.OnSet.Do(() => { callCount++; });
IndexerSetterAccess<int, int, int, string> access = new(1, 2, 3, "bar");

indexerSetup.DoSetResult(access, 2L);

await That(callCount).IsEqualTo(0);
}

[Fact]
public async Task ExecuteSetterCallback_WhenGenericTypeDoesNotMatch_ShouldNotExecute()
{
Expand Down Expand Up @@ -478,6 +531,34 @@ public async Task GetResult_WithBaseValue_StoresComputedValueForLaterLookup()
await That(stored).IsEqualTo("base");
}

[Fact]
public async Task GetResult_WithFuncGenerator_AndInitialization_ShouldUseInitializationValue()
{
IndexerSetup<string, int, int, int> setup = new(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
(IParameterMatch<int>)It.IsAny<int>(),
(IParameterMatch<int>)It.IsAny<int>(),
(IParameterMatch<int>)It.IsAny<int>());
setup.InitializeWith((p1, p2, p3) => $"{p1}-{p2}-{p3}");
IndexerValueStorage<string> storage = new();
IndexerGetterAccess<int, int, int> access1 = new(7, 8, 9)
{
Storage = storage,
};

string result = setup.GetResult<string>(access1, MockBehavior.Default, () => "fallback");

IndexerGetterAccess<int, int, int> access2 = new(7, 8, 9)
{
Storage = storage,
};
bool found = access2.TryFindStoredValue(out string stored);

await That(result).IsEqualTo("7-8-9");
await That(found).IsTrue();
await That(stored).IsEqualTo("7-8-9");
}

private sealed class MyIndexerSetup<T1, T2, T3>()
: IndexerSetup<string, T1, T2, T3>(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
Expand Down Expand Up @@ -558,11 +639,25 @@ public async Task ExecuteGetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
IndexerGetterAccess<int, int, int, int> access =
new(1, 2, 3, 4);

string result = indexerSetup.DoGetResult(access, "foo");
indexerSetup.DoGetResult(access, "foo");

await That(callCount).IsEqualTo(1);
}

[Fact]
public async Task ExecuteSetterCallback_WhenAssignedValueDoesNotCastToTValue_ShouldNotExecute()
{
int callCount = 0;
MyIndexerSetup<int, int, int, int> indexerSetup = new();
indexerSetup.OnSet.Do(() => { callCount++; });
IndexerSetterAccess<int, int, int, int, string> access =
new(1, 2, 3, 4, "bar");

indexerSetup.DoSetResult(access, 2L);

await That(callCount).IsEqualTo(0);
}

[Fact]
public async Task ExecuteSetterCallback_WhenGenericTypeDoesNotMatch_ShouldNotExecute()
{
Expand Down Expand Up @@ -649,6 +744,35 @@ public async Task GetResult_WithBaseValue_StoresComputedValueForLaterLookup()
await That(stored).IsEqualTo("base");
}

[Fact]
public async Task GetResult_WithFuncGenerator_AndInitialization_ShouldUseInitializationValue()
{
IndexerSetup<string, int, int, int, int> setup = new(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
(IParameterMatch<int>)It.IsAny<int>(),
(IParameterMatch<int>)It.IsAny<int>(),
(IParameterMatch<int>)It.IsAny<int>(),
(IParameterMatch<int>)It.IsAny<int>());
setup.InitializeWith((p1, p2, p3, p4) => $"{p1}-{p2}-{p3}-{p4}");
IndexerValueStorage<string> storage = new();
IndexerGetterAccess<int, int, int, int> access1 = new(6, 7, 8, 9)
{
Storage = storage,
};

string result = setup.GetResult<string>(access1, MockBehavior.Default, () => "fallback");

IndexerGetterAccess<int, int, int, int> access2 = new(6, 7, 8, 9)
{
Storage = storage,
};
bool found = access2.TryFindStoredValue(out string stored);

await That(result).IsEqualTo("6-7-8-9");
await That(found).IsTrue();
await That(stored).IsEqualTo("6-7-8-9");
}

private sealed class MyIndexerSetup<T1, T2, T3, T4>()
: IndexerSetup<string, T1, T2, T3, T4>(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
Expand Down Expand Up @@ -731,7 +855,7 @@ public async Task ExecuteGetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
IndexerGetterAccess<int, int, int, int, int> access =
new(1, 2, 3, 4, 5);

string result = indexerSetup.DoGetResult(access, "foo");
indexerSetup.DoGetResult(access, "foo");

await That(callCount).IsEqualTo(1);
}
Expand Down
43 changes: 43 additions & 0 deletions Tests/Mockolate.Internal.Tests/Setup/PropertySetupTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Mockolate.Exceptions;
using Mockolate.Internal.Tests.TestHelpers;
using Mockolate.Setup;

Expand Down Expand Up @@ -31,6 +32,48 @@ public async Task DefaultInvokeGetter_WhenRequestedTypeDiffersFromBackingType_Sh
await That(value).IsEqualTo(99f);
}

[Fact]
public async Task DefaultInvokeSetter_WhenValueIsNullAndUnderlyingTypeIsNullable_ShouldStoreDefault()
{
PropertySetup.Default<int?> setup = new("p", 5);
IInteractivePropertySetup interactive = setup;

interactive.InvokeSetter<object?>(null, null, MockBehavior.Default);

int? value = interactive.InvokeGetter<int?>(null, MockBehavior.Default, () => 42);
await That(value).IsNull();
}

[Fact]
public async Task DefaultInvokeSetter_WhenValueIsNullButUnderlyingTypeIsNonNullable_ShouldThrow()
{
PropertySetup.Default<int> setup = new("p", 5);
IInteractivePropertySetup interactive = setup;

void Act()
{
interactive.InvokeSetter<object?>(null, null, MockBehavior.Default);
}

await That(Act).Throws<MockException>()
.WithMessage("*int*").AsWildcard();
}

[Fact]
public async Task DefaultInvokeSetter_WhenValueTypeMismatch_ShouldThrowWithFormattedMessage()
{
PropertySetup.Default<int> setup = new("p", 5);
IInteractivePropertySetup interactive = setup;

void Act()
{
interactive.InvokeSetter<string>(null, "string-value", MockBehavior.Default);
}

await That(Act).Throws<MockException>()
.WithMessage("*'int'*'string'*").AsWildcard();
}

[Fact]
public async Task UserInitializeWith_SecondCall_ShouldNotOverwriteValue()
{
Expand Down
Loading