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

Fix running unit tests defined in nested classes #739

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/OmniSharp.Abstractions/Extensions/ISymbolExtensions.cs
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
namespace OmniSharp.Extensions
{
public static class ISymbolExtensions
{
private readonly static CachedStringBuilder s_cachedBuilder;

public static string GetMetadataName(this ISymbol symbol)
{
if (symbol == null)
{
throw new ArgumentNullException(nameof(symbol));
}

var symbols = new Stack<ISymbol>();

while (symbol != null)
{
if (symbol.Kind == SymbolKind.Assembly ||
symbol.Kind == SymbolKind.NetModule)
{
break;
}

if ((symbol as INamespaceSymbol)?.IsGlobalNamespace == true)
{
break;
}

symbols.Push(symbol);
symbol = symbol.ContainingSymbol;
}

var builder = s_cachedBuilder.Acquire();
try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm suprised this behavior is not part of the Roslyn Api

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Roslyn didn't want to be an API for metadata.

{
ISymbol current = null, previous = null;

while (symbols.Count > 0)
{
current = symbols.Pop();

if (previous != null)
{
if (previous.Kind == SymbolKind.NamedType &&
current.Kind == SymbolKind.NamedType)
{
builder.Append('+');
}
else
{
builder.Append('.');
}
}

builder.Append(current.MetadataName);

previous = current;
}

return builder.ToString();
}
finally
{
s_cachedBuilder.Release(builder);
}
}
}
}
15 changes: 8 additions & 7 deletions src/OmniSharp.DotNetTest/Helpers/TestFeaturesDiscover.cs
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using OmniSharp.Abstractions.Services;
using OmniSharp.Extensions;
using OmniSharp.Models;

namespace OmniSharp.DotNetTest.Helpers
Expand All @@ -25,7 +26,7 @@ public IEnumerable<SyntaxFeature> Discover(SyntaxNode node, SemanticModel semant
bool isTestMethod = false;
string featureName = null;

if(IsTestMethod(method, semanticModel, IsDerivedFromFactAttribute))
if (IsTestMethod(method, semanticModel, IsDerivedFromFactAttribute))
{
isTestMethod = true;
featureName = XunitFeatureName;
Expand All @@ -38,8 +39,7 @@ public IEnumerable<SyntaxFeature> Discover(SyntaxNode node, SemanticModel semant

if (isTestMethod)
{
var methodName = semanticModel.GetDeclaredSymbol(node).ToDisplayString();
methodName = methodName.Substring(0, methodName.IndexOf('('));
var methodName = semanticModel.GetDeclaredSymbol(node).GetMetadataName();

yield return new SyntaxFeature
{
Expand All @@ -50,8 +50,7 @@ public IEnumerable<SyntaxFeature> Discover(SyntaxNode node, SemanticModel semant
}
}

private bool IsTestMethod(MethodDeclarationSyntax node,
SemanticModel sematicModel, Func<ITypeSymbol, bool> predicate)
private bool IsTestMethod(MethodDeclarationSyntax node, SemanticModel sematicModel, Func<ITypeSymbol, bool> predicate)
{
return node.DescendantNodes()
.OfType<AttributeSyntax>()
Expand All @@ -71,7 +70,8 @@ private static bool IsDerivedFromFactAttribute(ITypeSymbol symbol)
}

symbol = symbol.BaseType;
} while (symbol.Name != "Object");
}
while (symbol.SpecialType != SpecialType.System_Object);

return false;
}
Expand All @@ -90,7 +90,8 @@ private bool IsNUnitTest(ITypeSymbol symbol)
}

symbol = symbol.BaseType;
} while (symbol.Name != "Object");
}
while (symbol.SpecialType != SpecialType.System_Object);

return false;
}
Expand Down