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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using Mockolate.Interactions;
using Mockolate.Internal.Tests.TestHelpers;
using Mockolate.Setup;
Expand All @@ -6,6 +7,13 @@ namespace Mockolate.Internal.Tests.Registry;

public sealed class MockRegistrySetupSnapshotTests
{
private static PropertySetup?[]? GetPropertySnapshotTable(MockRegistry registry)
{
FieldInfo field = typeof(MockRegistry).GetField(
"_propertySetupsByMemberId", BindingFlags.Instance | BindingFlags.NonPublic)!;
return (PropertySetup?[]?)field.GetValue(registry);
}

public sealed class MethodTests
{
[Fact]
Expand Down Expand Up @@ -177,6 +185,56 @@ public async Task PublishPropertyToMemberIdBucket_WithUserThenDefaultSetup_Retai
await That(observed).IsEqualTo(99);
}

[Fact]
public async Task SetupProperty_DefaultDoesNotOverwriteUserSetupAtSameMemberIdInSnapshot()
{
MockRegistry registry = new(MockBehavior.Default, new FastMockInteractions(0));
PropertySetup<int> userSetup = new(registry, "Pg");
userSetup.InitializeWith(7);

registry.SetupProperty(1, userSetup);
registry.SetupProperty(1, new PropertySetup.Default<int>("Pg", 0));

PropertySetup?[]? table = GetPropertySnapshotTable(registry);
await That(table).IsNotNull();
await That(table![1]).IsSameAs(userSetup);
}

[Fact]
public async Task SetupProperty_UserSetupDoesOverwriteDefaultAtSameMemberIdInSnapshot()
{
MockRegistry registry = new(MockBehavior.Default, new FastMockInteractions(0));
PropertySetup<int> userSetup = new(registry, "Pg");
userSetup.InitializeWith(7);
PropertySetup.Default<int> defaultSetup = new("Pg", 0);

registry.SetupProperty(1, defaultSetup);
registry.SetupProperty(1, userSetup);

PropertySetup?[]? table = GetPropertySnapshotTable(registry);
await That(table).IsNotNull();
await That(table![1]).IsSameAs(userSetup);
}

[Fact]
public async Task SetupProperty_WithGrowingMemberIds_PreservesEarlierEntriesViaArrayCopy()
{
MockRegistry registry = new(MockBehavior.Default, new FastMockInteractions(0));
PropertySetup<int> setupA = new(registry, "Pa");
setupA.InitializeWith(1);
PropertySetup<int> setupB = new(registry, "Pb");
setupB.InitializeWith(2);

registry.SetupProperty(0, setupA);
registry.SetupProperty(5, setupB);

PropertySetup?[]? table = GetPropertySnapshotTable(registry);
await That(table).IsNotNull();
await That(table!.Length).IsGreaterThanOrEqualTo(6);
await That(table[0]).IsSameAs(setupA);
await That(table[5]).IsSameAs(setupB);
}

[Fact]
public async Task SetupProperty_WithIncreasingMemberIds_GrowsTableLazily()
{
Expand All @@ -197,6 +255,21 @@ public async Task SetupProperty_WithIncreasingMemberIds_GrowsTableLazily()
await That(registry.GetPropertyFast(3, "P3", _ => -1)).IsEqualTo(30);
}

[Fact]
public async Task SetupProperty_WithMemberId_PopulatesSnapshotSlot()
{
MockRegistry registry = new(MockBehavior.Default, new FastMockInteractions(0));
PropertySetup<int> setup = new(registry, "P3");
setup.InitializeWith(33);

registry.SetupProperty(3, setup);

PropertySetup?[]? table = GetPropertySnapshotTable(registry);
await That(table).IsNotNull();
await That(table!.Length).IsGreaterThanOrEqualTo(4);
await That(table[3]).IsSameAs(setup);
}

[Fact]
public async Task SetupProperty_WithMemberId_PublishesToSnapshot()
{
Expand All @@ -223,6 +296,20 @@ public async Task SetupProperty_WithMemberId_PublishesUserSetupToSnapshotTable()
await That(snapshot).IsSameAs(setup);
}

[Fact]
public async Task SetupProperty_WithMemberIdAndDefaultScenario_PopulatesSnapshotSlot()
{
MockRegistry registry = new(MockBehavior.Default, new FastMockInteractions(0));
PropertySetup<int> setup = new(registry, "P3");
setup.InitializeWith(33);

registry.SetupProperty(3, "", setup);

PropertySetup?[]? table = GetPropertySnapshotTable(registry);
await That(table).IsNotNull();
await That(table![3]).IsSameAs(setup);
}

[Fact]
public async Task SetupProperty_WithMemberIdAndDefaultScenario_PublishesToSnapshot()
{
Expand Down Expand Up @@ -262,6 +349,19 @@ public async Task SetupProperty_WithMemberIdAndNamedScenario_DoesNotPublishToSna
await That(observed).IsEqualTo(-1);
}

[Fact]
public async Task SetupProperty_WithMemberIdAndNamedScenario_LeavesSnapshotUnpublished()
{
MockRegistry registry = new(MockBehavior.Default, new FastMockInteractions(0));
PropertySetup<int> setup = new(registry, "P3");
setup.InitializeWith(33);

registry.SetupProperty(3, "scope", setup);

PropertySetup?[]? table = GetPropertySnapshotTable(registry);
await That(table).IsNull();
}

[Fact]
public async Task SetupProperty_WithMemberIdAndNamedScenario_PreservesScenarioBucket()
{
Expand Down
71 changes: 71 additions & 0 deletions Tests/Mockolate.Internal.Tests/Setup/IndexerSetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,32 @@ public async Task GetResult_WithDefaultValueGenerator_StoresComputedValueForLate
await That(stored).IsEqualTo("generated");
}

[Fact]
public async Task MatchesAccess_WithUnknownAccessType_ShouldReturnFalse()
{
IndexerSetup<int, string> setup = new(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
(IParameterMatch<string>)It.IsAny<string>());
FakeIndexerAccess access = new();

bool matches = ((IInteractiveIndexerSetup)setup).Matches(access);

await That(matches).IsFalse();
}

[Fact]
public async Task SetResult_WhenValueDoesNotCastToTValue_ShouldNotInvokeSetterCallbacks()
{
MyIndexerSetup<int> setup = new();
int callCount = 0;
setup.OnSet.Do(() => callCount++);
IndexerSetterAccess<int, string> access = new(1, "stored");

setup.DoSetResult(access, 2L);

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

[Fact]
public async Task TryCast_WhenValueIsNotOfTargetTypeAndNotNull_ShouldReturnFalse()
{
Expand Down Expand Up @@ -356,6 +382,20 @@ public async Task GetResult_WithFuncGenerator_AndInitialization_ShouldUseInitial
await That(stored).IsEqualTo("7-9");
}

[Fact]
public async Task MatchesAccess_WithUnknownAccessType_ShouldReturnFalse()
{
IndexerSetup<string, int, int> setup = new(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
(IParameterMatch<int>)It.IsAny<int>(),
(IParameterMatch<int>)It.IsAny<int>());
FakeIndexerAccess access = new();

bool matches = ((IInteractiveIndexerSetup)setup).Matches(access);

await That(matches).IsFalse();
}

private sealed class MyIndexerSetup<T1, T2>()
: IndexerSetup<string, T1, T2>(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
Expand Down Expand Up @@ -559,6 +599,21 @@ public async Task GetResult_WithFuncGenerator_AndInitialization_ShouldUseInitial
await That(stored).IsEqualTo("7-8-9");
}

[Fact]
public async Task MatchesAccess_WithUnknownAccessType_ShouldReturnFalse()
{
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>());
FakeIndexerAccess access = new();

bool matches = ((IInteractiveIndexerSetup)setup).Matches(access);

await That(matches).IsFalse();
}

private sealed class MyIndexerSetup<T1, T2, T3>()
: IndexerSetup<string, T1, T2, T3>(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
Expand Down Expand Up @@ -773,6 +828,22 @@ public async Task GetResult_WithFuncGenerator_AndInitialization_ShouldUseInitial
await That(stored).IsEqualTo("6-7-8-9");
}

[Fact]
public async Task MatchesAccess_WithUnknownAccessType_ShouldReturnFalse()
{
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>());
FakeIndexerAccess access = new();

bool matches = ((IInteractiveIndexerSetup)setup).Matches(access);

await That(matches).IsFalse();
}

private sealed class MyIndexerSetup<T1, T2, T3, T4>()
: IndexerSetup<string, T1, T2, T3, T4>(
new MockRegistry(MockBehavior.Default, new FastMockInteractions(0)),
Expand Down
Loading
Loading