Skip to content

Commit

Permalink
Suggested change to clean up CA warnings (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo committed Jun 25, 2020
1 parent 5f543f8 commit ccc60eb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Moq.AutoMock/AutoMocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public object CreateInstance(Type type, bool enablePrivate)
/// <typeparam name="TService">The type that the instance will be registered as</typeparam>
/// <param name="service"></param>
public void Use<TService>([DisallowNull] TService service)
=> Use(typeof(TService), service);
=> Use(typeof(TService), service ?? throw new ArgumentNullException(nameof(service)));

/// <summary>
/// Adds an instance to the container.
Expand Down
21 changes: 21 additions & 0 deletions Moq.AutoMock/Resolvers/Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Moq.AutoMock.Resolvers
{
#if NET45
//Array.Empty does not exist in net45
//This duplicates what was added to .NET Core
internal static class Array
{
public static T[] Empty<T>()
{
return EmptyArray<T>.Value;
}

private static class EmptyArray<T>
{
#pragma warning disable CA1825 // this is the implementation of Array.Empty<T>()
internal static readonly T[] Value = new T[0];
#pragma warning restore CA1825
}
}
#endif
}
23 changes: 9 additions & 14 deletions Moq.AutoMock/Resolvers/MockResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,24 @@ public void Resolve(MockResolutionContext context)

var mockType = typeof(Mock<>).MakeGenericType(context.RequestType);

bool mayHaveDependencies = context.RequestType.IsClass
bool mayHaveDependencies = context.RequestType.IsClass
&& !typeof(Delegate).IsAssignableFrom(context.RequestType);

object?[] constructorArgs;

//Invoke the Mock<T>(MockBehavior, params object[]) constructor
object?[] constructorArgs = new object?[2];
constructorArgs[0] = _mockBehavior;

if (mayHaveDependencies)
{
constructorArgs = context.AutoMocker.CreateArguments(context.RequestType);
constructorArgs[1] = context.AutoMocker.CreateArguments(context.RequestType);
}
else
{
#pragma warning disable CA1825
// Compiler complains about empty array literal, but I can't find an alternative that will compile.
constructorArgs = new object[0];
#pragma warning restore CA1825
constructorArgs[1] = Array.Empty<object>();
}

// Create single params argument to appease the compiler. If we don't do this, the
// compiler will try to pass an object[] to the constructor, which probably won't work.
var args = new object[constructorArgs.Length + 1];
args[0] = _mockBehavior;
constructorArgs.CopyTo(args, 1);

if (Activator.CreateInstance(mockType, args) is Mock mock)
if (Activator.CreateInstance(mockType, constructorArgs) is Mock mock)
{
mock.DefaultValue = _defaultValue;
mock.CallBase = _callBase;
Expand Down

0 comments on commit ccc60eb

Please sign in to comment.