Skip to content

Commit

Permalink
Merge pull request #739 from DustinCampbell/fix-unit-tests-in-nested-…
Browse files Browse the repository at this point in the history
…classes

Fix running unit tests defined in nested classes
  • Loading branch information
DustinCampbell committed Jan 30, 2017
2 parents e56399e + 0bfe8b1 commit 0490de2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
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
{
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

0 comments on commit 0490de2

Please sign in to comment.