Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error messages don't display the parent class name of an inner class object #727

Closed
powerdude opened this issue Dec 1, 2018 · 6 comments
Assignees
Milestone

Comments

@powerdude
Copy link
Contributor

powerdude commented Dec 1, 2018

If i have classes like this:

public class Foo
{
   public class Bar
  {
 }
}

public class Foo1
{
   public class Bar{}
}

and try to verify a mock call:

mock.Verify( x=>x.Send(It.IsAny<Foo.Bar>());

and call the service service.Send(new Foo1.Bar()), the messages displayed fail to indicate the parent class name.

Expected invocation on the mock at least once, but was never performed: x=>x.Send(Bar)

Ideally, it should say:

Expected invocation on the mock at least once, but was never performed: x=>x.Send(Foo+Bar)
@stakx
Copy link
Contributor

stakx commented Dec 1, 2018

@powerdude, thanks for reporting this. Would you like to submit a PR for this?

This could be quite easily done by changing Moq's StringBuilderExtensions.AppendNameOf(this StringBuilder, Type) method.

It just so happens that I wrote a small library called TypeNameFormatter not too long ago; see its repo on GitHub, or the source code-only package on NuGet.org. The reason for a separate library is that formatting type names the way they look in e.g. C# turns out to be non-trivial due to a few gotchas (one of which you just described), so I figured let's encapsulate the logic for that in a library so we don't have to think too hard about it anymore.

I've simply not got around to applying that library to Moq yet, but AppendNameOf would become as simple as this:

+// <PackageReference Include="TypeNameFormatter.Sources" Version="1.0.0" PrivateAssets="All" />
+// using TypeNameFormatter;
+
 public static StringBuilder AppendNameOf(this StringBuilder stringBuilder, Type type)
 {
 	Debug.Assert(type != null);
 
+	return stringBuilder.AppendFormattedName(type);
-	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;
 }

(A few unit tests that verify diagnostic messages would start failing due to the improved formatting, but that would be trivial to fix.)

@powerdude
Copy link
Contributor Author

powerdude commented Dec 3, 2018

@stakx, sure, i'll try and have a look. Do you want me to include just the file or reference the library?

@stakx
Copy link
Contributor

stakx commented Dec 3, 2018

@powerdude, perhaps start with a NuGet reference. It's a source-code-only package, so the effect is much the same as including the source file manually. As long as we don't need to modify the formatting code, referencing a NuGet package should be simpler.

@stakx stakx added this to the 4.10.1 milestone Dec 3, 2018
@stakx
Copy link
Contributor

stakx commented Dec 3, 2018

Fixed by #728.

@stakx stakx closed this as completed Dec 3, 2018
@stakx
Copy link
Contributor

stakx commented Dec 3, 2018

@powerdude, I've just pushed Moq 4.10.1 to NuGet (it should become available for restore in a short moment). That version includes your change.

@powerdude
Copy link
Contributor Author

yayy!!. Thanks @stakx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants