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
2 changes: 1 addition & 1 deletion Docs/pages/04-create-mocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Both lists also apply to the nested namespaces, e.g. listing `Sdk` also covers `
- The property applies to the project whose `CreateMock` creates the mock: mocks created in a project without the property return no auto-mocks.
- `Directory.Build.props` is read before the Mockolate package sets the default included namespaces, so `$(MockolateAutoMockingIncludedNamespaces)` is still empty there: a list set there replaces the default, and an empty value there does not turn the default off. Set the list in the project file or in `Directory.Build.targets` instead.
- An interface that members return in several spellings, e.g. `IRepository<string?>` and `IRepository<string>`, gets one auto-mock whose `Mock` property accepts every spelling, unless the project creates a mock of one spelling with `CreateMock`.
- The generator skips interfaces whose mock would not compile cleanly, e.g. interfaces that are inaccessible, obsolete or experimental, nested in a generic type, have static abstract members or a member named `CreateMock`, or use pointers or unsupported ref structs. It also skips interfaces in the global namespace, `out` parameters, interfaces inside tuples and indexers that return a task. Such members still return `null`; set them up yourself or register a factory with `WithDefaultValueFor`.
- The generator skips interfaces whose mock would not compile cleanly, e.g. interfaces that are inaccessible, obsolete or experimental, nested in a generic type, have static abstract members or a member named `CreateMock`, or use pointers or unsupported ref structs. It also skips interfaces in the global namespace, `out` parameters, interfaces inside tuples and indexers that return a task. Such members still return `null`, or a task with a `null` result; set them up yourself or register a factory with `WithDefaultValueFor`.
- With the property, the generator discovers the auto-mocks again when you edit a file that declares one of the mocked or returned types, so with many mocked interfaces the IDE may take longer to update the generated code after such an edit. Edits in other files, e.g. in your tests, reuse the auto-mocks.

</details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1089,9 +1089,9 @@ private static void AppendIndexerSetup(StringBuilder sb, int numberOfParameters,
sb.Append("\t\t}").AppendLine();
sb.AppendLine();

// GetResult(behavior) - no-closure entry point used by the generated mock indexer body
sb.Append("\t\t/// <inheritdoc cref=\"global::Mockolate.Setup.IndexerSetup.GetResult{TResult}(global::Mockolate.Interactions.IndexerAccess, global::Mockolate.MockBehavior)\" />").AppendLine();
sb.Append("\t\tpublic override TResult GetResult<TResult>(global::Mockolate.Interactions.IndexerAccess access, global::Mockolate.MockBehavior behavior)").AppendLine();
// GetResult(Func<MockBehavior, TResult> defaultValueGenerator) - no-closure entry point used by the generated mock indexer body
sb.Append("\t\t/// <inheritdoc cref=\"global::Mockolate.Setup.IndexerSetup.GetResult{TResult}(global::Mockolate.Interactions.IndexerAccess, global::Mockolate.MockBehavior, global::System.Func{global::Mockolate.MockBehavior, TResult})\" />").AppendLine();
sb.Append("\t\tpublic override TResult GetResult<TResult>(global::Mockolate.Interactions.IndexerAccess access, global::Mockolate.MockBehavior behavior, global::System.Func<global::Mockolate.MockBehavior, TResult> defaultValueGenerator)").AppendLine();
sb.Append("\t\t{").AppendLine();
sb.Append("\t\t\tif (!TryExtractParameters(access");
for (int i = 1; i <= numberOfParameters; i++)
Expand All @@ -1101,7 +1101,7 @@ private static void AppendIndexerSetup(StringBuilder sb, int numberOfParameters,

sb.Append("))").AppendLine();
sb.Append("\t\t\t{").AppendLine();
sb.Append("\t\t\t\treturn behavior.DefaultValue.Generate(default(TResult)!);").AppendLine();
sb.Append("\t\t\t\treturn defaultValueGenerator(behavior);").AppendLine();
sb.Append("\t\t\t}").AppendLine();
sb.AppendLine();
sb.Append("\t\t\tTValue currentValue;").AppendLine();
Expand All @@ -1116,14 +1116,14 @@ private static void AppendIndexerSetup(StringBuilder sb, int numberOfParameters,
sb.Append("\t\t\telse").AppendLine();
sb.Append("\t\t\t{").AppendLine();
sb.Append(autoMocking
? "\t\t\t\tcurrentValue = TryCast(global::Mockolate.Internals.AutoMocks.Or(behavior, behavior.DefaultValue.Generate(default(TValue)!), access), out TValue casted, behavior) ? casted : default!;"
: "\t\t\t\tcurrentValue = TryCast(behavior.DefaultValue.Generate(default(TValue)!), out TValue casted, behavior) ? casted : default!;").AppendLine();
? "\t\t\t\tcurrentValue = TryCast(global::Mockolate.Internals.AutoMocks.Or(behavior, defaultValueGenerator(behavior), access), out TValue casted, behavior) ? casted : default!;"
: "\t\t\t\tcurrentValue = TryCast(defaultValueGenerator(behavior), out TValue casted, behavior) ? casted : default!;").AppendLine();
sb.Append("\t\t\t}").AppendLine();
sb.AppendLine();
sb.Append("\t\t\tcurrentValue = ExecuteGetterCallbacks(").Append(parameters).Append(", currentValue);").AppendLine();
sb.Append("\t\t\tcurrentValue = ExecuteReturnCallbacks(").Append(parameters).Append(", currentValue);").AppendLine();
sb.Append("\t\t\taccess.StoreValue(currentValue);").AppendLine();
sb.Append("\t\t\treturn TryCast(currentValue, out TResult result, behavior) ? result : behavior.DefaultValue.Generate(default(TResult)!);").AppendLine();
sb.Append("\t\t\treturn TryCast(currentValue, out TResult result, behavior) ? result : defaultValueGenerator(behavior);").AppendLine();
sb.Append("\t\t}").AppendLine();
sb.AppendLine();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1944,23 +1944,26 @@ private static void AppendMockSubject_ImplementClass_AddProperty(StringBuilder s

void AppendIndexerGetReturn(string indent, string accessVarName, string setupVarName)
{
string defaultValueGenerator = new StringBuilder("static b => ")
.AppendDefaultValueGeneratorFor(property.Type, "b.DefaultValue").ToString();
if (typedIndexerGet)
{
sb.Append(indent).Append("return ");
AppendTypedIndexerCall(sb, mockRegistry, "GetIndexer", setupVarName, property.Type,
property.IndexerParameters!.Value, null, signatureIndex);
property.IndexerParameters!.Value, defaultValueGenerator, signatureIndex);
sb.Append(';').AppendLine();
return;
}

sb.Append(indent).Append("return ").Append(setupVarName).Append(" is null").AppendLine();
sb.Append(indent).Append("\t? ").Append(mockRegistry).Append(".GetIndexerFallback<")
.AppendTypeOrWrapper(property.Type).Append(">(").Append(accessVarName).Append(", ")
.Append(signatureIndex).Append(")")
.Append(defaultValueGenerator).Append(", ").Append(signatureIndex).Append(")")
.AppendLine();
sb.Append(indent).Append("\t: ").Append(mockRegistry).Append(".ApplyIndexerSetup<")
.AppendTypeOrWrapper(property.Type).Append(">(").Append(accessVarName).Append(", ")
.Append(setupVarName).Append(", ").Append(signatureIndex).Append(");").AppendLine();
.Append(setupVarName).Append(", ").Append(defaultValueGenerator).Append(", ")
.Append(signatureIndex).Append(");").AppendLine();
}

void AppendIndexerGetWithBaseReturn(string indent, string accessVarName, string setupVarName,
Expand Down
13 changes: 0 additions & 13 deletions Source/Mockolate/IDefaultValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,3 @@ public interface IDefaultValueGenerator
/// </returns>
object? GenerateValue(Type type, params object?[] parameters);
}

internal static class DefaultValueGeneratorInternalExtensions
{
internal static T GenerateTyped<T>(this IDefaultValueGenerator generator)
{
if (generator.GenerateValue(typeof(T)) is T value)
{
return value;
}

return default!;
}
}
41 changes: 25 additions & 16 deletions Source/Mockolate/Internals/MockRegistry.Indexers.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading;
using Mockolate.Exceptions;
using Mockolate.Interactions;
Expand Down Expand Up @@ -175,21 +176,23 @@ private bool HasIndexerSetupsBeyond(int memberId)
/// <typeparam name="T1">The type of indexer parameter 1.</typeparam>
/// <param name="setup">The matching indexer setup, or <see langword="null" />.</param>
/// <param name="parameter1">The value of indexer parameter 1.</param>
/// <param name="defaultValueGenerator">Cached factory invoked with the active <see cref="MockBehavior" /> when a default value is needed.</param>
/// <param name="signatureIndex">Index into the mock's indexer-signature table (emitted by the source generator).</param>
/// <returns>The value of the indexer.</returns>
/// <exception cref="MockNotSetupException"><paramref name="setup" /> is <see langword="null" />, <see cref="MockBehavior.ThrowWhenNotSetup" /> is <see langword="true" /> and no value was stored.</exception>
public TValue GetIndexer<TValue, T1>(IndexerSetup<TValue, T1>? setup, T1 parameter1, int signatureIndex)
public TValue GetIndexer<TValue, T1>(IndexerSetup<TValue, T1>? setup, T1 parameter1,
Func<MockBehavior, TValue> defaultValueGenerator, int signatureIndex)
{
if (setup is not null && setup.GetType() != typeof(IndexerSetup<TValue, T1>))
{
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1>(parameter1), setup, signatureIndex);
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1>(parameter1), setup, defaultValueGenerator, signatureIndex);
}

IndexerValueStorage<TValue> leaf = Setup.Indexers.GetOrCreateStorage<TValue>(signatureIndex)
.GetOrAddChild(parameter1);
if (setup is not null)
{
return setup.GetResult(leaf, parameter1, Behavior);
return setup.GetResult(leaf, parameter1, Behavior, defaultValueGenerator);
}

if (leaf.HasValue)
Expand All @@ -203,7 +206,7 @@ public TValue GetIndexer<TValue, T1>(IndexerSetup<TValue, T1>? setup, T1 paramet
$"{new IndexerGetterAccess<T1>(parameter1)} was accessed without prior setup.");
}

TValue value = AutoMocks.Or(Behavior, Behavior.DefaultValue.GenerateTyped<TValue>(), leaf);
TValue value = AutoMocks.Or(Behavior, defaultValueGenerator(Behavior), leaf);
leaf.Store(value);
return value;
}
Expand Down Expand Up @@ -254,21 +257,23 @@ public TValue GetIndexer<TValue, T1>(IndexerSetup<TValue, T1>? setup, T1 paramet
/// <param name="setup">The matching indexer setup, or <see langword="null" />.</param>
/// <param name="parameter1">The value of indexer parameter 1.</param>
/// <param name="parameter2">The value of indexer parameter 2.</param>
/// <param name="defaultValueGenerator">Cached factory invoked with the active <see cref="MockBehavior" /> when a default value is needed.</param>
/// <param name="signatureIndex">Index into the mock's indexer-signature table (emitted by the source generator).</param>
/// <returns>The value of the indexer.</returns>
/// <exception cref="MockNotSetupException"><paramref name="setup" /> is <see langword="null" />, <see cref="MockBehavior.ThrowWhenNotSetup" /> is <see langword="true" /> and no value was stored.</exception>
public TValue GetIndexer<TValue, T1, T2>(IndexerSetup<TValue, T1, T2>? setup, T1 parameter1, T2 parameter2, int signatureIndex)
public TValue GetIndexer<TValue, T1, T2>(IndexerSetup<TValue, T1, T2>? setup, T1 parameter1, T2 parameter2,
Func<MockBehavior, TValue> defaultValueGenerator, int signatureIndex)
{
if (setup is not null && setup.GetType() != typeof(IndexerSetup<TValue, T1, T2>))
{
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1, T2>(parameter1, parameter2), setup, signatureIndex);
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1, T2>(parameter1, parameter2), setup, defaultValueGenerator, signatureIndex);
}

IndexerValueStorage<TValue> leaf = Setup.Indexers.GetOrCreateStorage<TValue>(signatureIndex)
.GetOrAddChild(parameter1).GetOrAddChild(parameter2);
if (setup is not null)
{
return setup.GetResult(leaf, parameter1, parameter2, Behavior);
return setup.GetResult(leaf, parameter1, parameter2, Behavior, defaultValueGenerator);
}

if (leaf.HasValue)
Expand All @@ -282,7 +287,7 @@ public TValue GetIndexer<TValue, T1, T2>(IndexerSetup<TValue, T1, T2>? setup, T1
$"{new IndexerGetterAccess<T1, T2>(parameter1, parameter2)} was accessed without prior setup.");
}

TValue value = AutoMocks.Or(Behavior, Behavior.DefaultValue.GenerateTyped<TValue>(), leaf);
TValue value = AutoMocks.Or(Behavior, defaultValueGenerator(Behavior), leaf);
leaf.Store(value);
return value;
}
Expand Down Expand Up @@ -337,21 +342,23 @@ public TValue GetIndexer<TValue, T1, T2>(IndexerSetup<TValue, T1, T2>? setup, T1
/// <param name="parameter1">The value of indexer parameter 1.</param>
/// <param name="parameter2">The value of indexer parameter 2.</param>
/// <param name="parameter3">The value of indexer parameter 3.</param>
/// <param name="defaultValueGenerator">Cached factory invoked with the active <see cref="MockBehavior" /> when a default value is needed.</param>
/// <param name="signatureIndex">Index into the mock's indexer-signature table (emitted by the source generator).</param>
/// <returns>The value of the indexer.</returns>
/// <exception cref="MockNotSetupException"><paramref name="setup" /> is <see langword="null" />, <see cref="MockBehavior.ThrowWhenNotSetup" /> is <see langword="true" /> and no value was stored.</exception>
public TValue GetIndexer<TValue, T1, T2, T3>(IndexerSetup<TValue, T1, T2, T3>? setup, T1 parameter1, T2 parameter2, T3 parameter3, int signatureIndex)
public TValue GetIndexer<TValue, T1, T2, T3>(IndexerSetup<TValue, T1, T2, T3>? setup, T1 parameter1, T2 parameter2, T3 parameter3,
Func<MockBehavior, TValue> defaultValueGenerator, int signatureIndex)
{
if (setup is not null && setup.GetType() != typeof(IndexerSetup<TValue, T1, T2, T3>))
{
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1, T2, T3>(parameter1, parameter2, parameter3), setup, signatureIndex);
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1, T2, T3>(parameter1, parameter2, parameter3), setup, defaultValueGenerator, signatureIndex);
}

IndexerValueStorage<TValue> leaf = Setup.Indexers.GetOrCreateStorage<TValue>(signatureIndex)
.GetOrAddChild(parameter1).GetOrAddChild(parameter2).GetOrAddChild(parameter3);
if (setup is not null)
{
return setup.GetResult(leaf, parameter1, parameter2, parameter3, Behavior);
return setup.GetResult(leaf, parameter1, parameter2, parameter3, Behavior, defaultValueGenerator);
}

if (leaf.HasValue)
Expand All @@ -365,7 +372,7 @@ public TValue GetIndexer<TValue, T1, T2, T3>(IndexerSetup<TValue, T1, T2, T3>? s
$"{new IndexerGetterAccess<T1, T2, T3>(parameter1, parameter2, parameter3)} was accessed without prior setup.");
}

TValue value = AutoMocks.Or(Behavior, Behavior.DefaultValue.GenerateTyped<TValue>(), leaf);
TValue value = AutoMocks.Or(Behavior, defaultValueGenerator(Behavior), leaf);
leaf.Store(value);
return value;
}
Expand Down Expand Up @@ -424,21 +431,23 @@ public TValue GetIndexer<TValue, T1, T2, T3>(IndexerSetup<TValue, T1, T2, T3>? s
/// <param name="parameter2">The value of indexer parameter 2.</param>
/// <param name="parameter3">The value of indexer parameter 3.</param>
/// <param name="parameter4">The value of indexer parameter 4.</param>
/// <param name="defaultValueGenerator">Cached factory invoked with the active <see cref="MockBehavior" /> when a default value is needed.</param>
/// <param name="signatureIndex">Index into the mock's indexer-signature table (emitted by the source generator).</param>
/// <returns>The value of the indexer.</returns>
/// <exception cref="MockNotSetupException"><paramref name="setup" /> is <see langword="null" />, <see cref="MockBehavior.ThrowWhenNotSetup" /> is <see langword="true" /> and no value was stored.</exception>
public TValue GetIndexer<TValue, T1, T2, T3, T4>(IndexerSetup<TValue, T1, T2, T3, T4>? setup, T1 parameter1, T2 parameter2, T3 parameter3, T4 parameter4, int signatureIndex)
public TValue GetIndexer<TValue, T1, T2, T3, T4>(IndexerSetup<TValue, T1, T2, T3, T4>? setup, T1 parameter1, T2 parameter2, T3 parameter3, T4 parameter4,
Func<MockBehavior, TValue> defaultValueGenerator, int signatureIndex)
{
if (setup is not null && setup.GetType() != typeof(IndexerSetup<TValue, T1, T2, T3, T4>))
{
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1, T2, T3, T4>(parameter1, parameter2, parameter3, parameter4), setup, signatureIndex);
return ApplyIndexerSetup<TValue>(new IndexerGetterAccess<T1, T2, T3, T4>(parameter1, parameter2, parameter3, parameter4), setup, defaultValueGenerator, signatureIndex);
}

IndexerValueStorage<TValue> leaf = Setup.Indexers.GetOrCreateStorage<TValue>(signatureIndex)
.GetOrAddChild(parameter1).GetOrAddChild(parameter2).GetOrAddChild(parameter3).GetOrAddChild(parameter4);
if (setup is not null)
{
return setup.GetResult(leaf, parameter1, parameter2, parameter3, parameter4, Behavior);
return setup.GetResult(leaf, parameter1, parameter2, parameter3, parameter4, Behavior, defaultValueGenerator);
}

if (leaf.HasValue)
Expand All @@ -452,7 +461,7 @@ public TValue GetIndexer<TValue, T1, T2, T3, T4>(IndexerSetup<TValue, T1, T2, T3
$"{new IndexerGetterAccess<T1, T2, T3, T4>(parameter1, parameter2, parameter3, parameter4)} was accessed without prior setup.");
}

TValue value = AutoMocks.Or(Behavior, Behavior.DefaultValue.GenerateTyped<TValue>(), leaf);
TValue value = AutoMocks.Or(Behavior, defaultValueGenerator(Behavior), leaf);
leaf.Store(value);
return value;
}
Expand Down
Loading
Loading