Skip to content

Commit

Permalink
Merge pull request #177 from hasaki/add-generic-type-to-exception
Browse files Browse the repository at this point in the history
Add generic parameter type informat to exception text.
  • Loading branch information
kzu committed Jun 8, 2015
2 parents 8bc9b11 + 26bbd82 commit 2802f12
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Source/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ public static string Format(this ICallContext invocation)
return invocation.Method.DeclaringType.Name + "." +
invocation.Method.Name.Substring(4) + " = " + GetValue(invocation.Arguments.First());
}

var genericParameters = invocation.Method.IsGenericMethod
? "<" + string.Join(", ", invocation.Method.GetGenericArguments().Select(t => t.Name)) + ">"
: "";

return invocation.Method.DeclaringType.Name + "." + invocation.Method.Name + "(" +
return invocation.Method.DeclaringType.Name + "." + invocation.Method.Name + genericParameters + "(" +
string.Join(", ", invocation.Arguments.Select(a => GetValue(a)).ToArray()) + ")";
}

Expand Down
59 changes: 59 additions & 0 deletions UnitTests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Moq;
using Moq.Properties;
using Moq.Protected;
Expand Down Expand Up @@ -175,6 +176,64 @@ public void DoTest()
#endif
#endregion

#region #176

public class Issue176
{
public interface ISomeInterface
{
TResult DoSomething<TResult>(int anInt);
int DoSomethingElse(int anInt);
}

[Fact]
public void when_a_mock_doesnt_match_generic_parameters_exception_indicates_generic_parameters()
{
var mock = new Mock<ISomeInterface>(MockBehavior.Strict);
mock.Setup(m => m.DoSomething<int>(0)).Returns(1);

try
{
mock.Object.DoSomething<string>(0);
}
catch (MockException exception)
{
var genericTypesRE = new Regex(@"\<.*?\>");
var match = genericTypesRE.Match(exception.Message);

Assert.True(match.Success);
Assert.Equal("<string>", match.Captures[0].Value, StringComparer.OrdinalIgnoreCase);
return;
}

Assert.True(false, "No exception was thrown when one should have been");
}

[Fact]
public void when_a_method_doesnt_have_generic_parameters_exception_doesnt_include_brackets()
{
var mock = new Mock<ISomeInterface>(MockBehavior.Strict);
mock.Setup(m => m.DoSomething<int>(0)).Returns(1);

try
{
mock.Object.DoSomethingElse(0);
}
catch (MockException exception)
{
var genericTypesRE = new Regex(@"\<.*?\>");
var match = genericTypesRE.Match(exception.Message);

Assert.False(match.Success);
return;
}

Assert.True(false, "No exception was thrown when one should have been");
}
}

#endregion // #176

// Old @ Google Code

#region #47
Expand Down

0 comments on commit 2802f12

Please sign in to comment.