Skip to content

Commit

Permalink
Fix parameter bug when same type in list more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
cwinland committed May 30, 2024
1 parent 8c5792a commit 6ccabe0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
36 changes: 31 additions & 5 deletions FastMoq.Core/Mocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,14 +786,40 @@ public static List<T> GetList<T>(int count, Func<T>? func) =>
GetMock<TMock>().Protected()
?.Setup<Task<TReturn>>(methodOrPropertyName, args ?? Array.Empty<object>());

/// <summary>

Check warning on line 789 in FastMoq.Core/Mocker.cs

View workflow job for this annotation

GitHub Actions / build, pack & publish

XML comment is not placed on a valid language element

Check warning on line 789 in FastMoq.Core/Mocker.cs

View workflow job for this annotation

GitHub Actions / build, pack & publish

XML comment is not placed on a valid language element

Check warning on line 789 in FastMoq.Core/Mocker.cs

View workflow job for this annotation

GitHub Actions / build, pack & publish

XML comment is not placed on a valid language element
/// Gets the method argument data.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="data">The data.</param>
/// <returns>System.Nullable&lt;System.Object&gt;[].</returns>
///// <exception cref="System.ArgumentNullException">method</exception>
//public object?[] GetMethodArgData(MethodInfo method, Dictionary<Type, object?>? data = null)
//{
// if (method == null)
// {
// throw new ArgumentNullException(nameof(method));
// }

// var args = new List<object?>();

// method.GetParameters().ToList().ForEach(p =>
// args.Add(data?.Any(x => x.Key == p.ParameterType) ?? false
// ? data.First(x => x.Key == p.ParameterType).Value
// : GetParameter(p.ParameterType)
// )
// );

// return args.ToArray();
//}

/// <summary>
/// Gets the method argument data.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="data">The data.</param>
/// <returns>System.Nullable&lt;System.Object&gt;[].</returns>
/// <exception cref="System.ArgumentNullException">method</exception>
public object?[] GetMethodArgData(MethodInfo method, Dictionary<Type, object?>? data = null)
public object?[] GetMethodArgData(MethodInfo method, List<KeyValuePair<Type, object?>>? data = null)
{
if (method == null)
{
Expand Down Expand Up @@ -1282,7 +1308,7 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no
{
ArgumentNullException.ThrowIfNull(method);

var parameters = GetMethodArgData(method.GetMethodInfo(), CreateParamTypeDictionary(method.GetMethodInfo(), args));
var parameters = GetMethodArgData(method.GetMethodInfo(), CreateArgPairList(method.GetMethodInfo(), args));
try
{
return (T?) method.DynamicInvoke(parameters);
Expand Down Expand Up @@ -1316,10 +1342,10 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no
/// <exception cref="System.ArgumentNullException" />
public void CallMethod(Delegate method, params object?[]? args) => CallMethod<object>(method, args);

internal Dictionary<Type, object?> CreateParamTypeDictionary(MethodBase info, params object?[]? args)
internal List<KeyValuePair<Type, object?>> CreateArgPairList(MethodBase info, params object?[]? args)
{
var paramList = info?.GetParameters().ToList() ?? new();
var newArgs = new Dictionary<Type, object?>();
var newArgs = new List<KeyValuePair<Type, object?>>();
args ??= [];

for (var i = 0; i < paramList.Count; i++)
Expand All @@ -1333,7 +1359,7 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no
_ => GetParameter(p.ParameterType),
};

newArgs.Add(p.ParameterType, val);
newArgs.Add(new (p.ParameterType, val));
}

return newArgs;
Expand Down
19 changes: 18 additions & 1 deletion FastMoq.Tests/MocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using FastMoq.Models;
using FastMoq.Tests.TestBase;
using FastMoq.Tests.TestClasses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -10,6 +13,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Threading;

Expand Down Expand Up @@ -1016,13 +1020,26 @@ public void CallMethod_WithNoParamsReturnInt()
result.Should().Be(0);
}


[Fact]
public void CallMethod_WithException()
{
Assert.Throws<ArgumentNullException>(() => Mocks.CallMethod<object?[]>(CallTestMethod, 4, null));
}

private static void LogException(Exception ex, ILogger log, string customMessage = "", [CallerMemberName] string caller = "")
{
log.LogError("[{caller}] - {customMessage}{errorMessage}", caller, $"{customMessage} ", ex.Message);
}

[Fact]
public void TestLogException_CallsLogError()
{
var ex = new Exception();

Mocks.AddType<ILogger, Logger<NullLogger>>();
Mocks.CallMethod<object>(LogException, ex);
}

private void CheckBestConstructor(object data, bool expected, bool nonPublic)
{
var constructor = Mocks.FindConstructor(true, typeof(TestClassNormal), nonPublic);
Expand Down

0 comments on commit 6ccabe0

Please sign in to comment.