Skip to content

Commit

Permalink
added TypeNameFormatter so that richer class names could be displayed…
Browse files Browse the repository at this point in the history
… in error messages.
  • Loading branch information
powerdude committed Dec 3, 2018
1 parent c5ffae8 commit a62e6af
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 38 deletions.
4 changes: 4 additions & 0 deletions src/Moq/Moq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<PackageReference Include="Castle.Core" Version="4.3.1" />
<PackageReference Include="IFluentInterface" Version="2.1.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0" />
<PackageReference Include="TypeNameFormatter.Sources" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
Expand Down
30 changes: 3 additions & 27 deletions src/Moq/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Reflection;
using System.Text;

using TypeNameFormatter;

namespace Moq
{
internal static class StringBuilderExtensions
Expand Down Expand Up @@ -38,33 +40,7 @@ public static StringBuilder AppendNameOf(this StringBuilder stringBuilder, Type
{
Debug.Assert(type != null);

var name = type.Name;
var backtickIndex = name.IndexOf('`');
if (backtickIndex >= 0)
{
stringBuilder.Append(name, 0, backtickIndex);
}
else
{
stringBuilder.Append(name);
}

if (type.GetTypeInfo().IsGenericType)
{
var genericArguments = type.GetGenericArguments();
stringBuilder.Append('<');
for (int i = 0, n = genericArguments.Length; i < n; ++i)
{
if (i > 0)
{
stringBuilder.Append(", ");
}
stringBuilder.AppendNameOf(genericArguments[i]);
}
stringBuilder.Append('>');
}

return stringBuilder;
return stringBuilder.AppendFormattedName(type);
}

public static StringBuilder AppendValueOf(this StringBuilder stringBuilder, object obj)
Expand Down
4 changes: 2 additions & 2 deletions tests/Moq.Tests/CustomMatcherFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Custom_matcher_property_appears_by_name_in_verification_error_messag

var ex = Assert.Throws<MockException>(() => child.Verify());

Assert.Contains(".PlayWith(Toy.IsRed)", ex.Message);
Assert.Contains(".PlayWith(CustomMatcherFixture.Toy.IsRed)", ex.Message);
}

[Fact]
Expand All @@ -84,7 +84,7 @@ public void Custom_matcher_method_appears_by_name_in_verification_error_message(

var ex = Assert.Throws<MockException>(() => child.Verify());

Assert.Contains(".PlayWith(Toy.IsGreen())", ex.Message);
Assert.Contains(".PlayWith(CustomMatcherFixture.Toy.IsGreen())", ex.Message);
}

public class Toy
Expand Down
2 changes: 1 addition & 1 deletion tests/Moq.Tests/ExpressionExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void PrefixesStaticGenericMethodWithClass()

var value = expr.ToStringFixed();

Assert.Contains("ExpressionExtensionsFixture.DoStaticGeneric<Int32>(5)", value);
Assert.Contains("ExpressionExtensionsFixture.DoStaticGeneric<int>(5)", value);
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions tests/Moq.Tests/MockRepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public void ShouldAggregateFailures()
Assert.NotNull(ex);
Assert.True(ex.Message.ContainsConsecutiveLines(
$"The following setups on mock \'{foo}\' were not matched:",
$"IFoo f => f.Do()",
$"MockRepositoryFixture.IFoo f => f.Do()",
$"",
$"The following setups on mock \'{bar}\' were not matched:",
$"IBar b => b.Redo()"));
$"MockRepositoryFixture.IBar b => b.Redo()"));
}

[Fact]
Expand Down
8 changes: 4 additions & 4 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2398,7 +2398,7 @@ public void Test()
Assert.True(e.IsVerificationError);

Assert.Contains(
"IFoo t => t.Submit(It.IsAny<String>(), It.IsAny<String>(), new[] { It.IsAny<Int32>() })",
"IFoo t => t.Submit(It.IsAny<string>(), It.IsAny<string>(), new[] { It.IsAny<int>() })",
e.Message);
}

Expand Down Expand Up @@ -3031,8 +3031,8 @@ public void Test()
var e = Assert.Throws<MockException>(() => mock.Verify(m => m.Execute(0)));
Assert.True(e.Message.ContainsConsecutiveLines(
"Configured setups: ",
"IFoo m => m.Execute(1)",
"IFoo m => m.Execute(It.IsInRange<Int32>(2, 20, Range.Exclusive))"));
"IssueReportsFixture._183.IFoo m => m.Execute(1)",
"IssueReportsFixture._183.IFoo m => m.Execute(It.IsInRange<int>(2, 20, Range.Exclusive))"));
}

[Fact]
Expand All @@ -3047,7 +3047,7 @@ public void TestGeneric()
var e = Assert.Throws<MockException>(() => mock.Verify(m => m.Execute<int>(1, 1)));
Assert.True(e.Message.ContainsConsecutiveLines(
"Configured setups: ",
"IFoo m => m.Execute<Int32>(1, 10)"));
"IssueReportsFixture._183.IFoo m => m.Execute<int>(1, 10)"));
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions tests/Moq.Tests/VerifyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void ThrowsWithExpressionIfVerifiableExpectationWithLambdaMatcherNotCalle

var mex = Assert.Throws<MockException>(() => mock.Verify());
Assert.True(mex.IsVerificationError);
Assert.Contains(@".Execute(It.Is<String>(s => String.IsNullOrEmpty(s)))", mex.Message);
Assert.Contains(@".Execute(It.Is<string>(s => string.IsNullOrEmpty(s)))", mex.Message);
}

[Fact]
Expand Down Expand Up @@ -1407,7 +1407,7 @@ public void Verification_error_message_contains_setup_for_delegate_mock_with_par
var ex = Record.Exception(() => mock.Verify(m => m(1, 2), Times.Once()));

Assert.Contains("Configured setups:", ex.Message);
Assert.Contains("Action<Int32, Int32> m => m(1, It.IsAny<Int32>())", ex.Message);
Assert.Contains("Action<int, int> m => m(1, It.IsAny<int>())", ex.Message);
}

[Fact]
Expand Down

0 comments on commit a62e6af

Please sign in to comment.